1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import { checkBadCountPagination, checkBadSortPagination, checkBadStartPagination } from '@server/tests/shared'
4 import { HttpStatusCode } from '@shared/models'
12 setAccessTokensToServers
13 } from '@shared/server-commands'
15 describe('Test server follows API validators', function () {
16 let server: PeerTubeServer
18 // ---------------------------------------------------------------
20 before(async function () {
23 server = await createSingleServer(1)
25 await setAccessTokensToServers([ server ])
28 describe('When managing following', function () {
29 let userAccessToken = null
31 before(async function () {
32 userAccessToken = await server.users.generateUserAndToken('user1')
35 describe('When adding follows', function () {
36 const path = '/api/v1/server/following'
38 it('Should fail with nothing', async function () {
39 await makePostBodyRequest({
42 token: server.accessToken,
43 expectedStatus: HttpStatusCode.BAD_REQUEST_400
47 it('Should fail if hosts is not composed by hosts', async function () {
48 await makePostBodyRequest({
51 fields: { hosts: [ '127.0.0.1:9002', '127.0.0.1:coucou' ] },
52 token: server.accessToken,
53 expectedStatus: HttpStatusCode.BAD_REQUEST_400
57 it('Should fail if hosts is composed with http schemes', async function () {
58 await makePostBodyRequest({
61 fields: { hosts: [ '127.0.0.1:9002', 'http://127.0.0.1:9003' ] },
62 token: server.accessToken,
63 expectedStatus: HttpStatusCode.BAD_REQUEST_400
67 it('Should fail if hosts are not unique', async function () {
68 await makePostBodyRequest({
71 fields: { urls: [ '127.0.0.1:9002', '127.0.0.1:9002' ] },
72 token: server.accessToken,
73 expectedStatus: HttpStatusCode.BAD_REQUEST_400
77 it('Should fail if handles is not composed by handles', async function () {
78 await makePostBodyRequest({
81 fields: { handles: [ 'hello@example.com', '127.0.0.1:9001' ] },
82 token: server.accessToken,
83 expectedStatus: HttpStatusCode.BAD_REQUEST_400
87 it('Should fail if handles are not unique', async function () {
88 await makePostBodyRequest({
91 fields: { urls: [ 'hello@example.com', 'hello@example.com' ] },
92 token: server.accessToken,
93 expectedStatus: HttpStatusCode.BAD_REQUEST_400
97 it('Should fail with an invalid token', async function () {
98 await makePostBodyRequest({
101 fields: { hosts: [ '127.0.0.1:9002' ] },
103 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
107 it('Should fail if the user is not an administrator', async function () {
108 await makePostBodyRequest({
111 fields: { hosts: [ '127.0.0.1:9002' ] },
112 token: userAccessToken,
113 expectedStatus: HttpStatusCode.FORBIDDEN_403
118 describe('When listing followings', function () {
119 const path = '/api/v1/server/following'
121 it('Should fail with a bad start pagination', async function () {
122 await checkBadStartPagination(server.url, path)
125 it('Should fail with a bad count pagination', async function () {
126 await checkBadCountPagination(server.url, path)
129 it('Should fail with an incorrect sort', async function () {
130 await checkBadSortPagination(server.url, path)
133 it('Should fail with an incorrect state', async function () {
134 await makeGetRequest({
143 it('Should fail with an incorrect actor type', async function () {
144 await makeGetRequest({
153 it('Should fail succeed with the correct params', async function () {
154 await makeGetRequest({
157 expectedStatus: HttpStatusCode.OK_200,
160 actorType: 'Application'
166 describe('When listing followers', function () {
167 const path = '/api/v1/server/followers'
169 it('Should fail with a bad start pagination', async function () {
170 await checkBadStartPagination(server.url, path)
173 it('Should fail with a bad count pagination', async function () {
174 await checkBadCountPagination(server.url, path)
177 it('Should fail with an incorrect sort', async function () {
178 await checkBadSortPagination(server.url, path)
181 it('Should fail with an incorrect actor type', async function () {
182 await makeGetRequest({
191 it('Should fail with an incorrect state', async function () {
192 await makeGetRequest({
197 actorType: 'Application'
202 it('Should fail succeed with the correct params', async function () {
203 await makeGetRequest({
206 expectedStatus: HttpStatusCode.OK_200,
214 describe('When removing a follower', function () {
215 const path = '/api/v1/server/followers'
217 it('Should fail with an invalid token', async function () {
218 await makeDeleteRequest({
220 path: path + '/toto@127.0.0.1:9002',
222 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
226 it('Should fail if the user is not an administrator', async function () {
227 await makeDeleteRequest({
229 path: path + '/toto@127.0.0.1:9002',
230 token: userAccessToken,
231 expectedStatus: HttpStatusCode.FORBIDDEN_403
235 it('Should fail with an invalid follower', async function () {
236 await makeDeleteRequest({
238 path: path + '/toto',
239 token: server.accessToken,
240 expectedStatus: HttpStatusCode.BAD_REQUEST_400
244 it('Should fail with an unknown follower', async function () {
245 await makeDeleteRequest({
247 path: path + '/toto@127.0.0.1:9003',
248 token: server.accessToken,
249 expectedStatus: HttpStatusCode.NOT_FOUND_404
254 describe('When accepting a follower', function () {
255 const path = '/api/v1/server/followers'
257 it('Should fail with an invalid token', async function () {
258 await makePostBodyRequest({
260 path: path + '/toto@127.0.0.1:9002/accept',
262 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
266 it('Should fail if the user is not an administrator', async function () {
267 await makePostBodyRequest({
269 path: path + '/toto@127.0.0.1:9002/accept',
270 token: userAccessToken,
271 expectedStatus: HttpStatusCode.FORBIDDEN_403
275 it('Should fail with an invalid follower', async function () {
276 await makePostBodyRequest({
278 path: path + '/toto/accept',
279 token: server.accessToken,
280 expectedStatus: HttpStatusCode.BAD_REQUEST_400
284 it('Should fail with an unknown follower', async function () {
285 await makePostBodyRequest({
287 path: path + '/toto@127.0.0.1:9003/accept',
288 token: server.accessToken,
289 expectedStatus: HttpStatusCode.NOT_FOUND_404
294 describe('When rejecting a follower', function () {
295 const path = '/api/v1/server/followers'
297 it('Should fail with an invalid token', async function () {
298 await makePostBodyRequest({
300 path: path + '/toto@127.0.0.1:9002/reject',
302 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
306 it('Should fail if the user is not an administrator', async function () {
307 await makePostBodyRequest({
309 path: path + '/toto@127.0.0.1:9002/reject',
310 token: userAccessToken,
311 expectedStatus: HttpStatusCode.FORBIDDEN_403
315 it('Should fail with an invalid follower', async function () {
316 await makePostBodyRequest({
318 path: path + '/toto/reject',
319 token: server.accessToken,
320 expectedStatus: HttpStatusCode.BAD_REQUEST_400
324 it('Should fail with an unknown follower', async function () {
325 await makePostBodyRequest({
327 path: path + '/toto@127.0.0.1:9003/reject',
328 token: server.accessToken,
329 expectedStatus: HttpStatusCode.NOT_FOUND_404
334 describe('When removing following', function () {
335 const path = '/api/v1/server/following'
337 it('Should fail with an invalid token', async function () {
338 await makeDeleteRequest({
340 path: path + '/127.0.0.1:9002',
342 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
346 it('Should fail if the user is not an administrator', async function () {
347 await makeDeleteRequest({
349 path: path + '/127.0.0.1:9002',
350 token: userAccessToken,
351 expectedStatus: HttpStatusCode.FORBIDDEN_403
355 it('Should fail if we do not follow this server', async function () {
356 await makeDeleteRequest({
358 path: path + '/example.com',
359 token: server.accessToken,
360 expectedStatus: HttpStatusCode.NOT_FOUND_404
366 after(async function () {
367 await cleanupTests([ server ])