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('Update runner name after adding', () => {
    let added_org;
    let added_runner;
    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);
        delete res1.data.registrationEnabled;
        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('valid update should return 200', async () => {
        let runnercopy = added_runner
        runnercopy.firstname = "second"
        runnercopy.group = added_runner.group.id;
        const res3 = await axios.put(base + '/api/runners/' + added_runner.id, runnercopy, axios_config);
        expect(res3.status).toEqual(200);
        expect(res3.headers['content-type']).toContain("application/json")
        delete added_org.contact;
        delete added_org.teams;
        runnercopy.group = added_org;
        delete res3.data.group.key;
        delete res3.data.group.registrationEnabled;
        delete runnercopy.group.registrationEnabled;
        delete res3.data.group.teams;
        expect(res3.data).toEqual(runnercopy);
    });
});
// ---------------
describe('Update runner group after adding', () => {
    let added_org_id;
    let added_org_2;
    let added_runner;
    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);
        let added_org = res1.data
        added_org_id = added_org.id;
        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 org with just a name should return 200', async () => {
        const res3 = await axios.post(base + '/api/organizations', {
            "name": "test123"
        }, axios_config);
        added_org_2 = res3.data
        delete added_org_2.contact;
        delete added_org_2.teams;
        expect(res3.status).toEqual(200);
        expect(res3.headers['content-type']).toContain("application/json")
    });
    it('valid group update should return 200', async () => {
        added_runner.group = added_org_2.id;
        delete added_org_2.registrationEnabled;
        const res3 = await axios.put(base + '/api/runners/' + added_runner.id, added_runner, axios_config);
        expect(res3.status).toEqual(200);
        expect(res3.headers['content-type']).toContain("application/json")
        delete res3.data.group.key;
        delete res3.data.group.registrationEnabled;
        delete res3.data.group.teams;
        expect(res3.data.group).toEqual(added_org_2);
    });
});
// ---------------
describe('Update runner id after adding(should fail)', () => {
    let added_org_id;
    let added_runner;
    let added_runner_id;
    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);
        let added_org = res1.data
        added_org_id = added_org.id;
        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;
        added_runner_id = added_runner.id;
        expect(res2.status).toEqual(200);
        expect(res2.headers['content-type']).toContain("application/json")
    });
    it('invalid update should return 406', async () => {
        added_runner.id++;
        added_runner.group = added_runner.group.id;
        const res3 = await axios.put(base + '/api/runners/' + added_runner_id, added_runner, axios_config);
        expect(res3.status).toEqual(406);
        expect(res3.headers['content-type']).toContain("application/json")
    });
});
// ---------------
describe('Update runner group with invalid group after adding', () => {
    let added_org;
    let added_runner;
    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('invalid group update should return 404', async () => {
        added_runner.group = 99999999999999999;
        const res3 = await axios.put(base + '/api/runners/' + added_runner.id, added_runner, axios_config);
        expect(res3.status).toEqual(404);
        expect(res3.headers['content-type']).toContain("application/json")
    });
});