]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/check-params/follows.ts
Only display accepted followers/followings in about page
[github/Chocobozzz/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 succeed with the correct params', async function () {
146 await makeGetRequest({
147 url: server.url,
148 path,
149 statusCodeExpected: 200,
150 query: {
151 state: 'accepted'
152 }
153 })
154 })
155 })
156
157 describe('When listing followers', function () {
158 const path = '/api/v1/server/followers'
159
160 it('Should fail with a bad start pagination', async function () {
161 await checkBadStartPagination(server.url, path)
162 })
163
164 it('Should fail with a bad count pagination', async function () {
165 await checkBadCountPagination(server.url, path)
166 })
167
168 it('Should fail with an incorrect sort', async function () {
169 await checkBadSortPagination(server.url, path)
170 })
171
172 it('Should fail with an incorrect state', async function () {
173 await makeGetRequest({
174 url: server.url,
175 path,
176 query: {
177 state: 'blabla'
178 }
179 })
180 })
181
182 it('Should fail succeed with the correct params', async function () {
183 await makeGetRequest({
184 url: server.url,
185 path,
186 statusCodeExpected: 200,
187 query: {
188 state: 'accepted'
189 }
190 })
191 })
192 })
193
194 describe('When removing a follower', function () {
195 const path = '/api/v1/server/followers'
196
197 it('Should fail with an invalid token', async function () {
198 await makeDeleteRequest({
199 url: server.url,
200 path: path + '/toto@localhost:9002',
201 token: 'fake_token',
202 statusCodeExpected: 401
203 })
204 })
205
206 it('Should fail if the user is not an administrator', async function () {
207 await makeDeleteRequest({
208 url: server.url,
209 path: path + '/toto@localhost:9002',
210 token: userAccessToken,
211 statusCodeExpected: 403
212 })
213 })
214
215 it('Should fail with an invalid follower', async function () {
216 await makeDeleteRequest({
217 url: server.url,
218 path: path + '/toto',
219 token: server.accessToken,
220 statusCodeExpected: 400
221 })
222 })
223
224 it('Should fail with an unknown follower', async function () {
225 await makeDeleteRequest({
226 url: server.url,
227 path: path + '/toto@localhost:9003',
228 token: server.accessToken,
229 statusCodeExpected: 404
230 })
231 })
232 })
233
234 describe('When accepting a follower', function () {
235 const path = '/api/v1/server/followers'
236
237 it('Should fail with an invalid token', async function () {
238 await makePostBodyRequest({
239 url: server.url,
240 path: path + '/toto@localhost:9002/accept',
241 token: 'fake_token',
242 statusCodeExpected: 401
243 })
244 })
245
246 it('Should fail if the user is not an administrator', async function () {
247 await makePostBodyRequest({
248 url: server.url,
249 path: path + '/toto@localhost:9002/accept',
250 token: userAccessToken,
251 statusCodeExpected: 403
252 })
253 })
254
255 it('Should fail with an invalid follower', async function () {
256 await makePostBodyRequest({
257 url: server.url,
258 path: path + '/toto/accept',
259 token: server.accessToken,
260 statusCodeExpected: 400
261 })
262 })
263
264 it('Should fail with an unknown follower', async function () {
265 await makePostBodyRequest({
266 url: server.url,
267 path: path + '/toto@localhost:9003/accept',
268 token: server.accessToken,
269 statusCodeExpected: 404
270 })
271 })
272 })
273
274 describe('When rejecting a follower', function () {
275 const path = '/api/v1/server/followers'
276
277 it('Should fail with an invalid token', async function () {
278 await makePostBodyRequest({
279 url: server.url,
280 path: path + '/toto@localhost:9002/reject',
281 token: 'fake_token',
282 statusCodeExpected: 401
283 })
284 })
285
286 it('Should fail if the user is not an administrator', async function () {
287 await makePostBodyRequest({
288 url: server.url,
289 path: path + '/toto@localhost:9002/reject',
290 token: userAccessToken,
291 statusCodeExpected: 403
292 })
293 })
294
295 it('Should fail with an invalid follower', async function () {
296 await makePostBodyRequest({
297 url: server.url,
298 path: path + '/toto/reject',
299 token: server.accessToken,
300 statusCodeExpected: 400
301 })
302 })
303
304 it('Should fail with an unknown follower', async function () {
305 await makePostBodyRequest({
306 url: server.url,
307 path: path + '/toto@localhost:9003/reject',
308 token: server.accessToken,
309 statusCodeExpected: 404
310 })
311 })
312 })
313
314 describe('When removing following', function () {
315 const path = '/api/v1/server/following'
316
317 it('Should fail with an invalid token', async function () {
318 await makeDeleteRequest({
319 url: server.url,
320 path: path + '/localhost:9002',
321 token: 'fake_token',
322 statusCodeExpected: 401
323 })
324 })
325
326 it('Should fail if the user is not an administrator', async function () {
327 await makeDeleteRequest({
328 url: server.url,
329 path: path + '/localhost:9002',
330 token: userAccessToken,
331 statusCodeExpected: 403
332 })
333 })
334
335 it('Should fail if we do not follow this server', async function () {
336 await makeDeleteRequest({
337 url: server.url,
338 path: path + '/example.com',
339 token: server.accessToken,
340 statusCodeExpected: 404
341 })
342 })
343 })
344 })
345
346 after(async function () {
347 await cleanupTests([ server ])
348 })
349 })