"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const server_commands_1 = require("../../../../shared/server-commands");
describe('Test video filters validators', function () {
    let server;
    let userAccessToken;
    let moderatorAccessToken;
    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 (0, server_commands_1.setDefaultVideoChannel)([server]);
            const user = { username: 'user1', password: 'my super password' };
            yield server.users.create({ username: user.username, password: user.password });
            userAccessToken = yield server.login.getAccessToken(user);
            const moderator = { username: 'moderator', password: 'my super password' };
            yield server.users.create({ username: moderator.username, password: moderator.password, role: 1 });
            moderatorAccessToken = yield server.login.getAccessToken(moderator);
        });
    });
    describe('When setting a deprecated video filter', function () {
        function testEndpoints(token, filter, expectedStatus) {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const paths = [
                    '/api/v1/video-channels/root_channel/videos',
                    '/api/v1/accounts/root/videos',
                    '/api/v1/videos',
                    '/api/v1/search/videos'
                ];
                for (const path of paths) {
                    yield (0, server_commands_1.makeGetRequest)({
                        url: server.url,
                        path,
                        token,
                        query: {
                            filter
                        },
                        expectedStatus
                    });
                }
            });
        }
        it('Should fail with a bad filter', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield testEndpoints(server.accessToken, 'bad-filter', 400);
            });
        });
        it('Should succeed with a good filter', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield testEndpoints(server.accessToken, 'local', 200);
            });
        });
        it('Should fail to list all-local/all with a simple user', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield testEndpoints(userAccessToken, 'all-local', 401);
                yield testEndpoints(userAccessToken, 'all', 401);
            });
        });
        it('Should succeed to list all-local/all with a moderator', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield testEndpoints(moderatorAccessToken, 'all-local', 200);
                yield testEndpoints(moderatorAccessToken, 'all', 200);
            });
        });
        it('Should succeed to list all-local/all with an admin', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield testEndpoints(server.accessToken, 'all-local', 200);
                yield testEndpoints(server.accessToken, 'all', 200);
            });
        });
        it('Should fail on the feeds endpoint with the all-local/all filter', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                for (const filter of ['all', 'all-local']) {
                    yield (0, server_commands_1.makeGetRequest)({
                        url: server.url,
                        path: '/feeds/videos.json',
                        expectedStatus: 401,
                        query: {
                            filter
                        }
                    });
                }
            });
        });
        it('Should succeed on the feeds endpoint with the local filter', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield (0, server_commands_1.makeGetRequest)({
                    url: server.url,
                    path: '/feeds/videos.json',
                    expectedStatus: 200,
                    query: {
                        filter: 'local'
                    }
                });
            });
        });
    });
    describe('When setting video filters', function () {
        const validIncludes = [
            0,
            4,
            1 | 2
        ];
        function testEndpoints(options) {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const paths = [
                    '/api/v1/video-channels/root_channel/videos',
                    '/api/v1/accounts/root/videos',
                    '/api/v1/videos',
                    '/api/v1/search/videos'
                ];
                for (const path of paths) {
                    yield (0, server_commands_1.makeGetRequest)({
                        url: server.url,
                        path,
                        token: options.token || server.accessToken,
                        query: {
                            isLocal: options.isLocal,
                            privacyOneOf: options.privacyOneOf,
                            include: options.include
                        },
                        expectedStatus: options.expectedStatus
                    });
                }
            });
        }
        it('Should fail with a bad privacyOneOf', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield testEndpoints({ privacyOneOf: ['toto'], expectedStatus: 400 });
            });
        });
        it('Should succeed with a good privacyOneOf', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield testEndpoints({ privacyOneOf: [4], expectedStatus: 200 });
            });
        });
        it('Should fail to use privacyOneOf with a simple user', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield testEndpoints({
                    privacyOneOf: [4],
                    token: userAccessToken,
                    expectedStatus: 401
                });
            });
        });
        it('Should fail with a bad include', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield testEndpoints({ include: 'toto', expectedStatus: 400 });
            });
        });
        it('Should succeed with a good include', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                for (const include of validIncludes) {
                    yield testEndpoints({ include, expectedStatus: 200 });
                }
            });
        });
        it('Should fail to include more videos with a simple user', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                for (const include of validIncludes) {
                    yield testEndpoints({ token: userAccessToken, include, expectedStatus: 401 });
                }
            });
        });
        it('Should succeed to list all local/all with a moderator', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                for (const include of validIncludes) {
                    yield testEndpoints({ token: moderatorAccessToken, include, expectedStatus: 200 });
                }
            });
        });
        it('Should succeed to list all local/all with an admin', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                for (const include of validIncludes) {
                    yield testEndpoints({ token: server.accessToken, include, expectedStatus: 200 });
                }
            });
        });
        it('Should fail on the feeds endpoint with the all filter', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                for (const include of [1]) {
                    yield (0, server_commands_1.makeGetRequest)({
                        url: server.url,
                        path: '/feeds/videos.json',
                        expectedStatus: 401,
                        query: {
                            include
                        }
                    });
                }
            });
        });
        it('Should succeed on the feeds endpoint with the local filter', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield (0, server_commands_1.makeGetRequest)({
                    url: server.url,
                    path: '/feeds/videos.json',
                    expectedStatus: 200,
                    query: {
                        isLocal: true
                    }
                });
            });
        });
    });
    after(function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield (0, server_commands_1.cleanupTests)([server]);
        });
    });
});
//# sourceMappingURL=videos-common-filters.js.map