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_track;
	it('correct distance input should return 200', async () => {
		const res = await axios.post(base + '/api/tracks', {
			"name": "apitrack",
			"distance": 1500
		}, axios_config);
		expect(res.status).toEqual(200);
		expect(res.headers['content-type']).toContain("application/json");
		added_track = res.data;
	});
	it('update with negative distance should return 400', async () => {
		const res2 = await axios.put(base + '/api/tracks/' + added_track.id, {
			"id": added_track.id,
			"name": "apitrack",
			"distance": -1
		}, axios_config);
		expect(res2.status).toEqual(400);
		expect(res2.headers['content-type']).toContain("application/json")
	});
	it('update with negative laptime should return 406', async () => {
		const res2 = await axios.put(base + '/api/tracks/' + added_track.id, {
			"id": added_track.id,
			"name": "apitrack",
			"distance": 2,
			"minimumLapTime": -1
		}, axios_config);
		expect(res2.status).toEqual(406);
		expect(res2.headers['content-type']).toContain("application/json")
	});
});
// ---------------
describe('adding + updating successfilly', () => {
	let added_track;
	it('correct distance input should return 200', async () => {
		const res = await axios.post(base + '/api/tracks', {
			"name": "apitrack2",
			"distance": 1500
		}, axios_config);
		expect(res.status).toEqual(200);
		expect(res.headers['content-type']).toContain("application/json");
		added_track = res.data;
	});
	it('valid name change should return 200', async () => {
		const res2 = await axios.put(base + '/api/tracks/' + added_track.id, {
			"id": added_track.id,
			"name": "apitrackk",
			"distance": 1500
		}, axios_config);
		expect(res2.status).toEqual(200);
		expect(res2.headers['content-type']).toContain("application/json")
	});
	it('valid distance change should return 200', async () => {
		const res2 = await axios.put(base + '/api/tracks/' + added_track.id, {
			"id": added_track.id,
			"name": "apitrack2",
			"distance": 5100
		}, axios_config);
		expect(res2.status).toEqual(200);
		expect(res2.headers['content-type']).toContain("application/json")
	});
	it('valid laptime change (to a set number) should return 200', async () => {
		const res2 = await axios.put(base + '/api/tracks/' + added_track.id, {
			"id": added_track.id,
			"name": "apitrack2",
			"distance": 5100,
			"minimumLapTime": 3
		}, axios_config);
		expect(res2.status).toEqual(200);
		expect(res2.headers['content-type']).toContain("application/json")
	});
	it('valid laptime change (to a set null) should return 200', async () => {
		const res2 = await axios.put(base + '/api/tracks/' + added_track.id, {
			"id": added_track.id,
			"name": "apitrack2",
			"distance": 5100,
			"minimumLapTime": null
		}, axios_config);
		expect(res2.status).toEqual(200);
		expect(res2.headers['content-type']).toContain("application/json")
	});
});
