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('POST /api/tracks illegally', () => {
	it('no distance input should return 400', async () => {
		const res = await axios.post(base + '/api/tracks', {
			"name": "string",
		}, axios_config);
		expect(res.status).toEqual(400);
		expect(res.headers['content-type']).toContain("application/json")
	});
	it('illegal distance input should return 400', async () => {
		const res = await axios.post(base + '/api/tracks', {
			"name": "string",
			"distance": -1
		}, axios_config);
		expect(res.status).toEqual(400);
		expect(res.headers['content-type']).toContain("application/json")
	});
	it('negative minimum lap time input should return 406', async () => {
		const res = await axios.post(base + '/api/tracks', {
			"name": "string",
			"distance": 200,
			"minimumLapTime": -1
		}, axios_config);
		expect(res.status).toEqual(406);
		expect(res.headers['content-type']).toContain("application/json")
	});
});
// ---------------
describe('POST /api/tracks successfully', () => {
	it('creating a track with the minimum amount of parameters should return 200', async () => {
		const res = await axios.post(base + '/api/tracks', {
			"name": "testtrack",
			"distance": 200,
		}, axios_config);
		expect(res.status).toEqual(200);
		expect(res.headers['content-type']).toContain("application/json");
		delete res.data.id;
		expect(res.data).toEqual({
			"name": "testtrack",
			"distance": 200,
			"minimumLapTime": null,
			"responseType": "TRACK"
		})
	});
	it('creating a track with all parameters (optional set to null) should return 200', async () => {
		const res = await axios.post(base + '/api/tracks', {
			"name": "testtrack",
			"distance": 200,
			"minimumLapTime": null
		}, axios_config);
		expect(res.status).toEqual(200);
		expect(res.headers['content-type']).toContain("application/json");
		delete res.data.id;
		expect(res.data).toEqual({
			"name": "testtrack",
			"distance": 200,
			"minimumLapTime": null,
			"responseType": "TRACK"
		})
	});
	it('creating a track with all parameters should return 200', async () => {
		const res = await axios.post(base + '/api/tracks', {
			"name": "testtrack",
			"distance": 200,
			"minimumLapTime": 123
		}, axios_config);
		expect(res.status).toEqual(200);
		expect(res.headers['content-type']).toContain("application/json");
		delete res.data.id;
		expect(res.data).toEqual({
			"name": "testtrack",
			"distance": 200,
			"minimumLapTime": 123,
			"responseType": "TRACK"
		})
	});
});
