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_resetASD312",
        "middlename": "demo_resetASD312",
        "lastname": "demo_resetASD312",
        "username": "demo_resetASD312",
        "password": "demo_resetASD312",
        "email": "demo_resetASD3121@dev.lauf-fuer-kaya.de"
    }, {
        headers: { "authorization": "Bearer " + res_login.data["access_token"] },
        validateStatus: undefined
    });
    await axios.post(base + '/api/users', {
        "firstname": "demo_resetASD3122",
        "middlename": "demo_resetASD3122",
        "lastname": "demo_resetASD3122",
        "username": "demo_resetASD3122",
        "password": "demo_resetASD3122",
        "email": "demo_resetASD3122@dev.lauf-fuer-kaya.de"
    }, {
        headers: { "authorization": "Bearer " + res_login.data["access_token"] },
        validateStatus: undefined
    });
});

describe('POST /api/auth/reset valid', () => {
    let reset_token;
    it('valid reset token request should return 200 (500 w/o correct auth)', async () => {
        const res1 = await axios.post(base + '/api/auth/reset', { email: "demo_resetASD3121@dev.lauf-fuer-kaya.de" }, axios_config);
        reset_token = res1.data.resetToken;
        expect(res1.status).toEqual(200);
    });
});
// ---------------
describe('POST /api/auth/reset invalid requests', () => {
    it('request another password reset before the timeout should return 406', async () => {
        const res1 = await axios.post(base + '/api/auth/reset', { email: "demo_resetASD3122@dev.lauf-fuer-kaya.de" }, axios_config);
        const res2 = await axios.post(base + '/api/auth/reset', { email: "demo_resetASD3122@dev.lauf-fuer-kaya.de" }, axios_config);
        expect(res2.status).toEqual(406);
    });
});
// ---------------
describe('POST /api/auth/reset invalid token', () => {
    it('providing a invalid reset token should return 401', async () => {
        const res2 = await axios.post(base + '/api/auth/reset/' + "123123", { password: "demo" }, axios_config);
        expect(res2.status).toEqual(401);
    });
    it('providing no reset token should return 400', async () => {
        const res2 = await axios.post(base + '/api/auth/reset/' + "", { password: "demo" }, axios_config);
        expect(res2.status).toEqual(400);
    });
});
// ---------------
describe('POST /api/auth/reset invalid body', () => {
    it('providing no password should return 400', async () => {
        const res2 = await axios.post(base + '/api/auth/reset/' + "123123", null, axios_config);
        expect(res2.status).toEqual(400);
    });
});