"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const shared_1 = require("../../shared");
const core_utils_1 = require("../../../../shared/core-utils");
const server_commands_1 = require("../../../../shared/server-commands");
describe('Test videos import in a channel API validator', function () {
    let server;
    const userInfo = {
        accessToken: '',
        channelName: 'fake_channel',
        channelId: -1,
        id: -1,
        videoQuota: -1,
        videoQuotaDaily: -1,
        channelSyncId: -1
    };
    let command;
    before(function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.timeout(120000);
            server = yield (0, server_commands_1.createSingleServer)(1);
            yield (0, server_commands_1.setAccessTokensToServers)([server]);
            yield (0, server_commands_1.setDefaultVideoChannel)([server]);
            yield server.config.enableImports();
            yield server.config.enableChannelSync();
            const userCreds = {
                username: 'fake',
                password: 'fake_password'
            };
            {
                const user = yield server.users.create({ username: userCreds.username, password: userCreds.password });
                userInfo.id = user.id;
                userInfo.accessToken = yield server.login.getAccessToken(userCreds);
                const info = yield server.users.getMyInfo({ token: userInfo.accessToken });
                userInfo.channelId = info.videoChannels[0].id;
            }
            {
                const { videoChannelSync } = yield server.channelSyncs.create({
                    token: userInfo.accessToken,
                    attributes: {
                        externalChannelUrl: shared_1.FIXTURE_URLS.youtubeChannel,
                        videoChannelId: userInfo.channelId
                    }
                });
                userInfo.channelSyncId = videoChannelSync.id;
            }
            command = server.channels;
        });
    });
    it('Should fail when HTTP upload is disabled', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield server.config.disableChannelSync();
            yield server.config.disableImports();
            yield command.importVideos({
                channelName: server.store.channel.name,
                externalChannelUrl: shared_1.FIXTURE_URLS.youtubeChannel,
                token: server.accessToken,
                expectedStatus: 403
            });
            yield server.config.enableImports();
        });
    });
    it('Should fail when externalChannelUrl is not provided', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield command.importVideos({
                channelName: server.store.channel.name,
                externalChannelUrl: null,
                token: server.accessToken,
                expectedStatus: 400
            });
        });
    });
    it('Should fail when externalChannelUrl is malformed', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield command.importVideos({
                channelName: server.store.channel.name,
                externalChannelUrl: 'not-a-url',
                token: server.accessToken,
                expectedStatus: 400
            });
        });
    });
    it('Should fail with a bad sync id', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield command.importVideos({
                channelName: server.store.channel.name,
                externalChannelUrl: shared_1.FIXTURE_URLS.youtubeChannel,
                videoChannelSyncId: 'toto',
                token: server.accessToken,
                expectedStatus: 400
            });
        });
    });
    it('Should fail with a unknown sync id', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield command.importVideos({
                channelName: server.store.channel.name,
                externalChannelUrl: shared_1.FIXTURE_URLS.youtubeChannel,
                videoChannelSyncId: 42,
                token: server.accessToken,
                expectedStatus: 404
            });
        });
    });
    it('Should fail with a sync id of another channel', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield command.importVideos({
                channelName: server.store.channel.name,
                externalChannelUrl: shared_1.FIXTURE_URLS.youtubeChannel,
                videoChannelSyncId: userInfo.channelSyncId,
                token: server.accessToken,
                expectedStatus: 403
            });
        });
    });
    it('Should fail with no authentication', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield command.importVideos({
                channelName: server.store.channel.name,
                externalChannelUrl: shared_1.FIXTURE_URLS.youtubeChannel,
                token: null,
                expectedStatus: 401
            });
        });
    });
    it('Should fail when sync is not owned by the user', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield command.importVideos({
                channelName: server.store.channel.name,
                externalChannelUrl: shared_1.FIXTURE_URLS.youtubeChannel,
                token: userInfo.accessToken,
                expectedStatus: 403
            });
        });
    });
    it('Should fail when the user has no quota', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield server.users.update({
                userId: userInfo.id,
                videoQuota: 0
            });
            yield command.importVideos({
                channelName: 'fake_channel',
                externalChannelUrl: shared_1.FIXTURE_URLS.youtubeChannel,
                token: userInfo.accessToken,
                expectedStatus: 413
            });
            yield server.users.update({
                userId: userInfo.id,
                videoQuota: userInfo.videoQuota
            });
        });
    });
    it('Should fail when the user has no daily quota', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield server.users.update({
                userId: userInfo.id,
                videoQuotaDaily: 0
            });
            yield command.importVideos({
                channelName: 'fake_channel',
                externalChannelUrl: shared_1.FIXTURE_URLS.youtubeChannel,
                token: userInfo.accessToken,
                expectedStatus: 413
            });
            yield server.users.update({
                userId: userInfo.id,
                videoQuotaDaily: userInfo.videoQuotaDaily
            });
        });
    });
    it('Should succeed when sync is run by its owner', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            if (!(0, core_utils_1.areHttpImportTestsDisabled)())
                return;
            yield command.importVideos({
                channelName: 'fake_channel',
                externalChannelUrl: shared_1.FIXTURE_URLS.youtubeChannel,
                token: userInfo.accessToken
            });
        });
    });
    it('Should succeed when sync is run with root and for another user\'s channel', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            if (!(0, core_utils_1.areHttpImportTestsDisabled)())
                return;
            yield command.importVideos({
                channelName: 'fake_channel',
                externalChannelUrl: shared_1.FIXTURE_URLS.youtubeChannel
            });
        });
    });
    after(function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield (0, server_commands_1.cleanupTests)([server]);
        });
    });
});
//# sourceMappingURL=channel-import-videos.js.map