"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 video imports API validator', function () {
    const path = '/api/v1/videos/imports';
    let server;
    let userAccessToken = '';
    let channelId;
    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 username = 'user1';
            const password = 'my super password';
            yield server.users.create({ username, password });
            userAccessToken = yield server.login.getAccessToken({ username, password });
            {
                const { videoChannels } = yield server.users.getMyInfo();
                channelId = videoChannels[0].id;
            }
        });
    });
    describe('When listing my video imports', function () {
        const myPath = '/api/v1/users/me/videos/imports';
        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 fail with a bad videoChannelSyncId param', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield (0, server_commands_1.makeGetRequest)({
                    url: server.url,
                    path: myPath,
                    query: { videoChannelSyncId: 'toto' },
                    token: server.accessToken
                });
            });
        });
        it('Should success with the correct parameters', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield (0, server_commands_1.makeGetRequest)({ url: server.url, path: myPath, expectedStatus: 200, token: server.accessToken });
            });
        });
    });
    describe('When adding a video import', function () {
        let baseCorrectParams;
        before(function () {
            baseCorrectParams = {
                targetUrl: shared_1.FIXTURE_URLS.goodVideo,
                name: 'my super name',
                category: 5,
                licence: 1,
                language: 'pt',
                nsfw: false,
                commentsEnabled: true,
                downloadEnabled: true,
                waitTranscoding: true,
                description: 'my super description',
                support: 'my super support text',
                tags: ['tag1', 'tag2'],
                privacy: 1,
                channelId
            };
        });
        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 without a target url', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = (0, core_utils_1.omit)(baseCorrectParams, ['targetUrl']);
                yield (0, server_commands_1.makePostBodyRequest)({
                    url: server.url,
                    path,
                    token: server.accessToken,
                    fields,
                    expectedStatus: 400
                });
            });
        });
        it('Should fail with a bad target url', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = Object.assign(Object.assign({}, baseCorrectParams), { targetUrl: 'htt://hello' });
                yield (0, server_commands_1.makePostBodyRequest)({ url: server.url, path, token: server.accessToken, fields });
            });
        });
        it('Should fail with localhost', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = Object.assign(Object.assign({}, baseCorrectParams), { targetUrl: 'http://localhost:8000' });
                yield (0, server_commands_1.makePostBodyRequest)({ url: server.url, path, token: server.accessToken, fields });
            });
        });
        it('Should fail with a private IP target urls', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const targetUrls = [
                    'http://127.0.0.1:8000',
                    'http://127.0.0.1',
                    'http://127.0.0.1/hello',
                    'https://192.168.1.42',
                    'http://192.168.1.42',
                    'http://127.0.0.1.cpy.re'
                ];
                for (const targetUrl of targetUrls) {
                    const fields = Object.assign(Object.assign({}, baseCorrectParams), { targetUrl });
                    yield (0, server_commands_1.makePostBodyRequest)({
                        url: server.url,
                        path,
                        token: server.accessToken,
                        fields,
                        expectedStatus: 403
                    });
                }
            });
        });
        it('Should fail with a long name', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = Object.assign(Object.assign({}, baseCorrectParams), { name: 'super'.repeat(65) });
                yield (0, server_commands_1.makePostBodyRequest)({ url: server.url, path, token: server.accessToken, fields });
            });
        });
        it('Should fail with a bad category', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = Object.assign(Object.assign({}, baseCorrectParams), { category: 125 });
                yield (0, server_commands_1.makePostBodyRequest)({ url: server.url, path, token: server.accessToken, fields });
            });
        });
        it('Should fail with a bad licence', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = Object.assign(Object.assign({}, baseCorrectParams), { licence: 125 });
                yield (0, server_commands_1.makePostBodyRequest)({ url: server.url, path, token: server.accessToken, fields });
            });
        });
        it('Should fail with a bad language', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = Object.assign(Object.assign({}, baseCorrectParams), { language: 'a'.repeat(15) });
                yield (0, server_commands_1.makePostBodyRequest)({ url: server.url, path, token: server.accessToken, fields });
            });
        });
        it('Should fail with a long description', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = Object.assign(Object.assign({}, baseCorrectParams), { description: 'super'.repeat(2500) });
                yield (0, server_commands_1.makePostBodyRequest)({ url: server.url, path, token: server.accessToken, fields });
            });
        });
        it('Should fail with a long support text', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = Object.assign(Object.assign({}, baseCorrectParams), { support: 'super'.repeat(201) });
                yield (0, server_commands_1.makePostBodyRequest)({ url: server.url, path, token: server.accessToken, fields });
            });
        });
        it('Should fail without a channel', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = (0, core_utils_1.omit)(baseCorrectParams, ['channelId']);
                yield (0, server_commands_1.makePostBodyRequest)({ url: server.url, path, token: server.accessToken, fields });
            });
        });
        it('Should fail with a bad channel', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = Object.assign(Object.assign({}, baseCorrectParams), { channelId: 545454 });
                yield (0, server_commands_1.makePostBodyRequest)({ url: server.url, path, token: server.accessToken, fields });
            });
        });
        it('Should fail with another user channel', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const user = {
                    username: 'fake',
                    password: 'fake_password'
                };
                yield server.users.create({ username: user.username, password: user.password });
                const accessTokenUser = yield server.login.getAccessToken(user);
                const { videoChannels } = yield server.users.getMyInfo({ token: accessTokenUser });
                const customChannelId = videoChannels[0].id;
                const fields = Object.assign(Object.assign({}, baseCorrectParams), { channelId: customChannelId });
                yield (0, server_commands_1.makePostBodyRequest)({ url: server.url, path, token: userAccessToken, fields });
            });
        });
        it('Should fail with too many tags', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = Object.assign(Object.assign({}, baseCorrectParams), { tags: ['tag1', 'tag2', 'tag3', 'tag4', 'tag5', 'tag6'] });
                yield (0, server_commands_1.makePostBodyRequest)({ url: server.url, path, token: server.accessToken, fields });
            });
        });
        it('Should fail with a tag length too low', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = Object.assign(Object.assign({}, baseCorrectParams), { tags: ['tag1', 't'] });
                yield (0, server_commands_1.makePostBodyRequest)({ url: server.url, path, token: server.accessToken, fields });
            });
        });
        it('Should fail with a tag length too big', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = Object.assign(Object.assign({}, baseCorrectParams), { tags: ['tag1', 'my_super_tag_too_long_long_long_long_long_long'] });
                yield (0, server_commands_1.makePostBodyRequest)({ url: server.url, path, token: server.accessToken, fields });
            });
        });
        it('Should fail with an incorrect thumbnail file', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = baseCorrectParams;
                const attaches = {
                    thumbnailfile: (0, core_utils_1.buildAbsoluteFixturePath)('video_short.mp4')
                };
                yield (0, server_commands_1.makeUploadRequest)({ url: server.url, path, token: server.accessToken, fields, attaches });
            });
        });
        it('Should fail with a big thumbnail file', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = baseCorrectParams;
                const attaches = {
                    thumbnailfile: (0, core_utils_1.buildAbsoluteFixturePath)('preview-big.png')
                };
                yield (0, server_commands_1.makeUploadRequest)({ url: server.url, path, token: server.accessToken, fields, attaches });
            });
        });
        it('Should fail with an incorrect preview file', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = baseCorrectParams;
                const attaches = {
                    previewfile: (0, core_utils_1.buildAbsoluteFixturePath)('video_short.mp4')
                };
                yield (0, server_commands_1.makeUploadRequest)({ url: server.url, path, token: server.accessToken, fields, attaches });
            });
        });
        it('Should fail with a big preview file', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = baseCorrectParams;
                const attaches = {
                    previewfile: (0, core_utils_1.buildAbsoluteFixturePath)('preview-big.png')
                };
                yield (0, server_commands_1.makeUploadRequest)({ url: server.url, path, token: server.accessToken, fields, attaches });
            });
        });
        it('Should fail with an invalid torrent file', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const fields = (0, core_utils_1.omit)(baseCorrectParams, ['targetUrl']);
                const attaches = {
                    torrentfile: (0, core_utils_1.buildAbsoluteFixturePath)('avatar-big.png')
                };
                yield (0, server_commands_1.makeUploadRequest)({ url: server.url, path, token: server.accessToken, fields, attaches });
            });
        });
        it('Should fail with an invalid magnet URI', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                let fields = (0, core_utils_1.omit)(baseCorrectParams, ['targetUrl']);
                fields = Object.assign(Object.assign({}, fields), { magnetUri: 'blabla' });
                yield (0, server_commands_1.makePostBodyRequest)({ url: server.url, path, token: server.accessToken, fields });
            });
        });
        it('Should succeed with the correct parameters', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(120000);
                yield (0, server_commands_1.makePostBodyRequest)({
                    url: server.url,
                    path,
                    token: server.accessToken,
                    fields: baseCorrectParams,
                    expectedStatus: 200
                });
            });
        });
        it('Should forbid to import http videos', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield server.config.updateCustomSubConfig({
                    newConfig: {
                        import: {
                            videos: {
                                http: {
                                    enabled: false
                                },
                                torrent: {
                                    enabled: true
                                }
                            }
                        }
                    }
                });
                yield (0, server_commands_1.makePostBodyRequest)({
                    url: server.url,
                    path,
                    token: server.accessToken,
                    fields: baseCorrectParams,
                    expectedStatus: 409
                });
            });
        });
        it('Should forbid to import torrent videos', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield server.config.updateCustomSubConfig({
                    newConfig: {
                        import: {
                            videos: {
                                http: {
                                    enabled: true
                                },
                                torrent: {
                                    enabled: false
                                }
                            }
                        }
                    }
                });
                let fields = (0, core_utils_1.omit)(baseCorrectParams, ['targetUrl']);
                fields = Object.assign(Object.assign({}, fields), { magnetUri: shared_1.FIXTURE_URLS.magnet });
                yield (0, server_commands_1.makePostBodyRequest)({
                    url: server.url,
                    path,
                    token: server.accessToken,
                    fields,
                    expectedStatus: 409
                });
                fields = (0, core_utils_1.omit)(fields, ['magnetUri']);
                const attaches = {
                    torrentfile: (0, core_utils_1.buildAbsoluteFixturePath)('video-720p.torrent')
                };
                yield (0, server_commands_1.makeUploadRequest)({
                    url: server.url,
                    path,
                    token: server.accessToken,
                    fields,
                    attaches,
                    expectedStatus: 409
                });
            });
        });
    });
    describe('Deleting/cancelling a video import', function () {
        let importId;
        function importVideo() {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const attributes = { channelId: server.store.channel.id, targetUrl: shared_1.FIXTURE_URLS.goodVideo };
                const res = yield server.imports.importVideo({ attributes });
                return res.id;
            });
        }
        before(function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                importId = yield importVideo();
            });
        });
        it('Should fail with an invalid import id', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield server.imports.cancel({ importId: 'artyom', expectedStatus: 400 });
                yield server.imports.delete({ importId: 'artyom', expectedStatus: 400 });
            });
        });
        it('Should fail with an unknown import id', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield server.imports.cancel({ importId: 42, expectedStatus: 404 });
                yield server.imports.delete({ importId: 42, expectedStatus: 404 });
            });
        });
        it('Should fail without token', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield server.imports.cancel({ importId, token: null, expectedStatus: 401 });
                yield server.imports.delete({ importId, token: null, expectedStatus: 401 });
            });
        });
        it('Should fail with another user token', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield server.imports.cancel({ importId, token: userAccessToken, expectedStatus: 403 });
                yield server.imports.delete({ importId, token: userAccessToken, expectedStatus: 403 });
            });
        });
        it('Should fail to cancel non pending import', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(60000);
                yield (0, server_commands_1.waitJobs)([server]);
                yield server.imports.cancel({ importId, expectedStatus: 409 });
            });
        });
        it('Should succeed to delete an import', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield server.imports.delete({ importId });
            });
        });
        it('Should fail to delete a pending import', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield server.jobs.pauseJobQueue();
                importId = yield importVideo();
                yield server.imports.delete({ importId, expectedStatus: 409 });
            });
        });
        it('Should succeed to cancel an import', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                importId = yield importVideo();
                yield server.imports.cancel({ importId });
            });
        });
    });
    after(function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield (0, server_commands_1.cleanupTests)([server]);
        });
    });
});
//# sourceMappingURL=video-imports.js.map