"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const core_utils_1 = require("../../../../shared/core-utils");
const server_commands_1 = require("../../../../shared/server-commands");
describe('Test videos files', function () {
    let servers;
    let userToken;
    let moderatorToken;
    before(function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.timeout(300000);
            servers = yield (0, server_commands_1.createMultipleServers)(2);
            yield (0, server_commands_1.setAccessTokensToServers)(servers);
            yield (0, server_commands_1.doubleFollow)(servers[0], servers[1]);
            userToken = yield servers[0].users.generateUserAndToken('user', 2);
            moderatorToken = yield servers[0].users.generateUserAndToken('moderator', 1);
        });
    });
    describe('Getting metadata', function () {
        let video;
        before(function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const { uuid } = yield servers[0].videos.quickUpload({ name: 'video', privacy: 3 });
                video = yield servers[0].videos.getWithToken({ id: uuid });
            });
        });
        it('Should not get metadata of private video without token', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                for (const file of (0, core_utils_1.getAllFiles)(video)) {
                    yield (0, server_commands_1.makeRawRequest)({ url: file.metadataUrl, expectedStatus: 401 });
                }
            });
        });
        it('Should not get metadata of private video without the appropriate token', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                for (const file of (0, core_utils_1.getAllFiles)(video)) {
                    yield (0, server_commands_1.makeRawRequest)({ url: file.metadataUrl, token: userToken, expectedStatus: 403 });
                }
            });
        });
        it('Should get metadata of private video with the appropriate token', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                for (const file of (0, core_utils_1.getAllFiles)(video)) {
                    yield (0, server_commands_1.makeRawRequest)({ url: file.metadataUrl, token: servers[0].accessToken, expectedStatus: 200 });
                }
            });
        });
    });
    describe('Deleting files', function () {
        let webtorrentId;
        let hlsId;
        let remoteId;
        let validId1;
        let validId2;
        let hlsFileId;
        let webtorrentFileId;
        let remoteHLSFileId;
        let remoteWebtorrentFileId;
        before(function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(300000);
                {
                    const { uuid } = yield servers[1].videos.quickUpload({ name: 'remote video' });
                    yield (0, server_commands_1.waitJobs)(servers);
                    const video = yield servers[1].videos.get({ id: uuid });
                    remoteId = video.uuid;
                    remoteHLSFileId = video.streamingPlaylists[0].files[0].id;
                    remoteWebtorrentFileId = video.files[0].id;
                }
                {
                    yield servers[0].config.enableTranscoding(true, true);
                    {
                        const { uuid } = yield servers[0].videos.quickUpload({ name: 'both 1' });
                        yield (0, server_commands_1.waitJobs)(servers);
                        const video = yield servers[0].videos.get({ id: uuid });
                        validId1 = video.uuid;
                        hlsFileId = video.streamingPlaylists[0].files[0].id;
                        webtorrentFileId = video.files[0].id;
                    }
                    {
                        const { uuid } = yield servers[0].videos.quickUpload({ name: 'both 2' });
                        validId2 = uuid;
                    }
                }
                yield (0, server_commands_1.waitJobs)(servers);
                {
                    yield servers[0].config.enableTranscoding(false, true);
                    const { uuid } = yield servers[0].videos.quickUpload({ name: 'hls' });
                    hlsId = uuid;
                }
                yield (0, server_commands_1.waitJobs)(servers);
                {
                    yield servers[0].config.enableTranscoding(false, true);
                    const { uuid } = yield servers[0].videos.quickUpload({ name: 'webtorrent' });
                    webtorrentId = uuid;
                }
                yield (0, server_commands_1.waitJobs)(servers);
            });
        });
        it('Should not delete files of a unknown video', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const expectedStatus = 404;
                yield servers[0].videos.removeHLSPlaylist({ videoId: 404, expectedStatus });
                yield servers[0].videos.removeAllWebTorrentFiles({ videoId: 404, expectedStatus });
                yield servers[0].videos.removeHLSFile({ videoId: 404, fileId: hlsFileId, expectedStatus });
                yield servers[0].videos.removeWebTorrentFile({ videoId: 404, fileId: webtorrentFileId, expectedStatus });
            });
        });
        it('Should not delete unknown files', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const expectedStatus = 404;
                yield servers[0].videos.removeHLSFile({ videoId: validId1, fileId: webtorrentFileId, expectedStatus });
                yield servers[0].videos.removeWebTorrentFile({ videoId: validId1, fileId: hlsFileId, expectedStatus });
            });
        });
        it('Should not delete files of a remote video', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const expectedStatus = 400;
                yield servers[0].videos.removeHLSPlaylist({ videoId: remoteId, expectedStatus });
                yield servers[0].videos.removeAllWebTorrentFiles({ videoId: remoteId, expectedStatus });
                yield servers[0].videos.removeHLSFile({ videoId: remoteId, fileId: remoteHLSFileId, expectedStatus });
                yield servers[0].videos.removeWebTorrentFile({ videoId: remoteId, fileId: remoteWebtorrentFileId, expectedStatus });
            });
        });
        it('Should not delete files by a non admin user', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const expectedStatus = 403;
                yield servers[0].videos.removeHLSPlaylist({ videoId: validId1, token: userToken, expectedStatus });
                yield servers[0].videos.removeHLSPlaylist({ videoId: validId1, token: moderatorToken, expectedStatus });
                yield servers[0].videos.removeAllWebTorrentFiles({ videoId: validId1, token: userToken, expectedStatus });
                yield servers[0].videos.removeAllWebTorrentFiles({ videoId: validId1, token: moderatorToken, expectedStatus });
                yield servers[0].videos.removeHLSFile({ videoId: validId1, fileId: hlsFileId, token: userToken, expectedStatus });
                yield servers[0].videos.removeHLSFile({ videoId: validId1, fileId: hlsFileId, token: moderatorToken, expectedStatus });
                yield servers[0].videos.removeWebTorrentFile({ videoId: validId1, fileId: webtorrentFileId, token: userToken, expectedStatus });
                yield servers[0].videos.removeWebTorrentFile({ videoId: validId1, fileId: webtorrentFileId, token: moderatorToken, expectedStatus });
            });
        });
        it('Should not delete files if the files are not available', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield servers[0].videos.removeHLSPlaylist({ videoId: hlsId, expectedStatus: 400 });
                yield servers[0].videos.removeAllWebTorrentFiles({ videoId: webtorrentId, expectedStatus: 400 });
                yield servers[0].videos.removeHLSFile({ videoId: hlsId, fileId: 404, expectedStatus: 404 });
                yield servers[0].videos.removeWebTorrentFile({ videoId: webtorrentId, fileId: 404, expectedStatus: 404 });
            });
        });
        it('Should not delete files if no both versions are available', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield servers[0].videos.removeHLSPlaylist({ videoId: hlsId, expectedStatus: 400 });
                yield servers[0].videos.removeAllWebTorrentFiles({ videoId: webtorrentId, expectedStatus: 400 });
            });
        });
        it('Should delete files if both versions are available', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield servers[0].videos.removeHLSFile({ videoId: validId1, fileId: hlsFileId });
                yield servers[0].videos.removeWebTorrentFile({ videoId: validId1, fileId: webtorrentFileId });
                yield servers[0].videos.removeHLSPlaylist({ videoId: validId1 });
                yield servers[0].videos.removeAllWebTorrentFiles({ videoId: validId2 });
            });
        });
    });
    after(function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield (0, server_commands_1.cleanupTests)(servers);
        });
    });
});
//# sourceMappingURL=video-files.js.map