"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const chai_1 = require("chai");
const server_commands_1 = require("../../../../shared/server-commands");
describe('Test logs API validators', function () {
    const path = '/api/v1/server/logs';
    let server;
    let userAccessToken = '';
    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]);
            const user = {
                username: 'user1',
                password: 'my super password'
            };
            yield server.users.create({ username: user.username, password: user.password });
            userAccessToken = yield server.login.getAccessToken(user);
        });
    });
    describe('When getting logs', function () {
        it('Should fail with a non authenticated user', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield (0, server_commands_1.makeGetRequest)({
                    url: server.url,
                    path,
                    expectedStatus: 401
                });
            });
        });
        it('Should fail with a non admin user', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield (0, server_commands_1.makeGetRequest)({
                    url: server.url,
                    path,
                    token: userAccessToken,
                    expectedStatus: 403
                });
            });
        });
        it('Should fail with a missing startDate query', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield (0, server_commands_1.makeGetRequest)({
                    url: server.url,
                    path,
                    token: server.accessToken,
                    expectedStatus: 400
                });
            });
        });
        it('Should fail with a bad startDate query', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield (0, server_commands_1.makeGetRequest)({
                    url: server.url,
                    path,
                    token: server.accessToken,
                    query: { startDate: 'toto' },
                    expectedStatus: 400
                });
            });
        });
        it('Should fail with a bad endDate query', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield (0, server_commands_1.makeGetRequest)({
                    url: server.url,
                    path,
                    token: server.accessToken,
                    query: { startDate: new Date().toISOString(), endDate: 'toto' },
                    expectedStatus: 400
                });
            });
        });
        it('Should fail with a bad level parameter', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield (0, server_commands_1.makeGetRequest)({
                    url: server.url,
                    path,
                    token: server.accessToken,
                    query: { startDate: new Date().toISOString(), level: 'toto' },
                    expectedStatus: 400
                });
            });
        });
        it('Should succeed with the correct params', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield (0, server_commands_1.makeGetRequest)({
                    url: server.url,
                    path,
                    token: server.accessToken,
                    query: { startDate: new Date().toISOString() },
                    expectedStatus: 200
                });
            });
        });
    });
    describe('When creating client logs', function () {
        const base = {
            level: 'warn',
            message: 'my super message',
            url: 'https://example.com/toto'
        };
        const expectedStatus = 400;
        it('Should fail with an invalid level', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield server.logs.createLogClient({ payload: Object.assign(Object.assign({}, base), { level: '' }), expectedStatus });
                yield server.logs.createLogClient({ payload: Object.assign(Object.assign({}, base), { level: undefined }), expectedStatus });
                yield server.logs.createLogClient({ payload: Object.assign(Object.assign({}, base), { level: 'toto' }), expectedStatus });
            });
        });
        it('Should fail with an invalid message', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield server.logs.createLogClient({ payload: Object.assign(Object.assign({}, base), { message: undefined }), expectedStatus });
                yield server.logs.createLogClient({ payload: Object.assign(Object.assign({}, base), { message: '' }), expectedStatus });
                yield server.logs.createLogClient({ payload: Object.assign(Object.assign({}, base), { message: 'm'.repeat(2500) }), expectedStatus });
            });
        });
        it('Should fail with an invalid url', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield server.logs.createLogClient({ payload: Object.assign(Object.assign({}, base), { url: undefined }), expectedStatus });
                yield server.logs.createLogClient({ payload: Object.assign(Object.assign({}, base), { url: 'toto' }), expectedStatus });
            });
        });
        it('Should fail with an invalid stackTrace', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield server.logs.createLogClient({ payload: Object.assign(Object.assign({}, base), { stackTrace: 's'.repeat(20000) }), expectedStatus });
            });
        });
        it('Should fail with an invalid userAgent', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield server.logs.createLogClient({ payload: Object.assign(Object.assign({}, base), { userAgent: 's'.repeat(500) }), expectedStatus });
            });
        });
        it('Should fail with an invalid meta', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield server.logs.createLogClient({ payload: Object.assign(Object.assign({}, base), { meta: 's'.repeat(10000) }), expectedStatus });
            });
        });
        it('Should succeed with the correct params', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield server.logs.createLogClient({ payload: Object.assign(Object.assign({}, base), { stackTrace: 'stackTrace', meta: '{toto}', userAgent: 'userAgent' }) });
            });
        });
        it('Should rate limit log creation', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                let fail = false;
                for (let i = 0; i < 10; i++) {
                    try {
                        yield server.logs.createLogClient({ token: null, payload: base });
                    }
                    catch (_a) {
                        fail = true;
                    }
                }
                (0, chai_1.expect)(fail).to.be.true;
            });
        });
    });
    after(function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield (0, server_commands_1.cleanupTests)([server]);
        });
    });
});
//# sourceMappingURL=logs.js.map