1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
8 flushAndRunMultipleServers,
13 setAccessTokensToServers
14 } from '../../../../shared/extra-utils'
16 checkBadCountPagination,
17 checkBadSortPagination,
18 checkBadStartPagination
19 } from '../../../../shared/extra-utils/requests/check-api-params'
20 import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
22 describe('Test blocklist API validators', function () {
23 let servers: ServerInfo[]
24 let server: ServerInfo
25 let userAccessToken: string
27 before(async function () {
30 servers = await flushAndRunMultipleServers(2)
31 await setAccessTokensToServers(servers)
35 const user = { username: 'user1', password: 'password' }
36 await server.users.create({ username: user.username, password: user.password })
38 userAccessToken = await server.login.getAccessToken(user)
40 await doubleFollow(servers[0], servers[1])
43 // ---------------------------------------------------------------
45 describe('When managing user blocklist', function () {
47 describe('When managing user accounts blocklist', function () {
48 const path = '/api/v1/users/me/blocklist/accounts'
50 describe('When listing blocked accounts', function () {
51 it('Should fail with an unauthenticated user', async function () {
52 await makeGetRequest({
55 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401
59 it('Should fail with a bad start pagination', async function () {
60 await checkBadStartPagination(server.url, path, server.accessToken)
63 it('Should fail with a bad count pagination', async function () {
64 await checkBadCountPagination(server.url, path, server.accessToken)
67 it('Should fail with an incorrect sort', async function () {
68 await checkBadSortPagination(server.url, path, server.accessToken)
72 describe('When blocking an account', function () {
73 it('Should fail with an unauthenticated user', async function () {
74 await makePostBodyRequest({
77 fields: { accountName: 'user1' },
78 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401
82 it('Should fail with an unknown account', async function () {
83 await makePostBodyRequest({
85 token: server.accessToken,
87 fields: { accountName: 'user2' },
88 statusCodeExpected: HttpStatusCode.NOT_FOUND_404
92 it('Should fail to block ourselves', async function () {
93 await makePostBodyRequest({
95 token: server.accessToken,
97 fields: { accountName: 'root' },
98 statusCodeExpected: HttpStatusCode.CONFLICT_409
102 it('Should succeed with the correct params', async function () {
103 await makePostBodyRequest({
105 token: server.accessToken,
107 fields: { accountName: 'user1' },
108 statusCodeExpected: HttpStatusCode.NO_CONTENT_204
113 describe('When unblocking an account', function () {
114 it('Should fail with an unauthenticated user', async function () {
115 await makeDeleteRequest({
117 path: path + '/user1',
118 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401
122 it('Should fail with an unknown account block', async function () {
123 await makeDeleteRequest({
125 path: path + '/user2',
126 token: server.accessToken,
127 statusCodeExpected: HttpStatusCode.NOT_FOUND_404
131 it('Should succeed with the correct params', async function () {
132 await makeDeleteRequest({
134 path: path + '/user1',
135 token: server.accessToken,
136 statusCodeExpected: HttpStatusCode.NO_CONTENT_204
142 describe('When managing user servers blocklist', function () {
143 const path = '/api/v1/users/me/blocklist/servers'
145 describe('When listing blocked servers', function () {
146 it('Should fail with an unauthenticated user', async function () {
147 await makeGetRequest({
150 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401
154 it('Should fail with a bad start pagination', async function () {
155 await checkBadStartPagination(server.url, path, server.accessToken)
158 it('Should fail with a bad count pagination', async function () {
159 await checkBadCountPagination(server.url, path, server.accessToken)
162 it('Should fail with an incorrect sort', async function () {
163 await checkBadSortPagination(server.url, path, server.accessToken)
167 describe('When blocking a server', function () {
168 it('Should fail with an unauthenticated user', async function () {
169 await makePostBodyRequest({
172 fields: { host: 'localhost:9002' },
173 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401
177 it('Should succeed with an unknown server', async function () {
178 await makePostBodyRequest({
180 token: server.accessToken,
182 fields: { host: 'localhost:9003' },
183 statusCodeExpected: HttpStatusCode.NO_CONTENT_204
187 it('Should fail with our own server', async function () {
188 await makePostBodyRequest({
190 token: server.accessToken,
192 fields: { host: 'localhost:' + server.port },
193 statusCodeExpected: HttpStatusCode.CONFLICT_409
197 it('Should succeed with the correct params', async function () {
198 await makePostBodyRequest({
200 token: server.accessToken,
202 fields: { host: 'localhost:' + servers[1].port },
203 statusCodeExpected: HttpStatusCode.NO_CONTENT_204
208 describe('When unblocking a server', function () {
209 it('Should fail with an unauthenticated user', async function () {
210 await makeDeleteRequest({
212 path: path + '/localhost:' + servers[1].port,
213 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401
217 it('Should fail with an unknown server block', async function () {
218 await makeDeleteRequest({
220 path: path + '/localhost:9004',
221 token: server.accessToken,
222 statusCodeExpected: HttpStatusCode.NOT_FOUND_404
226 it('Should succeed with the correct params', async function () {
227 await makeDeleteRequest({
229 path: path + '/localhost:' + servers[1].port,
230 token: server.accessToken,
231 statusCodeExpected: HttpStatusCode.NO_CONTENT_204
238 describe('When managing server blocklist', function () {
240 describe('When managing server accounts blocklist', function () {
241 const path = '/api/v1/server/blocklist/accounts'
243 describe('When listing blocked accounts', function () {
244 it('Should fail with an unauthenticated user', async function () {
245 await makeGetRequest({
248 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401
252 it('Should fail with a user without the appropriate rights', async function () {
253 await makeGetRequest({
255 token: userAccessToken,
257 statusCodeExpected: HttpStatusCode.FORBIDDEN_403
261 it('Should fail with a bad start pagination', async function () {
262 await checkBadStartPagination(server.url, path, server.accessToken)
265 it('Should fail with a bad count pagination', async function () {
266 await checkBadCountPagination(server.url, path, server.accessToken)
269 it('Should fail with an incorrect sort', async function () {
270 await checkBadSortPagination(server.url, path, server.accessToken)
274 describe('When blocking an account', function () {
275 it('Should fail with an unauthenticated user', async function () {
276 await makePostBodyRequest({
279 fields: { accountName: 'user1' },
280 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401
284 it('Should fail with a user without the appropriate rights', async function () {
285 await makePostBodyRequest({
287 token: userAccessToken,
289 fields: { accountName: 'user1' },
290 statusCodeExpected: HttpStatusCode.FORBIDDEN_403
294 it('Should fail with an unknown account', async function () {
295 await makePostBodyRequest({
297 token: server.accessToken,
299 fields: { accountName: 'user2' },
300 statusCodeExpected: HttpStatusCode.NOT_FOUND_404
304 it('Should fail to block ourselves', async function () {
305 await makePostBodyRequest({
307 token: server.accessToken,
309 fields: { accountName: 'root' },
310 statusCodeExpected: HttpStatusCode.CONFLICT_409
314 it('Should succeed with the correct params', async function () {
315 await makePostBodyRequest({
317 token: server.accessToken,
319 fields: { accountName: 'user1' },
320 statusCodeExpected: HttpStatusCode.NO_CONTENT_204
325 describe('When unblocking an account', function () {
326 it('Should fail with an unauthenticated user', async function () {
327 await makeDeleteRequest({
329 path: path + '/user1',
330 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401
334 it('Should fail with a user without the appropriate rights', async function () {
335 await makeDeleteRequest({
337 path: path + '/user1',
338 token: userAccessToken,
339 statusCodeExpected: HttpStatusCode.FORBIDDEN_403
343 it('Should fail with an unknown account block', async function () {
344 await makeDeleteRequest({
346 path: path + '/user2',
347 token: server.accessToken,
348 statusCodeExpected: HttpStatusCode.NOT_FOUND_404
352 it('Should succeed with the correct params', async function () {
353 await makeDeleteRequest({
355 path: path + '/user1',
356 token: server.accessToken,
357 statusCodeExpected: HttpStatusCode.NO_CONTENT_204
363 describe('When managing server servers blocklist', function () {
364 const path = '/api/v1/server/blocklist/servers'
366 describe('When listing blocked servers', function () {
367 it('Should fail with an unauthenticated user', async function () {
368 await makeGetRequest({
371 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401
375 it('Should fail with a user without the appropriate rights', async function () {
376 await makeGetRequest({
378 token: userAccessToken,
380 statusCodeExpected: HttpStatusCode.FORBIDDEN_403
384 it('Should fail with a bad start pagination', async function () {
385 await checkBadStartPagination(server.url, path, server.accessToken)
388 it('Should fail with a bad count pagination', async function () {
389 await checkBadCountPagination(server.url, path, server.accessToken)
392 it('Should fail with an incorrect sort', async function () {
393 await checkBadSortPagination(server.url, path, server.accessToken)
397 describe('When blocking a server', function () {
398 it('Should fail with an unauthenticated user', async function () {
399 await makePostBodyRequest({
402 fields: { host: 'localhost:' + servers[1].port },
403 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401
407 it('Should fail with a user without the appropriate rights', async function () {
408 await makePostBodyRequest({
410 token: userAccessToken,
412 fields: { host: 'localhost:' + servers[1].port },
413 statusCodeExpected: HttpStatusCode.FORBIDDEN_403
417 it('Should succeed with an unknown server', async function () {
418 await makePostBodyRequest({
420 token: server.accessToken,
422 fields: { host: 'localhost:9003' },
423 statusCodeExpected: HttpStatusCode.NO_CONTENT_204
427 it('Should fail with our own server', async function () {
428 await makePostBodyRequest({
430 token: server.accessToken,
432 fields: { host: 'localhost:' + server.port },
433 statusCodeExpected: HttpStatusCode.CONFLICT_409
437 it('Should succeed with the correct params', async function () {
438 await makePostBodyRequest({
440 token: server.accessToken,
442 fields: { host: 'localhost:' + servers[1].port },
443 statusCodeExpected: HttpStatusCode.NO_CONTENT_204
448 describe('When unblocking a server', function () {
449 it('Should fail with an unauthenticated user', async function () {
450 await makeDeleteRequest({
452 path: path + '/localhost:' + servers[1].port,
453 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401
457 it('Should fail with a user without the appropriate rights', async function () {
458 await makeDeleteRequest({
460 path: path + '/localhost:' + servers[1].port,
461 token: userAccessToken,
462 statusCodeExpected: HttpStatusCode.FORBIDDEN_403
466 it('Should fail with an unknown server block', async function () {
467 await makeDeleteRequest({
469 path: path + '/localhost:9004',
470 token: server.accessToken,
471 statusCodeExpected: HttpStatusCode.NOT_FOUND_404
475 it('Should succeed with the correct params', async function () {
476 await makeDeleteRequest({
478 path: path + '/localhost:' + servers[1].port,
479 token: server.accessToken,
480 statusCodeExpected: HttpStatusCode.NO_CONTENT_204
487 after(async function () {
488 await cleanupTests(servers)