]>
Commit | Line | Data |
---|---|---|
1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ | |
2 | ||
3 | import 'mocha' | |
4 | import { expect } from 'chai' | |
5 | import { HttpStatusCode } from '@shared/models' | |
6 | import { cleanupTests, createSingleServer, makeGetRequest, PeerTubeServer, setAccessTokensToServers } from '@shared/server-commands' | |
7 | ||
8 | describe('Test logs API validators', function () { | |
9 | const path = '/api/v1/server/logs' | |
10 | let server: PeerTubeServer | |
11 | let userAccessToken = '' | |
12 | ||
13 | // --------------------------------------------------------------- | |
14 | ||
15 | before(async function () { | |
16 | this.timeout(120000) | |
17 | ||
18 | server = await createSingleServer(1) | |
19 | ||
20 | await setAccessTokensToServers([ server ]) | |
21 | ||
22 | const user = { | |
23 | username: 'user1', | |
24 | password: 'my super password' | |
25 | } | |
26 | await server.users.create({ username: user.username, password: user.password }) | |
27 | userAccessToken = await server.login.getAccessToken(user) | |
28 | }) | |
29 | ||
30 | describe('When getting logs', function () { | |
31 | ||
32 | it('Should fail with a non authenticated user', async function () { | |
33 | await makeGetRequest({ | |
34 | url: server.url, | |
35 | path, | |
36 | expectedStatus: HttpStatusCode.UNAUTHORIZED_401 | |
37 | }) | |
38 | }) | |
39 | ||
40 | it('Should fail with a non admin user', async function () { | |
41 | await makeGetRequest({ | |
42 | url: server.url, | |
43 | path, | |
44 | token: userAccessToken, | |
45 | expectedStatus: HttpStatusCode.FORBIDDEN_403 | |
46 | }) | |
47 | }) | |
48 | ||
49 | it('Should fail with a missing startDate query', async function () { | |
50 | await makeGetRequest({ | |
51 | url: server.url, | |
52 | path, | |
53 | token: server.accessToken, | |
54 | expectedStatus: HttpStatusCode.BAD_REQUEST_400 | |
55 | }) | |
56 | }) | |
57 | ||
58 | it('Should fail with a bad startDate query', async function () { | |
59 | await makeGetRequest({ | |
60 | url: server.url, | |
61 | path, | |
62 | token: server.accessToken, | |
63 | query: { startDate: 'toto' }, | |
64 | expectedStatus: HttpStatusCode.BAD_REQUEST_400 | |
65 | }) | |
66 | }) | |
67 | ||
68 | it('Should fail with a bad endDate query', async function () { | |
69 | await makeGetRequest({ | |
70 | url: server.url, | |
71 | path, | |
72 | token: server.accessToken, | |
73 | query: { startDate: new Date().toISOString(), endDate: 'toto' }, | |
74 | expectedStatus: HttpStatusCode.BAD_REQUEST_400 | |
75 | }) | |
76 | }) | |
77 | ||
78 | it('Should fail with a bad level parameter', async function () { | |
79 | await makeGetRequest({ | |
80 | url: server.url, | |
81 | path, | |
82 | token: server.accessToken, | |
83 | query: { startDate: new Date().toISOString(), level: 'toto' }, | |
84 | expectedStatus: HttpStatusCode.BAD_REQUEST_400 | |
85 | }) | |
86 | }) | |
87 | ||
88 | it('Should succeed with the correct params', async function () { | |
89 | await makeGetRequest({ | |
90 | url: server.url, | |
91 | path, | |
92 | token: server.accessToken, | |
93 | query: { startDate: new Date().toISOString() }, | |
94 | expectedStatus: HttpStatusCode.OK_200 | |
95 | }) | |
96 | }) | |
97 | }) | |
98 | ||
99 | describe('When creating client logs', function () { | |
100 | const base = { | |
101 | level: 'warn' as 'warn', | |
102 | message: 'my super message', | |
103 | url: 'https://example.com/toto' | |
104 | } | |
105 | const expectedStatus = HttpStatusCode.BAD_REQUEST_400 | |
106 | ||
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 }) | |
111 | }) | |
112 | ||
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 }) | |
117 | }) | |
118 | ||
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 }) | |
122 | }) | |
123 | ||
124 | it('Should fail with an invalid stackTrace', async function () { | |
125 | await server.logs.createLogClient({ payload: { ...base, stackTrace: 's'.repeat(20000) }, expectedStatus }) | |
126 | }) | |
127 | ||
128 | it('Should fail with an invalid userAgent', async function () { | |
129 | await server.logs.createLogClient({ payload: { ...base, userAgent: 's'.repeat(500) }, expectedStatus }) | |
130 | }) | |
131 | ||
132 | it('Should fail with an invalid meta', async function () { | |
133 | await server.logs.createLogClient({ payload: { ...base, meta: 's'.repeat(10000) }, expectedStatus }) | |
134 | }) | |
135 | ||
136 | it('Should succeed with the correct params', async function () { | |
137 | await server.logs.createLogClient({ payload: { ...base, stackTrace: 'stackTrace', meta: '{toto}', userAgent: 'userAgent' } }) | |
138 | }) | |
139 | ||
140 | it('Should rate limit log creation', async function () { | |
141 | let fail = false | |
142 | ||
143 | for (let i = 0; i < 10; i++) { | |
144 | try { | |
145 | await server.logs.createLogClient({ token: null, payload: base }) | |
146 | } catch { | |
147 | fail = true | |
148 | } | |
149 | } | |
150 | ||
151 | expect(fail).to.be.true | |
152 | }) | |
153 | }) | |
154 | ||
155 | after(async function () { | |
156 | await cleanupTests([ server ]) | |
157 | }) | |
158 | }) |