]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/utils/server/follows.ts
Improve tests when waiting pending jobs
[github/Chocobozzz/PeerTube.git] / server / tests / utils / server / follows.ts
CommitLineData
4610bc5b 1import * as request from 'supertest'
c5d31dba 2import { wait } from '../miscs/miscs'
afffe988 3import { ServerInfo } from './servers'
3cd0734f 4import { waitJobs } from './jobs'
4610bc5b
C
5
6function getFollowersListPaginationAndSort (url: string, start: number, count: number, sort: string) {
afffe988 7 const path = '/api/v1/server/followers'
4610bc5b
C
8
9 return request(url)
10 .get(path)
11 .query({ start })
12 .query({ count })
13 .query({ sort })
14 .set('Accept', 'application/json')
15 .expect(200)
16 .expect('Content-Type', /json/)
17}
18
19function getFollowingListPaginationAndSort (url: string, start: number, count: number, sort: string) {
afffe988 20 const path = '/api/v1/server/following'
4610bc5b
C
21
22 return request(url)
23 .get(path)
24 .query({ start })
25 .query({ count })
26 .query({ sort })
27 .set('Accept', 'application/json')
28 .expect(200)
29 .expect('Content-Type', /json/)
30}
31
32async function follow (follower: string, following: string[], accessToken: string, expectedStatus = 204) {
9a27cdc2 33 const path = '/api/v1/server/following'
4610bc5b 34
afffe988 35 const followingHosts = following.map(f => f.replace(/^http:\/\//, ''))
4610bc5b
C
36 const res = await request(follower)
37 .post(path)
38 .set('Accept', 'application/json')
39 .set('Authorization', 'Bearer ' + accessToken)
afffe988 40 .send({ 'hosts': followingHosts })
4610bc5b
C
41 .expect(expectedStatus)
42
4610bc5b
C
43 return res
44}
45
50d6de9c
C
46async function unfollow (url: string, accessToken: string, target: ServerInfo, expectedStatus = 204) {
47 const path = '/api/v1/server/following/' + target.host
0f91ae62
C
48
49 const res = await request(url)
50 .delete(path)
51 .set('Accept', 'application/json')
52 .set('Authorization', 'Bearer ' + accessToken)
53 .expect(expectedStatus)
54
55 return res
56}
57
afffe988
C
58async function doubleFollow (server1: ServerInfo, server2: ServerInfo) {
59 await Promise.all([
60 follow(server1.url, [ server2.url ], server1.accessToken),
61 follow(server2.url, [ server1.url ], server2.accessToken)
62 ])
63
572f8d3d 64 // Wait request propagation
3cd0734f 65 await waitJobs([ server1, server2 ])
572f8d3d 66
afffe988
C
67 return true
68}
69
4610bc5b
C
70// ---------------------------------------------------------------------------
71
72export {
73 getFollowersListPaginationAndSort,
74 getFollowingListPaginationAndSort,
0f91ae62 75 unfollow,
afffe988
C
76 follow,
77 doubleFollow
4610bc5b 78}