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