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