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 () {
35 userAccessToken = await server.users.generateUserAndToken('user1')
38 describe('When adding follows', function () {
39 const path = '/api/v1/server/following'
41 it('Should fail with nothing', async function () {
42 await makePostBodyRequest({
45 token: server.accessToken,
46 expectedStatus: HttpStatusCode.BAD_REQUEST_400
50 it('Should fail if hosts is not composed by hosts', async function () {
51 await makePostBodyRequest({
54 fields: { hosts: [ 'localhost:9002', 'localhost:coucou' ] },
55 token: server.accessToken,
56 expectedStatus: HttpStatusCode.BAD_REQUEST_400
60 it('Should fail if hosts is composed with http schemes', async function () {
61 await makePostBodyRequest({
64 fields: { hosts: [ 'localhost:9002', 'http://localhost:9003' ] },
65 token: server.accessToken,
66 expectedStatus: HttpStatusCode.BAD_REQUEST_400
70 it('Should fail if hosts are not unique', async function () {
71 await makePostBodyRequest({
74 fields: { urls: [ 'localhost:9002', 'localhost:9002' ] },
75 token: server.accessToken,
76 expectedStatus: HttpStatusCode.BAD_REQUEST_400
80 it('Should fail if handles is not composed by handles', async function () {
81 await makePostBodyRequest({
84 fields: { handles: [ 'hello@example.com', 'localhost:9001' ] },
85 token: server.accessToken,
86 expectedStatus: HttpStatusCode.BAD_REQUEST_400
90 it('Should fail if handles are not unique', async function () {
91 await makePostBodyRequest({
94 fields: { urls: [ 'hello@example.com', 'hello@example.com' ] },
95 token: server.accessToken,
96 expectedStatus: HttpStatusCode.BAD_REQUEST_400
100 it('Should fail with an invalid token', async function () {
101 await makePostBodyRequest({
104 fields: { hosts: [ 'localhost:9002' ] },
106 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
110 it('Should fail if the user is not an administrator', async function () {
111 await makePostBodyRequest({
114 fields: { hosts: [ 'localhost:9002' ] },
115 token: userAccessToken,
116 expectedStatus: HttpStatusCode.FORBIDDEN_403
121 describe('When listing followings', function () {
122 const path = '/api/v1/server/following'
124 it('Should fail with a bad start pagination', async function () {
125 await checkBadStartPagination(server.url, path)
128 it('Should fail with a bad count pagination', async function () {
129 await checkBadCountPagination(server.url, path)
132 it('Should fail with an incorrect sort', async function () {
133 await checkBadSortPagination(server.url, path)
136 it('Should fail with an incorrect state', async function () {
137 await makeGetRequest({
146 it('Should fail with an incorrect actor type', async function () {
147 await makeGetRequest({
156 it('Should fail succeed with the correct params', async function () {
157 await makeGetRequest({
160 expectedStatus: HttpStatusCode.OK_200,
163 actorType: 'Application'
169 describe('When listing followers', function () {
170 const path = '/api/v1/server/followers'
172 it('Should fail with a bad start pagination', async function () {
173 await checkBadStartPagination(server.url, path)
176 it('Should fail with a bad count pagination', async function () {
177 await checkBadCountPagination(server.url, path)
180 it('Should fail with an incorrect sort', async function () {
181 await checkBadSortPagination(server.url, path)
184 it('Should fail with an incorrect actor type', async function () {
185 await makeGetRequest({
194 it('Should fail with an incorrect state', async function () {
195 await makeGetRequest({
200 actorType: 'Application'
205 it('Should fail succeed with the correct params', async function () {
206 await makeGetRequest({
209 expectedStatus: HttpStatusCode.OK_200,
217 describe('When removing a follower', function () {
218 const path = '/api/v1/server/followers'
220 it('Should fail with an invalid token', async function () {
221 await makeDeleteRequest({
223 path: path + '/toto@localhost:9002',
225 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
229 it('Should fail if the user is not an administrator', async function () {
230 await makeDeleteRequest({
232 path: path + '/toto@localhost:9002',
233 token: userAccessToken,
234 expectedStatus: HttpStatusCode.FORBIDDEN_403
238 it('Should fail with an invalid follower', async function () {
239 await makeDeleteRequest({
241 path: path + '/toto',
242 token: server.accessToken,
243 expectedStatus: HttpStatusCode.BAD_REQUEST_400
247 it('Should fail with an unknown follower', async function () {
248 await makeDeleteRequest({
250 path: path + '/toto@localhost:9003',
251 token: server.accessToken,
252 expectedStatus: HttpStatusCode.NOT_FOUND_404
257 describe('When accepting a follower', function () {
258 const path = '/api/v1/server/followers'
260 it('Should fail with an invalid token', async function () {
261 await makePostBodyRequest({
263 path: path + '/toto@localhost:9002/accept',
265 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
269 it('Should fail if the user is not an administrator', async function () {
270 await makePostBodyRequest({
272 path: path + '/toto@localhost:9002/accept',
273 token: userAccessToken,
274 expectedStatus: HttpStatusCode.FORBIDDEN_403
278 it('Should fail with an invalid follower', async function () {
279 await makePostBodyRequest({
281 path: path + '/toto/accept',
282 token: server.accessToken,
283 expectedStatus: HttpStatusCode.BAD_REQUEST_400
287 it('Should fail with an unknown follower', async function () {
288 await makePostBodyRequest({
290 path: path + '/toto@localhost:9003/accept',
291 token: server.accessToken,
292 expectedStatus: HttpStatusCode.NOT_FOUND_404
297 describe('When rejecting a follower', function () {
298 const path = '/api/v1/server/followers'
300 it('Should fail with an invalid token', async function () {
301 await makePostBodyRequest({
303 path: path + '/toto@localhost:9002/reject',
305 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
309 it('Should fail if the user is not an administrator', async function () {
310 await makePostBodyRequest({
312 path: path + '/toto@localhost:9002/reject',
313 token: userAccessToken,
314 expectedStatus: HttpStatusCode.FORBIDDEN_403
318 it('Should fail with an invalid follower', async function () {
319 await makePostBodyRequest({
321 path: path + '/toto/reject',
322 token: server.accessToken,
323 expectedStatus: HttpStatusCode.BAD_REQUEST_400
327 it('Should fail with an unknown follower', async function () {
328 await makePostBodyRequest({
330 path: path + '/toto@localhost:9003/reject',
331 token: server.accessToken,
332 expectedStatus: HttpStatusCode.NOT_FOUND_404
337 describe('When removing following', function () {
338 const path = '/api/v1/server/following'
340 it('Should fail with an invalid token', async function () {
341 await makeDeleteRequest({
343 path: path + '/localhost:9002',
345 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
349 it('Should fail if the user is not an administrator', async function () {
350 await makeDeleteRequest({
352 path: path + '/localhost:9002',
353 token: userAccessToken,
354 expectedStatus: HttpStatusCode.FORBIDDEN_403
358 it('Should fail if we do not follow this server', async function () {
359 await makeDeleteRequest({
361 path: path + '/example.com',
362 token: server.accessToken,
363 expectedStatus: HttpStatusCode.NOT_FOUND_404
369 after(async function () {
370 await cleanupTests([ server ])