From fd8710b897a67518d3a61c319e54b6a65ba443ef Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 10 Apr 2019 15:26:33 +0200 Subject: Add logs endpoint --- server/tests/api/check-params/index.ts | 1 + server/tests/api/check-params/logs.ts | 117 +++++++++++++++++++++++++++++++++ server/tests/api/server/index.ts | 1 + server/tests/api/server/logs.ts | 92 ++++++++++++++++++++++++++ 4 files changed, 211 insertions(+) create mode 100644 server/tests/api/check-params/logs.ts create mode 100644 server/tests/api/server/logs.ts (limited to 'server/tests/api') diff --git a/server/tests/api/check-params/index.ts b/server/tests/api/check-params/index.ts index ca51cd39a..bdac95025 100644 --- a/server/tests/api/check-params/index.ts +++ b/server/tests/api/check-params/index.ts @@ -4,6 +4,7 @@ import './config' import './contact-form' import './follows' import './jobs' +import './logs' import './redundancy' import './search' import './services' diff --git a/server/tests/api/check-params/logs.ts b/server/tests/api/check-params/logs.ts new file mode 100644 index 000000000..d6a40da61 --- /dev/null +++ b/server/tests/api/check-params/logs.ts @@ -0,0 +1,117 @@ +/* tslint:disable:no-unused-expression */ + +import 'mocha' + +import { + createUser, + flushTests, + killallServers, + runServer, + ServerInfo, + setAccessTokensToServers, + userLogin +} from '../../../../shared/utils' +import { makeGetRequest } from '../../../../shared/utils/requests/requests' + +describe('Test logs API validators', function () { + const path = '/api/v1/server/logs' + let server: ServerInfo + let userAccessToken = '' + + // --------------------------------------------------------------- + + before(async function () { + this.timeout(120000) + + await flushTests() + + server = await runServer(1) + + await setAccessTokensToServers([ server ]) + + const user = { + username: 'user1', + password: 'my super password' + } + await createUser(server.url, server.accessToken, user.username, user.password) + userAccessToken = await userLogin(server, user) + }) + + describe('When getting logs', function () { + + it('Should fail with a non authenticated user', async function () { + await makeGetRequest({ + url: server.url, + path, + statusCodeExpected: 401 + }) + }) + + it('Should fail with a non admin user', async function () { + await makeGetRequest({ + url: server.url, + path, + token: userAccessToken, + statusCodeExpected: 403 + }) + }) + + it('Should fail with a missing startDate query', async function () { + await makeGetRequest({ + url: server.url, + path, + token: server.accessToken, + statusCodeExpected: 400 + }) + }) + + it('Should fail with a bad startDate query', async function () { + await makeGetRequest({ + url: server.url, + path, + token: server.accessToken, + query: { startDate: 'toto' }, + statusCodeExpected: 400 + }) + }) + + it('Should fail with a bad endDate query', async function () { + await makeGetRequest({ + url: server.url, + path, + token: server.accessToken, + query: { startDate: new Date().toISOString(), endDate: 'toto' }, + statusCodeExpected: 400 + }) + }) + + it('Should fail with a bad level parameter', async function () { + await makeGetRequest({ + url: server.url, + path, + token: server.accessToken, + query: { startDate: new Date().toISOString(), level: 'toto' }, + statusCodeExpected: 400 + }) + }) + + it('Should succeed with the correct params', async function () { + await makeGetRequest({ + url: server.url, + path, + token: server.accessToken, + query: { startDate: new Date().toISOString() }, + statusCodeExpected: 200 + }) + }) + }) + + after(async function () { + killallServers([ server ]) + + // Keep the logs if the test failed + if (this['ok']) { + await flushTests() + } + }) +}) diff --git a/server/tests/api/server/index.ts b/server/tests/api/server/index.ts index 4e53074ab..94c15e0d0 100644 --- a/server/tests/api/server/index.ts +++ b/server/tests/api/server/index.ts @@ -6,6 +6,7 @@ import './follows' import './follows-moderation' import './handle-down' import './jobs' +import './logs' import './reverse-proxy' import './stats' import './tracker' diff --git a/server/tests/api/server/logs.ts b/server/tests/api/server/logs.ts new file mode 100644 index 000000000..05b0308de --- /dev/null +++ b/server/tests/api/server/logs.ts @@ -0,0 +1,92 @@ +/* tslint:disable:no-unused-expression */ + +import * as chai from 'chai' +import 'mocha' +import { flushTests, killallServers, runServer, ServerInfo, setAccessTokensToServers } from '../../../../shared/utils/index' +import { waitJobs } from '../../../../shared/utils/server/jobs' +import { uploadVideo } from '../../../../shared/utils/videos/videos' +import { getLogs } from '../../../../shared/utils/logs/logs' + +const expect = chai.expect + +describe('Test logs', function () { + let server: ServerInfo + + before(async function () { + this.timeout(30000) + + await flushTests() + + server = await runServer(1) + await setAccessTokensToServers([ server ]) + }) + + it('Should get logs with a start date', async function () { + this.timeout(10000) + + await uploadVideo(server.url, server.accessToken, { name: 'video 1' }) + await waitJobs([ server ]) + + const now = new Date() + + await uploadVideo(server.url, server.accessToken, { name: 'video 2' }) + await waitJobs([ server ]) + + const res = await getLogs(server.url, server.accessToken, now) + const logsString = JSON.stringify(res.body) + + expect(logsString.includes('video 1')).to.be.false + expect(logsString.includes('video 2')).to.be.true + }) + + it('Should get logs with an end date', async function () { + this.timeout(10000) + + await uploadVideo(server.url, server.accessToken, { name: 'video 3' }) + await waitJobs([ server ]) + + const now1 = new Date() + + await uploadVideo(server.url, server.accessToken, { name: 'video 4' }) + await waitJobs([ server ]) + + const now2 = new Date() + + await uploadVideo(server.url, server.accessToken, { name: 'video 5' }) + await waitJobs([ server ]) + + const res = await getLogs(server.url, server.accessToken, now1, now2) + const logsString = JSON.stringify(res.body) + + expect(logsString.includes('video 3')).to.be.false + expect(logsString.includes('video 4')).to.be.true + expect(logsString.includes('video 5')).to.be.false + }) + + it('Should get filter by level', async function () { + this.timeout(10000) + + const now = new Date() + + await uploadVideo(server.url, server.accessToken, { name: 'video 6' }) + await waitJobs([ server ]) + + { + const res = await getLogs(server.url, server.accessToken, now, undefined, 'info') + const logsString = JSON.stringify(res.body) + + expect(logsString.includes('video 6')).to.be.true + } + + { + const res = await getLogs(server.url, server.accessToken, now, undefined, 'warn') + const logsString = JSON.stringify(res.body) + + expect(logsString.includes('video 6')).to.be.false + } + }) + + after(async function () { + killallServers([ server ]) + }) +}) -- cgit v1.2.3