import axios from 'axios';
import { config } from '../../config';
const base = "http://localhost:" + config.internal_port

let access_token;
let axios_config;

beforeAll(async () => {
	jest.setTimeout(20000);
	const res = await axios.post(base + '/api/auth/login', { username: "demo", password: "demo" });
	access_token = res.data["access_token"];
	axios_config = {
		headers: { "authorization": "Bearer " + access_token },
		validateStatus: undefined
	};
});

describe('adding + updating fixed donation illegally', () => {
	let added_donor;
	let added_donation;
	it('creating a new donor with only needed params should return 200', async () => {
		const res = await axios.post(base + '/api/donors', {
			"firstname": "first",
			"lastname": "last"
		}, axios_config);
		added_donor = res.data
		expect(res.status).toEqual(200);
		expect(res.headers['content-type']).toContain("application/json")
	});
	it('creating a new fixed donation should return 200', async () => {
		const res = await axios.post(base + '/api/donations/fixed', {
			"donor": added_donor.id,
			"amount": 1000
		}, axios_config);
		added_donation = res.data;
		expect(res.status).toEqual(200);
		expect(res.headers['content-type']).toContain("application/json")
	});
	it('updating empty should return 400', async () => {
		const res = await axios.put(base + '/api/donations/fixed/' + added_donation.id, null, axios_config);
		expect(res.status).toEqual(400);
		expect(res.headers['content-type']).toContain("application/json")
	});
	it('updating with wrong id should return 406', async () => {
		const res = await axios.put(base + '/api/donations/fixed/' + added_donation.id, {
			"id": added_donation.id + 1,
			"donor": added_donor.id,
			"amount": 100
		}, axios_config);
		expect(res.status).toEqual(406);
		expect(res.headers['content-type']).toContain("application/json")
	});
	it('updating with negative amount should return 400', async () => {
		const res = await axios.put(base + '/api/donations/fixed/' + added_donation.id, {
			"id": added_donation.id,
			"donor": added_donor.id,
			"amount": -1
		}, axios_config);
		expect(res.status).toEqual(400);
		expect(res.headers['content-type']).toContain("application/json")
	});
	it('updating with invalid donor should return 404', async () => {
		const res = await axios.put(base + '/api/donations/fixed/' + added_donation.id, {
			"id": added_donation.id,
			"donor": 9999999999999999999,
			"amount": 100
		}, axios_config);
		expect(res.status).toEqual(404);
		expect(res.headers['content-type']).toContain("application/json")
	});
});
// ---------------
describe('adding + updating distance donation illegally', () => {
	let added_donor;
	let added_org;
	let added_runner;
	let added_donation;
	it('creating a new donor with only needed params should return 200', async () => {
		const res = await axios.post(base + '/api/donors', {
			"firstname": "first",
			"lastname": "last"
		}, axios_config);
		added_donor = res.data
		expect(res.status).toEqual(200);
		expect(res.headers['content-type']).toContain("application/json")
	});
	it('creating a new org with just a name should return 200', async () => {
		const res = await axios.post(base + '/api/organizations', {
			"name": "test123"
		}, axios_config);
		added_org = res.data
		expect(res.status).toEqual(200);
		expect(res.headers['content-type']).toContain("application/json")
	});
	it('creating a new runner with only needed params should return 200', async () => {
		const res = await axios.post(base + '/api/runners', {
			"firstname": "first",
			"lastname": "last",
			"group": added_org.id
		}, axios_config);
		added_runner = res.data;
		expect(res.status).toEqual(200);
		expect(res.headers['content-type']).toContain("application/json")
	});
	it('creating a new distance donation should return 200', async () => {
		const res = await axios.post(base + '/api/donations/distance', {
			"runner": added_runner.id,
			"amountPerDistance": 100,
			"donor": added_donor.id
		}, axios_config);
		added_donation = res.data;
		expect(res.status).toEqual(200);
		expect(res.headers['content-type']).toContain("application/json")
	});
	it('updating empty should return 400', async () => {
		const res = await axios.put(base + '/api/donations/distance/' + added_donation.id, null, axios_config);
		expect(res.status).toEqual(400);
		expect(res.headers['content-type']).toContain("application/json")
	});
	it('updating with wrong id should return 406', async () => {
		const res = await axios.put(base + '/api/donations/distance/' + added_donation.id, {
			"id": added_donation.id + 1,
			"runner": added_runner.id,
			"amountPerDistance": 100,
			"donor": added_donor.id
		}, axios_config);
		expect(res.status).toEqual(406);
		expect(res.headers['content-type']).toContain("application/json")
	});
	it('updating with negative amountPerDistance should return 400', async () => {
		const res = await axios.put(base + '/api/donations/distance/' + added_donation.id, {
			"id": added_donation.id,
			"runner": added_runner.id,
			"amountPerDistance": -1,
			"donor": added_donor.id
		}, axios_config);
		expect(res.status).toEqual(400);
		expect(res.headers['content-type']).toContain("application/json")
	});
	it('updating with invalid donor should return 404', async () => {
		const res = await axios.put(base + '/api/donations/distance/' + added_donation.id, {
			"id": added_donation.id,
			"runner": added_runner.id,
			"amountPerDistance": 100,
			"donor": 9999999999999999999
		}, axios_config);
		expect(res.status).toEqual(404);
		expect(res.headers['content-type']).toContain("application/json")
	});
	it('updating with invalid runner should return 404', async () => {
		const res = await axios.put(base + '/api/donations/distance/' + added_donation.id, {
			"id": added_donation.id,
			"runner": 9999999999999999999,
			"amountPerDistance": 100,
			"donor": added_donor.id
		}, axios_config);
		expect(res.status).toEqual(404);
		expect(res.headers['content-type']).toContain("application/json")
	});
});
// ---------------
describe('adding + updating fixed donation valid', () => {
	let added_donor;
	let added_donor2;
	let added_donation;
	it('creating a new donor with only needed params should return 200', async () => {
		const res = await axios.post(base + '/api/donors', {
			"firstname": "first",
			"lastname": "last"
		}, axios_config);
		added_donor = res.data
		expect(res.status).toEqual(200);
		expect(res.headers['content-type']).toContain("application/json")
	});
	it('creating a new donor with only needed params should return 200', async () => {
		const res = await axios.post(base + '/api/donors', {
			"firstname": "first",
			"lastname": "last"
		}, axios_config);
		delete res.data.donationAmount;
		added_donor2 = res.data
		expect(res.status).toEqual(200);
		expect(res.headers['content-type']).toContain("application/json")
	});
	it('creating a new fixed donation should return 200', async () => {
		const res = await axios.post(base + '/api/donations/fixed', {
			"donor": added_donor.id,
			"amount": 1000
		}, axios_config);
		delete res.data.donor.donationAmount;
		added_donation = res.data;
		expect(res.status).toEqual(200);
		expect(res.headers['content-type']).toContain("application/json")
	});
	it('updating nothing should return 200', async () => {
		const res = await axios.put(base + '/api/donations/fixed/' + added_donation.id, {
			"id": added_donation.id,
			"donor": added_donor.id,
			"amount": 1000
		}, axios_config);
		delete res.data.donor.donationAmount;
		expect(res.status).toEqual(200);
		expect(res.headers['content-type']).toContain("application/json");
		expect(res.data).toEqual(added_donation);
	});
	it('updating amount should return 200', async () => {
		const res = await axios.put(base + '/api/donations/fixed/' + added_donation.id, {
			"id": added_donation.id,
			"donor": added_donor.id,
			"amount": 42
		}, axios_config);
		expect(res.status).toEqual(200);
		expect(res.headers['content-type']).toContain("application/json");
		expect(res.data.amount).toEqual(42);
	});
	it('updating paidAmount should return 200', async () => {
		const res = await axios.put(base + '/api/donations/fixed/' + added_donation.id, {
			"id": added_donation.id,
			"donor": added_donor.id,
			"amount": 42,
			"paidAmount": 10
		}, axios_config);
		expect(res.status).toEqual(200);
		expect(res.headers['content-type']).toContain("application/json");
		expect(res.data.paidAmount).toEqual(10);
	});
	it('updating donor should return 200', async () => {
		const res = await axios.put(base + '/api/donations/fixed/' + added_donation.id, {
			"id": added_donation.id,
			"donor": added_donor2.id,
			"amount": 42
		}, axios_config);
		delete res.data.donor.donationAmount;
		expect(res.status).toEqual(200);
		expect(res.headers['content-type']).toContain("application/json");
		expect(res.data.donor).toEqual(added_donor2);
	});
});
// ---------------
describe('adding + updating distance donation valid', () => {
	let added_donor;
	let added_donor2;
	let added_org;
	let added_runner;
	let added_runner2;
	let added_donation;
	it('creating a new donor with only needed params should return 200', async () => {
		const res = await axios.post(base + '/api/donors', {
			"firstname": "first",
			"lastname": "last"
		}, axios_config);
		delete res.data.donationAmount;
		added_donor = res.data
		expect(res.status).toEqual(200);
		expect(res.headers['content-type']).toContain("application/json")
	});
	it('creating a new donor with only needed params should return 200', async () => {
		const res = await axios.post(base + '/api/donors', {
			"firstname": "first",
			"lastname": "last"
		}, axios_config);
		delete res.data.donationAmount;
		added_donor2 = res.data
		expect(res.status).toEqual(200);
		expect(res.headers['content-type']).toContain("application/json")
	});
	it('creating a new org with just a name should return 200', async () => {
		const res = await axios.post(base + '/api/organizations', {
			"name": "test123"
		}, axios_config);
		added_org = res.data
		expect(res.status).toEqual(200);
		expect(res.headers['content-type']).toContain("application/json")
	});
	it('creating a new runner with only needed params should return 200', async () => {
		const res = await axios.post(base + '/api/runners', {
			"firstname": "first",
			"lastname": "last",
			"group": added_org.id
		}, axios_config);
		added_runner = res.data;
		expect(res.status).toEqual(200);
		expect(res.headers['content-type']).toContain("application/json")
	});
	it('creating a new runner with only needed params should return 200', async () => {
		const res = await axios.post(base + '/api/runners', {
			"firstname": "first",
			"lastname": "last",
			"group": added_org.id
		}, axios_config);
		delete res.data.group;
		added_runner2 = res.data;
		expect(res.status).toEqual(200);
		expect(res.headers['content-type']).toContain("application/json")
	});
	it('creating a new distance donation should return 200', async () => {
		const res = await axios.post(base + '/api/donations/distance', {
			"runner": added_runner.id,
			"amountPerDistance": 100,
			"donor": added_donor.id
		}, axios_config);
		delete res.data.donor.donationAmount;
		added_donation = res.data;
		expect(res.status).toEqual(200);
		expect(res.headers['content-type']).toContain("application/json")
	});
	it('updating nothing should return 200', async () => {
		const res = await axios.put(base + '/api/donations/distance/' + added_donation.id, {
			"id": added_donation.id,
			"runner": added_runner.id,
			"amountPerDistance": 100,
			"donor": added_donor.id
		}, axios_config);
		delete res.data.donor.donationAmount;
		expect(res.status).toEqual(200);
		expect(res.headers['content-type']).toContain("application/json");
		expect(res.data).toEqual(added_donation);
	});
	it('updating amount should return 200', async () => {
		const res = await axios.put(base + '/api/donations/distance/' + added_donation.id, {
			"id": added_donation.id,
			"runner": added_runner.id,
			"amountPerDistance": 69,
			"donor": added_donor.id
		}, axios_config);
		delete res.data.donor.donationAmount;
		expect(res.status).toEqual(200);
		expect(res.headers['content-type']).toContain("application/json");
		expect(res.data.amountPerDistance).toEqual(69);
	});
	it('updating paidAmount should return 200', async () => {
		const res = await axios.put(base + '/api/donations/distance/' + added_donation.id, {
			"id": added_donation.id,
			"runner": added_runner.id,
			"amountPerDistance": 69,
			"donor": added_donor.id,
			"paidAmount": 10
		}, axios_config);
		delete res.data.donor.donationAmount;
		expect(res.status).toEqual(200);
		expect(res.headers['content-type']).toContain("application/json");
		expect(res.data.paidAmount).toEqual(10);
	});
	it('updating runner should return 200', async () => {
		const res = await axios.put(base + '/api/donations/distance/' + added_donation.id, {
			"id": added_donation.id,
			"runner": added_runner2.id,
			"amountPerDistance": 69,
			"donor": added_donor.id
		}, axios_config);
		expect(res.status).toEqual(200);
		expect(res.headers['content-type']).toContain("application/json");
		expect(res.data.runner).toEqual(added_runner2);
	});
	it('updating donor should return 200', async () => {
		const res = await axios.put(base + '/api/donations/distance/' + added_donation.id, {
			"id": added_donation.id,
			"runner": added_runner2.id,
			"amountPerDistance": 69,
			"donor": added_donor2.id
		}, axios_config);
		delete res.data.donor.donationAmount;
		expect(res.status).toEqual(200);
		expect(res.headers['content-type']).toContain("application/json");
		expect(res.data.donor).toEqual(added_donor2);
	});
});