"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const shared_1 = require("../../shared");
const core_utils_1 = require("../../../../shared/core-utils");
const server_commands_1 = require("../../../../shared/server-commands");
describe('Test users admin API validators', function () {
    const path = '/api/v1/users/';
    let userId;
    let rootId;
    let moderatorId;
    let server;
    let userToken = '';
    let moderatorToken = '';
    let emailPort;
    before(function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.timeout(30000);
            const emails = [];
            emailPort = yield shared_1.MockSmtpServer.Instance.collectEmails(emails);
            {
                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;
            }
            {
                const result = yield server.users.generate('moderator1', 1);
                moderatorToken = result.token;
            }
            {
                const result = yield server.users.generate('moderator2', 1);
                moderatorId = result.userId;
            }
        });
    });
    describe('When listing users', function () {
        it('Should fail with a bad start pagination', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield (0, shared_1.checkBadStartPagination)(server.url, path, server.accessToken);
            });
        });
        it('Should fail with a bad count pagination', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield (0, shared_1.checkBadCountPagination)(server.url, path, server.accessToken);
            });
        });
        it('Should fail with an incorrect sort', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield (0, shared_1.checkBadSortPagination)(server.url, path, server.accessToken);
            });
        });
        it('Should fail with a non authenticated user', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield (0, server_commands_1.makeGetRequest)({
                    url: server.url,
                    path,
                    expectedStatus: 401
                });
            });
        });
        it('Should fail with a non admin user', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield (0, server_commands_1.makeGetRequest)({
                    url: server.url,
                    path,
                    token: userToken,
                    expectedStatus: 403
                });
            });
        });
    });
    describe('When adding a new user', function () {
        const baseCorrectParams = {
            username: 'user2',
            email: 'test@example.com',
            password: 'my super password',
            videoQuota: -1,
            videoQuotaDaily: -1,
            role: 2,
            adminFlags: 1
        };
        it('Should fail with a too small username', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = Object.assign(Object.assign({}, baseCorrectParams), { username: '' });
                yield (0, server_commands_1.makePostBodyRequest)({ url: server.url, path, token: server.accessToken, fields });
            });
        });
        it('Should fail with a too long username', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = Object.assign(Object.assign({}, baseCorrectParams), { username: 'super'.repeat(50) });
                yield (0, server_commands_1.makePostBodyRequest)({ url: server.url, path, token: server.accessToken, fields });
            });
        });
        it('Should fail with a not lowercase username', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = Object.assign(Object.assign({}, baseCorrectParams), { username: 'Toto' });
                yield (0, server_commands_1.makePostBodyRequest)({ url: server.url, path, token: server.accessToken, fields });
            });
        });
        it('Should fail with an incorrect username', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = Object.assign(Object.assign({}, baseCorrectParams), { username: 'my username' });
                yield (0, server_commands_1.makePostBodyRequest)({ url: server.url, path, token: server.accessToken, fields });
            });
        });
        it('Should fail with a missing email', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = (0, core_utils_1.omit)(baseCorrectParams, ['email']);
                yield (0, server_commands_1.makePostBodyRequest)({ url: server.url, path, token: server.accessToken, fields });
            });
        });
        it('Should fail with an invalid email', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = Object.assign(Object.assign({}, baseCorrectParams), { email: 'test_example.com' });
                yield (0, server_commands_1.makePostBodyRequest)({ url: server.url, path, token: server.accessToken, fields });
            });
        });
        it('Should fail with a too small password', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = Object.assign(Object.assign({}, baseCorrectParams), { password: 'bla' });
                yield (0, server_commands_1.makePostBodyRequest)({ url: server.url, path, token: server.accessToken, fields });
            });
        });
        it('Should fail with a too long password', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = Object.assign(Object.assign({}, baseCorrectParams), { password: 'super'.repeat(61) });
                yield (0, server_commands_1.makePostBodyRequest)({ url: server.url, path, token: server.accessToken, fields });
            });
        });
        it('Should fail with empty password and no smtp configured', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = Object.assign(Object.assign({}, baseCorrectParams), { password: '' });
                yield (0, server_commands_1.makePostBodyRequest)({ url: server.url, path, token: server.accessToken, fields });
            });
        });
        it('Should succeed with no password on a server with smtp enabled', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(20000);
                yield (0, server_commands_1.killallServers)([server]);
                const config = {
                    smtp: {
                        hostname: '127.0.0.1',
                        port: emailPort
                    }
                };
                yield server.run(config);
                const fields = Object.assign(Object.assign({}, baseCorrectParams), { password: '', username: 'create_password', email: 'create_password@example.com' });
                yield (0, server_commands_1.makePostBodyRequest)({
                    url: server.url,
                    path,
                    token: server.accessToken,
                    fields,
                    expectedStatus: 200
                });
            });
        });
        it('Should fail with invalid admin flags', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = Object.assign(Object.assign({}, baseCorrectParams), { adminFlags: 'toto' });
                yield (0, server_commands_1.makePostBodyRequest)({ url: server.url, path, token: server.accessToken, fields });
            });
        });
        it('Should fail with an non authenticated user', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield (0, server_commands_1.makePostBodyRequest)({
                    url: server.url,
                    path,
                    token: 'super token',
                    fields: baseCorrectParams,
                    expectedStatus: 401
                });
            });
        });
        it('Should fail if we add a user with the same username', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = Object.assign(Object.assign({}, baseCorrectParams), { username: 'user1' });
                yield (0, server_commands_1.makePostBodyRequest)({
                    url: server.url,
                    path,
                    token: server.accessToken,
                    fields,
                    expectedStatus: 409
                });
            });
        });
        it('Should fail if we add a user with the same email', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = Object.assign(Object.assign({}, baseCorrectParams), { email: 'user1@example.com' });
                yield (0, server_commands_1.makePostBodyRequest)({
                    url: server.url,
                    path,
                    token: server.accessToken,
                    fields,
                    expectedStatus: 409
                });
            });
        });
        it('Should fail without a videoQuota', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = (0, core_utils_1.omit)(baseCorrectParams, ['videoQuota']);
                yield (0, server_commands_1.makePostBodyRequest)({ url: server.url, path, token: server.accessToken, fields });
            });
        });
        it('Should fail without a videoQuotaDaily', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = (0, core_utils_1.omit)(baseCorrectParams, ['videoQuotaDaily']);
                yield (0, server_commands_1.makePostBodyRequest)({ url: server.url, path, token: server.accessToken, fields });
            });
        });
        it('Should fail with an invalid videoQuota', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = Object.assign(Object.assign({}, baseCorrectParams), { videoQuota: -5 });
                yield (0, server_commands_1.makePostBodyRequest)({ url: server.url, path, token: server.accessToken, fields });
            });
        });
        it('Should fail with an invalid videoQuotaDaily', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = Object.assign(Object.assign({}, baseCorrectParams), { videoQuotaDaily: -7 });
                yield (0, server_commands_1.makePostBodyRequest)({ url: server.url, path, token: server.accessToken, fields });
            });
        });
        it('Should fail without a user role', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = (0, core_utils_1.omit)(baseCorrectParams, ['role']);
                yield (0, server_commands_1.makePostBodyRequest)({ url: server.url, path, token: server.accessToken, fields });
            });
        });
        it('Should fail with an invalid user role', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = Object.assign(Object.assign({}, baseCorrectParams), { role: 88989 });
                yield (0, server_commands_1.makePostBodyRequest)({ url: server.url, path, token: server.accessToken, fields });
            });
        });
        it('Should fail with a "peertube" username', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = Object.assign(Object.assign({}, baseCorrectParams), { username: 'peertube' });
                yield (0, server_commands_1.makePostBodyRequest)({
                    url: server.url,
                    path,
                    token: server.accessToken,
                    fields,
                    expectedStatus: 409
                });
            });
        });
        it('Should fail to create a moderator or an admin with a moderator', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                for (const role of [1, 0]) {
                    const fields = Object.assign(Object.assign({}, baseCorrectParams), { role });
                    yield (0, server_commands_1.makePostBodyRequest)({
                        url: server.url,
                        path,
                        token: moderatorToken,
                        fields,
                        expectedStatus: 403
                    });
                }
            });
        });
        it('Should succeed to create a user with a moderator', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = Object.assign(Object.assign({}, baseCorrectParams), { username: 'a4656', email: 'a4656@example.com', role: 2 });
                yield (0, server_commands_1.makePostBodyRequest)({
                    url: server.url,
                    path,
                    token: moderatorToken,
                    fields,
                    expectedStatus: 200
                });
            });
        });
        it('Should succeed with the correct params', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield (0, server_commands_1.makePostBodyRequest)({
                    url: server.url,
                    path,
                    token: server.accessToken,
                    fields: baseCorrectParams,
                    expectedStatus: 200
                });
            });
        });
        it('Should fail with a non admin user', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const user = { username: 'user1' };
                userToken = yield server.login.getAccessToken(user);
                const fields = {
                    username: 'user3',
                    email: 'test@example.com',
                    password: 'my super password',
                    videoQuota: 42000000
                };
                yield (0, server_commands_1.makePostBodyRequest)({ url: server.url, path, token: userToken, fields, expectedStatus: 403 });
            });
        });
    });
    describe('When getting a user', function () {
        it('Should fail with an non authenticated user', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield (0, server_commands_1.makeGetRequest)({
                    url: server.url,
                    path: path + userId,
                    token: 'super token',
                    expectedStatus: 401
                });
            });
        });
        it('Should fail with a non admin user', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield (0, server_commands_1.makeGetRequest)({ url: server.url, path, token: userToken, expectedStatus: 403 });
            });
        });
        it('Should succeed with the correct params', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield (0, server_commands_1.makeGetRequest)({ url: server.url, path: path + userId, token: server.accessToken, expectedStatus: 200 });
            });
        });
    });
    describe('When updating a user', function () {
        it('Should fail with an invalid email attribute', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = {
                    email: 'blabla'
                };
                yield (0, server_commands_1.makePutBodyRequest)({ url: server.url, path: path + userId, token: server.accessToken, fields });
            });
        });
        it('Should fail with an invalid emailVerified attribute', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = {
                    emailVerified: 'yes'
                };
                yield (0, server_commands_1.makePutBodyRequest)({ url: server.url, path: path + userId, token: server.accessToken, fields });
            });
        });
        it('Should fail with an invalid videoQuota attribute', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = {
                    videoQuota: -90
                };
                yield (0, server_commands_1.makePutBodyRequest)({ url: server.url, path: path + userId, token: server.accessToken, fields });
            });
        });
        it('Should fail with an invalid user role attribute', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = {
                    role: 54878
                };
                yield (0, server_commands_1.makePutBodyRequest)({ url: server.url, path: path + userId, token: server.accessToken, fields });
            });
        });
        it('Should fail with a too small password', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = {
                    currentPassword: 'password',
                    password: 'bla'
                };
                yield (0, server_commands_1.makePutBodyRequest)({ url: server.url, path: path + userId, token: server.accessToken, fields });
            });
        });
        it('Should fail with a too long password', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = {
                    currentPassword: 'password',
                    password: 'super'.repeat(61)
                };
                yield (0, server_commands_1.makePutBodyRequest)({ url: server.url, path: path + userId, token: server.accessToken, fields });
            });
        });
        it('Should fail with an non authenticated user', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = {
                    videoQuota: 42
                };
                yield (0, server_commands_1.makePutBodyRequest)({
                    url: server.url,
                    path: path + userId,
                    token: 'super token',
                    fields,
                    expectedStatus: 401
                });
            });
        });
        it('Should fail when updating root role', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = {
                    role: 1
                };
                yield (0, server_commands_1.makePutBodyRequest)({ url: server.url, path: path + rootId, token: server.accessToken, fields });
            });
        });
        it('Should fail with invalid admin flags', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = { adminFlags: 'toto' };
                yield (0, server_commands_1.makePutBodyRequest)({ url: server.url, path, token: server.accessToken, fields });
            });
        });
        it('Should fail to update an admin with a moderator', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = {
                    videoQuota: 42
                };
                yield (0, server_commands_1.makePutBodyRequest)({
                    url: server.url,
                    path: path + moderatorId,
                    token: moderatorToken,
                    fields,
                    expectedStatus: 403
                });
            });
        });
        it('Should succeed to update a user with a moderator', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = {
                    videoQuota: 42
                };
                yield (0, server_commands_1.makePutBodyRequest)({
                    url: server.url,
                    path: path + userId,
                    token: moderatorToken,
                    fields,
                    expectedStatus: 204
                });
            });
        });
        it('Should succeed with the correct params', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = {
                    email: 'email@example.com',
                    emailVerified: true,
                    videoQuota: 42,
                    role: 2
                };
                yield (0, server_commands_1.makePutBodyRequest)({
                    url: server.url,
                    path: path + userId,
                    token: server.accessToken,
                    fields,
                    expectedStatus: 204
                });
            });
        });
    });
    after(function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            shared_1.MockSmtpServer.Instance.kill();
            yield (0, server_commands_1.cleanupTests)([server]);
        });
    });
});
//# sourceMappingURL=users-admin.js.map