"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const chai_1 = require("chai");
const core_utils_1 = require("../../../../shared/core-utils");
const server_commands_1 = require("../../../../shared/server-commands");
describe('Test application behind a reverse proxy', function () {
    let server;
    let userAccessToken;
    let videoId;
    before(function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.timeout(60000);
            const config = {
                rates_limit: {
                    api: {
                        max: 50,
                        window: 5000
                    },
                    signup: {
                        max: 3,
                        window: 5000
                    },
                    login: {
                        max: 20
                    }
                },
                signup: {
                    limit: 20
                }
            };
            server = yield (0, server_commands_1.createSingleServer)(1, config);
            yield (0, server_commands_1.setAccessTokensToServers)([server]);
            userAccessToken = yield server.users.generateUserAndToken('user');
            const { uuid } = yield server.videos.upload();
            videoId = uuid;
        });
    });
    it('Should view a video only once with the same IP by default', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.timeout(40000);
            yield server.views.simulateView({ id: videoId });
            yield server.views.simulateView({ id: videoId });
            yield (0, core_utils_1.wait)(8000);
            const video = yield server.videos.get({ id: videoId });
            (0, chai_1.expect)(video.views).to.equal(1);
        });
    });
    it('Should view a video 2 times with the X-Forwarded-For header set', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.timeout(20000);
            yield server.views.simulateView({ id: videoId, xForwardedFor: '0.0.0.1,127.0.0.1' });
            yield server.views.simulateView({ id: videoId, xForwardedFor: '0.0.0.2,127.0.0.1' });
            yield (0, core_utils_1.wait)(8000);
            const video = yield server.videos.get({ id: videoId });
            (0, chai_1.expect)(video.views).to.equal(3);
        });
    });
    it('Should view a video only once with the same client IP in the X-Forwarded-For header', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.timeout(20000);
            yield server.views.simulateView({ id: videoId, xForwardedFor: '0.0.0.4,0.0.0.3,::ffff:127.0.0.1' });
            yield server.views.simulateView({ id: videoId, xForwardedFor: '0.0.0.5,0.0.0.3,127.0.0.1' });
            yield (0, core_utils_1.wait)(8000);
            const video = yield server.videos.get({ id: videoId });
            (0, chai_1.expect)(video.views).to.equal(4);
        });
    });
    it('Should view a video two times with a different client IP in the X-Forwarded-For header', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.timeout(20000);
            yield server.views.simulateView({ id: videoId, xForwardedFor: '0.0.0.8,0.0.0.6,127.0.0.1' });
            yield server.views.simulateView({ id: videoId, xForwardedFor: '0.0.0.8,0.0.0.7,127.0.0.1' });
            yield (0, core_utils_1.wait)(8000);
            const video = yield server.videos.get({ id: videoId });
            (0, chai_1.expect)(video.views).to.equal(6);
        });
    });
    it('Should rate limit logins', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const user = { username: 'root', password: 'fail' };
            for (let i = 0; i < 18; i++) {
                yield server.login.login({ user, expectedStatus: 400 });
            }
            yield server.login.login({ user, expectedStatus: 429 });
        });
    });
    it('Should rate limit signup', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            for (let i = 0; i < 10; i++) {
                try {
                    yield server.users.register({ username: 'test' + i });
                }
                catch (_a) {
                }
            }
            yield server.users.register({ username: 'test42', expectedStatus: 429 });
        });
    });
    it('Should not rate limit failed signup', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.timeout(30000);
            yield (0, core_utils_1.wait)(7000);
            for (let i = 0; i < 3; i++) {
                yield server.users.register({ username: 'test' + i, expectedStatus: 409 });
            }
            yield server.users.register({ username: 'test43', expectedStatus: 204 });
        });
    });
    it('Should rate limit API calls', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.timeout(30000);
            yield (0, core_utils_1.wait)(7000);
            for (let i = 0; i < 100; i++) {
                try {
                    yield server.videos.get({ id: videoId });
                }
                catch (_a) {
                }
            }
            yield server.videos.get({ id: videoId, expectedStatus: 429 });
        });
    });
    it('Should rate limit API calls with a user but not with an admin', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield server.videos.get({ id: videoId, token: userAccessToken, expectedStatus: 429 });
            yield server.videos.get({ id: videoId, token: server.accessToken, expectedStatus: 200 });
        });
    });
    after(function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield (0, server_commands_1.cleanupTests)([server]);
        });
    });
});
//# sourceMappingURL=reverse-proxy.js.map