"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
require("mocha");
const lodash_1 = require("lodash");
const shared_1 = require("../../shared");
const server_commands_1 = require("../../../../shared/server-commands");
describe('Test users API validators', function () {
    const path = '/api/v1/users/';
    let server;
    let serverWithRegistrationDisabled;
    before(function () {
        return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
            this.timeout(30000);
            const res = yield Promise.all([
                (0, server_commands_1.createSingleServer)(1, { signup: { limit: 3 } }),
                (0, server_commands_1.createSingleServer)(2)
            ]);
            server = res[0];
            serverWithRegistrationDisabled = res[1];
            yield (0, server_commands_1.setAccessTokensToServers)([server]);
            yield server.users.generate('moderator2', 1);
        });
    });
    describe('When registering a new user', function () {
        const registrationPath = path + '/register';
        const baseCorrectParams = {
            username: 'user3',
            displayName: 'super user',
            email: 'test3@example.com',
            password: 'my super password'
        };
        it('Should fail with a too small username', function () {
            return (0, 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: registrationPath, token: server.accessToken, fields });
            });
        });
        it('Should fail with a too long username', function () {
            return (0, 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: registrationPath, token: server.accessToken, fields });
            });
        });
        it('Should fail with an incorrect username', function () {
            return (0, 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: registrationPath, token: server.accessToken, fields });
            });
        });
        it('Should fail with a missing email', function () {
            return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                const fields = (0, lodash_1.omit)(baseCorrectParams, 'email');
                yield (0, server_commands_1.makePostBodyRequest)({ url: server.url, path: registrationPath, token: server.accessToken, fields });
            });
        });
        it('Should fail with an invalid email', function () {
            return (0, 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: registrationPath, token: server.accessToken, fields });
            });
        });
        it('Should fail with a too small password', function () {
            return (0, 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: registrationPath, token: server.accessToken, fields });
            });
        });
        it('Should fail with a too long password', function () {
            return (0, 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: registrationPath, token: server.accessToken, fields });
            });
        });
        it('Should fail if we register a user with the same username', function () {
            return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                const fields = Object.assign(Object.assign({}, baseCorrectParams), { username: 'root' });
                yield (0, server_commands_1.makePostBodyRequest)({
                    url: server.url,
                    path: registrationPath,
                    token: server.accessToken,
                    fields,
                    expectedStatus: 409
                });
            });
        });
        it('Should fail with a "peertube" username', function () {
            return (0, 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: registrationPath,
                    token: server.accessToken,
                    fields,
                    expectedStatus: 409
                });
            });
        });
        it('Should fail if we register a user with the same email', function () {
            return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                const fields = Object.assign(Object.assign({}, baseCorrectParams), { email: 'admin' + server.internalServerNumber + '@example.com' });
                yield (0, server_commands_1.makePostBodyRequest)({
                    url: server.url,
                    path: registrationPath,
                    token: server.accessToken,
                    fields,
                    expectedStatus: 409
                });
            });
        });
        it('Should fail with a bad display name', function () {
            return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                const fields = Object.assign(Object.assign({}, baseCorrectParams), { displayName: 'a'.repeat(150) });
                yield (0, server_commands_1.makePostBodyRequest)({ url: server.url, path: registrationPath, token: server.accessToken, fields });
            });
        });
        it('Should fail with a bad channel name', function () {
            return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                const fields = Object.assign(Object.assign({}, baseCorrectParams), { channel: { name: '[]azf', displayName: 'toto' } });
                yield (0, server_commands_1.makePostBodyRequest)({ url: server.url, path: registrationPath, token: server.accessToken, fields });
            });
        });
        it('Should fail with a bad channel display name', function () {
            return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                const fields = Object.assign(Object.assign({}, baseCorrectParams), { channel: { name: 'toto', displayName: '' } });
                yield (0, server_commands_1.makePostBodyRequest)({ url: server.url, path: registrationPath, token: server.accessToken, fields });
            });
        });
        it('Should fail with a channel name that is the same as username', function () {
            return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                const source = { username: 'super_user', channel: { name: 'super_user', displayName: 'display name' } };
                const fields = Object.assign(Object.assign({}, baseCorrectParams), source);
                yield (0, server_commands_1.makePostBodyRequest)({ url: server.url, path: registrationPath, token: server.accessToken, fields });
            });
        });
        it('Should fail with an existing channel', function () {
            return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                const attributes = { name: 'existing_channel', displayName: 'hello', description: 'super description' };
                yield server.channels.create({ attributes });
                const fields = Object.assign(Object.assign({}, baseCorrectParams), { channel: { name: 'existing_channel', displayName: 'toto' } });
                yield (0, server_commands_1.makePostBodyRequest)({
                    url: server.url,
                    path: registrationPath,
                    token: server.accessToken,
                    fields,
                    expectedStatus: 409
                });
            });
        });
        it('Should succeed with the correct params', function () {
            return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                const fields = Object.assign(Object.assign({}, baseCorrectParams), { channel: { name: 'super_channel', displayName: 'toto' } });
                yield (0, server_commands_1.makePostBodyRequest)({
                    url: server.url,
                    path: registrationPath,
                    token: server.accessToken,
                    fields: fields,
                    expectedStatus: 204
                });
            });
        });
        it('Should fail on a server with registration disabled', function () {
            return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                const fields = {
                    username: 'user4',
                    email: 'test4@example.com',
                    password: 'my super password 4'
                };
                yield (0, server_commands_1.makePostBodyRequest)({
                    url: serverWithRegistrationDisabled.url,
                    path: registrationPath,
                    token: serverWithRegistrationDisabled.accessToken,
                    fields,
                    expectedStatus: 403
                });
            });
        });
    });
    describe('When registering multiple users on a server with users limit', function () {
        it('Should fail when after 3 registrations', function () {
            return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                yield server.users.register({ username: 'user42', expectedStatus: 403 });
            });
        });
    });
    describe('When asking a password reset', function () {
        const path = '/api/v1/users/ask-reset-password';
        it('Should fail with a missing email', function () {
            return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                const fields = {};
                yield (0, server_commands_1.makePostBodyRequest)({ url: server.url, path, token: server.accessToken, fields });
            });
        });
        it('Should fail with an invalid email', function () {
            return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                const fields = { email: 'hello' };
                yield (0, server_commands_1.makePostBodyRequest)({ url: server.url, path, token: server.accessToken, fields });
            });
        });
        it('Should success with the correct params', function () {
            return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                const fields = { email: 'admin@example.com' };
                yield (0, server_commands_1.makePostBodyRequest)({
                    url: server.url,
                    path,
                    token: server.accessToken,
                    fields,
                    expectedStatus: 204
                });
            });
        });
    });
    describe('When asking for an account verification email', function () {
        const path = '/api/v1/users/ask-send-verify-email';
        it('Should fail with a missing email', function () {
            return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                const fields = {};
                yield (0, server_commands_1.makePostBodyRequest)({ url: server.url, path, token: server.accessToken, fields });
            });
        });
        it('Should fail with an invalid email', function () {
            return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                const fields = { email: 'hello' };
                yield (0, server_commands_1.makePostBodyRequest)({ url: server.url, path, token: server.accessToken, fields });
            });
        });
        it('Should succeed with the correct params', function () {
            return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                const fields = { email: 'admin@example.com' };
                yield (0, server_commands_1.makePostBodyRequest)({
                    url: server.url,
                    path,
                    token: server.accessToken,
                    fields,
                    expectedStatus: 204
                });
            });
        });
    });
    after(function () {
        return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
            shared_1.MockSmtpServer.Instance.kill();
            yield (0, server_commands_1.cleanupTests)([server, serverWithRegistrationDisabled]);
        });
    });
});
//# sourceMappingURL=users.js.map