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('GET /api/runners', () => {
    it('basic get should return 200', async () => {
        const res = await axios.get(base + '/api/runners', axios_config);
        expect(res.status).toEqual(200);
        expect(res.headers['content-type']).toContain("application/json")
    });
});
// ---------------
describe('GET /api/runners/0', () => {
    it('basic get should return 404', async () => {
        const res = await axios.get(base + '/api/runners/0', axios_config);
        expect(res.status).toEqual(404);
        expect(res.headers['content-type']).toContain("application/json")
    });
});
// ---------------
describe('GET /api/runners after adding', () => {
    let added_org_id;
    let added_runner;
    it('creating a new org with just a name should return 200', async () => {
        const res = await axios.post(base + '/api/organizations', {
            "name": "test123"
        }, axios_config);
        let added_org = res.data
        added_org_id = added_org.id;
        expect(res.status).toEqual(200);
        expect(res.headers['content-type']).toContain("application/json")
    });
    it('creating a new runner with only needed params should return 200', async () => {
        const res = await axios.post(base + '/api/runners', {
            "firstname": "first",
            "lastname": "last",
            "group": added_org_id
        }, axios_config);
        added_runner = res.data;
        expect(res.status).toEqual(200);
        expect(res.headers['content-type']).toContain("application/json")
    });
    it('explicit get should return 200', async () => {
        const res = await axios.get(base + '/api/runners/' + added_runner.id, axios_config);
        expect(res.status).toEqual(200);
        expect(res.headers['content-type']).toContain("application/json")
        let gotten_runner = res.data
        expect(gotten_runner).toEqual(added_runner);
    });
    it('get from all runners should return 200', async () => {
        const res = await axios.get(base + '/api/runners/', axios_config);
        expect(res.status).toEqual(200);
        expect(res.headers['content-type']).toContain("application/json")
        let gotten_runners = res.data
        expect(gotten_runners).toContainEqual(added_runner);
    });
});
// ---------------
describe('GET /api/organizations/:id/runners after adding', () => {
    let added_org_id;
    let added_runner;
    it('creating a new org with just a name should return 200', async () => {
        const res = await axios.post(base + '/api/organizations', {
            "name": "test123"
        }, axios_config);
        let added_org = res.data
        added_org_id = added_org.id;
        expect(res.status).toEqual(200);
        expect(res.headers['content-type']).toContain("application/json")
    });
    it('creating a new runner with only needed params should return 200', async () => {
        const res = await axios.post(base + '/api/runners', {
            "firstname": "first",
            "lastname": "last",
            "group": added_org_id
        }, axios_config);
        added_runner = res.data;
        expect(res.status).toEqual(200);
        expect(res.headers['content-type']).toContain("application/json")
    });
    it('check if scans was added via the orgs/runners endpoint.', async () => {
        const res = await axios.get(base + '/api/organizations/' + added_org_id + "/runners", axios_config);
        expect(res.status).toEqual(200);
        expect(res.headers['content-type']).toContain("application/json");
        expect(res.data).toContainEqual(added_runner);
    });
});
// ---------------
describe('GET /api/teams/:id/runners after adding', () => {
    let added_org_id;
    let added_team;
    let added_runner;
    it('creating a new org with just a name should return 200', async () => {
        const res = await axios.post(base + '/api/organizations', {
            "name": "test123"
        }, axios_config);
        let added_org = res.data
        added_org_id = added_org.id;
        expect(res.status).toEqual(200);
        expect(res.headers['content-type']).toContain("application/json")
    });
    it('creating a new team with a parent org should return 200', async () => {
        const res = await axios.post(base + '/api/teams', {
            "name": "test_team",
            "parentGroup": added_org_id
        }, axios_config);
        added_team = res.data;
        expect(res.status).toEqual(200);
        expect(res.headers['content-type']).toContain("application/json")
    });
    it('creating a new runner with only needed params should return 200', async () => {
        const res = await axios.post(base + '/api/runners', {
            "firstname": "first",
            "lastname": "last",
            "group": added_team.id
        }, axios_config);
        added_runner = res.data;
        expect(res.status).toEqual(200);
        expect(res.headers['content-type']).toContain("application/json")
    });
    it('check if runner was added via the teams/runners endpoint.', async () => {
        const res = await axios.get(base + '/api/teams/' + added_team.id + "/runners", axios_config);
        expect(res.status).toEqual(200);
        expect(res.headers['content-type']).toContain("application/json");
        expect(res.data).toContainEqual(added_runner);
    });
});