"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const chai_1 = require("chai");
const core_utils_1 = require("../../../../shared/core-utils");
const server_commands_1 = require("../../../../shared/server-commands");
describe('Test video lives API validator', function () {
    const path = '/api/v1/videos/live';
    let server;
    let userAccessToken = '';
    let channelId;
    let video;
    let videoIdNotLive;
    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]);
            yield server.config.updateCustomSubConfig({
                newConfig: {
                    live: {
                        enabled: true,
                        latencySetting: {
                            enabled: false
                        },
                        maxInstanceLives: 20,
                        maxUserLives: 20,
                        allowReplay: true
                    }
                }
            });
            const username = 'user1';
            const password = 'my super password';
            yield server.users.create({ username, password });
            userAccessToken = yield server.login.getAccessToken({ username, password });
            {
                const { videoChannels } = yield server.users.getMyInfo();
                channelId = videoChannels[0].id;
            }
            {
                videoIdNotLive = (yield server.videos.quickUpload({ name: 'not live' })).id;
            }
            command = server.live;
        });
    });
    describe('When creating a live', function () {
        let baseCorrectParams;
        before(function () {
            baseCorrectParams = {
                name: 'my super name',
                category: 5,
                licence: 1,
                language: 'pt',
                nsfw: false,
                commentsEnabled: true,
                downloadEnabled: true,
                waitTranscoding: true,
                description: 'my super description',
                support: 'my super support text',
                tags: ['tag1', 'tag2'],
                privacy: 1,
                channelId,
                saveReplay: false,
                permanentLive: false,
                latencyMode: 1
            };
        });
        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, 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), { name: 'super'.repeat(65) });
                yield (0, server_commands_1.makePostBodyRequest)({ url: server.url, path, token: server.accessToken, fields });
            });
        });
        it('Should fail with a bad category', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = Object.assign(Object.assign({}, baseCorrectParams), { category: 125 });
                yield (0, server_commands_1.makePostBodyRequest)({ url: server.url, path, token: server.accessToken, fields });
            });
        });
        it('Should fail with a bad licence', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = Object.assign(Object.assign({}, baseCorrectParams), { licence: 125 });
                yield (0, server_commands_1.makePostBodyRequest)({ url: server.url, path, token: server.accessToken, fields });
            });
        });
        it('Should fail with a bad language', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = Object.assign(Object.assign({}, baseCorrectParams), { language: 'a'.repeat(15) });
                yield (0, server_commands_1.makePostBodyRequest)({ 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(2500) });
                yield (0, server_commands_1.makePostBodyRequest)({ 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.makePostBodyRequest)({ url: server.url, path, token: server.accessToken, fields });
            });
        });
        it('Should fail without a channel', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = (0, core_utils_1.omit)(baseCorrectParams, ['channelId']);
                yield (0, server_commands_1.makePostBodyRequest)({ url: server.url, path, token: server.accessToken, fields });
            });
        });
        it('Should fail with a bad channel', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = Object.assign(Object.assign({}, baseCorrectParams), { channelId: 545454 });
                yield (0, server_commands_1.makePostBodyRequest)({ url: server.url, path, token: server.accessToken, fields });
            });
        });
        it('Should fail with another user channel', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const user = {
                    username: 'fake',
                    password: 'fake_password'
                };
                yield server.users.create({ username: user.username, password: user.password });
                const accessTokenUser = yield server.login.getAccessToken(user);
                const { videoChannels } = yield server.users.getMyInfo({ token: accessTokenUser });
                const customChannelId = videoChannels[0].id;
                const fields = Object.assign(Object.assign({}, baseCorrectParams), { channelId: customChannelId });
                yield (0, server_commands_1.makePostBodyRequest)({ url: server.url, path, token: userAccessToken, fields });
            });
        });
        it('Should fail with too many tags', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = Object.assign(Object.assign({}, baseCorrectParams), { tags: ['tag1', 'tag2', 'tag3', 'tag4', 'tag5', 'tag6'] });
                yield (0, server_commands_1.makePostBodyRequest)({ url: server.url, path, token: server.accessToken, fields });
            });
        });
        it('Should fail with a tag length too low', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = Object.assign(Object.assign({}, baseCorrectParams), { tags: ['tag1', 't'] });
                yield (0, server_commands_1.makePostBodyRequest)({ url: server.url, path, token: server.accessToken, fields });
            });
        });
        it('Should fail with a tag length too big', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = Object.assign(Object.assign({}, baseCorrectParams), { tags: ['tag1', 'my_super_tag_too_long_long_long_long_long_long'] });
                yield (0, server_commands_1.makePostBodyRequest)({ url: server.url, path, token: server.accessToken, fields });
            });
        });
        it('Should fail with an incorrect thumbnail file', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = baseCorrectParams;
                const attaches = {
                    thumbnailfile: (0, core_utils_1.buildAbsoluteFixturePath)('video_short.mp4')
                };
                yield (0, server_commands_1.makeUploadRequest)({ url: server.url, path, token: server.accessToken, fields, attaches });
            });
        });
        it('Should fail with a big thumbnail file', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = baseCorrectParams;
                const attaches = {
                    thumbnailfile: (0, core_utils_1.buildAbsoluteFixturePath)('preview-big.png')
                };
                yield (0, server_commands_1.makeUploadRequest)({ url: server.url, path, token: server.accessToken, fields, attaches });
            });
        });
        it('Should fail with an incorrect preview file', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = baseCorrectParams;
                const attaches = {
                    previewfile: (0, core_utils_1.buildAbsoluteFixturePath)('video_short.mp4')
                };
                yield (0, server_commands_1.makeUploadRequest)({ url: server.url, path, token: server.accessToken, fields, attaches });
            });
        });
        it('Should fail with a big preview file', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = baseCorrectParams;
                const attaches = {
                    previewfile: (0, core_utils_1.buildAbsoluteFixturePath)('preview-big.png')
                };
                yield (0, server_commands_1.makeUploadRequest)({ url: server.url, path, token: server.accessToken, fields, attaches });
            });
        });
        it('Should fail with bad latency setting', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = Object.assign(Object.assign({}, baseCorrectParams), { latencyMode: 42 });
                yield (0, server_commands_1.makePostBodyRequest)({ url: server.url, path, token: server.accessToken, fields });
            });
        });
        it('Should fail to set latency if the server does not allow it', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = Object.assign(Object.assign({}, baseCorrectParams), { latencyMode: 2 });
                yield (0, server_commands_1.makePostBodyRequest)({ url: server.url, path, token: server.accessToken, fields, expectedStatus: 403 });
            });
        });
        it('Should succeed with the correct parameters', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(30000);
                const res = yield (0, server_commands_1.makePostBodyRequest)({
                    url: server.url,
                    path,
                    token: server.accessToken,
                    fields: baseCorrectParams,
                    expectedStatus: 200
                });
                video = res.body.video;
            });
        });
        it('Should forbid if live is disabled', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield server.config.updateCustomSubConfig({
                    newConfig: {
                        live: {
                            enabled: false
                        }
                    }
                });
                yield (0, server_commands_1.makePostBodyRequest)({
                    url: server.url,
                    path,
                    token: server.accessToken,
                    fields: baseCorrectParams,
                    expectedStatus: 403
                });
            });
        });
        it('Should forbid to save replay if not enabled by the admin', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = Object.assign(Object.assign({}, baseCorrectParams), { saveReplay: true });
                yield server.config.updateCustomSubConfig({
                    newConfig: {
                        live: {
                            enabled: true,
                            allowReplay: false
                        }
                    }
                });
                yield (0, server_commands_1.makePostBodyRequest)({
                    url: server.url,
                    path,
                    token: server.accessToken,
                    fields,
                    expectedStatus: 403
                });
            });
        });
        it('Should allow to save replay if enabled by the admin', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = Object.assign(Object.assign({}, baseCorrectParams), { saveReplay: true });
                yield server.config.updateCustomSubConfig({
                    newConfig: {
                        live: {
                            enabled: true,
                            allowReplay: true
                        }
                    }
                });
                yield (0, server_commands_1.makePostBodyRequest)({
                    url: server.url,
                    path,
                    token: server.accessToken,
                    fields,
                    expectedStatus: 200
                });
            });
        });
        it('Should not allow live if max instance lives is reached', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield server.config.updateCustomSubConfig({
                    newConfig: {
                        live: {
                            enabled: true,
                            maxInstanceLives: 1
                        }
                    }
                });
                yield (0, server_commands_1.makePostBodyRequest)({
                    url: server.url,
                    path,
                    token: server.accessToken,
                    fields: baseCorrectParams,
                    expectedStatus: 403
                });
            });
        });
        it('Should not allow live if max user lives is reached', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield server.config.updateCustomSubConfig({
                    newConfig: {
                        live: {
                            enabled: true,
                            maxInstanceLives: 20,
                            maxUserLives: 1
                        }
                    }
                });
                yield (0, server_commands_1.makePostBodyRequest)({
                    url: server.url,
                    path,
                    token: server.accessToken,
                    fields: baseCorrectParams,
                    expectedStatus: 403
                });
            });
        });
    });
    describe('When getting live information', function () {
        it('Should fail with a bad access token', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield command.get({ token: 'toto', videoId: video.id, expectedStatus: 401 });
            });
        });
        it('Should not display private information without access token', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const live = yield command.get({ token: '', videoId: video.id });
                (0, chai_1.expect)(live.rtmpUrl).to.not.exist;
                (0, chai_1.expect)(live.streamKey).to.not.exist;
                (0, chai_1.expect)(live.latencyMode).to.exist;
            });
        });
        it('Should not display private information with token of another user', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const live = yield command.get({ token: userAccessToken, videoId: video.id });
                (0, chai_1.expect)(live.rtmpUrl).to.not.exist;
                (0, chai_1.expect)(live.streamKey).to.not.exist;
                (0, chai_1.expect)(live.latencyMode).to.exist;
            });
        });
        it('Should display private information with appropriate token', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const live = yield command.get({ videoId: video.id });
                (0, chai_1.expect)(live.rtmpUrl).to.exist;
                (0, chai_1.expect)(live.streamKey).to.exist;
                (0, chai_1.expect)(live.latencyMode).to.exist;
            });
        });
        it('Should fail with a bad video id', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield command.get({ videoId: 'toto', expectedStatus: 400 });
            });
        });
        it('Should fail with an unknown video id', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield command.get({ videoId: 454555, expectedStatus: 404 });
            });
        });
        it('Should fail with a non live video', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield command.get({ videoId: videoIdNotLive, expectedStatus: 404 });
            });
        });
        it('Should succeed with the correct params', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield command.get({ videoId: video.id });
                yield command.get({ videoId: video.uuid });
                yield command.get({ videoId: video.shortUUID });
            });
        });
    });
    describe('When getting live sessions', function () {
        it('Should fail with a bad access token', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield command.listSessions({ token: 'toto', videoId: video.id, expectedStatus: 401 });
            });
        });
        it('Should fail without token', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield command.listSessions({ token: null, videoId: video.id, expectedStatus: 401 });
            });
        });
        it('Should fail with the token of another user', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield command.listSessions({ token: userAccessToken, videoId: video.id, expectedStatus: 403 });
            });
        });
        it('Should fail with a bad video id', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield command.listSessions({ videoId: 'toto', expectedStatus: 400 });
            });
        });
        it('Should fail with an unknown video id', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield command.listSessions({ videoId: 454555, expectedStatus: 404 });
            });
        });
        it('Should fail with a non live video', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield command.listSessions({ videoId: videoIdNotLive, expectedStatus: 404 });
            });
        });
        it('Should succeed with the correct params', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield command.listSessions({ videoId: video.id });
            });
        });
    });
    describe('When getting live session of a replay', function () {
        it('Should fail with a bad video id', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield command.getReplaySession({ videoId: 'toto', expectedStatus: 400 });
            });
        });
        it('Should fail with an unknown video id', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield command.getReplaySession({ videoId: 454555, expectedStatus: 404 });
            });
        });
        it('Should fail with a non replay video', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield command.getReplaySession({ videoId: videoIdNotLive, expectedStatus: 404 });
            });
        });
    });
    describe('When updating live information', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            it('Should fail without access token', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    yield command.update({ token: '', videoId: video.id, fields: {}, expectedStatus: 401 });
                });
            });
            it('Should fail with a bad access token', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    yield command.update({ token: 'toto', videoId: video.id, fields: {}, expectedStatus: 401 });
                });
            });
            it('Should fail with access token of another user', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    yield command.update({ token: userAccessToken, videoId: video.id, fields: {}, expectedStatus: 403 });
                });
            });
            it('Should fail with a bad video id', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    yield command.update({ videoId: 'toto', fields: {}, expectedStatus: 400 });
                });
            });
            it('Should fail with an unknown video id', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    yield command.update({ videoId: 454555, fields: {}, expectedStatus: 404 });
                });
            });
            it('Should fail with a non live video', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    yield command.update({ videoId: videoIdNotLive, fields: {}, expectedStatus: 404 });
                });
            });
            it('Should fail with bad latency setting', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    const fields = { latencyMode: 42 };
                    yield command.update({ videoId: video.id, fields, expectedStatus: 400 });
                });
            });
            it('Should fail to set latency if the server does not allow it', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    const fields = { latencyMode: 2 };
                    yield command.update({ videoId: video.id, fields, expectedStatus: 403 });
                });
            });
            it('Should succeed with the correct params', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    yield command.update({ videoId: video.id, fields: { saveReplay: false } });
                    yield command.update({ videoId: video.uuid, fields: { saveReplay: false } });
                    yield command.update({ videoId: video.shortUUID, fields: { saveReplay: false } });
                });
            });
            it('Should fail to update replay status if replay is not allowed on the instance', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    yield server.config.updateCustomSubConfig({
                        newConfig: {
                            live: {
                                enabled: true,
                                allowReplay: false
                            }
                        }
                    });
                    yield command.update({ videoId: video.id, fields: { saveReplay: true }, expectedStatus: 403 });
                });
            });
            it('Should fail to update a live if it has already started', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    this.timeout(40000);
                    const live = yield command.get({ videoId: video.id });
                    const ffmpegCommand = (0, server_commands_1.sendRTMPStream)({ rtmpBaseUrl: live.rtmpUrl, streamKey: live.streamKey });
                    yield command.waitUntilPublished({ videoId: video.id });
                    yield command.update({ videoId: video.id, fields: {}, expectedStatus: 400 });
                    yield (0, server_commands_1.stopFfmpeg)(ffmpegCommand);
                });
            });
            it('Should fail to change live privacy if it has already started', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    this.timeout(40000);
                    const live = yield command.get({ videoId: video.id });
                    const ffmpegCommand = (0, server_commands_1.sendRTMPStream)({ rtmpBaseUrl: live.rtmpUrl, streamKey: live.streamKey });
                    yield command.waitUntilPublished({ videoId: video.id });
                    yield server.videos.update({
                        id: video.id,
                        attributes: { privacy: 1 },
                        expectedStatus: 400
                    });
                    yield (0, server_commands_1.stopFfmpeg)(ffmpegCommand);
                });
            });
            it('Should fail to stream twice in the save live', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    this.timeout(40000);
                    const live = yield command.get({ videoId: video.id });
                    const ffmpegCommand = (0, server_commands_1.sendRTMPStream)({ rtmpBaseUrl: live.rtmpUrl, streamKey: live.streamKey });
                    yield command.waitUntilPublished({ videoId: video.id });
                    yield command.runAndTestStreamError({ videoId: video.id, shouldHaveError: true });
                    yield (0, server_commands_1.stopFfmpeg)(ffmpegCommand);
                });
            });
        });
    });
    after(function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield (0, server_commands_1.cleanupTests)([server]);
        });
    });
});
//# sourceMappingURL=live.js.map