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