1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
8 makeDeleteRequest, makeGetRequest,
11 setAccessTokensToServers
12 } from '../../../../shared/extra-utils'
14 checkBadCountPagination,
15 checkBadSortPagination,
16 checkBadStartPagination
17 } from '../../../../shared/extra-utils/requests/check-api-params'
18 import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
20 describe('Test server follows API validators', function () {
21 let server: PeerTubeServer
23 // ---------------------------------------------------------------
25 before(async function () {
28 server = await createSingleServer(1)
30 await setAccessTokensToServers([ server ])
33 describe('When managing following', function () {
34 let userAccessToken = null
36 before(async function () {
42 await server.users.create({ username: user.username, password: user.password })
43 userAccessToken = await server.login.getAccessToken(user)
46 describe('When adding follows', function () {
47 const path = '/api/v1/server/following'
49 it('Should fail without hosts', async function () {
50 await makePostBodyRequest({
53 token: server.accessToken,
54 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400
58 it('Should fail if hosts is not an array', async function () {
59 await makePostBodyRequest({
62 token: server.accessToken,
63 fields: { hosts: 'localhost:9002' },
64 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400
68 it('Should fail if the array is not composed by hosts', async function () {
69 await makePostBodyRequest({
72 fields: { hosts: [ 'localhost:9002', 'localhost:coucou' ] },
73 token: server.accessToken,
74 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400
78 it('Should fail if the array is composed with http schemes', async function () {
79 await makePostBodyRequest({
82 fields: { hosts: [ 'localhost:9002', 'http://localhost:9003' ] },
83 token: server.accessToken,
84 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400
88 it('Should fail if hosts are not unique', async function () {
89 await makePostBodyRequest({
92 fields: { urls: [ 'localhost:9002', 'localhost:9002' ] },
93 token: server.accessToken,
94 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400
98 it('Should fail with an invalid token', async function () {
99 await makePostBodyRequest({
102 fields: { hosts: [ 'localhost:9002' ] },
104 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401
108 it('Should fail if the user is not an administrator', async function () {
109 await makePostBodyRequest({
112 fields: { hosts: [ 'localhost:9002' ] },
113 token: userAccessToken,
114 statusCodeExpected: HttpStatusCode.FORBIDDEN_403
119 describe('When listing followings', function () {
120 const path = '/api/v1/server/following'
122 it('Should fail with a bad start pagination', async function () {
123 await checkBadStartPagination(server.url, path)
126 it('Should fail with a bad count pagination', async function () {
127 await checkBadCountPagination(server.url, path)
130 it('Should fail with an incorrect sort', async function () {
131 await checkBadSortPagination(server.url, path)
134 it('Should fail with an incorrect state', async function () {
135 await makeGetRequest({
144 it('Should fail with an incorrect actor type', async function () {
145 await makeGetRequest({
154 it('Should fail succeed with the correct params', async function () {
155 await makeGetRequest({
158 statusCodeExpected: HttpStatusCode.OK_200,
161 actorType: 'Application'
167 describe('When listing followers', function () {
168 const path = '/api/v1/server/followers'
170 it('Should fail with a bad start pagination', async function () {
171 await checkBadStartPagination(server.url, path)
174 it('Should fail with a bad count pagination', async function () {
175 await checkBadCountPagination(server.url, path)
178 it('Should fail with an incorrect sort', async function () {
179 await checkBadSortPagination(server.url, path)
182 it('Should fail with an incorrect actor type', async function () {
183 await makeGetRequest({
192 it('Should fail with an incorrect state', async function () {
193 await makeGetRequest({
198 actorType: 'Application'
203 it('Should fail succeed with the correct params', async function () {
204 await makeGetRequest({
207 statusCodeExpected: HttpStatusCode.OK_200,
215 describe('When removing a follower', function () {
216 const path = '/api/v1/server/followers'
218 it('Should fail with an invalid token', async function () {
219 await makeDeleteRequest({
221 path: path + '/toto@localhost:9002',
223 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401
227 it('Should fail if the user is not an administrator', async function () {
228 await makeDeleteRequest({
230 path: path + '/toto@localhost:9002',
231 token: userAccessToken,
232 statusCodeExpected: HttpStatusCode.FORBIDDEN_403
236 it('Should fail with an invalid follower', async function () {
237 await makeDeleteRequest({
239 path: path + '/toto',
240 token: server.accessToken,
241 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400
245 it('Should fail with an unknown follower', async function () {
246 await makeDeleteRequest({
248 path: path + '/toto@localhost:9003',
249 token: server.accessToken,
250 statusCodeExpected: HttpStatusCode.NOT_FOUND_404
255 describe('When accepting a follower', function () {
256 const path = '/api/v1/server/followers'
258 it('Should fail with an invalid token', async function () {
259 await makePostBodyRequest({
261 path: path + '/toto@localhost:9002/accept',
263 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401
267 it('Should fail if the user is not an administrator', async function () {
268 await makePostBodyRequest({
270 path: path + '/toto@localhost:9002/accept',
271 token: userAccessToken,
272 statusCodeExpected: HttpStatusCode.FORBIDDEN_403
276 it('Should fail with an invalid follower', async function () {
277 await makePostBodyRequest({
279 path: path + '/toto/accept',
280 token: server.accessToken,
281 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400
285 it('Should fail with an unknown follower', async function () {
286 await makePostBodyRequest({
288 path: path + '/toto@localhost:9003/accept',
289 token: server.accessToken,
290 statusCodeExpected: HttpStatusCode.NOT_FOUND_404
295 describe('When rejecting a follower', function () {
296 const path = '/api/v1/server/followers'
298 it('Should fail with an invalid token', async function () {
299 await makePostBodyRequest({
301 path: path + '/toto@localhost:9002/reject',
303 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401
307 it('Should fail if the user is not an administrator', async function () {
308 await makePostBodyRequest({
310 path: path + '/toto@localhost:9002/reject',
311 token: userAccessToken,
312 statusCodeExpected: HttpStatusCode.FORBIDDEN_403
316 it('Should fail with an invalid follower', async function () {
317 await makePostBodyRequest({
319 path: path + '/toto/reject',
320 token: server.accessToken,
321 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400
325 it('Should fail with an unknown follower', async function () {
326 await makePostBodyRequest({
328 path: path + '/toto@localhost:9003/reject',
329 token: server.accessToken,
330 statusCodeExpected: HttpStatusCode.NOT_FOUND_404
335 describe('When removing following', function () {
336 const path = '/api/v1/server/following'
338 it('Should fail with an invalid token', async function () {
339 await makeDeleteRequest({
341 path: path + '/localhost:9002',
343 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401
347 it('Should fail if the user is not an administrator', async function () {
348 await makeDeleteRequest({
350 path: path + '/localhost:9002',
351 token: userAccessToken,
352 statusCodeExpected: HttpStatusCode.FORBIDDEN_403
356 it('Should fail if we do not follow this server', async function () {
357 await makeDeleteRequest({
359 path: path + '/example.com',
360 token: server.accessToken,
361 statusCodeExpected: HttpStatusCode.NOT_FOUND_404
367 after(async function () {
368 await cleanupTests([ server ])