Playlist server API
[oweals/peertube.git] / server / tests / api / check-params / user-subscriptions.ts
1 /* tslint:disable:no-unused-expression */
2
3 import 'mocha'
4
5 import {
6   createUser,
7   flushTests,
8   killallServers,
9   makeDeleteRequest,
10   makeGetRequest,
11   makePostBodyRequest,
12   runServer,
13   ServerInfo,
14   setAccessTokensToServers,
15   userLogin
16 } from '../../../../shared/utils'
17
18 import {
19   checkBadCountPagination,
20   checkBadSortPagination,
21   checkBadStartPagination
22 } from '../../../../shared/utils/requests/check-api-params'
23 import { waitJobs } from '../../../../shared/utils/server/jobs'
24
25 describe('Test user subscriptions API validators', function () {
26   const path = '/api/v1/users/me/subscriptions'
27   let server: ServerInfo
28   let userAccessToken = ''
29
30   // ---------------------------------------------------------------
31
32   before(async function () {
33     this.timeout(30000)
34
35     await flushTests()
36
37     server = await runServer(1)
38
39     await setAccessTokensToServers([ server ])
40
41     const user = {
42       username: 'user1',
43       password: 'my super password'
44     }
45     await createUser(server.url, server.accessToken, user.username, user.password)
46     userAccessToken = await userLogin(server, user)
47   })
48
49   describe('When listing my subscriptions', function () {
50     it('Should fail with a bad start pagination', async function () {
51       await checkBadStartPagination(server.url, path, server.accessToken)
52     })
53
54     it('Should fail with a bad count pagination', async function () {
55       await checkBadCountPagination(server.url, path, server.accessToken)
56     })
57
58     it('Should fail with an incorrect sort', async function () {
59       await checkBadSortPagination(server.url, path, server.accessToken)
60     })
61
62     it('Should fail with a non authenticated user', async function () {
63       await makeGetRequest({
64         url: server.url,
65         path,
66         statusCodeExpected: 401
67       })
68     })
69
70     it('Should succeed with the correct parameters', async function () {
71       await makeGetRequest({
72         url: server.url,
73         path,
74         token: userAccessToken,
75         statusCodeExpected: 200
76       })
77     })
78   })
79
80   describe('When listing my subscriptions videos', function () {
81     const path = '/api/v1/users/me/subscriptions/videos'
82
83     it('Should fail with a bad start pagination', async function () {
84       await checkBadStartPagination(server.url, path, server.accessToken)
85     })
86
87     it('Should fail with a bad count pagination', async function () {
88       await checkBadCountPagination(server.url, path, server.accessToken)
89     })
90
91     it('Should fail with an incorrect sort', async function () {
92       await checkBadSortPagination(server.url, path, server.accessToken)
93     })
94
95     it('Should fail with a non authenticated user', async function () {
96       await makeGetRequest({
97         url: server.url,
98         path,
99         statusCodeExpected: 401
100       })
101     })
102
103     it('Should succeed with the correct parameters', async function () {
104       await makeGetRequest({
105         url: server.url,
106         path,
107         token: userAccessToken,
108         statusCodeExpected: 200
109       })
110     })
111   })
112
113   describe('When adding a subscription', function () {
114     it('Should fail with a non authenticated user', async function () {
115       await makePostBodyRequest({
116         url: server.url,
117         path,
118         fields: { uri: 'user1_channel@localhost:9001' },
119         statusCodeExpected: 401
120       })
121     })
122
123     it('Should fail with bad URIs', async function () {
124       await makePostBodyRequest({
125         url: server.url,
126         path,
127         token: server.accessToken,
128         fields: { uri: 'root' },
129         statusCodeExpected: 400
130       })
131
132       await makePostBodyRequest({
133         url: server.url,
134         path,
135         token: server.accessToken,
136         fields: { uri: 'root@' },
137         statusCodeExpected: 400
138       })
139
140       await makePostBodyRequest({
141         url: server.url,
142         path,
143         token: server.accessToken,
144         fields: { uri: 'root@hello@' },
145         statusCodeExpected: 400
146       })
147     })
148
149     it('Should succeed with the correct parameters', async function () {
150       this.timeout(20000)
151
152       await makePostBodyRequest({
153         url: server.url,
154         path,
155         token: server.accessToken,
156         fields: { uri: 'user1_channel@localhost:9001' },
157         statusCodeExpected: 204
158       })
159
160       await waitJobs([ server ])
161     })
162   })
163
164   describe('When getting a subscription', function () {
165     it('Should fail with a non authenticated user', async function () {
166       await makeGetRequest({
167         url: server.url,
168         path: path + '/user1_channel@localhost:9001',
169         statusCodeExpected: 401
170       })
171     })
172
173     it('Should fail with bad URIs', async function () {
174       await makeGetRequest({
175         url: server.url,
176         path: path + '/root',
177         token: server.accessToken,
178         statusCodeExpected: 400
179       })
180
181       await makeGetRequest({
182         url: server.url,
183         path: path + '/root@',
184         token: server.accessToken,
185         statusCodeExpected: 400
186       })
187
188       await makeGetRequest({
189         url: server.url,
190         path: path + '/root@hello@',
191         token: server.accessToken,
192         statusCodeExpected: 400
193       })
194     })
195
196     it('Should fail with an unknown subscription', async function () {
197       await makeGetRequest({
198         url: server.url,
199         path: path + '/root1@localhost:9001',
200         token: server.accessToken,
201         statusCodeExpected: 404
202       })
203     })
204
205     it('Should succeed with the correct parameters', async function () {
206       await makeGetRequest({
207         url: server.url,
208         path: path + '/user1_channel@localhost:9001',
209         token: server.accessToken,
210         statusCodeExpected: 200
211       })
212     })
213   })
214
215   describe('When checking if subscriptions exist', function () {
216     const existPath = path + '/exist'
217
218     it('Should fail with a non authenticated user', async function () {
219       await makeGetRequest({
220         url: server.url,
221         path: existPath,
222         statusCodeExpected: 401
223       })
224     })
225
226     it('Should fail with bad URIs', async function () {
227       await makeGetRequest({
228         url: server.url,
229         path: existPath,
230         query: { uris: 'toto' },
231         token: server.accessToken,
232         statusCodeExpected: 400
233       })
234
235       await makeGetRequest({
236         url: server.url,
237         path: existPath,
238         query: { 'uris[]': 1 },
239         token: server.accessToken,
240         statusCodeExpected: 400
241       })
242     })
243
244     it('Should succeed with the correct parameters', async function () {
245       await makeGetRequest({
246         url: server.url,
247         path: existPath,
248         query: { 'uris[]': 'coucou@localhost:9001' },
249         token: server.accessToken,
250         statusCodeExpected: 200
251       })
252     })
253   })
254
255   describe('When removing a subscription', function () {
256     it('Should fail with a non authenticated user', async function () {
257       await makeDeleteRequest({
258         url: server.url,
259         path: path + '/user1_channel@localhost:9001',
260         statusCodeExpected: 401
261       })
262     })
263
264     it('Should fail with bad URIs', async function () {
265       await makeDeleteRequest({
266         url: server.url,
267         path: path + '/root',
268         token: server.accessToken,
269         statusCodeExpected: 400
270       })
271
272       await makeDeleteRequest({
273         url: server.url,
274         path: path + '/root@',
275         token: server.accessToken,
276         statusCodeExpected: 400
277       })
278
279       await makeDeleteRequest({
280         url: server.url,
281         path: path + '/root@hello@',
282         token: server.accessToken,
283         statusCodeExpected: 400
284       })
285     })
286
287     it('Should fail with an unknown subscription', async function () {
288       await makeDeleteRequest({
289         url: server.url,
290         path: path + '/root1@localhost:9001',
291         token: server.accessToken,
292         statusCodeExpected: 404
293       })
294     })
295
296     it('Should succeed with the correct parameters', async function () {
297       await makeDeleteRequest({
298         url: server.url,
299         path: path + '/user1_channel@localhost:9001',
300         token: server.accessToken,
301         statusCodeExpected: 204
302       })
303     })
304   })
305
306   after(async function () {
307     killallServers([ server ])
308
309     // Keep the logs if the test failed
310     if (this['ok']) {
311       await flushTests()
312     }
313   })
314 })