1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import { expect } from 'chai'
4 import { HttpStatusCode } from '@shared/models'
5 import { cleanupTests, createSingleServer, makeGetRequest, PeerTubeServer, setAccessTokensToServers } from '@shared/server-commands'
7 describe('Test logs API validators', function () {
8 const path = '/api/v1/server/logs'
9 let server: PeerTubeServer
10 let userAccessToken = ''
12 // ---------------------------------------------------------------
14 before(async function () {
17 server = await createSingleServer(1)
19 await setAccessTokensToServers([ server ])
23 password: 'my super password'
25 await server.users.create({ username: user.username, password: user.password })
26 userAccessToken = await server.login.getAccessToken(user)
29 describe('When getting logs', function () {
31 it('Should fail with a non authenticated user', async function () {
32 await makeGetRequest({
35 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
39 it('Should fail with a non admin user', async function () {
40 await makeGetRequest({
43 token: userAccessToken,
44 expectedStatus: HttpStatusCode.FORBIDDEN_403
48 it('Should fail with a missing startDate query', async function () {
49 await makeGetRequest({
52 token: server.accessToken,
53 expectedStatus: HttpStatusCode.BAD_REQUEST_400
57 it('Should fail with a bad startDate query', async function () {
58 await makeGetRequest({
61 token: server.accessToken,
62 query: { startDate: 'toto' },
63 expectedStatus: HttpStatusCode.BAD_REQUEST_400
67 it('Should fail with a bad endDate query', async function () {
68 await makeGetRequest({
71 token: server.accessToken,
72 query: { startDate: new Date().toISOString(), endDate: 'toto' },
73 expectedStatus: HttpStatusCode.BAD_REQUEST_400
77 it('Should fail with a bad level parameter', async function () {
78 await makeGetRequest({
81 token: server.accessToken,
82 query: { startDate: new Date().toISOString(), level: 'toto' },
83 expectedStatus: HttpStatusCode.BAD_REQUEST_400
87 it('Should succeed with the correct params', async function () {
88 await makeGetRequest({
91 token: server.accessToken,
92 query: { startDate: new Date().toISOString() },
93 expectedStatus: HttpStatusCode.OK_200
98 describe('When creating client logs', function () {
100 level: 'warn' as 'warn',
101 message: 'my super message',
102 url: 'https://example.com/toto'
104 const expectedStatus = HttpStatusCode.BAD_REQUEST_400
106 it('Should fail with an invalid level', async function () {
107 await server.logs.createLogClient({ payload: { ...base, level: '' as any }, expectedStatus })
108 await server.logs.createLogClient({ payload: { ...base, level: undefined }, expectedStatus })
109 await server.logs.createLogClient({ payload: { ...base, level: 'toto' as any }, expectedStatus })
112 it('Should fail with an invalid message', async function () {
113 await server.logs.createLogClient({ payload: { ...base, message: undefined }, expectedStatus })
114 await server.logs.createLogClient({ payload: { ...base, message: '' }, expectedStatus })
115 await server.logs.createLogClient({ payload: { ...base, message: 'm'.repeat(2500) }, expectedStatus })
118 it('Should fail with an invalid url', async function () {
119 await server.logs.createLogClient({ payload: { ...base, url: undefined }, expectedStatus })
120 await server.logs.createLogClient({ payload: { ...base, url: 'toto' }, expectedStatus })
123 it('Should fail with an invalid stackTrace', async function () {
124 await server.logs.createLogClient({ payload: { ...base, stackTrace: 's'.repeat(20000) }, expectedStatus })
127 it('Should fail with an invalid userAgent', async function () {
128 await server.logs.createLogClient({ payload: { ...base, userAgent: 's'.repeat(500) }, expectedStatus })
131 it('Should fail with an invalid meta', async function () {
132 await server.logs.createLogClient({ payload: { ...base, meta: 's'.repeat(10000) }, expectedStatus })
135 it('Should succeed with the correct params', async function () {
136 await server.logs.createLogClient({ payload: { ...base, stackTrace: 'stackTrace', meta: '{toto}', userAgent: 'userAgent' } })
139 it('Should rate limit log creation', async function () {
142 for (let i = 0; i < 10; i++) {
144 await server.logs.createLogClient({ token: null, payload: base })
150 expect(fail).to.be.true
154 after(async function () {
155 await cleanupTests([ server ])