"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const chai_1 = require("chai");
const shared_1 = require("../../shared");
const server_commands_1 = require("../../../../shared/server-commands");
function login(options) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const { server, username, password, otpToken, expectedStatus } = options;
        const user = { username, password };
        const { res, body: { access_token: token } } = yield server.login.loginAndGetResponse({ user, otpToken, expectedStatus });
        return { res, token };
    });
}
describe('Test users', function () {
    let server;
    let otpSecret;
    let requestToken;
    const userUsername = 'user1';
    let userId;
    let userPassword;
    let userToken;
    before(function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.timeout(30000);
            server = yield (0, server_commands_1.createSingleServer)(1);
            yield (0, server_commands_1.setAccessTokensToServers)([server]);
            const res = yield server.users.generate(userUsername);
            userId = res.userId;
            userPassword = res.password;
            userToken = res.token;
        });
    });
    it('Should not add the header on login if two factor is not enabled', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const { res, token } = yield login({ server, username: userUsername, password: userPassword });
            (0, chai_1.expect)(res.header['x-peertube-otp']).to.not.exist;
            yield server.users.getMyInfo({ token });
        });
    });
    it('Should request two factor and get the secret and uri', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const { otpRequest } = yield server.twoFactor.request({ userId, token: userToken, currentPassword: userPassword });
            (0, chai_1.expect)(otpRequest.requestToken).to.exist;
            (0, chai_1.expect)(otpRequest.secret).to.exist;
            (0, chai_1.expect)(otpRequest.secret).to.have.lengthOf(32);
            (0, chai_1.expect)(otpRequest.uri).to.exist;
            (0, shared_1.expectStartWith)(otpRequest.uri, 'otpauth://');
            (0, chai_1.expect)(otpRequest.uri).to.include(otpRequest.secret);
            requestToken = otpRequest.requestToken;
            otpSecret = otpRequest.secret;
        });
    });
    it('Should not have two factor confirmed yet', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const { twoFactorEnabled } = yield server.users.getMyInfo({ token: userToken });
            (0, chai_1.expect)(twoFactorEnabled).to.be.false;
        });
    });
    it('Should confirm two factor', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield server.twoFactor.confirmRequest({
                userId,
                token: userToken,
                otpToken: server_commands_1.TwoFactorCommand.buildOTP({ secret: otpSecret }).generate(),
                requestToken
            });
        });
    });
    it('Should not add the header on login if two factor is enabled and password is incorrect', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const { res, token } = yield login({ server, username: userUsername, password: 'fake', expectedStatus: 400 });
            (0, chai_1.expect)(res.header['x-peertube-otp']).to.not.exist;
            (0, chai_1.expect)(token).to.not.exist;
        });
    });
    it('Should add the header on login if two factor is enabled and password is correct', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const { res, token } = yield login({
                server,
                username: userUsername,
                password: userPassword,
                expectedStatus: 401
            });
            (0, chai_1.expect)(res.header['x-peertube-otp']).to.exist;
            (0, chai_1.expect)(token).to.not.exist;
            yield server.users.getMyInfo({ token });
        });
    });
    it('Should not login with correct password and incorrect otp secret', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const otp = server_commands_1.TwoFactorCommand.buildOTP({ secret: 'a'.repeat(32) });
            const { res, token } = yield login({
                server,
                username: userUsername,
                password: userPassword,
                otpToken: otp.generate(),
                expectedStatus: 400
            });
            (0, chai_1.expect)(res.header['x-peertube-otp']).to.not.exist;
            (0, chai_1.expect)(token).to.not.exist;
        });
    });
    it('Should not login with correct password and incorrect otp code', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const { res, token } = yield login({
                server,
                username: userUsername,
                password: userPassword,
                otpToken: '123456',
                expectedStatus: 400
            });
            (0, chai_1.expect)(res.header['x-peertube-otp']).to.not.exist;
            (0, chai_1.expect)(token).to.not.exist;
        });
    });
    it('Should not login with incorrect password and correct otp code', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const otpToken = server_commands_1.TwoFactorCommand.buildOTP({ secret: otpSecret }).generate();
            const { res, token } = yield login({
                server,
                username: userUsername,
                password: 'fake',
                otpToken,
                expectedStatus: 400
            });
            (0, chai_1.expect)(res.header['x-peertube-otp']).to.not.exist;
            (0, chai_1.expect)(token).to.not.exist;
        });
    });
    it('Should correctly login with correct password and otp code', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const otpToken = server_commands_1.TwoFactorCommand.buildOTP({ secret: otpSecret }).generate();
            const { res, token } = yield login({ server, username: userUsername, password: userPassword, otpToken });
            (0, chai_1.expect)(res.header['x-peertube-otp']).to.not.exist;
            (0, chai_1.expect)(token).to.exist;
            yield server.users.getMyInfo({ token });
        });
    });
    it('Should have two factor enabled when getting my info', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const { twoFactorEnabled } = yield server.users.getMyInfo({ token: userToken });
            (0, chai_1.expect)(twoFactorEnabled).to.be.true;
        });
    });
    it('Should disable two factor and be able to login without otp token', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield server.twoFactor.disable({ userId, token: userToken, currentPassword: userPassword });
            const { res, token } = yield login({ server, username: userUsername, password: userPassword });
            (0, chai_1.expect)(res.header['x-peertube-otp']).to.not.exist;
            yield server.users.getMyInfo({ token });
        });
    });
    it('Should have two factor disabled when getting my info', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const { twoFactorEnabled } = yield server.users.getMyInfo({ token: userToken });
            (0, chai_1.expect)(twoFactorEnabled).to.be.false;
        });
    });
    it('Should enable two factor auth without password from an admin', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const { otpRequest } = yield server.twoFactor.request({ userId });
            yield server.twoFactor.confirmRequest({
                userId,
                otpToken: server_commands_1.TwoFactorCommand.buildOTP({ secret: otpRequest.secret }).generate(),
                requestToken: otpRequest.requestToken
            });
            const { twoFactorEnabled } = yield server.users.getMyInfo({ token: userToken });
            (0, chai_1.expect)(twoFactorEnabled).to.be.true;
        });
    });
    it('Should disable two factor auth without password from an admin', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield server.twoFactor.disable({ userId });
            const { twoFactorEnabled } = yield server.users.getMyInfo({ token: userToken });
            (0, chai_1.expect)(twoFactorEnabled).to.be.false;
        });
    });
    after(function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield (0, server_commands_1.cleanupTests)([server]);
        });
    });
});
//# sourceMappingURL=two-factor.js.map