Add tests to handle down server
[oweals/peertube.git] / server / tests / api / server / follows.ts
1 /* tslint:disable:no-unused-expression */
2
3 import * as chai from 'chai'
4 import 'mocha'
5 import { Video, VideoPrivacy } from '../../../../shared/models/videos'
6 import { VideoComment, VideoCommentThreadTree } from '../../../../shared/models/videos/video-comment.model'
7 import { completeVideoCheck } from '../../utils'
8
9 import {
10   flushAndRunMultipleServers, flushTests, getVideosList, killallServers, ServerInfo, setAccessTokensToServers, uploadVideo,
11   wait
12 } from '../../utils/index'
13 import { dateIsValid } from '../../utils/miscs/miscs'
14 import { follow, getFollowersListPaginationAndSort, getFollowingListPaginationAndSort, unfollow } from '../../utils/server/follows'
15 import { userLogin } from '../../utils/users/login'
16 import { createUser } from '../../utils/users/users'
17 import {
18   addVideoCommentReply, addVideoCommentThread, getVideoCommentThreads,
19   getVideoThreadComments
20 } from '../../utils/videos/video-comments'
21 import { rateVideo } from '../../utils/videos/videos'
22
23 const expect = chai.expect
24
25 describe('Test follows', function () {
26   let servers: ServerInfo[] = []
27
28   before(async function () {
29     this.timeout(20000)
30
31     servers = await flushAndRunMultipleServers(3)
32
33     // Get the access tokens
34     await setAccessTokensToServers(servers)
35   })
36
37   it('Should not have followers', async function () {
38     for (const server of servers) {
39       const res = await getFollowersListPaginationAndSort(server.url, 0, 5, 'createdAt')
40       const follows = res.body.data
41
42       expect(res.body.total).to.equal(0)
43       expect(follows).to.be.an('array')
44       expect(follows.length).to.equal(0)
45     }
46   })
47
48   it('Should not have following', async function () {
49     for (const server of servers) {
50       const res = await getFollowingListPaginationAndSort(server.url, 0, 5, 'createdAt')
51       const follows = res.body.data
52
53       expect(res.body.total).to.equal(0)
54       expect(follows).to.be.an('array')
55       expect(follows.length).to.equal(0)
56     }
57   })
58
59   it('Should have server 1 following server 2 and 3', async function () {
60     this.timeout(10000)
61
62     await follow(servers[0].url, [ servers[1].url, servers[2].url ], servers[0].accessToken)
63
64     await wait(7000)
65   })
66
67   it('Should have 2 followings on server 1', async function () {
68     let res = await getFollowingListPaginationAndSort(servers[0].url, 0, 1, 'createdAt')
69     let follows = res.body.data
70
71     expect(res.body.total).to.equal(2)
72     expect(follows).to.be.an('array')
73     expect(follows.length).to.equal(1)
74
75     res = await getFollowingListPaginationAndSort(servers[0].url, 1, 1, 'createdAt')
76     follows = follows.concat(res.body.data)
77
78     const server2Follow = follows.find(f => f.following.host === 'localhost:9002')
79     const server3Follow = follows.find(f => f.following.host === 'localhost:9003')
80
81     expect(server2Follow).to.not.be.undefined
82     expect(server3Follow).to.not.be.undefined
83     expect(server2Follow.state).to.equal('accepted')
84     expect(server3Follow.state).to.equal('accepted')
85   })
86
87   it('Should have 0 followings on server 1 and 2', async function () {
88     for (const server of [ servers[1], servers[2] ]) {
89       const res = await getFollowingListPaginationAndSort(server.url, 0, 5, 'createdAt')
90       const follows = res.body.data
91
92       expect(res.body.total).to.equal(0)
93       expect(follows).to.be.an('array')
94       expect(follows.length).to.equal(0)
95     }
96   })
97
98   it('Should have 1 followers on server 2 and 3', async function () {
99     for (const server of [ servers[1], servers[2] ]) {
100       let res = await getFollowersListPaginationAndSort(server.url, 0, 1, 'createdAt')
101
102       let follows = res.body.data
103       expect(res.body.total).to.equal(1)
104       expect(follows).to.be.an('array')
105       expect(follows.length).to.equal(1)
106       expect(follows[0].follower.host).to.equal('localhost:9001')
107     }
108   })
109
110   it('Should have 0 followers on server 1', async function () {
111     const res = await getFollowersListPaginationAndSort(servers[0].url, 0, 5, 'createdAt')
112     const follows = res.body.data
113
114     expect(res.body.total).to.equal(0)
115     expect(follows).to.be.an('array')
116     expect(follows.length).to.equal(0)
117   })
118
119   it('Should unfollow server 3 on server 1', async function () {
120     this.timeout(5000)
121
122     await unfollow(servers[0].url, servers[0].accessToken, servers[2])
123
124     await wait(3000)
125   })
126
127   it('Should not follow server 3 on server 1 anymore', async function () {
128     const res = await getFollowingListPaginationAndSort(servers[0].url, 0, 2, 'createdAt')
129     let follows = res.body.data
130
131     expect(res.body.total).to.equal(1)
132     expect(follows).to.be.an('array')
133     expect(follows.length).to.equal(1)
134
135     expect(follows[0].following.host).to.equal('localhost:9002')
136   })
137
138   it('Should not have server 1 as follower on server 3 anymore', async function () {
139     const res = await getFollowersListPaginationAndSort(servers[2].url, 0, 1, 'createdAt')
140
141     let follows = res.body.data
142     expect(res.body.total).to.equal(0)
143     expect(follows).to.be.an('array')
144     expect(follows.length).to.equal(0)
145   })
146
147   it('Should upload a video on server 2 ans 3 and propagate only the video of server 2', async function () {
148     this.timeout(10000)
149
150     await uploadVideo(servers[1].url, servers[1].accessToken, { name: 'server2' })
151     await uploadVideo(servers[2].url, servers[2].accessToken, { name: 'server3' })
152
153     await wait(5000)
154
155     let res = await getVideosList(servers[0].url)
156     expect(res.body.total).to.equal(1)
157     expect(res.body.data[0].name).to.equal('server2')
158
159     res = await getVideosList(servers[1].url)
160     expect(res.body.total).to.equal(1)
161     expect(res.body.data[0].name).to.equal('server2')
162
163     res = await getVideosList(servers[2].url)
164     expect(res.body.total).to.equal(1)
165     expect(res.body.data[0].name).to.equal('server3')
166   })
167
168   describe('Should propagate data on a new following', async function () {
169     let video4: Video
170
171     before(async function () {
172       this.timeout(20000)
173
174       const video4Attributes = {
175         name: 'server3-4',
176         category: 2,
177         nsfw: true,
178         licence: 6,
179         tags: [ 'tag1', 'tag2', 'tag3' ]
180       }
181
182       await uploadVideo(servers[ 2 ].url, servers[ 2 ].accessToken, { name: 'server3-2' })
183       await uploadVideo(servers[ 2 ].url, servers[ 2 ].accessToken, { name: 'server3-3' })
184       await uploadVideo(servers[ 2 ].url, servers[ 2 ].accessToken, video4Attributes)
185       await uploadVideo(servers[ 2 ].url, servers[ 2 ].accessToken, { name: 'server3-5' })
186       await uploadVideo(servers[ 2 ].url, servers[ 2 ].accessToken, { name: 'server3-6' })
187
188       {
189         const user = { username: 'captain', password: 'password' }
190         await createUser(servers[ 2 ].url, servers[ 2 ].accessToken, user.username, user.password)
191         const userAccessToken = await userLogin(servers[ 2 ], user)
192
193         const resVideos = await getVideosList(servers[ 2 ].url)
194         video4 = resVideos.body.data.find(v => v.name === 'server3-4')
195
196         {
197           await rateVideo(servers[ 2 ].url, servers[ 2 ].accessToken, video4.id, 'like')
198           await rateVideo(servers[ 2 ].url, userAccessToken, video4.id, 'dislike')
199         }
200
201         {
202           const text = 'my super first comment'
203           const res = await addVideoCommentThread(servers[ 2 ].url, servers[ 2 ].accessToken, video4.id, text)
204           const threadId = res.body.comment.id
205
206           const text1 = 'my super answer to thread 1'
207           const childCommentRes = await addVideoCommentReply(servers[ 2 ].url, servers[ 2 ].accessToken, video4.id, threadId, text1)
208           const childCommentId = childCommentRes.body.comment.id
209
210           const text2 = 'my super answer to answer of thread 1'
211           await addVideoCommentReply(servers[ 2 ].url, servers[ 2 ].accessToken, video4.id, childCommentId, text2)
212
213           const text3 = 'my second answer to thread 1'
214           await addVideoCommentReply(servers[ 2 ].url, servers[ 2 ].accessToken, video4.id, threadId, text3)
215         }
216       }
217
218       await wait(5000)
219
220       // Server 1 follows server 3
221       await follow(servers[ 0 ].url, [ servers[ 2 ].url ], servers[ 0 ].accessToken)
222
223       await wait(7000)
224     })
225
226     it('Should propagate videos', async function () {
227       const res = await getVideosList(servers[ 0 ].url)
228       expect(res.body.total).to.equal(7)
229
230       const video2 = res.body.data.find(v => v.name === 'server3-2')
231       video4 = res.body.data.find(v => v.name === 'server3-4')
232       const video6 = res.body.data.find(v => v.name === 'server3-6')
233
234       expect(video2).to.not.be.undefined
235       expect(video4).to.not.be.undefined
236       expect(video6).to.not.be.undefined
237
238       const isLocal = false
239       const checkAttributes = {
240         name: 'server3-4',
241         category: 2,
242         licence: 6,
243         language: 3,
244         nsfw: true,
245         description: 'my super description',
246         host: 'localhost:9003',
247         account: 'root',
248         isLocal,
249         commentsEnabled: true,
250         duration: 5,
251         tags: [ 'tag1', 'tag2', 'tag3' ],
252         privacy: VideoPrivacy.PUBLIC,
253         likes: 1,
254         dislikes: 1,
255         channel: {
256           name: 'Default root channel',
257           description: '',
258           isLocal
259         },
260         fixture: 'video_short.webm',
261         files: [
262           {
263             resolution: 720,
264             size: 218910
265           }
266         ]
267       }
268       await completeVideoCheck(servers[ 0 ].url, video4, checkAttributes)
269     })
270
271     it('Should propagate comments', async function () {
272       const res1 = await getVideoCommentThreads(servers[0].url, video4.id, 0, 5)
273
274       expect(res1.body.total).to.equal(1)
275       expect(res1.body.data).to.be.an('array')
276       expect(res1.body.data).to.have.lengthOf(1)
277
278       const comment: VideoComment = res1.body.data[0]
279       expect(comment.inReplyToCommentId).to.be.null
280       expect(comment.text).equal('my super first comment')
281       expect(comment.videoId).to.equal(video4.id)
282       expect(comment.id).to.equal(comment.threadId)
283       expect(comment.account.name).to.equal('root')
284       expect(comment.account.host).to.equal('localhost:9003')
285       expect(comment.totalReplies).to.equal(3)
286       expect(dateIsValid(comment.createdAt as string)).to.be.true
287       expect(dateIsValid(comment.updatedAt as string)).to.be.true
288
289       const threadId = comment.threadId
290
291       const res2 = await getVideoThreadComments(servers[0].url, video4.id, threadId)
292
293       const tree: VideoCommentThreadTree = res2.body
294       expect(tree.comment.text).equal('my super first comment')
295       expect(tree.children).to.have.lengthOf(2)
296
297       const firstChild = tree.children[0]
298       expect(firstChild.comment.text).to.equal('my super answer to thread 1')
299       expect(firstChild.children).to.have.lengthOf(1)
300
301       const childOfFirstChild = firstChild.children[0]
302       expect(childOfFirstChild.comment.text).to.equal('my super answer to answer of thread 1')
303       expect(childOfFirstChild.children).to.have.lengthOf(0)
304
305       const secondChild = tree.children[1]
306       expect(secondChild.comment.text).to.equal('my second answer to thread 1')
307       expect(secondChild.children).to.have.lengthOf(0)
308     })
309   })
310
311   after(async function () {
312     killallServers(servers)
313
314     // Keep the logs if the test failed
315     if (this['ok']) {
316       await flushTests()
317     }
318   })
319 })