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

let access_token;
let axios_config;

beforeAll(async () => {
	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('GET /api/tracks', () => {
	it('basic get should return 200', async () => {
		const res = await axios.get(base + '/api/tracks', axios_config);
		expect(res.status).toEqual(200);
		expect(res.headers['content-type']).toContain("application/json")
	});
	it('correct distance input should return 200', async () => {
		const res = await axios.post(base + '/api/tracks', {
			"name": "string",
			"distance": 400
		}, axios_config);
		expect(res.status).toEqual(200);
		expect(res.headers['content-type']).toContain("application/json")
	});
});
// ---------------
describe('POST /api/tracks', () => {
	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('correct distance input should return 200', async () => {
		const res = await axios.post(base + '/api/tracks', {
			"name": "string",
			"distance": 400
		}, axios_config);
		expect(res.status).toEqual(200);
		expect(res.headers['content-type']).toContain("application/json")
	});
});
// ---------------
describe('adding + getting tracks', () => {
	it('correct distance input should return 200', async () => {
		const res = await axios.post(base + '/api/tracks', {
			"name": "string",
			"distance": 1000
		}, axios_config);
		expect(res.status).toEqual(200);
		expect(res.headers['content-type']).toContain("application/json")
	});
	it('check if track was added', async () => {
		const res = await axios.get(base + '/api/tracks', axios_config);
		expect(res.status).toEqual(200);
		expect(res.headers['content-type']).toContain("application/json")
		let added_track = res.data[res.data.length - 1]
		delete added_track.id
		expect(added_track).toEqual({
			"name": "string",
			"distance": 1000
		})
	});
});
// ---------------
describe('adding + getting + updating', () => {
	let added_track;
	it('correct distance input should return 200', async () => {
		const res = await axios.post(base + '/api/tracks', {
			"name": "string",
			"distance": 1500
		}, axios_config);
		expect(res.status).toEqual(200);
		expect(res.headers['content-type']).toContain("application/json");
		added_track = res.data;
	});
	it('get should return 200', async () => {
		const res1 = await axios.get(base + '/api/tracks/' + added_track.id, axios_config);
		expect(res1.status).toEqual(200);
		expect(res1.headers['content-type']).toContain("application/json")
		const compareTrack = res1.data;
		expect(compareTrack).toEqual(added_track)
	})
	it('get should return 200', async () => {
		const res2 = await axios.put(base + '/api/tracks/' + added_track.id, {
			"id": added_track.id,
			"name": "apitrack",
			"distance": 5100
		}, axios_config);
		expect(res2.status).toEqual(200);
		expect(res2.headers['content-type']).toContain("application/json")
	})
	it('get should return 200', async () => {
		const res3 = await axios.get(base + '/api/tracks/' + added_track.id, axios_config);
		expect(res3.status).toEqual(200);
		expect(res3.headers['content-type']).toContain("application/json")
		let added_track2 = res3.data;
		delete added_track2.id
		expect(added_track2).toEqual({
			"name": "apitrack",
			"distance": 5100
		})
	});
});
