urls: makefriends/quitfriends -> make-friends/quit-friends
[oweals/peertube.git] / server / tests / utils / pods.ts
1 import * as request from 'supertest'
2
3 import { wait } from './miscs'
4
5 function getFriendsList (url: string) {
6   const path = '/api/v1/pods/'
7
8   return request(url)
9           .get(path)
10           .set('Accept', 'application/json')
11           .expect(200)
12           .expect('Content-Type', /json/)
13 }
14
15 async function makeFriends (url: string, accessToken: string, expectedStatus = 204) {
16   // Which pod makes friends with which pod
17   const friendsMatrix = {
18     'http://localhost:9001': [
19       'localhost:9002'
20     ],
21     'http://localhost:9002': [
22       'localhost:9003'
23     ],
24     'http://localhost:9003': [
25       'localhost:9001'
26     ],
27     'http://localhost:9004': [
28       'localhost:9002'
29     ],
30     'http://localhost:9005': [
31       'localhost:9001',
32       'localhost:9004'
33     ],
34     'http://localhost:9006': [
35       'localhost:9001',
36       'localhost:9002',
37       'localhost:9003'
38     ]
39   }
40   const path = '/api/v1/pods/make-friends'
41
42   // The first pod make friend with the third
43   const res = await request(url)
44                       .post(path)
45                       .set('Accept', 'application/json')
46                       .set('Authorization', 'Bearer ' + accessToken)
47                       .send({ 'hosts': friendsMatrix[url] })
48                       .expect(expectedStatus)
49
50   // Wait request propagation
51   await wait(1000)
52
53   return res
54 }
55
56 async function quitFriends (url: string, accessToken: string, expectedStatus = 204) {
57   const path = '/api/v1/pods/quit-friends'
58
59   // The first pod make friend with the third
60   const res = await request(url)
61                       .get(path)
62                       .set('Accept', 'application/json')
63                       .set('Authorization', 'Bearer ' + accessToken)
64                       .expect(expectedStatus)
65
66   // Wait request propagation
67   await wait(1000)
68
69   return res
70 }
71
72 function quitOneFriend (url: string, accessToken: string, friendId: number, expectedStatus = 204) {
73   const path = '/api/v1/pods/' + friendId
74
75   return request(url)
76           .delete(path)
77           .set('Accept', 'application/json')
78           .set('Authorization', 'Bearer ' + accessToken)
79           .expect(expectedStatus)
80 }
81
82 // ---------------------------------------------------------------------------
83
84 export {
85   getFriendsList,
86   makeFriends,
87   quitFriends,
88   quitOneFriend
89 }