"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const chai_1 = require("chai");
const magnet_uri_1 = require("magnet-uri");
const shared_1 = require("../../shared");
const core_utils_1 = require("../../../../shared/core-utils");
const server_commands_1 = require("../../../../shared/server-commands");
describe('Test video static file privacy', function () {
    let server;
    let userToken;
    before(function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.timeout(50000);
            server = yield (0, server_commands_1.createSingleServer)(1);
            yield (0, server_commands_1.setAccessTokensToServers)([server]);
            yield (0, server_commands_1.setDefaultVideoChannel)([server]);
            userToken = yield server.users.generateUserAndToken('user1');
        });
    });
    describe('VOD static file path', function () {
        function runSuite() {
            function checkPrivateFiles(uuid) {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    const video = yield server.videos.getWithToken({ id: uuid });
                    for (const file of video.files) {
                        (0, chai_1.expect)(file.fileDownloadUrl).to.not.include('/private/');
                        (0, shared_1.expectStartWith)(file.fileUrl, server.url + '/static/webseed/private/');
                        const torrent = yield (0, server_commands_1.parseTorrentVideo)(server, file);
                        (0, chai_1.expect)(torrent.urlList).to.have.lengthOf(0);
                        const magnet = (0, magnet_uri_1.decode)(file.magnetUri);
                        (0, chai_1.expect)(magnet.urlList).to.have.lengthOf(0);
                        yield (0, server_commands_1.makeRawRequest)({ url: file.fileUrl, token: server.accessToken, expectedStatus: 200 });
                    }
                    const hls = video.streamingPlaylists[0];
                    if (hls) {
                        (0, shared_1.expectStartWith)(hls.playlistUrl, server.url + '/static/streaming-playlists/hls/private/');
                        (0, shared_1.expectStartWith)(hls.segmentsSha256Url, server.url + '/static/streaming-playlists/hls/private/');
                        yield (0, server_commands_1.makeRawRequest)({ url: hls.playlistUrl, token: server.accessToken, expectedStatus: 200 });
                        yield (0, server_commands_1.makeRawRequest)({ url: hls.segmentsSha256Url, token: server.accessToken, expectedStatus: 200 });
                    }
                });
            }
            function checkPublicFiles(uuid) {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    const video = yield server.videos.get({ id: uuid });
                    for (const file of (0, core_utils_1.getAllFiles)(video)) {
                        (0, chai_1.expect)(file.fileDownloadUrl).to.not.include('/private/');
                        (0, chai_1.expect)(file.fileUrl).to.not.include('/private/');
                        const torrent = yield (0, server_commands_1.parseTorrentVideo)(server, file);
                        (0, chai_1.expect)(torrent.urlList[0]).to.not.include('private');
                        const magnet = (0, magnet_uri_1.decode)(file.magnetUri);
                        (0, chai_1.expect)(magnet.urlList[0]).to.not.include('private');
                        yield (0, server_commands_1.makeRawRequest)({ url: file.fileUrl, expectedStatus: 200 });
                        yield (0, server_commands_1.makeRawRequest)({ url: torrent.urlList[0], expectedStatus: 200 });
                        yield (0, server_commands_1.makeRawRequest)({ url: magnet.urlList[0], expectedStatus: 200 });
                    }
                    const hls = video.streamingPlaylists[0];
                    if (hls) {
                        (0, chai_1.expect)(hls.playlistUrl).to.not.include('private');
                        (0, chai_1.expect)(hls.segmentsSha256Url).to.not.include('private');
                        yield (0, server_commands_1.makeRawRequest)({ url: hls.playlistUrl, expectedStatus: 200 });
                        yield (0, server_commands_1.makeRawRequest)({ url: hls.segmentsSha256Url, expectedStatus: 200 });
                    }
                });
            }
            it('Should upload a private/internal video and have a private static path', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    this.timeout(120000);
                    for (const privacy of [3, 4]) {
                        const { uuid } = yield server.videos.quickUpload({ name: 'video', privacy });
                        yield (0, server_commands_1.waitJobs)([server]);
                        yield checkPrivateFiles(uuid);
                    }
                });
            });
            it('Should upload a public video and update it as private/internal to have a private static path', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    this.timeout(120000);
                    for (const privacy of [3, 4]) {
                        const { uuid } = yield server.videos.quickUpload({ name: 'video', privacy: 1 });
                        yield (0, server_commands_1.waitJobs)([server]);
                        yield server.videos.update({ id: uuid, attributes: { privacy } });
                        yield (0, server_commands_1.waitJobs)([server]);
                        yield checkPrivateFiles(uuid);
                    }
                });
            });
            it('Should upload a private video and update it to unlisted to have a public static path', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    this.timeout(120000);
                    const { uuid } = yield server.videos.quickUpload({ name: 'video', privacy: 3 });
                    yield (0, server_commands_1.waitJobs)([server]);
                    yield server.videos.update({ id: uuid, attributes: { privacy: 2 } });
                    yield (0, server_commands_1.waitJobs)([server]);
                    yield checkPublicFiles(uuid);
                });
            });
            it('Should upload an internal video and update it to public to have a public static path', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    this.timeout(120000);
                    const { uuid } = yield server.videos.quickUpload({ name: 'video', privacy: 4 });
                    yield (0, server_commands_1.waitJobs)([server]);
                    yield server.videos.update({ id: uuid, attributes: { privacy: 1 } });
                    yield (0, server_commands_1.waitJobs)([server]);
                    yield checkPublicFiles(uuid);
                });
            });
            it('Should upload an internal video and schedule a public publish', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    this.timeout(120000);
                    const attributes = {
                        name: 'video',
                        privacy: 3,
                        scheduleUpdate: {
                            updateAt: new Date(Date.now() + 1000).toISOString(),
                            privacy: 1
                        }
                    };
                    const { uuid } = yield server.videos.upload({ attributes });
                    yield (0, server_commands_1.waitJobs)([server]);
                    yield (0, core_utils_1.wait)(1000);
                    yield server.debug.sendCommand({ body: { command: 'process-update-videos-scheduler' } });
                    yield (0, server_commands_1.waitJobs)([server]);
                    yield checkPublicFiles(uuid);
                });
            });
        }
        describe('Without transcoding', function () {
            runSuite();
        });
        describe('With transcoding', function () {
            before(function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    yield server.config.enableMinimumTranscoding();
                });
            });
            runSuite();
        });
    });
    describe('VOD static file right check', function () {
        let unrelatedFileToken;
        function checkVideoFiles(options) {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const { id, expectedStatus, token, videoFileToken } = options;
                const video = yield server.videos.getWithToken({ id });
                for (const file of (0, core_utils_1.getAllFiles)(video)) {
                    yield (0, server_commands_1.makeRawRequest)({ url: file.fileUrl, token, expectedStatus });
                    yield (0, server_commands_1.makeRawRequest)({ url: file.fileDownloadUrl, token, expectedStatus });
                    yield (0, server_commands_1.makeRawRequest)({ url: file.fileUrl, query: { videoFileToken }, expectedStatus });
                    yield (0, server_commands_1.makeRawRequest)({ url: file.fileDownloadUrl, query: { videoFileToken }, expectedStatus });
                }
                const hls = video.streamingPlaylists[0];
                yield (0, server_commands_1.makeRawRequest)({ url: hls.playlistUrl, token, expectedStatus });
                yield (0, server_commands_1.makeRawRequest)({ url: hls.segmentsSha256Url, token, expectedStatus });
                yield (0, server_commands_1.makeRawRequest)({ url: hls.playlistUrl, query: { videoFileToken }, expectedStatus });
                yield (0, server_commands_1.makeRawRequest)({ url: hls.segmentsSha256Url, query: { videoFileToken }, expectedStatus });
            });
        }
        before(function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield server.config.enableMinimumTranscoding();
                const { uuid } = yield server.videos.quickUpload({ name: 'another video' });
                unrelatedFileToken = yield server.videoToken.getVideoFileToken({ videoId: uuid });
            });
        });
        it('Should not be able to access a private video files without OAuth token and file token', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(120000);
                const { uuid } = yield server.videos.quickUpload({ name: 'video', privacy: 4 });
                yield (0, server_commands_1.waitJobs)([server]);
                yield checkVideoFiles({ id: uuid, expectedStatus: 403, token: null, videoFileToken: null });
            });
        });
        it('Should not be able to access an internal video files without appropriate OAuth token and file token', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(120000);
                const { uuid } = yield server.videos.quickUpload({ name: 'video', privacy: 3 });
                yield (0, server_commands_1.waitJobs)([server]);
                yield checkVideoFiles({
                    id: uuid,
                    expectedStatus: 403,
                    token: userToken,
                    videoFileToken: unrelatedFileToken
                });
            });
        });
        it('Should be able to access a private video files with appropriate OAuth token or file token', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(120000);
                const { uuid } = yield server.videos.quickUpload({ name: 'video', privacy: 3 });
                const videoFileToken = yield server.videoToken.getVideoFileToken({ videoId: uuid });
                yield (0, server_commands_1.waitJobs)([server]);
                yield checkVideoFiles({ id: uuid, expectedStatus: 200, token: server.accessToken, videoFileToken });
            });
        });
        it('Should reinject video file token', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(120000);
                const { uuid } = yield server.videos.quickUpload({ name: 'video', privacy: 3 });
                const videoFileToken = yield server.videoToken.getVideoFileToken({ videoId: uuid });
                yield (0, server_commands_1.waitJobs)([server]);
                const video = yield server.videos.getWithToken({ id: uuid });
                const hls = video.streamingPlaylists[0];
                {
                    const query = { videoFileToken };
                    const { text } = yield (0, server_commands_1.makeRawRequest)({ url: hls.playlistUrl, query, expectedStatus: 200 });
                    (0, chai_1.expect)(text).to.not.include(videoFileToken);
                }
                {
                    yield (0, shared_1.checkVideoFileTokenReinjection)({
                        server,
                        videoUUID: uuid,
                        videoFileToken,
                        resolutions: [240, 720],
                        isLive: false
                    });
                }
            });
        });
        it('Should be able to access a private video of another user with an admin OAuth token or file token', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(120000);
                const { uuid } = yield server.videos.quickUpload({ name: 'video', token: userToken, privacy: 3 });
                const videoFileToken = yield server.videoToken.getVideoFileToken({ videoId: uuid });
                yield (0, server_commands_1.waitJobs)([server]);
                yield checkVideoFiles({ id: uuid, expectedStatus: 200, token: server.accessToken, videoFileToken });
            });
        });
    });
    describe('Live static file path and check', function () {
        let normalLiveId;
        let normalLive;
        let permanentLiveId;
        let permanentLive;
        let unrelatedFileToken;
        function checkLiveFiles(live, liveId) {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const ffmpegCommand = (0, server_commands_1.sendRTMPStream)({ rtmpBaseUrl: live.rtmpUrl, streamKey: live.streamKey });
                yield server.live.waitUntilPublished({ videoId: liveId });
                const video = yield server.videos.getWithToken({ id: liveId });
                const fileToken = yield server.videoToken.getVideoFileToken({ videoId: video.uuid });
                const hls = video.streamingPlaylists[0];
                for (const url of [hls.playlistUrl, hls.segmentsSha256Url]) {
                    (0, shared_1.expectStartWith)(url, server.url + '/static/streaming-playlists/hls/private/');
                    yield (0, server_commands_1.makeRawRequest)({ url, token: server.accessToken, expectedStatus: 200 });
                    yield (0, server_commands_1.makeRawRequest)({ url, query: { videoFileToken: fileToken }, expectedStatus: 200 });
                    yield (0, server_commands_1.makeRawRequest)({ url, token: userToken, expectedStatus: 403 });
                    yield (0, server_commands_1.makeRawRequest)({ url, expectedStatus: 403 });
                    yield (0, server_commands_1.makeRawRequest)({ url, query: { videoFileToken: unrelatedFileToken }, expectedStatus: 403 });
                }
                yield (0, server_commands_1.stopFfmpeg)(ffmpegCommand);
            });
        }
        function checkReplay(replay) {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fileToken = yield server.videoToken.getVideoFileToken({ videoId: replay.uuid });
                const hls = replay.streamingPlaylists[0];
                (0, chai_1.expect)(hls.files).to.not.have.lengthOf(0);
                for (const file of hls.files) {
                    yield (0, server_commands_1.makeRawRequest)({ url: file.fileUrl, token: server.accessToken, expectedStatus: 200 });
                    yield (0, server_commands_1.makeRawRequest)({ url: file.fileUrl, query: { videoFileToken: fileToken }, expectedStatus: 200 });
                    yield (0, server_commands_1.makeRawRequest)({ url: file.fileUrl, token: userToken, expectedStatus: 403 });
                    yield (0, server_commands_1.makeRawRequest)({ url: file.fileUrl, expectedStatus: 403 });
                    yield (0, server_commands_1.makeRawRequest)({
                        url: file.fileUrl,
                        query: { videoFileToken: unrelatedFileToken },
                        expectedStatus: 403
                    });
                }
                for (const url of [hls.playlistUrl, hls.segmentsSha256Url]) {
                    (0, shared_1.expectStartWith)(url, server.url + '/static/streaming-playlists/hls/private/');
                    yield (0, server_commands_1.makeRawRequest)({ url, token: server.accessToken, expectedStatus: 200 });
                    yield (0, server_commands_1.makeRawRequest)({ url, query: { videoFileToken: fileToken }, expectedStatus: 200 });
                    yield (0, server_commands_1.makeRawRequest)({ url, token: userToken, expectedStatus: 403 });
                    yield (0, server_commands_1.makeRawRequest)({ url, expectedStatus: 403 });
                    yield (0, server_commands_1.makeRawRequest)({ url, query: { videoFileToken: unrelatedFileToken }, expectedStatus: 403 });
                }
            });
        }
        before(function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield server.config.enableMinimumTranscoding();
                const { uuid } = yield server.videos.quickUpload({ name: 'another video' });
                unrelatedFileToken = yield server.videoToken.getVideoFileToken({ videoId: uuid });
                yield server.config.enableLive({
                    allowReplay: true,
                    transcoding: true,
                    resolutions: 'min'
                });
                {
                    const { video, live } = yield server.live.quickCreate({ saveReplay: true, permanentLive: false, privacy: 3 });
                    normalLiveId = video.uuid;
                    normalLive = live;
                }
                {
                    const { video, live } = yield server.live.quickCreate({ saveReplay: true, permanentLive: true, privacy: 3 });
                    permanentLiveId = video.uuid;
                    permanentLive = live;
                }
            });
        });
        it('Should create a private normal live and have a private static path', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(240000);
                yield checkLiveFiles(normalLive, normalLiveId);
            });
        });
        it('Should create a private permanent live and have a private static path', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(240000);
                yield checkLiveFiles(permanentLive, permanentLiveId);
            });
        });
        it('Should reinject video file token on permanent live', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(240000);
                const ffmpegCommand = (0, server_commands_1.sendRTMPStream)({ rtmpBaseUrl: permanentLive.rtmpUrl, streamKey: permanentLive.streamKey });
                yield server.live.waitUntilPublished({ videoId: permanentLiveId });
                const video = yield server.videos.getWithToken({ id: permanentLiveId });
                const videoFileToken = yield server.videoToken.getVideoFileToken({ videoId: video.uuid });
                const hls = video.streamingPlaylists[0];
                {
                    const query = { videoFileToken };
                    const { text } = yield (0, server_commands_1.makeRawRequest)({ url: hls.playlistUrl, query, expectedStatus: 200 });
                    (0, chai_1.expect)(text).to.not.include(videoFileToken);
                }
                {
                    yield (0, shared_1.checkVideoFileTokenReinjection)({
                        server,
                        videoUUID: permanentLiveId,
                        videoFileToken,
                        resolutions: [720],
                        isLive: true
                    });
                }
                yield (0, server_commands_1.stopFfmpeg)(ffmpegCommand);
            });
        });
        it('Should have created a replay of the normal live with a private static path', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(240000);
                yield server.live.waitUntilReplacedByReplay({ videoId: normalLiveId });
                const replay = yield server.videos.getWithToken({ id: normalLiveId });
                yield checkReplay(replay);
            });
        });
        it('Should have created a replay of the permanent live with a private static path', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(240000);
                yield server.live.waitUntilWaiting({ videoId: permanentLiveId });
                yield (0, server_commands_1.waitJobs)([server]);
                const live = yield server.videos.getWithToken({ id: permanentLiveId });
                const replayFromList = yield (0, server_commands_1.findExternalSavedVideo)(server, live);
                const replay = yield server.videos.getWithToken({ id: replayFromList.id });
                yield checkReplay(replay);
            });
        });
    });
    describe('With static file right check disabled', function () {
        let videoUUID;
        before(function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(240000);
                yield server.kill();
                yield server.run({
                    static_files: {
                        private_files_require_auth: false
                    }
                });
                const { uuid } = yield server.videos.quickUpload({ name: 'video', privacy: 4 });
                videoUUID = uuid;
                yield (0, server_commands_1.waitJobs)([server]);
            });
        });
        it('Should not check auth for private static files', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const video = yield server.videos.getWithToken({ id: videoUUID });
                for (const file of (0, core_utils_1.getAllFiles)(video)) {
                    yield (0, server_commands_1.makeRawRequest)({ url: file.fileUrl, expectedStatus: 200 });
                }
                const hls = video.streamingPlaylists[0];
                yield (0, server_commands_1.makeRawRequest)({ url: hls.playlistUrl, expectedStatus: 200 });
                yield (0, server_commands_1.makeRawRequest)({ url: hls.segmentsSha256Url, expectedStatus: 200 });
            });
        });
    });
    after(function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield (0, server_commands_1.cleanupTests)([server]);
        });
    });
});
//# sourceMappingURL=video-static-file-privacy.js.map