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