"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const chai_1 = require("chai");
const server_commands_1 = require("../../../../shared/server-commands");
function createOverviewRes(overview) {
    const videos = overview.categories[0].videos;
    return { data: videos, total: videos.length };
}
describe('Test video NSFW policy', function () {
    let server;
    let userAccessToken;
    let customConfig;
    function getVideosFunctions(token, query = {}) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const user = yield server.users.getMyInfo();
            const channelName = user.videoChannels[0].name;
            const accountName = user.account.name + '@' + user.account.host;
            const hasQuery = Object.keys(query).length !== 0;
            let promises;
            if (token) {
                promises = [
                    server.search.advancedVideoSearch({ token, search: Object.assign({ search: 'n', sort: '-publishedAt' }, query) }),
                    server.videos.listWithToken(Object.assign({ token }, query)),
                    server.videos.listByAccount(Object.assign({ token, handle: accountName }, query)),
                    server.videos.listByChannel(Object.assign({ token, handle: channelName }, query))
                ];
                if (!hasQuery) {
                    const p = server.overviews.getVideos({ page: 1, token })
                        .then(res => createOverviewRes(res));
                    promises.push(p);
                }
                return Promise.all(promises);
            }
            promises = [
                server.search.searchVideos({ search: 'n', sort: '-publishedAt' }),
                server.videos.list(),
                server.videos.listByAccount({ token: null, handle: accountName }),
                server.videos.listByChannel({ token: null, handle: channelName })
            ];
            if (!hasQuery) {
                const p = server.overviews.getVideos({ page: 1 })
                    .then(res => createOverviewRes(res));
                promises.push(p);
            }
            return Promise.all(promises);
        });
    }
    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]);
            {
                const attributes = { name: 'nsfw', nsfw: true, category: 1 };
                yield server.videos.upload({ attributes });
            }
            {
                const attributes = { name: 'normal', nsfw: false, category: 1 };
                yield server.videos.upload({ attributes });
            }
            customConfig = yield server.config.getCustomConfig();
        });
    });
    describe('Instance default NSFW policy', function () {
        it('Should display NSFW videos with display default NSFW policy', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const serverConfig = yield server.config.getConfig();
                (0, chai_1.expect)(serverConfig.instance.defaultNSFWPolicy).to.equal('display');
                for (const body of yield getVideosFunctions()) {
                    (0, chai_1.expect)(body.total).to.equal(2);
                    const videos = body.data;
                    (0, chai_1.expect)(videos).to.have.lengthOf(2);
                    (0, chai_1.expect)(videos[0].name).to.equal('normal');
                    (0, chai_1.expect)(videos[1].name).to.equal('nsfw');
                }
            });
        });
        it('Should not display NSFW videos with do_not_list default NSFW policy', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                customConfig.instance.defaultNSFWPolicy = 'do_not_list';
                yield server.config.updateCustomConfig({ newCustomConfig: customConfig });
                const serverConfig = yield server.config.getConfig();
                (0, chai_1.expect)(serverConfig.instance.defaultNSFWPolicy).to.equal('do_not_list');
                for (const body of yield getVideosFunctions()) {
                    (0, chai_1.expect)(body.total).to.equal(1);
                    const videos = body.data;
                    (0, chai_1.expect)(videos).to.have.lengthOf(1);
                    (0, chai_1.expect)(videos[0].name).to.equal('normal');
                }
            });
        });
        it('Should display NSFW videos with blur default NSFW policy', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                customConfig.instance.defaultNSFWPolicy = 'blur';
                yield server.config.updateCustomConfig({ newCustomConfig: customConfig });
                const serverConfig = yield server.config.getConfig();
                (0, chai_1.expect)(serverConfig.instance.defaultNSFWPolicy).to.equal('blur');
                for (const body of yield getVideosFunctions()) {
                    (0, chai_1.expect)(body.total).to.equal(2);
                    const videos = body.data;
                    (0, chai_1.expect)(videos).to.have.lengthOf(2);
                    (0, chai_1.expect)(videos[0].name).to.equal('normal');
                    (0, chai_1.expect)(videos[1].name).to.equal('nsfw');
                }
            });
        });
    });
    describe('User NSFW policy', function () {
        it('Should create a user having the default nsfw policy', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const username = 'user1';
                const password = 'my super password';
                yield server.users.create({ username, password });
                userAccessToken = yield server.login.getAccessToken({ username, password });
                const user = yield server.users.getMyInfo({ token: userAccessToken });
                (0, chai_1.expect)(user.nsfwPolicy).to.equal('blur');
            });
        });
        it('Should display NSFW videos with blur user NSFW policy', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                customConfig.instance.defaultNSFWPolicy = 'do_not_list';
                yield server.config.updateCustomConfig({ newCustomConfig: customConfig });
                for (const body of yield getVideosFunctions(userAccessToken)) {
                    (0, chai_1.expect)(body.total).to.equal(2);
                    const videos = body.data;
                    (0, chai_1.expect)(videos).to.have.lengthOf(2);
                    (0, chai_1.expect)(videos[0].name).to.equal('normal');
                    (0, chai_1.expect)(videos[1].name).to.equal('nsfw');
                }
            });
        });
        it('Should display NSFW videos with display user NSFW policy', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield server.users.updateMe({ nsfwPolicy: 'display' });
                for (const body of yield getVideosFunctions(server.accessToken)) {
                    (0, chai_1.expect)(body.total).to.equal(2);
                    const videos = body.data;
                    (0, chai_1.expect)(videos).to.have.lengthOf(2);
                    (0, chai_1.expect)(videos[0].name).to.equal('normal');
                    (0, chai_1.expect)(videos[1].name).to.equal('nsfw');
                }
            });
        });
        it('Should not display NSFW videos with do_not_list user NSFW policy', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield server.users.updateMe({ nsfwPolicy: 'do_not_list' });
                for (const body of yield getVideosFunctions(server.accessToken)) {
                    (0, chai_1.expect)(body.total).to.equal(1);
                    const videos = body.data;
                    (0, chai_1.expect)(videos).to.have.lengthOf(1);
                    (0, chai_1.expect)(videos[0].name).to.equal('normal');
                }
            });
        });
        it('Should be able to see my NSFW videos even with do_not_list user NSFW policy', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const { total, data } = yield server.videos.listMyVideos();
                (0, chai_1.expect)(total).to.equal(2);
                (0, chai_1.expect)(data).to.have.lengthOf(2);
                (0, chai_1.expect)(data[0].name).to.equal('normal');
                (0, chai_1.expect)(data[1].name).to.equal('nsfw');
            });
        });
        it('Should display NSFW videos when the nsfw param === true', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                for (const body of yield getVideosFunctions(server.accessToken, { nsfw: 'true' })) {
                    (0, chai_1.expect)(body.total).to.equal(1);
                    const videos = body.data;
                    (0, chai_1.expect)(videos).to.have.lengthOf(1);
                    (0, chai_1.expect)(videos[0].name).to.equal('nsfw');
                }
            });
        });
        it('Should hide NSFW videos when the nsfw param === true', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                for (const body of yield getVideosFunctions(server.accessToken, { nsfw: 'false' })) {
                    (0, chai_1.expect)(body.total).to.equal(1);
                    const videos = body.data;
                    (0, chai_1.expect)(videos).to.have.lengthOf(1);
                    (0, chai_1.expect)(videos[0].name).to.equal('normal');
                }
            });
        });
        it('Should display both videos when the nsfw param === both', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                for (const body of yield getVideosFunctions(server.accessToken, { nsfw: 'both' })) {
                    (0, chai_1.expect)(body.total).to.equal(2);
                    const videos = body.data;
                    (0, chai_1.expect)(videos).to.have.lengthOf(2);
                    (0, chai_1.expect)(videos[0].name).to.equal('normal');
                    (0, chai_1.expect)(videos[1].name).to.equal('nsfw');
                }
            });
        });
    });
    after(function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield (0, server_commands_1.cleanupTests)([server]);
        });
    });
});
//# sourceMappingURL=video-nsfw.js.map