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;
	let added_station;
	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);
		added_track = res.data;
		expect(res.status).toEqual(200);
		expect(res.headers['content-type']).toContain("application/json");
	});
	it('creating a station with minimum parameters should return 200', async () => {
		const res = await axios.post(base + '/api/stations', {
			"track": added_track.id
		}, axios_config);
		added_station = res.data;
		expect(res.status).toEqual(200);
		expect(res.headers['content-type']).toContain("application/json");
	});
	it('updateing id should return 406', async () => {
		const res2 = await axios.put(base + '/api/stations/' + added_station.id, {
			"id": added_station.id + 1
		}, axios_config);
		expect(res2.status).toEqual(406);
		expect(res2.headers['content-type']).toContain("application/json")
	});
});
// ---------------
describe('adding + updating successfilly', () => {
	let added_track;
	let added_station;
	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);
		added_track = res.data;
		expect(res.status).toEqual(200);
		expect(res.headers['content-type']).toContain("application/json");
	});
	it('creating a station with minimum parameters should return 200', async () => {
		const res = await axios.post(base + '/api/stations', {
			"track": added_track.id
		}, axios_config);
		added_station = res.data;
		expect(res.status).toEqual(200);
		expect(res.headers['content-type']).toContain("application/json");
	});
	it('updateing nothing should return 200', async () => {
		const res = await axios.put(base + '/api/stations/' + added_station.id, {
			"id": added_station.id
		}, axios_config);
		expect(res.status).toEqual(200);
		expect(res.headers['content-type']).toContain("application/json");
		delete res.data.key;
		delete added_station.key;
		expect(res.data).toEqual(added_station);
	});
	it('updateing description should return 200', async () => {
		const res = await axios.put(base + '/api/stations/' + added_station.id, {
			"id": added_station.id,
			"description": "Hello there! General stationi you're a scanning one."
		}, axios_config);
		expect(res.status).toEqual(200);
		expect(res.headers['content-type']).toContain("application/json");
		expect(res.data.description).toEqual("Hello there! General stationi you're a scanning one.");
	});
	it('updateing enabled to false should return 200', async () => {
		const res = await axios.put(base + '/api/stations/' + added_station.id, {
			"id": added_station.id,
			"enabled": false
		}, axios_config);
		expect(res.status).toEqual(200);
		expect(res.headers['content-type']).toContain("application/json");
		expect(res.data.enabled).toEqual(false);
	});
	it('updateing enabled to true should return 200', async () => {
		const res = await axios.put(base + '/api/stations/' + added_station.id, {
			"id": added_station.id,
			"enabled": true
		}, axios_config);
		expect(res.status).toEqual(200);
		expect(res.headers['content-type']).toContain("application/json");
		expect(res.data.enabled).toEqual(true);
	});
});
