]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/friends-basic.ts
Add channels to upload form
[github/Chocobozzz/PeerTube.git] / server / tests / api / friends-basic.ts
CommitLineData
0e1dc3e7
C
1/* tslint:disable:no-unused-expression */
2
3import 'mocha'
4import * as chai from 'chai'
5const expect = chai.expect
6
7import {
8 ServerInfo,
9 flushTests,
10 quitFriends,
11 wait,
12 setAccessTokensToServers,
13 flushAndRunMultipleServers,
14 killallServers,
15 makeFriends,
16 getFriendsList,
17 dateIsValid,
8a02bd04
C
18 quitOneFriend,
19 getPodsListPaginationAndSort
0e1dc3e7
C
20} from '../utils'
21
22describe('Test basic friends', function () {
23 let servers = []
24
25 function makeFriendsWrapper (podNumber: number) {
26 const server = servers[podNumber - 1]
27 return makeFriends(server.url, server.accessToken)
28 }
29
30 async function testMadeFriends (servers: ServerInfo[], serverToTest: ServerInfo) {
31 const friends = []
32 for (let i = 0; i < servers.length; i++) {
33 if (servers[i].url === serverToTest.url) continue
34 friends.push(servers[i].host)
35 }
36
37 const res = await getFriendsList(serverToTest.url)
38
39 const result = res.body.data
40 expect(result).to.be.an('array')
41 expect(result.length).to.equal(2)
42
43 const resultHosts = [ result[0].host, result[1].host ]
44 expect(resultHosts[0]).to.not.equal(resultHosts[1])
45
46 const errorString = 'Friends host do not correspond for ' + serverToTest.host
47 expect(friends).to.contain(resultHosts[0], errorString)
48 expect(friends).to.contain(resultHosts[1], errorString)
49 }
50
51 // ---------------------------------------------------------------
52
53 before(async function () {
54 this.timeout(120000)
55
56 servers = await flushAndRunMultipleServers(3)
57
58 await setAccessTokensToServers(servers)
59 })
60
61 it('Should not have friends', async function () {
62 for (const server of servers) {
63 const res = await getFriendsList(server.url)
64
65 const result = res.body.data
66 expect(result).to.be.an('array')
67 expect(result.length).to.equal(0)
68 }
69 })
70
71 it('Should make friends', async function () {
72 this.timeout(120000)
73
74 // The second pod make friend with the third
75 await makeFriendsWrapper(2)
76
77 // Wait for the request between pods
78 await wait(11000)
79
80 // The second pod should have the third as a friend
81 const res1 = await getFriendsList(servers[1].url)
82
83 const friends = res1.body.data
84 expect(friends).to.be.an('array')
85 expect(friends.length).to.equal(1)
86
87 const pod1 = friends[0]
88 expect(pod1.host).to.equal(servers[2].host)
89 expect(pod1.email).to.equal('admin3@example.com')
5f04dd2f 90 expect(pod1.score).to.be.at.least(20)
0e1dc3e7
C
91 expect(dateIsValid(pod1.createdAt)).to.be.true
92
93 // Same here, the third pod should have the second pod as a friend
94 const res2 = await getFriendsList(servers[2].url)
95 const result = res2.body.data
96 expect(result).to.be.an('array')
97 expect(result.length).to.equal(1)
98
99 const pod2 = result[0]
100 expect(pod2.host).to.equal(servers[1].host)
101 expect(pod2.email).to.equal('admin2@example.com')
5f04dd2f 102 expect(pod2.score).to.be.at.least(20)
0e1dc3e7
C
103 expect(dateIsValid(pod2.createdAt)).to.be.true
104
105 // Finally the first pod make friend with the second pod
106 await makeFriendsWrapper(1)
107
108 // Wait for the request between pods
109 await wait(11000)
110
111 // Now each pod should be friend with the other ones
112 for (const server of servers) {
113 await testMadeFriends(servers, server)
114 }
115 })
116
117 it('Should not be allowed to make friend again', async function () {
118 this.timeout(10000)
119
120 const server = servers[1]
121 await makeFriends(server.url, server.accessToken, 409)
122 })
123
8a02bd04
C
124 it('Should list friends correctly', async function () {
125 const start = 1
126 const count = 1
127 const sort = '-host'
128
129 const res = await getPodsListPaginationAndSort(servers[0].url, start, count, sort)
130 expect(res.body.total).to.equal(2)
131 expect(res.body.data).to.have.lengthOf(1)
132
133 const pod = res.body.data[0]
134 expect(pod.host).to.equal('localhost:9002')
135 expect(pod.email).to.equal('admin2@example.com')
5f04dd2f 136 expect(pod.score).to.be.at.least(20)
8a02bd04
C
137 expect(dateIsValid(pod.createdAt)).to.be.true
138 })
139
0e1dc3e7
C
140 it('Should quit friends of pod 2', async function () {
141 this.timeout(10000)
142
143 // Pod 1 quit friends
144 await quitFriends(servers[1].url, servers[1].accessToken)
145
146 // Pod 1 should not have friends anymore
147 const res = await getFriendsList(servers[1].url)
148 const friends = res.body.data
149 expect(friends).to.be.an('array')
150 expect(friends).to.have.lengthOf(0)
151
152 // Other pods shouldn't have pod 1 too
153 const serversToTest = [ servers[0].url, servers[2].url ]
154 for (const url of serversToTest) {
155 const res = await getFriendsList(url)
156 const friends = res.body.data
157
158 expect(friends).to.be.an('array')
159 expect(friends.length).to.equal(1)
160 expect(friends[0].host).not.to.be.equal(servers[1].host)
161 }
162 })
163
164 it('Should allow pod 2 to make friend again', async function () {
165 this.timeout(120000)
166
167 const server = servers[1]
168 await makeFriends(server.url, server.accessToken)
169 await wait(11000)
170
171 for (const server of servers) {
172 await testMadeFriends(servers, server)
173 }
174 })
175
176 it('Should allow pod 1 to quit only pod 2', async function () {
177 // Pod 1 quits pod 2
178 const server = servers[0]
179
180 // Get pod 2 id so we can query it
181 const res1 = await getFriendsList(server.url)
182 const friends1 = res1.body.data
183 let pod1 = friends1.find(friend => (friend.host === servers[1].host))
184
185 // Remove it from the friends list
186 await quitOneFriend(server.url, server.accessToken, pod1.id)
187
188 // Pod 1 should have only pod 3 in its friends list
189 const res2 = await getFriendsList(servers[0].url)
190 const friends2 = res2.body.data
191 expect(friends2).to.be.an('array')
192 expect(friends2.length).to.equal(1)
193
194 const pod2 = friends2[0]
195 expect(pod2.host).to.equal(servers[2].host)
196
197 // Pod 2 should have only pod 3 in its friends list
198 const res3 = await getFriendsList(servers[1].url)
199 const friends3 = res3.body.data
200 expect(friends3).to.be.an('array')
201 expect(friends3.length).to.equal(1)
202
203 const pod = friends3[0]
204 expect(pod.host).to.equal(servers[2].host)
205
206 // Pod 3 should have both pods in its friends list
207 const res4 = await getFriendsList(servers[2].url)
208 const friends4 = res4.body.data
209 expect(friends4).to.be.an('array')
210 expect(friends4.length).to.equal(2)
211 })
212
213 after(async function () {
214 killallServers(servers)
215
216 if (this['ok']) {
217 await flushTests()
218 }
219 })
220})