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