]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/check-params/follows.ts
Cleanup tests
[github/Chocobozzz/PeerTube.git] / server / tests / api / check-params / follows.ts
1 /* tslint:disable:no-unused-expression */
2
3 import 'mocha'
4
5 import {
6 createUser, flushTests, killallServers, makeDeleteRequest, makePostBodyRequest, flushAndRunServer, ServerInfo, setAccessTokensToServers,
7 userLogin
8 } from '../../../../shared/extra-utils'
9 import {
10 checkBadCountPagination,
11 checkBadSortPagination,
12 checkBadStartPagination
13 } from '../../../../shared/extra-utils/requests/check-api-params'
14
15 describe('Test server follows API validators', function () {
16 let server: ServerInfo
17
18 // ---------------------------------------------------------------
19
20 before(async function () {
21 this.timeout(30000)
22
23 server = await flushAndRunServer(1)
24
25 await setAccessTokensToServers([ server ])
26 })
27
28 describe('When managing following', function () {
29 let userAccessToken = null
30
31 before(async function () {
32 const user = {
33 username: 'user1',
34 password: 'password'
35 }
36
37 await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
38 userAccessToken = await userLogin(server, user)
39 })
40
41 describe('When adding follows', function () {
42 const path = '/api/v1/server/following'
43
44 it('Should fail without hosts', async function () {
45 await makePostBodyRequest({
46 url: server.url,
47 path,
48 token: server.accessToken,
49 statusCodeExpected: 400
50 })
51 })
52
53 it('Should fail if hosts is not an array', async function () {
54 await makePostBodyRequest({
55 url: server.url,
56 path,
57 token: server.accessToken,
58 fields: { hosts: 'localhost:9002' },
59 statusCodeExpected: 400
60 })
61 })
62
63 it('Should fail if the array is not composed by hosts', async function () {
64 await makePostBodyRequest({
65 url: server.url,
66 path,
67 fields: { hosts: [ 'localhost:9002', 'localhost:coucou' ] },
68 token: server.accessToken,
69 statusCodeExpected: 400
70 })
71 })
72
73 it('Should fail if the array is composed with http schemes', async function () {
74 await makePostBodyRequest({
75 url: server.url,
76 path,
77 fields: { hosts: [ 'localhost:9002', 'http://localhost:9003' ] },
78 token: server.accessToken,
79 statusCodeExpected: 400
80 })
81 })
82
83 it('Should fail if hosts are not unique', async function () {
84 await makePostBodyRequest({
85 url: server.url,
86 path,
87 fields: { urls: [ 'localhost:9002', 'localhost:9002' ] },
88 token: server.accessToken,
89 statusCodeExpected: 400
90 })
91 })
92
93 it('Should fail with an invalid token', async function () {
94 await makePostBodyRequest({
95 url: server.url,
96 path,
97 fields: { hosts: [ 'localhost:9002' ] },
98 token: 'fake_token',
99 statusCodeExpected: 401
100 })
101 })
102
103 it('Should fail if the user is not an administrator', async function () {
104 await makePostBodyRequest({
105 url: server.url,
106 path,
107 fields: { hosts: [ 'localhost:9002' ] },
108 token: userAccessToken,
109 statusCodeExpected: 403
110 })
111 })
112 })
113
114 describe('When listing followings', function () {
115 const path = '/api/v1/server/following'
116
117 it('Should fail with a bad start pagination', async function () {
118 await checkBadStartPagination(server.url, path)
119 })
120
121 it('Should fail with a bad count pagination', async function () {
122 await checkBadCountPagination(server.url, path)
123 })
124
125 it('Should fail with an incorrect sort', async function () {
126 await checkBadSortPagination(server.url, path)
127 })
128 })
129
130 describe('When listing followers', function () {
131 const path = '/api/v1/server/followers'
132
133 it('Should fail with a bad start pagination', async function () {
134 await checkBadStartPagination(server.url, path)
135 })
136
137 it('Should fail with a bad count pagination', async function () {
138 await checkBadCountPagination(server.url, path)
139 })
140
141 it('Should fail with an incorrect sort', async function () {
142 await checkBadSortPagination(server.url, path)
143 })
144 })
145
146 describe('When removing a follower', function () {
147 const path = '/api/v1/server/followers'
148
149 it('Should fail with an invalid token', async function () {
150 await makeDeleteRequest({
151 url: server.url,
152 path: path + '/toto@localhost:9002',
153 token: 'fake_token',
154 statusCodeExpected: 401
155 })
156 })
157
158 it('Should fail if the user is not an administrator', async function () {
159 await makeDeleteRequest({
160 url: server.url,
161 path: path + '/toto@localhost:9002',
162 token: userAccessToken,
163 statusCodeExpected: 403
164 })
165 })
166
167 it('Should fail with an invalid follower', async function () {
168 await makeDeleteRequest({
169 url: server.url,
170 path: path + '/toto',
171 token: server.accessToken,
172 statusCodeExpected: 400
173 })
174 })
175
176 it('Should fail with an unknown follower', async function () {
177 await makeDeleteRequest({
178 url: server.url,
179 path: path + '/toto@localhost:9003',
180 token: server.accessToken,
181 statusCodeExpected: 404
182 })
183 })
184 })
185
186 describe('When accepting a follower', function () {
187 const path = '/api/v1/server/followers'
188
189 it('Should fail with an invalid token', async function () {
190 await makePostBodyRequest({
191 url: server.url,
192 path: path + '/toto@localhost:9002/accept',
193 token: 'fake_token',
194 statusCodeExpected: 401
195 })
196 })
197
198 it('Should fail if the user is not an administrator', async function () {
199 await makePostBodyRequest({
200 url: server.url,
201 path: path + '/toto@localhost:9002/accept',
202 token: userAccessToken,
203 statusCodeExpected: 403
204 })
205 })
206
207 it('Should fail with an invalid follower', async function () {
208 await makePostBodyRequest({
209 url: server.url,
210 path: path + '/toto/accept',
211 token: server.accessToken,
212 statusCodeExpected: 400
213 })
214 })
215
216 it('Should fail with an unknown follower', async function () {
217 await makePostBodyRequest({
218 url: server.url,
219 path: path + '/toto@localhost:9003/accept',
220 token: server.accessToken,
221 statusCodeExpected: 404
222 })
223 })
224 })
225
226 describe('When rejecting a follower', function () {
227 const path = '/api/v1/server/followers'
228
229 it('Should fail with an invalid token', async function () {
230 await makePostBodyRequest({
231 url: server.url,
232 path: path + '/toto@localhost:9002/reject',
233 token: 'fake_token',
234 statusCodeExpected: 401
235 })
236 })
237
238 it('Should fail if the user is not an administrator', async function () {
239 await makePostBodyRequest({
240 url: server.url,
241 path: path + '/toto@localhost:9002/reject',
242 token: userAccessToken,
243 statusCodeExpected: 403
244 })
245 })
246
247 it('Should fail with an invalid follower', async function () {
248 await makePostBodyRequest({
249 url: server.url,
250 path: path + '/toto/reject',
251 token: server.accessToken,
252 statusCodeExpected: 400
253 })
254 })
255
256 it('Should fail with an unknown follower', async function () {
257 await makePostBodyRequest({
258 url: server.url,
259 path: path + '/toto@localhost:9003/reject',
260 token: server.accessToken,
261 statusCodeExpected: 404
262 })
263 })
264 })
265
266 describe('When removing following', function () {
267 const path = '/api/v1/server/following'
268
269 it('Should fail with an invalid token', async function () {
270 await makeDeleteRequest({
271 url: server.url,
272 path: path + '/localhost:9002',
273 token: 'fake_token',
274 statusCodeExpected: 401
275 })
276 })
277
278 it('Should fail if the user is not an administrator', async function () {
279 await makeDeleteRequest({
280 url: server.url,
281 path: path + '/localhost:9002',
282 token: userAccessToken,
283 statusCodeExpected: 403
284 })
285 })
286
287 it('Should fail if we do not follow this server', async function () {
288 await makeDeleteRequest({
289 url: server.url,
290 path: path + '/example.com',
291 token: server.accessToken,
292 statusCodeExpected: 404
293 })
294 })
295 })
296 })
297
298 after(function () {
299 killallServers([ server ])
300 })
301 })