aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared/extra-utils/server/follows.ts
diff options
context:
space:
mode:
Diffstat (limited to 'shared/extra-utils/server/follows.ts')
-rw-r--r--shared/extra-utils/server/follows.ts111
1 files changed, 111 insertions, 0 deletions
diff --git a/shared/extra-utils/server/follows.ts b/shared/extra-utils/server/follows.ts
new file mode 100644
index 000000000..1505804de
--- /dev/null
+++ b/shared/extra-utils/server/follows.ts
@@ -0,0 +1,111 @@
1import * as request from 'supertest'
2import { ServerInfo } from './servers'
3import { waitJobs } from './jobs'
4import { makeGetRequest, makePostBodyRequest } from '..'
5
6function getFollowersListPaginationAndSort (url: string, start: number, count: number, sort: string, search?: string) {
7 const path = '/api/v1/server/followers'
8
9 return request(url)
10 .get(path)
11 .query({ start })
12 .query({ count })
13 .query({ sort })
14 .query({ search })
15 .set('Accept', 'application/json')
16 .expect(200)
17 .expect('Content-Type', /json/)
18}
19
20function acceptFollower (url: string, token: string, follower: string, statusCodeExpected = 204) {
21 const path = '/api/v1/server/followers/' + follower + '/accept'
22
23 return makePostBodyRequest({
24 url,
25 token,
26 path,
27 statusCodeExpected
28 })
29}
30
31function rejectFollower (url: string, token: string, follower: string, statusCodeExpected = 204) {
32 const path = '/api/v1/server/followers/' + follower + '/reject'
33
34 return makePostBodyRequest({
35 url,
36 token,
37 path,
38 statusCodeExpected
39 })
40}
41
42function getFollowingListPaginationAndSort (url: string, start: number, count: number, sort: string, search?: string) {
43 const path = '/api/v1/server/following'
44
45 return request(url)
46 .get(path)
47 .query({ start })
48 .query({ count })
49 .query({ sort })
50 .query({ search })
51 .set('Accept', 'application/json')
52 .expect(200)
53 .expect('Content-Type', /json/)
54}
55
56function follow (follower: string, following: string[], accessToken: string, expectedStatus = 204) {
57 const path = '/api/v1/server/following'
58
59 const followingHosts = following.map(f => f.replace(/^http:\/\//, ''))
60 return request(follower)
61 .post(path)
62 .set('Accept', 'application/json')
63 .set('Authorization', 'Bearer ' + accessToken)
64 .send({ 'hosts': followingHosts })
65 .expect(expectedStatus)
66}
67
68async function unfollow (url: string, accessToken: string, target: ServerInfo, expectedStatus = 204) {
69 const path = '/api/v1/server/following/' + target.host
70
71 return request(url)
72 .delete(path)
73 .set('Accept', 'application/json')
74 .set('Authorization', 'Bearer ' + accessToken)
75 .expect(expectedStatus)
76}
77
78function removeFollower (url: string, accessToken: string, follower: ServerInfo, expectedStatus = 204) {
79 const path = '/api/v1/server/followers/peertube@' + follower.host
80
81 return request(url)
82 .delete(path)
83 .set('Accept', 'application/json')
84 .set('Authorization', 'Bearer ' + accessToken)
85 .expect(expectedStatus)
86}
87
88async function doubleFollow (server1: ServerInfo, server2: ServerInfo) {
89 await Promise.all([
90 follow(server1.url, [ server2.url ], server1.accessToken),
91 follow(server2.url, [ server1.url ], server2.accessToken)
92 ])
93
94 // Wait request propagation
95 await waitJobs([ server1, server2 ])
96
97 return true
98}
99
100// ---------------------------------------------------------------------------
101
102export {
103 getFollowersListPaginationAndSort,
104 getFollowingListPaginationAndSort,
105 unfollow,
106 removeFollower,
107 follow,
108 doubleFollow,
109 acceptFollower,
110 rejectFollower
111}