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