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