"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const chai_1 = require("chai");
const shared_1 = require("../../shared");
const core_utils_1 = require("../../../../shared/core-utils");
const server_commands_1 = require("../../../../shared/server-commands");
describe('Test proxy', function () {
    let servers = [];
    let proxy;
    const goodEnv = { HTTP_PROXY: '' };
    const badEnv = { HTTP_PROXY: 'http://127.0.0.1:9000' };
    before(function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.timeout(120000);
            proxy = new shared_1.MockProxy();
            const proxyPort = yield proxy.initialize();
            servers = yield (0, server_commands_1.createMultipleServers)(2);
            goodEnv.HTTP_PROXY = 'http://127.0.0.1:' + proxyPort;
            yield (0, server_commands_1.setAccessTokensToServers)(servers);
            yield (0, server_commands_1.setDefaultVideoChannel)(servers);
            yield (0, server_commands_1.doubleFollow)(servers[0], servers[1]);
        });
    });
    describe('Federation', function () {
        it('Should succeed federation with the appropriate proxy config', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(40000);
                yield servers[0].kill();
                yield servers[0].run({}, { env: goodEnv });
                yield servers[0].videos.quickUpload({ name: 'video 1' });
                yield (0, server_commands_1.waitJobs)(servers);
                for (const server of servers) {
                    const { total, data } = yield server.videos.list();
                    (0, chai_1.expect)(total).to.equal(1);
                    (0, chai_1.expect)(data).to.have.lengthOf(1);
                }
            });
        });
        it('Should fail federation with a wrong proxy config', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(40000);
                yield servers[0].kill();
                yield servers[0].run({}, { env: badEnv });
                yield servers[0].videos.quickUpload({ name: 'video 2' });
                yield (0, server_commands_1.waitJobs)(servers);
                {
                    const { total, data } = yield servers[0].videos.list();
                    (0, chai_1.expect)(total).to.equal(2);
                    (0, chai_1.expect)(data).to.have.lengthOf(2);
                }
                {
                    const { total, data } = yield servers[1].videos.list();
                    (0, chai_1.expect)(total).to.equal(1);
                    (0, chai_1.expect)(data).to.have.lengthOf(1);
                }
            });
        });
    });
    describe('Videos import', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            function quickImport(expectedStatus = 200) {
                return servers[0].imports.importVideo({
                    attributes: {
                        name: 'video import',
                        channelId: servers[0].store.channel.id,
                        privacy: 1,
                        targetUrl: shared_1.FIXTURE_URLS.peertube_long
                    },
                    expectedStatus
                });
            }
            it('Should succeed import with the appropriate proxy config', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    this.timeout(120000);
                    yield servers[0].kill();
                    yield servers[0].run({}, { env: goodEnv });
                    yield quickImport();
                    yield (0, server_commands_1.waitJobs)(servers);
                    const { total, data } = yield servers[0].videos.list();
                    (0, chai_1.expect)(total).to.equal(3);
                    (0, chai_1.expect)(data).to.have.lengthOf(3);
                });
            });
            it('Should fail import with a wrong proxy config', function () {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    this.timeout(120000);
                    yield servers[0].kill();
                    yield servers[0].run({}, { env: badEnv });
                    yield quickImport(400);
                });
            });
        });
    });
    describe('Object storage', function () {
        if ((0, core_utils_1.areMockObjectStorageTestsDisabled)())
            return;
        before(function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(30000);
                yield server_commands_1.ObjectStorageCommand.prepareDefaultMockBuckets();
            });
        });
        it('Should succeed to upload to object storage with the appropriate proxy config', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(120000);
                yield servers[0].kill();
                yield servers[0].run(server_commands_1.ObjectStorageCommand.getDefaultMockConfig(), { env: goodEnv });
                const { uuid } = yield servers[0].videos.quickUpload({ name: 'video' });
                yield (0, server_commands_1.waitJobs)(servers);
                const video = yield servers[0].videos.get({ id: uuid });
                (0, shared_1.expectStartWith)(video.files[0].fileUrl, server_commands_1.ObjectStorageCommand.getMockWebTorrentBaseUrl());
            });
        });
        it('Should fail to upload to object storage with a wrong proxy config', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(120000);
                yield servers[0].kill();
                yield servers[0].run(server_commands_1.ObjectStorageCommand.getDefaultMockConfig(), { env: badEnv });
                const { uuid } = yield servers[0].videos.quickUpload({ name: 'video' });
                yield (0, server_commands_1.waitJobs)(servers, { skipDelayed: true });
                const video = yield servers[0].videos.get({ id: uuid });
                (0, shared_1.expectNotStartWith)(video.files[0].fileUrl, server_commands_1.ObjectStorageCommand.getMockWebTorrentBaseUrl());
            });
        });
    });
    after(function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield proxy.terminate();
            yield (0, server_commands_1.cleanupTests)(servers);
        });
    });
});
//# sourceMappingURL=proxy.js.map