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 illegally', () => {
	let added_org;
	let added_runner;
	let added_scan;
	it('creating a new org with just a name should return 200', async () => {
		const res1 = await axios.post(base + '/api/organizations', {
			"name": "test123"
		}, axios_config);
		added_org = res1.data
		expect(res1.status).toEqual(200);
		expect(res1.headers['content-type']).toContain("application/json")
	});
	it('creating a new runner with only needed params should return 200', async () => {
		const res2 = await axios.post(base + '/api/runners', {
			"firstname": "first",
			"lastname": "last",
			"group": added_org.id
		}, axios_config);
		delete res2.data.group;
		added_runner = res2.data;
		expect(res2.status).toEqual(200);
		expect(res2.headers['content-type']).toContain("application/json")
	});
	it('creating a scan with the minimum amount of parameters should return 200', async () => {
		const res = await axios.post(base + '/api/scans', {
			"runner": added_runner.id,
			"distance": 200
		}, axios_config);
		expect(res.status).toEqual(200);
		expect(res.headers['content-type']).toContain("application/json");
		added_scan = res.data;
	});
	it('updating empty should return 400', async () => {
		const res2 = await axios.put(base + '/api/scans/' + added_scan.id, null, axios_config);
		expect(res2.status).toEqual(400);
		expect(res2.headers['content-type']).toContain("application/json")
	});
	it('updating with wrong id should return 406', async () => {
		const res2 = await axios.put(base + '/api/scans/' + added_scan.id, {
			"id": added_scan.id + 1,
			"runner": added_runner.id,
			"distance": added_scan.distance
		}, axios_config);
		expect(res2.status).toEqual(406);
		expect(res2.headers['content-type']).toContain("application/json")
	});
	it('update with negative distance should return 400', async () => {
		const res2 = await axios.put(base + '/api/scans/' + added_scan.id, {
			"id": added_scan.id,
			"runner": added_runner.id,
			"distance": -1
		}, axios_config);
		expect(res2.status).toEqual(400);
		expect(res2.headers['content-type']).toContain("application/json")
	});
	it('update with invalid runner id should return 404', async () => {
		const res2 = await axios.put(base + '/api/scans/' + added_scan.id, {
			"id": added_scan.id,
			"runner": 9999999999999999999999999,
			"distance": 123
		}, axios_config);
		expect(res2.status).toEqual(404);
		expect(res2.headers['content-type']).toContain("application/json")
	});
});
// ---------------
describe('adding + updating successfilly', () => {
	let added_org;
	let added_runner;
	let added_runner2;
	let added_scan;
	it('creating a new org with just a name should return 200', async () => {
		const res1 = await axios.post(base + '/api/organizations', {
			"name": "test123"
		}, axios_config);
		added_org = res1.data
		expect(res1.status).toEqual(200);
		expect(res1.headers['content-type']).toContain("application/json")
	});
	it('creating a new runner with only needed params should return 200', async () => {
		const res2 = await axios.post(base + '/api/runners', {
			"firstname": "first",
			"lastname": "last",
			"group": added_org.id
		}, axios_config);
		delete res2.data.group;
		delete res2.data.distance;
		added_runner = res2.data;
		expect(res2.status).toEqual(200);
		expect(res2.headers['content-type']).toContain("application/json")
	});
	it('creating a scan with the minimum amount of parameters should return 200', async () => {
		const res = await axios.post(base + '/api/scans', {
			"runner": added_runner.id,
			"distance": 200
		}, axios_config);
		expect(res.status).toEqual(200);
		expect(res.headers['content-type']).toContain("application/json");
		added_scan = res.data;
	});
	it('valid distance update should return 200', async () => {
		const res2 = await axios.put(base + '/api/scans/' + added_scan.id, {
			"id": added_scan.id,
			"runner": added_runner.id,
			"distance": 100
		}, axios_config);
		expect(res2.status).toEqual(200);
		expect(res2.headers['content-type']).toContain("application/json")
		delete res2.data.runner.distance;
		delete res2.data.runner.group;
		expect(res2.data).toEqual({
			"id": added_scan.id,
			"runner": added_runner,
			"distance": 100,
			"valid": true,
			"responseType": "SCAN"
		});
	});
	it('valid valid update should return 200', async () => {
		const res2 = await axios.put(base + '/api/scans/' + added_scan.id, {
			"id": added_scan.id,
			"runner": added_runner.id,
			"distance": 100,
			"valid": false
		}, axios_config);
		expect(res2.status).toEqual(200);
		expect(res2.headers['content-type']).toContain("application/json");
		delete res2.data.runner.distance;
		delete res2.data.runner.group;
		expect(res2.data).toEqual({
			"id": added_scan.id,
			"runner": added_runner,
			"distance": 100,
			"valid": false,
			"responseType": "SCAN"
		});
	});
	it('creating a new runner with only needed params should return 200', async () => {
		const res2 = await axios.post(base + '/api/runners', {
			"firstname": "first",
			"lastname": "last",
			"group": added_org.id
		}, axios_config);
		delete res2.data.group;
		delete res2.data.distance;
		added_runner2 = res2.data;
		expect(res2.status).toEqual(200);
		expect(res2.headers['content-type']).toContain("application/json")
	});
	it('valid runner update should return 200', async () => {
		const res2 = await axios.put(base + '/api/scans/' + added_scan.id, {
			"id": added_scan.id,
			"runner": added_runner2.id,
			"distance": added_scan.distance
		}, axios_config);
		expect(res2.status).toEqual(200);
		expect(res2.headers['content-type']).toContain("application/json");
		delete res2.data.runner.distance;
		delete res2.data.runner.group;
		expect(res2.data).toEqual({
			"id": added_scan.id,
			"runner": added_runner2,
			"distance": added_scan.distance,
			"valid": added_scan.valid,
			"responseType": "SCAN"
		});
	});
});
