"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 my user API validators', function () {
    const path = '/api/v1/users/';
    let userId;
    let rootId;
    let moderatorId;
    let video;
    let server;
    let userToken = '';
    let moderatorToken = '';
    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;
            }
            {
                const result = yield server.users.generate('moderator1', 1);
                moderatorToken = result.token;
            }
            {
                const result = yield server.users.generate('moderator2', 1);
                moderatorId = result.userId;
            }
            {
                video = yield server.videos.upload();
            }
        });
    });
    describe('When updating my account', 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 + 'me', 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 + 'me', token: userToken, 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 + 'me', token: userToken, fields });
            });
        });
        it('Should fail without the current 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 + 'me', token: userToken, fields });
            });
        });
        it('Should fail with an invalid current password', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = {
                    currentPassword: 'my super password fail',
                    password: 'super'.repeat(61)
                };
                yield (0, server_commands_1.makePutBodyRequest)({
                    url: server.url,
                    path: path + 'me',
                    token: userToken,
                    fields,
                    expectedStatus: 401
                });
            });
        });
        it('Should fail with an invalid NSFW policy attribute', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = {
                    nsfwPolicy: 'hello'
                };
                yield (0, server_commands_1.makePutBodyRequest)({ url: server.url, path: path + 'me', token: userToken, fields });
            });
        });
        it('Should fail with an invalid autoPlayVideo attribute', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = {
                    autoPlayVideo: -1
                };
                yield (0, server_commands_1.makePutBodyRequest)({ url: server.url, path: path + 'me', token: userToken, fields });
            });
        });
        it('Should fail with an invalid autoPlayNextVideo attribute', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = {
                    autoPlayNextVideo: -1
                };
                yield (0, server_commands_1.makePutBodyRequest)({ url: server.url, path: path + 'me', token: userToken, fields });
            });
        });
        it('Should fail with an invalid videosHistoryEnabled attribute', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = {
                    videosHistoryEnabled: -1
                };
                yield (0, server_commands_1.makePutBodyRequest)({ url: server.url, path: path + 'me', token: userToken, fields });
            });
        });
        it('Should fail with an non authenticated user', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = {
                    currentPassword: 'password',
                    password: 'my super password'
                };
                yield (0, server_commands_1.makePutBodyRequest)({
                    url: server.url,
                    path: path + 'me',
                    token: 'super token',
                    fields,
                    expectedStatus: 401
                });
            });
        });
        it('Should fail with a too long description', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = {
                    description: 'super'.repeat(201)
                };
                yield (0, server_commands_1.makePutBodyRequest)({ url: server.url, path: path + 'me', token: userToken, fields });
            });
        });
        it('Should fail with an invalid videoLanguages attribute', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                {
                    const fields = {
                        videoLanguages: 'toto'
                    };
                    yield (0, server_commands_1.makePutBodyRequest)({ url: server.url, path: path + 'me', token: userToken, fields });
                }
                {
                    const languages = [];
                    for (let i = 0; i < 1000; i++) {
                        languages.push('fr');
                    }
                    const fields = {
                        videoLanguages: languages
                    };
                    yield (0, server_commands_1.makePutBodyRequest)({ url: server.url, path: path + 'me', token: userToken, fields });
                }
            });
        });
        it('Should fail with an invalid theme', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = { theme: 'invalid' };
                yield (0, server_commands_1.makePutBodyRequest)({ url: server.url, path: path + 'me', token: userToken, fields });
            });
        });
        it('Should fail with an unknown theme', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = { theme: 'peertube-theme-unknown' };
                yield (0, server_commands_1.makePutBodyRequest)({ url: server.url, path: path + 'me', token: userToken, fields });
            });
        });
        it('Should fail with invalid no modal attributes', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const keys = [
                    'noInstanceConfigWarningModal',
                    'noAccountSetupWarningModal',
                    'noWelcomeModal'
                ];
                for (const key of keys) {
                    const fields = {
                        [key]: -1
                    };
                    yield (0, server_commands_1.makePutBodyRequest)({ url: server.url, path: path + 'me', token: userToken, fields });
                }
            });
        });
        it('Should succeed to change password with the correct params', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = {
                    currentPassword: 'password',
                    password: 'my super password',
                    nsfwPolicy: 'blur',
                    autoPlayVideo: false,
                    email: 'super_email@example.com',
                    theme: 'default',
                    noInstanceConfigWarningModal: true,
                    noWelcomeModal: true,
                    noAccountSetupWarningModal: true
                };
                yield (0, server_commands_1.makePutBodyRequest)({
                    url: server.url,
                    path: path + 'me',
                    token: userToken,
                    fields,
                    expectedStatus: 204
                });
            });
        });
        it('Should succeed without password change with the correct params', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = {
                    nsfwPolicy: 'blur',
                    autoPlayVideo: false
                };
                yield (0, server_commands_1.makePutBodyRequest)({
                    url: server.url,
                    path: path + 'me',
                    token: userToken,
                    fields,
                    expectedStatus: 204
                });
            });
        });
    });
    describe('When updating my avatar', function () {
        it('Should fail without an incorrect input file', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = {};
                const attaches = {
                    avatarfile: (0, core_utils_1.buildAbsoluteFixturePath)('video_short.mp4')
                };
                yield (0, server_commands_1.makeUploadRequest)({ url: server.url, path: path + '/me/avatar/pick', token: server.accessToken, fields, attaches });
            });
        });
        it('Should fail with a big file', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = {};
                const attaches = {
                    avatarfile: (0, core_utils_1.buildAbsoluteFixturePath)('avatar-big.png')
                };
                yield (0, server_commands_1.makeUploadRequest)({ url: server.url, path: path + '/me/avatar/pick', token: server.accessToken, fields, attaches });
            });
        });
        it('Should fail with an unauthenticated user', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = {};
                const attaches = {
                    avatarfile: (0, core_utils_1.buildAbsoluteFixturePath)('avatar.png')
                };
                yield (0, server_commands_1.makeUploadRequest)({
                    url: server.url,
                    path: path + '/me/avatar/pick',
                    fields,
                    attaches,
                    expectedStatus: 401
                });
            });
        });
        it('Should succeed with the correct params', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = {};
                const attaches = {
                    avatarfile: (0, core_utils_1.buildAbsoluteFixturePath)('avatar.png')
                };
                yield (0, server_commands_1.makeUploadRequest)({
                    url: server.url,
                    path: path + '/me/avatar/pick',
                    token: server.accessToken,
                    fields,
                    attaches,
                    expectedStatus: 200
                });
            });
        });
    });
    describe('When managing my scoped tokens', function () {
        it('Should fail to get my scoped tokens with an non authenticated user', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield server.users.getMyScopedTokens({ token: null, expectedStatus: 401 });
            });
        });
        it('Should fail to get my scoped tokens with a bad token', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield server.users.getMyScopedTokens({ token: 'bad', expectedStatus: 401 });
            });
        });
        it('Should succeed to get my scoped tokens', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield server.users.getMyScopedTokens();
            });
        });
        it('Should fail to renew my scoped tokens with an non authenticated user', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield server.users.renewMyScopedTokens({ token: null, expectedStatus: 401 });
            });
        });
        it('Should fail to renew my scoped tokens with a bad token', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield server.users.renewMyScopedTokens({ token: 'bad', expectedStatus: 401 });
            });
        });
        it('Should succeed to renew my scoped tokens', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield server.users.renewMyScopedTokens();
            });
        });
    });
    describe('When getting my information', function () {
        it('Should fail with a non authenticated user', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield server.users.getMyInfo({ token: 'fake_token', expectedStatus: 401 });
            });
        });
        it('Should success with the correct parameters', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield server.users.getMyInfo({ token: userToken });
            });
        });
    });
    describe('When getting my video rating', function () {
        let command;
        before(function () {
            command = server.users;
        });
        it('Should fail with a non authenticated user', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield command.getMyRating({ token: 'fake_token', videoId: video.id, expectedStatus: 401 });
            });
        });
        it('Should fail with an incorrect video uuid', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield command.getMyRating({ videoId: 'blabla', expectedStatus: 400 });
            });
        });
        it('Should fail with an unknown video', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield command.getMyRating({ videoId: '4da6fde3-88f7-4d16-b119-108df5630b06', expectedStatus: 404 });
            });
        });
        it('Should succeed with the correct parameters', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield command.getMyRating({ videoId: video.id });
                yield command.getMyRating({ videoId: video.uuid });
                yield command.getMyRating({ videoId: video.shortUUID });
            });
        });
    });
    describe('When retrieving my global ratings', function () {
        const path = '/api/v1/accounts/user1/ratings';
        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, userToken);
            });
        });
        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, userToken);
            });
        });
        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, userToken);
            });
        });
        it('Should fail with a unauthenticated 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 another user', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield (0, server_commands_1.makeGetRequest)({ url: server.url, path, token: server.accessToken, expectedStatus: 403 });
            });
        });
        it('Should fail with a bad type', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield (0, server_commands_1.makeGetRequest)({
                    url: server.url,
                    path,
                    token: userToken,
                    query: { rating: 'toto ' },
                    expectedStatus: 400
                });
            });
        });
        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, token: userToken, expectedStatus: 200 });
            });
        });
    });
    describe('When getting my global followers', function () {
        const path = '/api/v1/accounts/user1/followers';
        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, userToken);
            });
        });
        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, userToken);
            });
        });
        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, userToken);
            });
        });
        it('Should fail with a unauthenticated 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 another user', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield (0, server_commands_1.makeGetRequest)({ url: server.url, path, token: server.accessToken, 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, token: userToken, expectedStatus: 200 });
            });
        });
    });
    describe('When blocking/unblocking/removing user', function () {
        it('Should fail with an incorrect id', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const options = { userId: 'blabla', expectedStatus: 400 };
                yield server.users.remove(options);
                yield server.users.banUser({ userId: 'blabla', expectedStatus: 400 });
                yield server.users.unbanUser({ userId: 'blabla', expectedStatus: 400 });
            });
        });
        it('Should fail with the root user', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const options = { userId: rootId, expectedStatus: 400 };
                yield server.users.remove(options);
                yield server.users.banUser(options);
                yield server.users.unbanUser(options);
            });
        });
        it('Should return 404 with a non existing id', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const options = { userId: 4545454, expectedStatus: 404 };
                yield server.users.remove(options);
                yield server.users.banUser(options);
                yield server.users.unbanUser(options);
            });
        });
        it('Should fail with a non admin user', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const options = { userId, token: userToken, expectedStatus: 403 };
                yield server.users.remove(options);
                yield server.users.banUser(options);
                yield server.users.unbanUser(options);
            });
        });
        it('Should fail on a moderator with a moderator', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const options = { userId: moderatorId, token: moderatorToken, expectedStatus: 403 };
                yield server.users.remove(options);
                yield server.users.banUser(options);
                yield server.users.unbanUser(options);
            });
        });
        it('Should succeed on a user with a moderator', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const options = { userId, token: moderatorToken };
                yield server.users.banUser(options);
                yield server.users.unbanUser(options);
            });
        });
    });
    describe('When deleting our account', function () {
        it('Should fail with with the root account', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield server.users.deleteMe({ expectedStatus: 400 });
            });
        });
    });
    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=my-user.js.map