"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', function () {
    let server;
    let logsCommand;
    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]);
            logsCommand = server.logs;
        });
    });
    describe('With the standard log file', function () {
        it('Should get logs with a start date', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(20000);
                yield server.videos.upload({ attributes: { name: 'video 1' } });
                yield (0, server_commands_1.waitJobs)([server]);
                const now = new Date();
                yield server.videos.upload({ attributes: { name: 'video 2' } });
                yield (0, server_commands_1.waitJobs)([server]);
                const body = yield logsCommand.getLogs({ startDate: now });
                const logsString = JSON.stringify(body);
                (0, chai_1.expect)(logsString.includes('video 1')).to.be.false;
                (0, chai_1.expect)(logsString.includes('video 2')).to.be.true;
            });
        });
        it('Should get logs with an end date', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(30000);
                yield server.videos.upload({ attributes: { name: 'video 3' } });
                yield (0, server_commands_1.waitJobs)([server]);
                const now1 = new Date();
                yield server.videos.upload({ attributes: { name: 'video 4' } });
                yield (0, server_commands_1.waitJobs)([server]);
                const now2 = new Date();
                yield server.videos.upload({ attributes: { name: 'video 5' } });
                yield (0, server_commands_1.waitJobs)([server]);
                const body = yield logsCommand.getLogs({ startDate: now1, endDate: now2 });
                const logsString = JSON.stringify(body);
                (0, chai_1.expect)(logsString.includes('video 3')).to.be.false;
                (0, chai_1.expect)(logsString.includes('video 4')).to.be.true;
                (0, chai_1.expect)(logsString.includes('video 5')).to.be.false;
            });
        });
        it('Should filter by level', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(20000);
                const now = new Date();
                yield server.videos.upload({ attributes: { name: 'video 6' } });
                yield (0, server_commands_1.waitJobs)([server]);
                {
                    const body = yield logsCommand.getLogs({ startDate: now, level: 'info' });
                    const logsString = JSON.stringify(body);
                    (0, chai_1.expect)(logsString.includes('video 6')).to.be.true;
                }
                {
                    const body = yield logsCommand.getLogs({ startDate: now, level: 'warn' });
                    const logsString = JSON.stringify(body);
                    (0, chai_1.expect)(logsString.includes('video 6')).to.be.false;
                }
            });
        });
        it('Should filter by tag', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const now = new Date();
                const { uuid } = yield server.videos.upload({ attributes: { name: 'video 6' } });
                yield (0, server_commands_1.waitJobs)([server]);
                {
                    const body = yield logsCommand.getLogs({ startDate: now, level: 'debug', tagsOneOf: ['toto'] });
                    (0, chai_1.expect)(body).to.have.lengthOf(0);
                }
                {
                    const body = yield logsCommand.getLogs({ startDate: now, level: 'debug', tagsOneOf: [uuid] });
                    (0, chai_1.expect)(body).to.not.have.lengthOf(0);
                    for (const line of body) {
                        (0, chai_1.expect)(line.tags).to.contain(uuid);
                    }
                }
            });
        });
        it('Should log ping requests', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(10000);
                const now = new Date();
                yield server.servers.ping();
                const body = yield logsCommand.getLogs({ startDate: now, level: 'info' });
                const logsString = JSON.stringify(body);
                (0, chai_1.expect)(logsString.includes('/api/v1/ping')).to.be.true;
            });
        });
        it('Should not log ping requests', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(30000);
                yield (0, server_commands_1.killallServers)([server]);
                yield server.run({ log: { log_ping_requests: false } });
                const now = new Date();
                yield server.servers.ping();
                const body = yield logsCommand.getLogs({ startDate: now, level: 'info' });
                const logsString = JSON.stringify(body);
                (0, chai_1.expect)(logsString.includes('/api/v1/ping')).to.be.false;
            });
        });
    });
    describe('With the audit log', function () {
        it('Should get logs with a start date', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(20000);
                yield server.videos.upload({ attributes: { name: 'video 7' } });
                yield (0, server_commands_1.waitJobs)([server]);
                const now = new Date();
                yield server.videos.upload({ attributes: { name: 'video 8' } });
                yield (0, server_commands_1.waitJobs)([server]);
                const body = yield logsCommand.getAuditLogs({ startDate: now });
                const logsString = JSON.stringify(body);
                (0, chai_1.expect)(logsString.includes('video 7')).to.be.false;
                (0, chai_1.expect)(logsString.includes('video 8')).to.be.true;
                (0, chai_1.expect)(body).to.have.lengthOf(1);
                const item = body[0];
                const message = JSON.parse(item.message);
                (0, chai_1.expect)(message.domain).to.equal('videos');
                (0, chai_1.expect)(message.action).to.equal('create');
            });
        });
        it('Should get logs with an end date', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(30000);
                yield server.videos.upload({ attributes: { name: 'video 9' } });
                yield (0, server_commands_1.waitJobs)([server]);
                const now1 = new Date();
                yield server.videos.upload({ attributes: { name: 'video 10' } });
                yield (0, server_commands_1.waitJobs)([server]);
                const now2 = new Date();
                yield server.videos.upload({ attributes: { name: 'video 11' } });
                yield (0, server_commands_1.waitJobs)([server]);
                const body = yield logsCommand.getAuditLogs({ startDate: now1, endDate: now2 });
                const logsString = JSON.stringify(body);
                (0, chai_1.expect)(logsString.includes('video 9')).to.be.false;
                (0, chai_1.expect)(logsString.includes('video 10')).to.be.true;
                (0, chai_1.expect)(logsString.includes('video 11')).to.be.false;
            });
        });
    });
    describe('When creating log from the client', function () {
        it('Should create a warn client log', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const now = new Date();
                yield server.logs.createLogClient({
                    payload: {
                        level: 'warn',
                        url: 'http://example.com',
                        message: 'my super client message'
                    },
                    token: null
                });
                const body = yield logsCommand.getLogs({ startDate: now });
                const logsString = JSON.stringify(body);
                (0, chai_1.expect)(logsString.includes('my super client message')).to.be.true;
            });
        });
        it('Should create an error authenticated client log', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const now = new Date();
                yield server.logs.createLogClient({
                    payload: {
                        url: 'https://example.com/page1',
                        level: 'error',
                        message: 'my super client message 2',
                        userAgent: 'super user agent',
                        meta: '{hello}',
                        stackTrace: 'super stack trace'
                    }
                });
                const body = yield logsCommand.getLogs({ startDate: now });
                const logsString = JSON.stringify(body);
                (0, chai_1.expect)(logsString.includes('my super client message 2')).to.be.true;
                (0, chai_1.expect)(logsString.includes('super user agent')).to.be.true;
                (0, chai_1.expect)(logsString.includes('super stack trace')).to.be.true;
                (0, chai_1.expect)(logsString.includes('{hello}')).to.be.true;
                (0, chai_1.expect)(logsString.includes('https://example.com/page1')).to.be.true;
            });
        });
        it('Should refuse to create client logs', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield server.kill();
                yield server.run({
                    log: {
                        accept_client_log: false
                    }
                });
                yield server.logs.createLogClient({
                    payload: {
                        level: 'warn',
                        url: 'http://example.com',
                        message: 'my super client message'
                    },
                    expectedStatus: 403
                });
            });
        });
    });
    after(function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield (0, server_commands_1.cleanupTests)([server]);
        });
    });
});
//# sourceMappingURL=logs.js.map