"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const chai_1 = require("chai");
const shared_1 = require("../../shared");
const core_utils_1 = require("../../../../shared/core-utils");
const server_commands_1 = require("../../../../shared/server-commands");
describe('Test video channels API validator', function () {
    const videoChannelPath = '/api/v1/video-channels';
    let server;
    const userInfo = {
        accessToken: '',
        channelName: 'fake_channel',
        id: -1,
        videoQuota: -1,
        videoQuotaDaily: -1
    };
    let command;
    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 userCreds = {
                username: 'fake',
                password: 'fake_password'
            };
            {
                const user = yield server.users.create({ username: userCreds.username, password: userCreds.password });
                userInfo.id = user.id;
                userInfo.accessToken = yield server.login.getAccessToken(userCreds);
            }
            command = server.channels;
        });
    });
    describe('When listing a video channels', 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, videoChannelPath, 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, videoChannelPath, 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, videoChannelPath, server.accessToken);
            });
        });
    });
    describe('When listing account video channels', function () {
        const accountChannelPath = '/api/v1/accounts/fake/video-channels';
        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, accountChannelPath, 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, accountChannelPath, 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, accountChannelPath, server.accessToken);
            });
        });
        it('Should fail with a unknown account', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield server.channels.listByAccount({ accountName: 'unknown', expectedStatus: 404 });
            });
        });
        it('Should succeed with the correct parameters', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield (0, server_commands_1.makeGetRequest)({
                    url: server.url,
                    path: accountChannelPath,
                    expectedStatus: 200
                });
            });
        });
    });
    describe('When adding a video channel', function () {
        const baseCorrectParams = {
            name: 'super_channel',
            displayName: 'hello',
            description: 'super description',
            support: 'super support text'
        };
        it('Should fail with a non authenticated user', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield (0, server_commands_1.makePostBodyRequest)({
                    url: server.url,
                    path: videoChannelPath,
                    token: 'none',
                    fields: baseCorrectParams,
                    expectedStatus: 401
                });
            });
        });
        it('Should fail with nothing', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = {};
                yield (0, server_commands_1.makePostBodyRequest)({ url: server.url, path: videoChannelPath, token: server.accessToken, fields });
            });
        });
        it('Should fail without a name', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = (0, core_utils_1.omit)(baseCorrectParams, ['name']);
                yield (0, server_commands_1.makePostBodyRequest)({ url: server.url, path: videoChannelPath, token: server.accessToken, fields });
            });
        });
        it('Should fail with a bad name', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = Object.assign(Object.assign({}, baseCorrectParams), { name: 'super name' });
                yield (0, server_commands_1.makePostBodyRequest)({ url: server.url, path: videoChannelPath, token: server.accessToken, fields });
            });
        });
        it('Should fail without a name', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = (0, core_utils_1.omit)(baseCorrectParams, ['displayName']);
                yield (0, server_commands_1.makePostBodyRequest)({ url: server.url, path: videoChannelPath, token: server.accessToken, fields });
            });
        });
        it('Should fail with a long name', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = Object.assign(Object.assign({}, baseCorrectParams), { displayName: 'super'.repeat(25) });
                yield (0, server_commands_1.makePostBodyRequest)({ url: server.url, path: videoChannelPath, token: server.accessToken, fields });
            });
        });
        it('Should fail with a long description', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = Object.assign(Object.assign({}, baseCorrectParams), { description: 'super'.repeat(201) });
                yield (0, server_commands_1.makePostBodyRequest)({ url: server.url, path: videoChannelPath, token: server.accessToken, fields });
            });
        });
        it('Should fail with a long support text', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = Object.assign(Object.assign({}, baseCorrectParams), { support: 'super'.repeat(201) });
                yield (0, server_commands_1.makePostBodyRequest)({ url: server.url, path: videoChannelPath, token: server.accessToken, fields });
            });
        });
        it('Should succeed with the correct parameters', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield (0, server_commands_1.makePostBodyRequest)({
                    url: server.url,
                    path: videoChannelPath,
                    token: server.accessToken,
                    fields: baseCorrectParams,
                    expectedStatus: 200
                });
            });
        });
        it('Should fail when adding a channel with the same username', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield (0, server_commands_1.makePostBodyRequest)({
                    url: server.url,
                    path: videoChannelPath,
                    token: server.accessToken,
                    fields: baseCorrectParams,
                    expectedStatus: 409
                });
            });
        });
    });
    describe('When updating a video channel', function () {
        const baseCorrectParams = {
            displayName: 'hello',
            description: 'super description',
            support: 'toto',
            bulkVideosSupportUpdate: false
        };
        let path;
        before(function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                path = videoChannelPath + '/super_channel';
            });
        });
        it('Should fail with a non authenticated user', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield (0, server_commands_1.makePutBodyRequest)({
                    url: server.url,
                    path,
                    token: 'hi',
                    fields: baseCorrectParams,
                    expectedStatus: 401
                });
            });
        });
        it('Should fail with another authenticated user', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield (0, server_commands_1.makePutBodyRequest)({
                    url: server.url,
                    path,
                    token: userInfo.accessToken,
                    fields: baseCorrectParams,
                    expectedStatus: 403
                });
            });
        });
        it('Should fail with a long name', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = Object.assign(Object.assign({}, baseCorrectParams), { displayName: 'super'.repeat(25) });
                yield (0, server_commands_1.makePutBodyRequest)({ url: server.url, path, token: server.accessToken, fields });
            });
        });
        it('Should fail with a long description', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = Object.assign(Object.assign({}, baseCorrectParams), { description: 'super'.repeat(201) });
                yield (0, server_commands_1.makePutBodyRequest)({ url: server.url, path, token: server.accessToken, fields });
            });
        });
        it('Should fail with a long support text', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = Object.assign(Object.assign({}, baseCorrectParams), { support: 'super'.repeat(201) });
                yield (0, server_commands_1.makePutBodyRequest)({ url: server.url, path, token: server.accessToken, fields });
            });
        });
        it('Should fail with a bad bulkVideosSupportUpdate field', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = Object.assign(Object.assign({}, baseCorrectParams), { bulkVideosSupportUpdate: 'super' });
                yield (0, server_commands_1.makePutBodyRequest)({ url: server.url, path, token: server.accessToken, fields });
            });
        });
        it('Should succeed with the correct parameters', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield (0, server_commands_1.makePutBodyRequest)({
                    url: server.url,
                    path,
                    token: server.accessToken,
                    fields: baseCorrectParams,
                    expectedStatus: 204
                });
            });
        });
    });
    describe('When updating video channel avatars/banners', function () {
        const types = ['avatar', 'banner'];
        let path;
        before(function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                path = videoChannelPath + '/super_channel';
            });
        });
        it('Should fail with an incorrect input file', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                for (const type of types) {
                    const fields = {};
                    const attaches = {
                        [type + 'file']: (0, core_utils_1.buildAbsoluteFixturePath)('video_short.mp4')
                    };
                    yield (0, server_commands_1.makeUploadRequest)({ url: server.url, path: `${path}/${type}/pick`, token: server.accessToken, fields, attaches });
                }
            });
        });
        it('Should fail with a big file', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                for (const type of types) {
                    const fields = {};
                    const attaches = {
                        [type + 'file']: (0, core_utils_1.buildAbsoluteFixturePath)('avatar-big.png')
                    };
                    yield (0, server_commands_1.makeUploadRequest)({ url: server.url, path: `${path}/${type}/pick`, token: server.accessToken, fields, attaches });
                }
            });
        });
        it('Should fail with an unauthenticated user', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                for (const type of types) {
                    const fields = {};
                    const attaches = {
                        [type + 'file']: (0, core_utils_1.buildAbsoluteFixturePath)('avatar.png')
                    };
                    yield (0, server_commands_1.makeUploadRequest)({
                        url: server.url,
                        path: `${path}/${type}/pick`,
                        fields,
                        attaches,
                        expectedStatus: 401
                    });
                }
            });
        });
        it('Should succeed with the correct params', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                for (const type of types) {
                    const fields = {};
                    const attaches = {
                        [type + 'file']: (0, core_utils_1.buildAbsoluteFixturePath)('avatar.png')
                    };
                    yield (0, server_commands_1.makeUploadRequest)({
                        url: server.url,
                        path: `${path}/${type}/pick`,
                        token: server.accessToken,
                        fields,
                        attaches,
                        expectedStatus: 200
                    });
                }
            });
        });
    });
    describe('When getting a video channel', function () {
        it('Should return the list of the video channels with nothing', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const res = yield (0, server_commands_1.makeGetRequest)({
                    url: server.url,
                    path: videoChannelPath,
                    expectedStatus: 200
                });
                (0, chai_1.expect)(res.body.data).to.be.an('array');
            });
        });
        it('Should return 404 with an incorrect video channel', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield (0, server_commands_1.makeGetRequest)({
                    url: server.url,
                    path: videoChannelPath + '/super_channel2',
                    expectedStatus: 404
                });
            });
        });
        it('Should succeed with the correct parameters', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield (0, server_commands_1.makeGetRequest)({
                    url: server.url,
                    path: videoChannelPath + '/super_channel',
                    expectedStatus: 200
                });
            });
        });
    });
    describe('When getting channel followers', function () {
        const path = '/api/v1/video-channels/super_channel/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, 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 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: userInfo.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: server.accessToken, expectedStatus: 200 });
            });
        });
    });
    describe('When deleting a video channel', function () {
        it('Should fail with a non authenticated user', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield command.delete({ token: 'coucou', channelName: 'super_channel', expectedStatus: 401 });
            });
        });
        it('Should fail with another authenticated user', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield command.delete({ token: userInfo.accessToken, channelName: 'super_channel', expectedStatus: 403 });
            });
        });
        it('Should fail with an unknown video channel id', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield command.delete({ channelName: 'super_channel2', expectedStatus: 404 });
            });
        });
        it('Should succeed with the correct parameters', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield command.delete({ channelName: 'super_channel' });
            });
        });
        it('Should fail to delete the last user video channel', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield command.delete({ channelName: 'root_channel', expectedStatus: 409 });
            });
        });
    });
    after(function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield (0, server_commands_1.cleanupTests)([server]);
        });
    });
});
//# sourceMappingURL=video-channels.js.map