import { faker } from '@faker-js/faker';
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('delete selfservice runner invalid', () => {
    let added_runner;
    it('registering as citizen with minimal params should return 200', async () => {
        const res = await axios.post(base + '/api/runners/register', {
            "firstname": "string",
            "lastname": "string",
            "email": faker.internet.exampleEmail(),
        }, axios_config);
        added_runner = res.data;
        expect(res.status).toEqual(200);
        expect(res.headers['content-type']).toContain("application/json");
    });
    it('delete with valid jwt should return 200', async () => {
        const res = await axios.delete(base + '/api/runners/me/' + added_runner.token, axios_config);
        expect(res.status).toEqual(200);
        expect(res.headers['content-type']).toContain("application/json");
    });
    it('delete with valid jwt but non-existant runner should return 200', async () => {
        const res = await axios.delete(base + '/api/runners/me/' + added_runner.token, axios_config);
        expect(res.status).toEqual(404);
        expect(res.headers['content-type']).toContain("application/json");
    });
    it('delete with invalid jwt should return 401', async () => {
        const res = await axios.delete(base + '/api/runners/me/123.123', axios_config);
        expect(res.status).toEqual(401);
        expect(res.headers['content-type']).toContain("application/json");
    });
});
// ---------------
describe('delete selfservice runner valid', () => {
    let added_runner;
    it('registering as citizen with minimal params should return 200', async () => {
        const res = await axios.post(base + '/api/runners/register', {
            "firstname": "string",
            "lastname": "string",
            "email": faker.internet.exampleEmail(),
        }, axios_config);
        added_runner = res.data;
        expect(res.status).toEqual(200);
        expect(res.headers['content-type']).toContain("application/json");
    });
    it('delete with valid jwt should return 200', async () => {
        const res = await axios.delete(base + '/api/runners/me/' + added_runner.token, axios_config);
        expect(res.status).toEqual(200);
        expect(res.headers['content-type']).toContain("application/json");
        delete added_runner.token;
        expect(res.data).toEqual(added_runner);
    });
});