1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
5 checkBadCountPagination,
6 checkBadSortPagination,
7 checkBadStartPagination,
14 setAccessTokensToServers
15 } from '@shared/extra-utils'
16 import { HttpStatusCode } from '@shared/models'
18 describe('Test server follows API validators', function () {
19 let server: PeerTubeServer
21 // ---------------------------------------------------------------
23 before(async function () {
26 server = await createSingleServer(1)
28 await setAccessTokensToServers([ server ])
31 describe('When managing following', function () {
32 let userAccessToken = null
34 before(async function () {
40 await server.users.create({ username: user.username, password: user.password })
41 userAccessToken = await server.login.getAccessToken(user)
44 describe('When adding follows', function () {
45 const path = '/api/v1/server/following'
47 it('Should fail without hosts', async function () {
48 await makePostBodyRequest({
51 token: server.accessToken,
52 expectedStatus: HttpStatusCode.BAD_REQUEST_400
56 it('Should fail if hosts is not an array', async function () {
57 await makePostBodyRequest({
60 token: server.accessToken,
61 fields: { hosts: 'localhost:9002' },
62 expectedStatus: HttpStatusCode.BAD_REQUEST_400
66 it('Should fail if the array is not composed by hosts', async function () {
67 await makePostBodyRequest({
70 fields: { hosts: [ 'localhost:9002', 'localhost:coucou' ] },
71 token: server.accessToken,
72 expectedStatus: HttpStatusCode.BAD_REQUEST_400
76 it('Should fail if the array is composed with http schemes', async function () {
77 await makePostBodyRequest({
80 fields: { hosts: [ 'localhost:9002', 'http://localhost:9003' ] },
81 token: server.accessToken,
82 expectedStatus: HttpStatusCode.BAD_REQUEST_400
86 it('Should fail if hosts are not unique', async function () {
87 await makePostBodyRequest({
90 fields: { urls: [ 'localhost:9002', 'localhost:9002' ] },
91 token: server.accessToken,
92 expectedStatus: HttpStatusCode.BAD_REQUEST_400
96 it('Should fail with an invalid token', async function () {
97 await makePostBodyRequest({
100 fields: { hosts: [ 'localhost:9002' ] },
102 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
106 it('Should fail if the user is not an administrator', async function () {
107 await makePostBodyRequest({
110 fields: { hosts: [ 'localhost:9002' ] },
111 token: userAccessToken,
112 expectedStatus: HttpStatusCode.FORBIDDEN_403
117 describe('When listing followings', function () {
118 const path = '/api/v1/server/following'
120 it('Should fail with a bad start pagination', async function () {
121 await checkBadStartPagination(server.url, path)
124 it('Should fail with a bad count pagination', async function () {
125 await checkBadCountPagination(server.url, path)
128 it('Should fail with an incorrect sort', async function () {
129 await checkBadSortPagination(server.url, path)
132 it('Should fail with an incorrect state', async function () {
133 await makeGetRequest({
142 it('Should fail with an incorrect actor type', async function () {
143 await makeGetRequest({
152 it('Should fail succeed with the correct params', async function () {
153 await makeGetRequest({
156 expectedStatus: HttpStatusCode.OK_200,
159 actorType: 'Application'
165 describe('When listing followers', function () {
166 const path = '/api/v1/server/followers'
168 it('Should fail with a bad start pagination', async function () {
169 await checkBadStartPagination(server.url, path)
172 it('Should fail with a bad count pagination', async function () {
173 await checkBadCountPagination(server.url, path)
176 it('Should fail with an incorrect sort', async function () {
177 await checkBadSortPagination(server.url, path)
180 it('Should fail with an incorrect actor type', async function () {
181 await makeGetRequest({
190 it('Should fail with an incorrect state', async function () {
191 await makeGetRequest({
196 actorType: 'Application'
201 it('Should fail succeed with the correct params', async function () {
202 await makeGetRequest({
205 expectedStatus: HttpStatusCode.OK_200,
213 describe('When removing a follower', function () {
214 const path = '/api/v1/server/followers'
216 it('Should fail with an invalid token', async function () {
217 await makeDeleteRequest({
219 path: path + '/toto@localhost:9002',
221 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
225 it('Should fail if the user is not an administrator', async function () {
226 await makeDeleteRequest({
228 path: path + '/toto@localhost:9002',
229 token: userAccessToken,
230 expectedStatus: HttpStatusCode.FORBIDDEN_403
234 it('Should fail with an invalid follower', async function () {
235 await makeDeleteRequest({
237 path: path + '/toto',
238 token: server.accessToken,
239 expectedStatus: HttpStatusCode.BAD_REQUEST_400
243 it('Should fail with an unknown follower', async function () {
244 await makeDeleteRequest({
246 path: path + '/toto@localhost:9003',
247 token: server.accessToken,
248 expectedStatus: HttpStatusCode.NOT_FOUND_404
253 describe('When accepting a follower', function () {
254 const path = '/api/v1/server/followers'
256 it('Should fail with an invalid token', async function () {
257 await makePostBodyRequest({
259 path: path + '/toto@localhost:9002/accept',
261 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
265 it('Should fail if the user is not an administrator', async function () {
266 await makePostBodyRequest({
268 path: path + '/toto@localhost:9002/accept',
269 token: userAccessToken,
270 expectedStatus: HttpStatusCode.FORBIDDEN_403
274 it('Should fail with an invalid follower', async function () {
275 await makePostBodyRequest({
277 path: path + '/toto/accept',
278 token: server.accessToken,
279 expectedStatus: HttpStatusCode.BAD_REQUEST_400
283 it('Should fail with an unknown follower', async function () {
284 await makePostBodyRequest({
286 path: path + '/toto@localhost:9003/accept',
287 token: server.accessToken,
288 expectedStatus: HttpStatusCode.NOT_FOUND_404
293 describe('When rejecting a follower', function () {
294 const path = '/api/v1/server/followers'
296 it('Should fail with an invalid token', async function () {
297 await makePostBodyRequest({
299 path: path + '/toto@localhost:9002/reject',
301 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
305 it('Should fail if the user is not an administrator', async function () {
306 await makePostBodyRequest({
308 path: path + '/toto@localhost:9002/reject',
309 token: userAccessToken,
310 expectedStatus: HttpStatusCode.FORBIDDEN_403
314 it('Should fail with an invalid follower', async function () {
315 await makePostBodyRequest({
317 path: path + '/toto/reject',
318 token: server.accessToken,
319 expectedStatus: HttpStatusCode.BAD_REQUEST_400
323 it('Should fail with an unknown follower', async function () {
324 await makePostBodyRequest({
326 path: path + '/toto@localhost:9003/reject',
327 token: server.accessToken,
328 expectedStatus: HttpStatusCode.NOT_FOUND_404
333 describe('When removing following', function () {
334 const path = '/api/v1/server/following'
336 it('Should fail with an invalid token', async function () {
337 await makeDeleteRequest({
339 path: path + '/localhost:9002',
341 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
345 it('Should fail if the user is not an administrator', async function () {
346 await makeDeleteRequest({
348 path: path + '/localhost:9002',
349 token: userAccessToken,
350 expectedStatus: HttpStatusCode.FORBIDDEN_403
354 it('Should fail if we do not follow this server', async function () {
355 await makeDeleteRequest({
357 path: path + '/example.com',
358 token: server.accessToken,
359 expectedStatus: HttpStatusCode.NOT_FOUND_404
365 after(async function () {
366 await cleanupTests([ server ])