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_card;
	it('creating card without runner should return 200', async () => {
		const res = await axios.post(base + '/api/cards', null, axios_config);
		added_card = res.data;
		expect(res.status).toEqual(200);
		expect(res.headers['content-type']).toContain("application/json")
	});
	it('updating empty should return 400', async () => {
		const res2 = await axios.put(base + '/api/cards/' + added_card.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/cards/' + added_card.id, {
			"id": added_card.id + 1
		}, axios_config);
		expect(res2.status).toEqual(406);
		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/cards/' + added_card.id, {
			"id": added_card.id,
			"runner": 9999999999999999999999999
		}, axios_config);
		expect(res2.status).toEqual(404);
		expect(res2.headers['content-type']).toContain("application/json")
	});
});
// ---------------
describe('adding + updating card.runner successfully', () => {
	let added_org;
	let added_runner;
	let added_runner2;
	let added_card;
	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);
		added_runner = res2.data;
		expect(res2.status).toEqual(200);
		expect(res2.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);
		added_runner2 = res2.data;
		expect(res2.status).toEqual(200);
		expect(res2.headers['content-type']).toContain("application/json")
	});
	it('creating card without runner should return 200', async () => {
		const res = await axios.post(base + '/api/cards', null, axios_config);
		added_card = res.data;
		expect(res.status).toEqual(200);
		expect(res.headers['content-type']).toContain("application/json")
	});
	it('valid runner update (add runner) should return 200', async () => {
		const res2 = await axios.put(base + '/api/cards/' + added_card.id, {
			"id": added_card.id,
			"runner": added_runner.id
		}, axios_config);
		expect(res2.status).toEqual(200);
		expect(res2.headers['content-type']).toContain("application/json");
		expect(res2.data).toEqual({
			"id": added_card.id,
			"runner": added_runner,
			"enabled": true,
			"code": added_card.code,
			"responseType": "RUNNERCARD"
		});
	});
	it('valid runner update (change runner) should return 200', async () => {
		const res2 = await axios.put(base + '/api/cards/' + added_card.id, {
			"id": added_card.id,
			"runner": added_runner2.id
		}, axios_config);
		expect(res2.status).toEqual(200);
		expect(res2.headers['content-type']).toContain("application/json");
		expect(res2.data).toEqual({
			"id": added_card.id,
			"runner": added_runner2,
			"enabled": true,
			"code": added_card.code,
			"responseType": "RUNNERCARD"
		});
	});
});
// ---------------
describe('adding + updating other values successfully', () => {
	let added_card;
	it('creating card without runner should return 200', async () => {
		const res = await axios.post(base + '/api/cards', null, axios_config);
		added_card = res.data;
		expect(res.status).toEqual(200);
		expect(res.headers['content-type']).toContain("application/json")
	});
	it('valid update changeing nothing should return 200', async () => {
		const res2 = await axios.put(base + '/api/cards/' + added_card.id, {
			"id": added_card.id
		}, axios_config);
		expect(res2.status).toEqual(200);
		expect(res2.headers['content-type']).toContain("application/json");
		expect(res2.data).toEqual(added_card);
	});
	it('valid disable update should return 200', async () => {
		const res2 = await axios.put(base + '/api/cards/' + added_card.id, {
			"id": added_card.id,
			"enabled": false
		}, axios_config);
		expect(res2.status).toEqual(200);
		expect(res2.headers['content-type']).toContain("application/json");
		expect(res2.data).toEqual({
			"id": added_card.id,
			"runner": null,
			"enabled": false,
			"code": added_card.code,
			"responseType": "RUNNERCARD"
		});
	});
	it('valid enable update should return 200', async () => {
		const res2 = await axios.put(base + '/api/cards/' + added_card.id, {
			"id": added_card.id,
			"enabled": true
		}, axios_config);
		expect(res2.status).toEqual(200);
		expect(res2.headers['content-type']).toContain("application/json");
		expect(res2.data).toEqual({
			"id": added_card.id,
			"runner": null,
			"enabled": true,
			"code": added_card.code,
			"responseType": "RUNNERCARD"
		});
	});
});

