From 78d62f4d182be2f72f4901c38a0d10bb147896e8 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 13 Jan 2021 09:38:19 +0100 Subject: Fix and add skipping ping log tests --- server.ts | 4 +--- server/tests/api/server/logs.ts | 31 ++++++++++++++++++++++++++++++- shared/extra-utils/server/servers.ts | 10 ++++++++++ 3 files changed, 41 insertions(+), 4 deletions(-) diff --git a/server.ts b/server.ts index f844c9564..e3cddfd33 100644 --- a/server.ts +++ b/server.ts @@ -159,9 +159,7 @@ morgan.token('user-agent', (req: express.Request) => { }) app.use(morgan('combined', { stream: { write: logger.info.bind(logger) }, - skip: function (req, res) { - return (req.path === '/api/v1/ping' && CONFIG.LOG.LOG_PING_REQUESTS === false) - }, + skip: req => CONFIG.LOG.LOG_PING_REQUESTS === false && req.originalUrl === '/api/v1/ping' })) // For body requests diff --git a/server/tests/api/server/logs.ts b/server/tests/api/server/logs.ts index b8714c7a1..c458789b6 100644 --- a/server/tests/api/server/logs.ts +++ b/server/tests/api/server/logs.ts @@ -2,7 +2,7 @@ import * as chai from 'chai' import 'mocha' -import { cleanupTests, flushAndRunServer, ServerInfo, setAccessTokensToServers } from '../../../../shared/extra-utils/index' +import { cleanupTests, flushAndRunServer, killallServers, makeGetRequest, makePingRequest, reRunServer, ServerInfo, setAccessTokensToServers } from '../../../../shared/extra-utils/index' import { waitJobs } from '../../../../shared/extra-utils/server/jobs' import { uploadVideo } from '../../../../shared/extra-utils/videos/videos' import { getAuditLogs, getLogs } from '../../../../shared/extra-utils/logs/logs' @@ -20,6 +20,7 @@ describe('Test logs', function () { }) describe('With the standard log file', function () { + it('Should get logs with a start date', async function () { this.timeout(10000) @@ -84,6 +85,34 @@ describe('Test logs', function () { expect(logsString.includes('video 6')).to.be.false } }) + + it('Should log ping requests', async function () { + const now = new Date() + + await makePingRequest(server) + + const res = await getLogs(server.url, server.accessToken, now, undefined, 'info') + const logsString = JSON.stringify(res.body) + + expect(logsString.includes('/api/v1/ping')).to.be.true + }) + + it('Should not log ping requests', async function () { + this.timeout(30000) + + killallServers([ server ]) + + await reRunServer(server, { log: { log_ping_requests: false } }) + + const now = new Date() + + await makePingRequest(server) + + const res = await getLogs(server.url, server.accessToken, now, undefined, 'info') + const logsString = JSON.stringify(res.body) + + expect(logsString.includes('/api/v1/ping')).to.be.false + }) }) describe('With the audit log', function () { diff --git a/shared/extra-utils/server/servers.ts b/shared/extra-utils/server/servers.ts index 5e1de6ece..6ab7bee90 100644 --- a/shared/extra-utils/server/servers.ts +++ b/shared/extra-utils/server/servers.ts @@ -7,6 +7,7 @@ import { join } from 'path' import { randomInt } from '../../core-utils/miscs/miscs' import { VideoChannel } from '../../models/videos' import { buildServerDirectory, getFileSize, isGithubCI, root, wait } from '../miscs/miscs' +import { makeGetRequest } from '../requests/requests' interface ServerInfo { app: ChildProcess @@ -347,6 +348,14 @@ async function getServerFileSize (server: ServerInfo, subPath: string) { return getFileSize(path) } +function makePingRequest (server: ServerInfo) { + return makeGetRequest({ + url: server.url, + path: '/api/v1/ping', + statusCodeExpected: 200 + }) +} + // --------------------------------------------------------------------------- export { @@ -358,6 +367,7 @@ export { cleanupTests, flushAndRunMultipleServers, flushTests, + makePingRequest, flushAndRunServer, killallServers, reRunServer, -- cgit v1.2.3