"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const shared_1 = require("../../shared");
const server_commands_1 = require("../../../../shared/server-commands");
describe('Test video channel sync API validator', () => {
    const path = '/api/v1/video-channel-syncs';
    let server;
    let command;
    let rootChannelId;
    let rootChannelSyncId;
    const userInfo = {
        accessToken: '',
        username: 'user1',
        id: -1,
        channelId: -1,
        syncId: -1
    };
    function withChannelSyncDisabled(callback) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            try {
                yield server.config.disableChannelSync();
                yield callback();
            }
            finally {
                yield server.config.enableChannelSync();
            }
        });
    }
    function withMaxSyncsPerUser(maxSync, callback) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const origConfig = yield server.config.getCustomConfig();
            yield server.config.updateExistingSubConfig({
                newConfig: {
                    import: {
                        videoChannelSynchronization: {
                            maxPerUser: maxSync
                        }
                    }
                }
            });
            try {
                yield callback();
            }
            finally {
                yield server.config.updateCustomConfig({ newCustomConfig: origConfig });
            }
        });
    }
    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]);
            command = server.channelSyncs;
            rootChannelId = server.store.channel.id;
            {
                userInfo.accessToken = yield server.users.generateUserAndToken(userInfo.username);
                const { videoChannels, id: userId } = yield server.users.getMyInfo({ token: userInfo.accessToken });
                userInfo.id = userId;
                userInfo.channelId = videoChannels[0].id;
            }
            yield server.config.enableChannelSync();
        });
    });
    describe('When creating a sync', function () {
        let baseCorrectParams;
        before(function () {
            baseCorrectParams = {
                externalChannelUrl: shared_1.FIXTURE_URLS.youtubeChannel,
                videoChannelId: rootChannelId
            };
        });
        it('Should fail when sync is disabled', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield withChannelSyncDisabled(() => tslib_1.__awaiter(this, void 0, void 0, function* () {
                    yield command.create({
                        token: server.accessToken,
                        attributes: baseCorrectParams,
                        expectedStatus: 403
                    });
                }));
            });
        });
        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,
                    expectedStatus: 400
                });
            });
        });
        it('Should fail with no authentication', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield command.create({
                    token: null,
                    attributes: baseCorrectParams,
                    expectedStatus: 401
                });
            });
        });
        it('Should fail without a target url', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const attributes = Object.assign(Object.assign({}, baseCorrectParams), { externalChannelUrl: null });
                yield command.create({
                    token: server.accessToken,
                    attributes,
                    expectedStatus: 400
                });
            });
        });
        it('Should fail without a channelId', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const attributes = Object.assign(Object.assign({}, baseCorrectParams), { videoChannelId: null });
                yield command.create({
                    token: server.accessToken,
                    attributes,
                    expectedStatus: 400
                });
            });
        });
        it('Should fail with a channelId refering nothing', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const attributes = Object.assign(Object.assign({}, baseCorrectParams), { videoChannelId: 42 });
                yield command.create({
                    token: server.accessToken,
                    attributes,
                    expectedStatus: 404
                });
            });
        });
        it('Should fail to create a sync when the user does not own the channel', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield command.create({
                    token: userInfo.accessToken,
                    attributes: baseCorrectParams,
                    expectedStatus: 403
                });
            });
        });
        it('Should succeed to create a sync with root and for another user\'s channel', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const { videoChannelSync } = yield command.create({
                    token: server.accessToken,
                    attributes: Object.assign(Object.assign({}, baseCorrectParams), { videoChannelId: userInfo.channelId }),
                    expectedStatus: 200
                });
                userInfo.syncId = videoChannelSync.id;
            });
        });
        it('Should succeed with the correct parameters', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const { videoChannelSync } = yield command.create({
                    token: server.accessToken,
                    attributes: baseCorrectParams,
                    expectedStatus: 200
                });
                rootChannelSyncId = videoChannelSync.id;
            });
        });
        it('Should fail when the user exceeds allowed number of synchronizations', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield withMaxSyncsPerUser(1, () => tslib_1.__awaiter(this, void 0, void 0, function* () {
                    yield command.create({
                        token: server.accessToken,
                        attributes: Object.assign(Object.assign({}, baseCorrectParams), { videoChannelId: userInfo.channelId }),
                        expectedStatus: 400
                    });
                }));
            });
        });
    });
    describe('When listing my channel syncs', function () {
        const myPath = '/api/v1/accounts/root/video-channel-syncs';
        it('Should fail with a bad start pagination', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield (0, shared_1.checkBadStartPagination)(server.url, myPath, server.accessToken);
            });
        });
        it('Should fail with a bad count pagination', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield (0, shared_1.checkBadCountPagination)(server.url, myPath, server.accessToken);
            });
        });
        it('Should fail with an incorrect sort', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield (0, shared_1.checkBadSortPagination)(server.url, myPath, server.accessToken);
            });
        });
        it('Should succeed with the correct parameters', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield command.listByAccount({
                    accountName: 'root',
                    token: server.accessToken,
                    expectedStatus: 200
                });
            });
        });
        it('Should fail with no authentication', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield command.listByAccount({
                    accountName: 'root',
                    token: null,
                    expectedStatus: 401
                });
            });
        });
        it('Should fail when a simple user lists another user\'s synchronizations', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield command.listByAccount({
                    accountName: 'root',
                    token: userInfo.accessToken,
                    expectedStatus: 403
                });
            });
        });
        it('Should succeed when root lists another user\'s synchronizations', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield command.listByAccount({
                    accountName: userInfo.username,
                    token: server.accessToken,
                    expectedStatus: 200
                });
            });
        });
        it('Should succeed even with synchronization disabled', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield withChannelSyncDisabled(function () {
                    return tslib_1.__awaiter(this, void 0, void 0, function* () {
                        yield command.listByAccount({
                            accountName: 'root',
                            token: server.accessToken,
                            expectedStatus: 200
                        });
                    });
                });
            });
        });
    });
    describe('When triggering deletion', function () {
        it('should fail with no authentication', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield command.delete({
                    channelSyncId: userInfo.syncId,
                    token: null,
                    expectedStatus: 401
                });
            });
        });
        it('Should fail when channelSyncId does not refer to any sync', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield command.delete({
                    channelSyncId: 42,
                    token: server.accessToken,
                    expectedStatus: 404
                });
            });
        });
        it('Should fail when sync is not owned by the user', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield command.delete({
                    channelSyncId: rootChannelSyncId,
                    token: userInfo.accessToken,
                    expectedStatus: 403
                });
            });
        });
        it('Should succeed when root delete a sync they do not own', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield command.delete({
                    channelSyncId: userInfo.syncId,
                    token: server.accessToken,
                    expectedStatus: 204
                });
            });
        });
        it('should succeed when user delete a sync they own', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const { videoChannelSync } = yield command.create({
                    attributes: {
                        externalChannelUrl: shared_1.FIXTURE_URLS.youtubeChannel,
                        videoChannelId: userInfo.channelId
                    },
                    token: server.accessToken,
                    expectedStatus: 200
                });
                yield command.delete({
                    channelSyncId: videoChannelSync.id,
                    token: server.accessToken,
                    expectedStatus: 204
                });
            });
        });
        it('Should succeed even when synchronization is disabled', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield withChannelSyncDisabled(function () {
                    return tslib_1.__awaiter(this, void 0, void 0, function* () {
                        yield command.delete({
                            channelSyncId: rootChannelSyncId,
                            token: server.accessToken,
                            expectedStatus: 204
                        });
                    });
                });
            });
        });
    });
    after(function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield (server === null || server === void 0 ? void 0 : server.kill());
        });
    });
});
//# sourceMappingURL=video-channel-syncs.js.map