"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const server_commands_1 = require("../../../../shared/server-commands");
describe('Test two factor API validators', function () {
    let server;
    let rootId;
    let rootPassword;
    let rootRequestToken;
    let rootOTPToken;
    let userId;
    let userToken = '';
    let userPassword;
    let userRequestToken;
    let userOTPToken;
    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 result = yield server.users.generate('user1');
                userToken = result.token;
                userId = result.userId;
                userPassword = result.password;
            }
            {
                const { id } = yield server.users.getMyInfo();
                rootId = id;
                rootPassword = server.store.user.password;
            }
        });
    });
    describe('When requesting two factor', function () {
        it('Should fail with an unknown user id', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield server.twoFactor.request({ userId: 42, currentPassword: rootPassword, expectedStatus: 404 });
            });
        });
        it('Should fail with an invalid user id', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield server.twoFactor.request({
                    userId: 'invalid',
                    currentPassword: rootPassword,
                    expectedStatus: 400
                });
            });
        });
        it('Should fail to request another user two factor without the appropriate rights', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield server.twoFactor.request({
                    userId: rootId,
                    token: userToken,
                    currentPassword: userPassword,
                    expectedStatus: 403
                });
            });
        });
        it('Should succeed to request another user two factor with the appropriate rights', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield server.twoFactor.request({ userId, currentPassword: rootPassword });
            });
        });
        it('Should fail to request two factor without a password', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield server.twoFactor.request({
                    userId,
                    token: userToken,
                    currentPassword: undefined,
                    expectedStatus: 400
                });
            });
        });
        it('Should fail to request two factor with an incorrect password', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield server.twoFactor.request({
                    userId,
                    token: userToken,
                    currentPassword: rootPassword,
                    expectedStatus: 403
                });
            });
        });
        it('Should succeed to request two factor without a password when targeting a remote user with an admin account', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield server.twoFactor.request({ userId });
            });
        });
        it('Should fail to request two factor without a password when targeting myself with an admin account', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield server.twoFactor.request({ userId: rootId, expectedStatus: 400 });
                yield server.twoFactor.request({ userId: rootId, currentPassword: 'bad', expectedStatus: 403 });
            });
        });
        it('Should succeed to request my two factor auth', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                {
                    const { otpRequest } = yield server.twoFactor.request({ userId, token: userToken, currentPassword: userPassword });
                    userRequestToken = otpRequest.requestToken;
                    userOTPToken = server_commands_1.TwoFactorCommand.buildOTP({ secret: otpRequest.secret }).generate();
                }
                {
                    const { otpRequest } = yield server.twoFactor.request({ userId: rootId, currentPassword: rootPassword });
                    rootRequestToken = otpRequest.requestToken;
                    rootOTPToken = server_commands_1.TwoFactorCommand.buildOTP({ secret: otpRequest.secret }).generate();
                }
            });
        });
    });
    describe('When confirming two factor request', function () {
        it('Should fail with an unknown user id', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield server.twoFactor.confirmRequest({
                    userId: 42,
                    requestToken: rootRequestToken,
                    otpToken: rootOTPToken,
                    expectedStatus: 404
                });
            });
        });
        it('Should fail with an invalid user id', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield server.twoFactor.confirmRequest({
                    userId: 'invalid',
                    requestToken: rootRequestToken,
                    otpToken: rootOTPToken,
                    expectedStatus: 400
                });
            });
        });
        it('Should fail to confirm another user two factor request without the appropriate rights', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield server.twoFactor.confirmRequest({
                    userId: rootId,
                    token: userToken,
                    requestToken: rootRequestToken,
                    otpToken: rootOTPToken,
                    expectedStatus: 403
                });
            });
        });
        it('Should fail without request token', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield server.twoFactor.confirmRequest({
                    userId,
                    requestToken: undefined,
                    otpToken: userOTPToken,
                    expectedStatus: 400
                });
            });
        });
        it('Should fail with an invalid request token', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield server.twoFactor.confirmRequest({
                    userId,
                    requestToken: 'toto',
                    otpToken: userOTPToken,
                    expectedStatus: 403
                });
            });
        });
        it('Should fail with request token of another user', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield server.twoFactor.confirmRequest({
                    userId,
                    requestToken: rootRequestToken,
                    otpToken: userOTPToken,
                    expectedStatus: 403
                });
            });
        });
        it('Should fail without an otp token', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield server.twoFactor.confirmRequest({
                    userId,
                    requestToken: userRequestToken,
                    otpToken: undefined,
                    expectedStatus: 400
                });
            });
        });
        it('Should fail with a bad otp token', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield server.twoFactor.confirmRequest({
                    userId,
                    requestToken: userRequestToken,
                    otpToken: '123456',
                    expectedStatus: 403
                });
            });
        });
        it('Should succeed to confirm another user two factor request with the appropriate rights', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield server.twoFactor.confirmRequest({
                    userId,
                    requestToken: userRequestToken,
                    otpToken: userOTPToken
                });
                yield server.twoFactor.disable({ userId, currentPassword: rootPassword });
            });
        });
        it('Should succeed to confirm my two factor request', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield server.twoFactor.confirmRequest({
                    userId,
                    token: userToken,
                    requestToken: userRequestToken,
                    otpToken: userOTPToken
                });
            });
        });
        it('Should fail to confirm again two factor request', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield server.twoFactor.confirmRequest({
                    userId,
                    token: userToken,
                    requestToken: userRequestToken,
                    otpToken: userOTPToken,
                    expectedStatus: 400
                });
            });
        });
    });
    describe('When disabling two factor', function () {
        it('Should fail with an unknown user id', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield server.twoFactor.disable({
                    userId: 42,
                    currentPassword: rootPassword,
                    expectedStatus: 404
                });
            });
        });
        it('Should fail with an invalid user id', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield server.twoFactor.disable({
                    userId: 'invalid',
                    currentPassword: rootPassword,
                    expectedStatus: 400
                });
            });
        });
        it('Should fail to disable another user two factor without the appropriate rights', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield server.twoFactor.disable({
                    userId: rootId,
                    token: userToken,
                    currentPassword: userPassword,
                    expectedStatus: 403
                });
            });
        });
        it('Should fail to disable two factor with an incorrect password', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield server.twoFactor.disable({
                    userId,
                    token: userToken,
                    currentPassword: rootPassword,
                    expectedStatus: 403
                });
            });
        });
        it('Should succeed to disable two factor without a password when targeting a remote user with an admin account', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield server.twoFactor.disable({ userId });
                yield server.twoFactor.requestAndConfirm({ userId });
            });
        });
        it('Should fail to disable two factor without a password when targeting myself with an admin account', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield server.twoFactor.disable({ userId: rootId, expectedStatus: 400 });
                yield server.twoFactor.disable({ userId: rootId, currentPassword: 'bad', expectedStatus: 403 });
            });
        });
        it('Should succeed to disable another user two factor with the appropriate rights', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield server.twoFactor.disable({ userId, currentPassword: rootPassword });
                yield server.twoFactor.requestAndConfirm({ userId });
            });
        });
        it('Should succeed to update my two factor auth', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield server.twoFactor.disable({ userId, token: userToken, currentPassword: userPassword });
            });
        });
        it('Should fail to disable again two factor', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield server.twoFactor.disable({
                    userId,
                    token: userToken,
                    currentPassword: userPassword,
                    expectedStatus: 400
                });
            });
        });
    });
    after(function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield (0, server_commands_1.cleanupTests)([server]);
        });
    });
});
//# sourceMappingURL=two-factor.js.map