Filter on follows actor types in about page
[oweals/peertube.git] / server / tests / api / check-params / follows.ts
1 /* tslint:disable:no-unused-expression */
2
3 import 'mocha'
4
5 import {
6   cleanupTests,
7   createUser,
8   flushAndRunServer,
9   makeDeleteRequest, makeGetRequest,
10   makePostBodyRequest,
11   ServerInfo,
12   setAccessTokensToServers,
13   userLogin
14 } from '../../../../shared/extra-utils'
15 import {
16   checkBadCountPagination,
17   checkBadSortPagination,
18   checkBadStartPagination
19 } from '../../../../shared/extra-utils/requests/check-api-params'
20
21 describe('Test server follows API validators', function () {
22   let server: ServerInfo
23
24   // ---------------------------------------------------------------
25
26   before(async function () {
27     this.timeout(30000)
28
29     server = await flushAndRunServer(1)
30
31     await setAccessTokensToServers([ server ])
32   })
33
34   describe('When managing following', function () {
35     let userAccessToken = null
36
37     before(async function () {
38       const user = {
39         username: 'user1',
40         password: 'password'
41       }
42
43       await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
44       userAccessToken = await userLogin(server, user)
45     })
46
47     describe('When adding follows', function () {
48       const path = '/api/v1/server/following'
49
50       it('Should fail without hosts', async function () {
51         await makePostBodyRequest({
52           url: server.url,
53           path,
54           token: server.accessToken,
55           statusCodeExpected: 400
56         })
57       })
58
59       it('Should fail if hosts is not an array', async function () {
60         await makePostBodyRequest({
61           url: server.url,
62           path,
63           token: server.accessToken,
64           fields: { hosts: 'localhost:9002' },
65           statusCodeExpected: 400
66         })
67       })
68
69       it('Should fail if the array is not composed by hosts', async function () {
70         await makePostBodyRequest({
71           url: server.url,
72           path,
73           fields: { hosts: [ 'localhost:9002', 'localhost:coucou' ] },
74           token: server.accessToken,
75           statusCodeExpected: 400
76         })
77       })
78
79       it('Should fail if the array is composed with http schemes', async function () {
80         await makePostBodyRequest({
81           url: server.url,
82           path,
83           fields: { hosts: [ 'localhost:9002', 'http://localhost:9003' ] },
84           token: server.accessToken,
85           statusCodeExpected: 400
86         })
87       })
88
89       it('Should fail if hosts are not unique', async function () {
90         await makePostBodyRequest({
91           url: server.url,
92           path,
93           fields: { urls: [ 'localhost:9002', 'localhost:9002' ] },
94           token: server.accessToken,
95           statusCodeExpected: 400
96         })
97       })
98
99       it('Should fail with an invalid token', async function () {
100         await makePostBodyRequest({
101           url: server.url,
102           path,
103           fields: { hosts: [ 'localhost:9002' ] },
104           token: 'fake_token',
105           statusCodeExpected: 401
106         })
107       })
108
109       it('Should fail if the user is not an administrator', async function () {
110         await makePostBodyRequest({
111           url: server.url,
112           path,
113           fields: { hosts: [ 'localhost:9002' ] },
114           token: userAccessToken,
115           statusCodeExpected: 403
116         })
117       })
118     })
119
120     describe('When listing followings', function () {
121       const path = '/api/v1/server/following'
122
123       it('Should fail with a bad start pagination', async function () {
124         await checkBadStartPagination(server.url, path)
125       })
126
127       it('Should fail with a bad count pagination', async function () {
128         await checkBadCountPagination(server.url, path)
129       })
130
131       it('Should fail with an incorrect sort', async function () {
132         await checkBadSortPagination(server.url, path)
133       })
134
135       it('Should fail with an incorrect state', async function () {
136         await makeGetRequest({
137           url: server.url,
138           path,
139           query: {
140             state: 'blabla'
141           }
142         })
143       })
144
145       it('Should fail with an incorrect actor type', async function () {
146         await makeGetRequest({
147           url: server.url,
148           path,
149           query: {
150             actorType: 'blabla'
151           }
152         })
153       })
154
155       it('Should fail succeed with the correct params', async function () {
156         await makeGetRequest({
157           url: server.url,
158           path,
159           statusCodeExpected: 200,
160           query: {
161             state: 'accepted',
162             actorType: 'Application'
163           }
164         })
165       })
166     })
167
168     describe('When listing followers', function () {
169       const path = '/api/v1/server/followers'
170
171       it('Should fail with a bad start pagination', async function () {
172         await checkBadStartPagination(server.url, path)
173       })
174
175       it('Should fail with a bad count pagination', async function () {
176         await checkBadCountPagination(server.url, path)
177       })
178
179       it('Should fail with an incorrect sort', async function () {
180         await checkBadSortPagination(server.url, path)
181       })
182
183       it('Should fail with an incorrect actor type', async function () {
184         await makeGetRequest({
185           url: server.url,
186           path,
187           query: {
188             actorType: 'blabla'
189           }
190         })
191       })
192
193       it('Should fail with an incorrect state', async function () {
194         await makeGetRequest({
195           url: server.url,
196           path,
197           query: {
198             state: 'blabla',
199             actorType: 'Application'
200           }
201         })
202       })
203
204       it('Should fail succeed with the correct params', async function () {
205         await makeGetRequest({
206           url: server.url,
207           path,
208           statusCodeExpected: 200,
209           query: {
210             state: 'accepted'
211           }
212         })
213       })
214     })
215
216     describe('When removing a follower', function () {
217       const path = '/api/v1/server/followers'
218
219       it('Should fail with an invalid token', async function () {
220         await makeDeleteRequest({
221           url: server.url,
222           path: path + '/toto@localhost:9002',
223           token: 'fake_token',
224           statusCodeExpected: 401
225         })
226       })
227
228       it('Should fail if the user is not an administrator', async function () {
229         await makeDeleteRequest({
230           url: server.url,
231           path: path + '/toto@localhost:9002',
232           token: userAccessToken,
233           statusCodeExpected: 403
234         })
235       })
236
237       it('Should fail with an invalid follower', async function () {
238         await makeDeleteRequest({
239           url: server.url,
240           path: path + '/toto',
241           token: server.accessToken,
242           statusCodeExpected: 400
243         })
244       })
245
246       it('Should fail with an unknown follower', async function () {
247         await makeDeleteRequest({
248           url: server.url,
249           path: path + '/toto@localhost:9003',
250           token: server.accessToken,
251           statusCodeExpected: 404
252         })
253       })
254     })
255
256     describe('When accepting a follower', function () {
257       const path = '/api/v1/server/followers'
258
259       it('Should fail with an invalid token', async function () {
260         await makePostBodyRequest({
261           url: server.url,
262           path: path + '/toto@localhost:9002/accept',
263           token: 'fake_token',
264           statusCodeExpected: 401
265         })
266       })
267
268       it('Should fail if the user is not an administrator', async function () {
269         await makePostBodyRequest({
270           url: server.url,
271           path: path + '/toto@localhost:9002/accept',
272           token: userAccessToken,
273           statusCodeExpected: 403
274         })
275       })
276
277       it('Should fail with an invalid follower', async function () {
278         await makePostBodyRequest({
279           url: server.url,
280           path: path + '/toto/accept',
281           token: server.accessToken,
282           statusCodeExpected: 400
283         })
284       })
285
286       it('Should fail with an unknown follower', async function () {
287         await makePostBodyRequest({
288           url: server.url,
289           path: path + '/toto@localhost:9003/accept',
290           token: server.accessToken,
291           statusCodeExpected: 404
292         })
293       })
294     })
295
296     describe('When rejecting a follower', function () {
297       const path = '/api/v1/server/followers'
298
299       it('Should fail with an invalid token', async function () {
300         await makePostBodyRequest({
301           url: server.url,
302           path: path + '/toto@localhost:9002/reject',
303           token: 'fake_token',
304           statusCodeExpected: 401
305         })
306       })
307
308       it('Should fail if the user is not an administrator', async function () {
309         await makePostBodyRequest({
310           url: server.url,
311           path: path + '/toto@localhost:9002/reject',
312           token: userAccessToken,
313           statusCodeExpected: 403
314         })
315       })
316
317       it('Should fail with an invalid follower', async function () {
318         await makePostBodyRequest({
319           url: server.url,
320           path: path + '/toto/reject',
321           token: server.accessToken,
322           statusCodeExpected: 400
323         })
324       })
325
326       it('Should fail with an unknown follower', async function () {
327         await makePostBodyRequest({
328           url: server.url,
329           path: path + '/toto@localhost:9003/reject',
330           token: server.accessToken,
331           statusCodeExpected: 404
332         })
333       })
334     })
335
336     describe('When removing following', function () {
337       const path = '/api/v1/server/following'
338
339       it('Should fail with an invalid token', async function () {
340         await makeDeleteRequest({
341           url: server.url,
342           path: path + '/localhost:9002',
343           token: 'fake_token',
344           statusCodeExpected: 401
345         })
346       })
347
348       it('Should fail if the user is not an administrator', async function () {
349         await makeDeleteRequest({
350           url: server.url,
351           path: path + '/localhost:9002',
352           token: userAccessToken,
353           statusCodeExpected: 403
354         })
355       })
356
357       it('Should fail if we do not follow this server', async function () {
358         await makeDeleteRequest({
359           url: server.url,
360           path: path + '/example.com',
361           token: server.accessToken,
362           statusCodeExpected: 404
363         })
364       })
365     })
366   })
367
368   after(async function () {
369     await cleanupTests([ server ])
370   })
371 })