]>
Commit | Line | Data |
---|---|---|
4610bc5b | 1 | import * as request from 'supertest' |
afffe988 | 2 | import { ServerInfo } from './servers' |
3cd0734f | 3 | import { waitJobs } from './jobs' |
ad3405d0 | 4 | import { makePostBodyRequest } from '../requests/requests' |
4610bc5b | 5 | |
b014b6b9 | 6 | function getFollowersListPaginationAndSort (url: string, start: number, count: number, sort: string, search?: string) { |
afffe988 | 7 | const path = '/api/v1/server/followers' |
4610bc5b | 8 | |
60919831 C |
9 | const query = { |
10 | start, | |
11 | count, | |
12 | sort, | |
13 | search | |
14 | } | |
15 | ||
4610bc5b C |
16 | return request(url) |
17 | .get(path) | |
60919831 | 18 | .query(query) |
4610bc5b C |
19 | .set('Accept', 'application/json') |
20 | .expect(200) | |
21 | .expect('Content-Type', /json/) | |
22 | } | |
23 | ||
14893eb7 C |
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 | ||
b014b6b9 | 46 | function getFollowingListPaginationAndSort (url: string, start: number, count: number, sort: string, search?: string) { |
afffe988 | 47 | const path = '/api/v1/server/following' |
4610bc5b | 48 | |
60919831 C |
49 | const query = { |
50 | start, | |
51 | count, | |
52 | sort, | |
53 | search | |
54 | } | |
55 | ||
4610bc5b C |
56 | return request(url) |
57 | .get(path) | |
60919831 | 58 | .query(query) |
4610bc5b C |
59 | .set('Accept', 'application/json') |
60 | .expect(200) | |
61 | .expect('Content-Type', /json/) | |
62 | } | |
63 | ||
14893eb7 | 64 | function follow (follower: string, following: string[], accessToken: string, expectedStatus = 204) { |
9a27cdc2 | 65 | const path = '/api/v1/server/following' |
4610bc5b | 66 | |
afffe988 | 67 | const followingHosts = following.map(f => f.replace(/^http:\/\//, '')) |
14893eb7 | 68 | return request(follower) |
4610bc5b C |
69 | .post(path) |
70 | .set('Accept', 'application/json') | |
71 | .set('Authorization', 'Bearer ' + accessToken) | |
afffe988 | 72 | .send({ 'hosts': followingHosts }) |
4610bc5b | 73 | .expect(expectedStatus) |
4610bc5b C |
74 | } |
75 | ||
50d6de9c C |
76 | async function unfollow (url: string, accessToken: string, target: ServerInfo, expectedStatus = 204) { |
77 | const path = '/api/v1/server/following/' + target.host | |
0f91ae62 | 78 | |
0e9c48c2 | 79 | return request(url) |
0f91ae62 C |
80 | .delete(path) |
81 | .set('Accept', 'application/json') | |
82 | .set('Authorization', 'Bearer ' + accessToken) | |
83 | .expect(expectedStatus) | |
0e9c48c2 | 84 | } |
0f91ae62 | 85 | |
0e9c48c2 C |
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) | |
0f91ae62 C |
94 | } |
95 | ||
afffe988 C |
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 | ||
572f8d3d | 102 | // Wait request propagation |
3cd0734f | 103 | await waitJobs([ server1, server2 ]) |
572f8d3d | 104 | |
afffe988 C |
105 | return true |
106 | } | |
107 | ||
4610bc5b C |
108 | // --------------------------------------------------------------------------- |
109 | ||
110 | export { | |
111 | getFollowersListPaginationAndSort, | |
112 | getFollowingListPaginationAndSort, | |
0f91ae62 | 113 | unfollow, |
0e9c48c2 | 114 | removeFollower, |
afffe988 | 115 | follow, |
14893eb7 C |
116 | doubleFollow, |
117 | acceptFollower, | |
118 | rejectFollower | |
4610bc5b | 119 | } |