1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import { expect } from 'chai'
5 import { HttpStatusCode } from '@shared/models'
6 import { cleanupTests, createSingleServer, makeGetRequest, PeerTubeServer, setAccessTokensToServers } from '@shared/server-commands'
8 describe('Test logs API validators', function () {
9 const path = '/api/v1/server/logs'
10 let server: PeerTubeServer
11 let userAccessToken = ''
13 // ---------------------------------------------------------------
15 before(async function () {
18 server = await createSingleServer(1)
20 await setAccessTokensToServers([ server ])
24 password: 'my super password'
26 await server.users.create({ username: user.username, password: user.password })
27 userAccessToken = await server.login.getAccessToken(user)
30 describe('When getting logs', function () {
32 it('Should fail with a non authenticated user', async function () {
33 await makeGetRequest({
36 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
40 it('Should fail with a non admin user', async function () {
41 await makeGetRequest({
44 token: userAccessToken,
45 expectedStatus: HttpStatusCode.FORBIDDEN_403
49 it('Should fail with a missing startDate query', async function () {
50 await makeGetRequest({
53 token: server.accessToken,
54 expectedStatus: HttpStatusCode.BAD_REQUEST_400
58 it('Should fail with a bad startDate query', async function () {
59 await makeGetRequest({
62 token: server.accessToken,
63 query: { startDate: 'toto' },
64 expectedStatus: HttpStatusCode.BAD_REQUEST_400
68 it('Should fail with a bad endDate query', async function () {
69 await makeGetRequest({
72 token: server.accessToken,
73 query: { startDate: new Date().toISOString(), endDate: 'toto' },
74 expectedStatus: HttpStatusCode.BAD_REQUEST_400
78 it('Should fail with a bad level parameter', async function () {
79 await makeGetRequest({
82 token: server.accessToken,
83 query: { startDate: new Date().toISOString(), level: 'toto' },
84 expectedStatus: HttpStatusCode.BAD_REQUEST_400
88 it('Should succeed with the correct params', async function () {
89 await makeGetRequest({
92 token: server.accessToken,
93 query: { startDate: new Date().toISOString() },
94 expectedStatus: HttpStatusCode.OK_200
99 describe('When creating client logs', function () {
101 level: 'warn' as 'warn',
102 message: 'my super message',
103 url: 'https://example.com/toto'
105 const expectedStatus = HttpStatusCode.BAD_REQUEST_400
107 it('Should fail with an invalid level', async function () {
108 await server.logs.createLogClient({ payload: { ...base, level: '' as any }, expectedStatus })
109 await server.logs.createLogClient({ payload: { ...base, level: undefined }, expectedStatus })
110 await server.logs.createLogClient({ payload: { ...base, level: 'toto' as any }, expectedStatus })
113 it('Should fail with an invalid message', async function () {
114 await server.logs.createLogClient({ payload: { ...base, message: undefined }, expectedStatus })
115 await server.logs.createLogClient({ payload: { ...base, message: '' }, expectedStatus })
116 await server.logs.createLogClient({ payload: { ...base, message: 'm'.repeat(2500) }, expectedStatus })
119 it('Should fail with an invalid url', async function () {
120 await server.logs.createLogClient({ payload: { ...base, url: undefined }, expectedStatus })
121 await server.logs.createLogClient({ payload: { ...base, url: 'toto' }, expectedStatus })
124 it('Should fail with an invalid stackTrace', async function () {
125 await server.logs.createLogClient({ payload: { ...base, stackTrace: 's'.repeat(10000) }, expectedStatus })
128 it('Should fail with an invalid userAgent', async function () {
129 await server.logs.createLogClient({ payload: { ...base, userAgent: 's'.repeat(500) }, expectedStatus })
132 it('Should fail with an invalid meta', async function () {
133 await server.logs.createLogClient({ payload: { ...base, meta: 's'.repeat(10000) }, expectedStatus })
136 it('Should succeed with the correct params', async function () {
137 await server.logs.createLogClient({ payload: { ...base, stackTrace: 'stackTrace', meta: '{toto}', userAgent: 'userAgent' } })
140 it('Should rate limit log creation', async function () {
143 for (let i = 0; i < 10; i++) {
145 await server.logs.createLogClient({ token: null, payload: base })
151 expect(fail).to.be.true
155 after(async function () {
156 await cleanupTests([ server ])