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 invalid org', () => {
    it('getting random org via selfservice should return 4040', async () => {
        const res = await axios.get(base + '/api/organizations/selfservice/asfdasfasdfsdafsadfsadfasdfasdfsdf', axios_config);
        expect(res.status).toEqual(404);
    });
});

// ---------------
describe('get valid org w/teams', () => {
    let added_org;
    let added_team;
    it('creating a new org with just a name and registration enabled should return 200', async () => {
        const res = await axios.post(base + '/api/organizations', {
            "name": "test123",
            "registrationEnabled": true
        }, axios_config);
        added_org = res.data;
        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('getting org via selfservice should return 200', async () => {
        const res = await axios.get(base + '/api/organizations/selfservice/' + added_org.registrationKey, axios_config);
        expect(res.status).toEqual(200);
        expect(res.headers['content-type']).toContain("application/json");
        expect(res.data.name).toEqual(added_org.name);
        expect(res.data.teams[0]).toEqual({ name: added_team.name, id: added_team.id, responseType: "SELFSERVICETEAM" });
    });
});