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