1 import * as request from 'supertest'
2 import { ServerInfo } from './servers'
3 import { waitJobs } from './jobs'
4 import { makePostBodyRequest } from '../requests/requests'
5 import { ActivityPubActorType, FollowState } from '@shared/models'
6 import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
8 function getFollowersListPaginationAndSort (options: {
14 actorType?: ActivityPubActorType
17 const { url, start, count, sort, search, state, actorType } = options
18 const path = '/api/v1/server/followers'
32 .set('Accept', 'application/json')
33 .expect(HttpStatusCode.OK_200)
34 .expect('Content-Type', /json/)
37 function acceptFollower (url: string, token: string, follower: string, statusCodeExpected = HttpStatusCode.NO_CONTENT_204) {
38 const path = '/api/v1/server/followers/' + follower + '/accept'
40 return makePostBodyRequest({
48 function rejectFollower (url: string, token: string, follower: string, statusCodeExpected = HttpStatusCode.NO_CONTENT_204) {
49 const path = '/api/v1/server/followers/' + follower + '/reject'
51 return makePostBodyRequest({
59 function getFollowingListPaginationAndSort (options: {
65 actorType?: ActivityPubActorType
68 const { url, start, count, sort, search, state, actorType } = options
69 const path = '/api/v1/server/following'
83 .set('Accept', 'application/json')
84 .expect(HttpStatusCode.OK_200)
85 .expect('Content-Type', /json/)
88 function follow (follower: string, following: string[], accessToken: string, expectedStatus = HttpStatusCode.NO_CONTENT_204) {
89 const path = '/api/v1/server/following'
91 const followingHosts = following.map(f => f.replace(/^http:\/\//, ''))
92 return request(follower)
94 .set('Accept', 'application/json')
95 .set('Authorization', 'Bearer ' + accessToken)
96 .send({ hosts: followingHosts })
97 .expect(expectedStatus)
100 async function unfollow (url: string, accessToken: string, target: ServerInfo, expectedStatus = HttpStatusCode.NO_CONTENT_204) {
101 const path = '/api/v1/server/following/' + target.host
105 .set('Accept', 'application/json')
106 .set('Authorization', 'Bearer ' + accessToken)
107 .expect(expectedStatus)
110 function removeFollower (url: string, accessToken: string, follower: ServerInfo, expectedStatus = HttpStatusCode.NO_CONTENT_204) {
111 const path = '/api/v1/server/followers/peertube@' + follower.host
115 .set('Accept', 'application/json')
116 .set('Authorization', 'Bearer ' + accessToken)
117 .expect(expectedStatus)
120 async function doubleFollow (server1: ServerInfo, server2: ServerInfo) {
122 follow(server1.url, [ server2.url ], server1.accessToken),
123 follow(server2.url, [ server1.url ], server2.accessToken)
126 // Wait request propagation
127 await waitJobs([ server1, server2 ])
132 // ---------------------------------------------------------------------------
135 getFollowersListPaginationAndSort,
136 getFollowingListPaginationAndSort,