1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import { checkBadCountPagination, checkBadSortPagination, checkBadStartPagination } from '@server/tests/shared'
5 import { HttpStatusCode } from '@shared/models'
13 setAccessTokensToServers
14 } from '@shared/server-commands'
16 describe('Test server follows API validators', function () {
17 let server: PeerTubeServer
19 // ---------------------------------------------------------------
21 before(async function () {
24 server = await createSingleServer(1)
26 await setAccessTokensToServers([ server ])
29 describe('When managing following', function () {
30 let userAccessToken = null
32 before(async function () {
33 userAccessToken = await server.users.generateUserAndToken('user1')
36 describe('When adding follows', function () {
37 const path = '/api/v1/server/following'
39 it('Should fail with nothing', async function () {
40 await makePostBodyRequest({
43 token: server.accessToken,
44 expectedStatus: HttpStatusCode.BAD_REQUEST_400
48 it('Should fail if hosts is not composed by hosts', async function () {
49 await makePostBodyRequest({
52 fields: { hosts: [ 'localhost:9002', 'localhost:coucou' ] },
53 token: server.accessToken,
54 expectedStatus: HttpStatusCode.BAD_REQUEST_400
58 it('Should fail if hosts is composed with http schemes', async function () {
59 await makePostBodyRequest({
62 fields: { hosts: [ 'localhost:9002', 'http://localhost:9003' ] },
63 token: server.accessToken,
64 expectedStatus: HttpStatusCode.BAD_REQUEST_400
68 it('Should fail if hosts are not unique', async function () {
69 await makePostBodyRequest({
72 fields: { urls: [ 'localhost:9002', 'localhost:9002' ] },
73 token: server.accessToken,
74 expectedStatus: HttpStatusCode.BAD_REQUEST_400
78 it('Should fail if handles is not composed by handles', async function () {
79 await makePostBodyRequest({
82 fields: { handles: [ 'hello@example.com', 'localhost:9001' ] },
83 token: server.accessToken,
84 expectedStatus: HttpStatusCode.BAD_REQUEST_400
88 it('Should fail if handles are not unique', async function () {
89 await makePostBodyRequest({
92 fields: { urls: [ 'hello@example.com', 'hello@example.com' ] },
93 token: server.accessToken,
94 expectedStatus: HttpStatusCode.BAD_REQUEST_400
98 it('Should fail with an invalid token', async function () {
99 await makePostBodyRequest({
102 fields: { hosts: [ 'localhost:9002' ] },
104 expectedStatus: 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 expectedStatus: 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 expectedStatus: 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 expectedStatus: 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 expectedStatus: 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 expectedStatus: 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 expectedStatus: 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 expectedStatus: 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 expectedStatus: 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 expectedStatus: 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 expectedStatus: 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 expectedStatus: 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 expectedStatus: 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 expectedStatus: 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 expectedStatus: 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 expectedStatus: 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 expectedStatus: 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 expectedStatus: 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 expectedStatus: HttpStatusCode.NOT_FOUND_404
367 after(async function () {
368 await cleanupTests([ server ])