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_refreshASD312",
        "middlename": "demo_refreshASD312",
        "lastname": "demo_refreshASD312",
        "username": "demo_refreshASD312",
        "password": "demo_refreshASD312",
        "email": "demo_refreshASD312@dev.lauf-fuer-kaya.de"
    }, {
        headers: { "authorization": "Bearer " + res_login.data["access_token"] },
        validateStatus: undefined
    });
});

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