import axios from 'axios';
import { config } from '../../config';

const base = "http://localhost:" + config.internal_port

const axios_config = {
    validateStatus: undefined
};;

beforeAll(async () => {
    jest.setTimeout(20000);
    const res_login = await axios.post(base + '/api/auth/login', { username: "demo", password: "demo" });
    await axios.post(base + '/api/users', {
        "firstname": "demo_logoutASD123",
        "middlename": "demo_logoutASD123",
        "lastname": "demo_logoutASD123",
        "username": "demo_logoutASD123",
        "password": "demo_logoutASD123",
        "email": "demo_logoutASD123@dev.lauf-fuer-kaya.de"
    }, {
        headers: { "authorization": "Bearer " + res_login.data["access_token"] },
        validateStatus: undefined
    });
});

describe('POST /api/auth/logout valid', () => {
    let refresh_coookie;
    it('valid logout with token in cookie should return 200', async () => {
        const res_login = await axios.post(base + '/api/auth/login', { username: "demo_logoutASD123", password: "demo_logoutASD123" });
        refresh_coookie = res_login.headers["set-cookie"];
        const res = await axios.post(base + '/api/auth/logout', null, {
            headers: { "Cookie": refresh_coookie },
            validateStatus: undefined
        });
        expect(res.status).toEqual(200);
    });
    it('valid logout with token in body should return 200', async () => {
        const res_login = await axios.post(base + '/api/auth/login', { username: "demo_logoutASD123", password: "demo_logoutASD123" });
        const res = await axios.post(base + '/api/auth/logout', { token: res_login.data["refresh_token"] }, axios_config);
        expect(res.status).toEqual(200);
    });
    it('getting users after valid logout should return 401', async () => {
        const res = await axios.get(base + '/api/users', {
            headers: { "Cookie": refresh_coookie },
            validateStatus: undefined
        });
        expect(res.status).toEqual(401);
    });
});
// ---------------
describe('POST /api/auth/logout ivalid', () => {
    it('invalid logout without token should return 406', async () => {
        const res = await axios.post(base + '/api/auth/logout', null, axios_config);
        expect(res.status).toEqual(406);
    });
    it('invalid logout with invalid token in body should return 401', async () => {
        const res = await axios.post(base + '/api/auth/logout', { token: "1" }, axios_config);
        expect(res.status).toEqual(401);
    });
});