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