1 /* tslint:disable:no-unused-expression */
3 import * as chai from 'chai'
5 import { Video, VideoPrivacy } from '../../../../shared/models/videos'
6 import { VideoComment, VideoCommentThreadTree } from '../../../../shared/models/videos/video-comment.model'
7 import { cleanupTests, completeVideoCheck } from '../../../../shared/extra-utils'
9 flushAndRunMultipleServers,
12 setAccessTokensToServers,
14 } from '../../../../shared/extra-utils/index'
15 import { dateIsValid } from '../../../../shared/extra-utils/miscs/miscs'
18 getFollowersListPaginationAndSort,
19 getFollowingListPaginationAndSort,
21 } from '../../../../shared/extra-utils/server/follows'
22 import { expectAccountFollows } from '../../../../shared/extra-utils/users/accounts'
23 import { userLogin } from '../../../../shared/extra-utils/users/login'
24 import { createUser } from '../../../../shared/extra-utils/users/users'
27 addVideoCommentThread,
28 getVideoCommentThreads,
29 getVideoThreadComments
30 } from '../../../../shared/extra-utils/videos/video-comments'
31 import { rateVideo } from '../../../../shared/extra-utils/videos/videos'
32 import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
33 import { createVideoCaption, listVideoCaptions, testCaptionFile } from '../../../../shared/extra-utils/videos/video-captions'
34 import { VideoCaption } from '../../../../shared/models/videos/caption/video-caption.model'
36 const expect = chai.expect
38 describe('Test follows', function () {
39 let servers: ServerInfo[] = []
41 before(async function () {
44 servers = await flushAndRunMultipleServers(3)
46 // Get the access tokens
47 await setAccessTokensToServers(servers)
50 it('Should not have followers', async function () {
51 for (const server of servers) {
52 const res = await getFollowersListPaginationAndSort(server.url, 0, 5, 'createdAt')
53 const follows = res.body.data
55 expect(res.body.total).to.equal(0)
56 expect(follows).to.be.an('array')
57 expect(follows.length).to.equal(0)
61 it('Should not have following', async function () {
62 for (const server of servers) {
63 const res = await getFollowingListPaginationAndSort(server.url, 0, 5, 'createdAt')
64 const follows = res.body.data
66 expect(res.body.total).to.equal(0)
67 expect(follows).to.be.an('array')
68 expect(follows.length).to.equal(0)
72 it('Should have server 1 following server 2 and 3', async function () {
75 await follow(servers[0].url, [ servers[1].url, servers[2].url ], servers[0].accessToken)
77 await waitJobs(servers)
80 it('Should have 2 followings on server 1', async function () {
81 let res = await getFollowingListPaginationAndSort(servers[0].url, 0, 1, 'createdAt')
82 let follows = res.body.data
84 expect(res.body.total).to.equal(2)
85 expect(follows).to.be.an('array')
86 expect(follows.length).to.equal(1)
88 res = await getFollowingListPaginationAndSort(servers[0].url, 1, 1, 'createdAt')
89 follows = follows.concat(res.body.data)
91 const server2Follow = follows.find(f => f.following.host === 'localhost:' + servers[1].port)
92 const server3Follow = follows.find(f => f.following.host === 'localhost:' + servers[2].port)
94 expect(server2Follow).to.not.be.undefined
95 expect(server3Follow).to.not.be.undefined
96 expect(server2Follow.state).to.equal('accepted')
97 expect(server3Follow.state).to.equal('accepted')
100 it('Should search followings on server 1', async function () {
102 const res = await getFollowingListPaginationAndSort(servers[ 0 ].url, 0, 1, 'createdAt', ':' + servers[1].port)
103 const follows = res.body.data
105 expect(res.body.total).to.equal(1)
106 expect(follows.length).to.equal(1)
107 expect(follows[ 0 ].following.host).to.equal('localhost:' + servers[1].port)
111 const res = await getFollowingListPaginationAndSort(servers[ 0 ].url, 0, 1, 'createdAt', 'bla')
112 const follows = res.body.data
114 expect(res.body.total).to.equal(0)
115 expect(follows.length).to.equal(0)
119 it('Should have 0 followings on server 2 and 3', async function () {
120 for (const server of [ servers[1], servers[2] ]) {
121 const res = await getFollowingListPaginationAndSort(server.url, 0, 5, 'createdAt')
122 const follows = res.body.data
124 expect(res.body.total).to.equal(0)
125 expect(follows).to.be.an('array')
126 expect(follows.length).to.equal(0)
130 it('Should have 1 followers on server 2 and 3', async function () {
131 for (const server of [ servers[1], servers[2] ]) {
132 let res = await getFollowersListPaginationAndSort(server.url, 0, 1, 'createdAt')
134 let follows = res.body.data
135 expect(res.body.total).to.equal(1)
136 expect(follows).to.be.an('array')
137 expect(follows.length).to.equal(1)
138 expect(follows[0].follower.host).to.equal('localhost:' + servers[0].port)
142 it('Should search followers on server 2', async function () {
144 const res = await getFollowersListPaginationAndSort(servers[ 2 ].url, 0, 5, 'createdAt', servers[0].port + '')
145 const follows = res.body.data
147 expect(res.body.total).to.equal(1)
148 expect(follows.length).to.equal(1)
149 expect(follows[ 0 ].following.host).to.equal('localhost:' + servers[2].port)
153 const res = await getFollowersListPaginationAndSort(servers[ 2 ].url, 0, 5, 'createdAt', 'bla')
154 const follows = res.body.data
156 expect(res.body.total).to.equal(0)
157 expect(follows.length).to.equal(0)
161 it('Should have 0 followers on server 1', async function () {
162 const res = await getFollowersListPaginationAndSort(servers[0].url, 0, 5, 'createdAt')
163 const follows = res.body.data
165 expect(res.body.total).to.equal(0)
166 expect(follows).to.be.an('array')
167 expect(follows.length).to.equal(0)
170 it('Should have the correct follows counts', async function () {
171 await expectAccountFollows(servers[0].url, 'peertube@localhost:' + servers[0].port, 0, 2)
172 await expectAccountFollows(servers[0].url, 'peertube@localhost:' + servers[1].port, 1, 0)
173 await expectAccountFollows(servers[0].url, 'peertube@localhost:' + servers[2].port, 1, 0)
175 // Server 2 and 3 does not know server 1 follow another server (there was not a refresh)
176 await expectAccountFollows(servers[1].url, 'peertube@localhost:' + servers[0].port, 0, 1)
177 await expectAccountFollows(servers[1].url, 'peertube@localhost:' + servers[1].port, 1, 0)
179 await expectAccountFollows(servers[2].url, 'peertube@localhost:' + servers[0].port, 0, 1)
180 await expectAccountFollows(servers[2].url, 'peertube@localhost:' + servers[2].port, 1, 0)
183 it('Should unfollow server 3 on server 1', async function () {
186 await unfollow(servers[0].url, servers[0].accessToken, servers[2])
188 await waitJobs(servers)
191 it('Should not follow server 3 on server 1 anymore', async function () {
192 const res = await getFollowingListPaginationAndSort(servers[0].url, 0, 2, 'createdAt')
193 let follows = res.body.data
195 expect(res.body.total).to.equal(1)
196 expect(follows).to.be.an('array')
197 expect(follows.length).to.equal(1)
199 expect(follows[0].following.host).to.equal('localhost:' + servers[1].port)
202 it('Should not have server 1 as follower on server 3 anymore', async function () {
203 const res = await getFollowersListPaginationAndSort(servers[2].url, 0, 1, 'createdAt')
205 let follows = res.body.data
206 expect(res.body.total).to.equal(0)
207 expect(follows).to.be.an('array')
208 expect(follows.length).to.equal(0)
211 it('Should have the correct follows counts 2', async function () {
212 await expectAccountFollows(servers[0].url, 'peertube@localhost:' + servers[0].port, 0, 1)
213 await expectAccountFollows(servers[0].url, 'peertube@localhost:' + servers[1].port, 1, 0)
215 await expectAccountFollows(servers[1].url, 'peertube@localhost:' + servers[0].port, 0, 1)
216 await expectAccountFollows(servers[1].url, 'peertube@localhost:' + servers[1].port, 1, 0)
218 await expectAccountFollows(servers[2].url, 'peertube@localhost:' + servers[0].port, 0, 0)
219 await expectAccountFollows(servers[2].url, 'peertube@localhost:' + servers[2].port, 0, 0)
222 it('Should upload a video on server 2 and 3 and propagate only the video of server 2', async function () {
225 await uploadVideo(servers[1].url, servers[1].accessToken, { name: 'server2' })
226 await uploadVideo(servers[2].url, servers[2].accessToken, { name: 'server3' })
228 await waitJobs(servers)
230 let res = await getVideosList(servers[0].url)
231 expect(res.body.total).to.equal(1)
232 expect(res.body.data[0].name).to.equal('server2')
234 res = await getVideosList(servers[1].url)
235 expect(res.body.total).to.equal(1)
236 expect(res.body.data[0].name).to.equal('server2')
238 res = await getVideosList(servers[2].url)
239 expect(res.body.total).to.equal(1)
240 expect(res.body.data[0].name).to.equal('server3')
243 describe('Should propagate data on a new following', function () {
246 before(async function () {
249 const video4Attributes = {
254 tags: [ 'tag1', 'tag2', 'tag3' ]
257 await uploadVideo(servers[ 2 ].url, servers[ 2 ].accessToken, { name: 'server3-2' })
258 await uploadVideo(servers[ 2 ].url, servers[ 2 ].accessToken, { name: 'server3-3' })
259 await uploadVideo(servers[ 2 ].url, servers[ 2 ].accessToken, video4Attributes)
260 await uploadVideo(servers[ 2 ].url, servers[ 2 ].accessToken, { name: 'server3-5' })
261 await uploadVideo(servers[ 2 ].url, servers[ 2 ].accessToken, { name: 'server3-6' })
264 const user = { username: 'captain', password: 'password' }
265 await createUser({ url: servers[ 2 ].url, accessToken: servers[ 2 ].accessToken, username: user.username, password: user.password })
266 const userAccessToken = await userLogin(servers[ 2 ], user)
268 const resVideos = await getVideosList(servers[ 2 ].url)
269 video4 = resVideos.body.data.find(v => v.name === 'server3-4')
272 await rateVideo(servers[ 2 ].url, servers[ 2 ].accessToken, video4.id, 'like')
273 await rateVideo(servers[ 2 ].url, userAccessToken, video4.id, 'dislike')
277 const text = 'my super first comment'
278 const res = await addVideoCommentThread(servers[ 2 ].url, servers[ 2 ].accessToken, video4.id, text)
279 const threadId = res.body.comment.id
281 const text1 = 'my super answer to thread 1'
282 const childCommentRes = await addVideoCommentReply(servers[ 2 ].url, servers[ 2 ].accessToken, video4.id, threadId, text1)
283 const childCommentId = childCommentRes.body.comment.id
285 const text2 = 'my super answer to answer of thread 1'
286 await addVideoCommentReply(servers[ 2 ].url, servers[ 2 ].accessToken, video4.id, childCommentId, text2)
288 const text3 = 'my second answer to thread 1'
289 await addVideoCommentReply(servers[ 2 ].url, servers[ 2 ].accessToken, video4.id, threadId, text3)
293 await createVideoCaption({
295 accessToken: servers[2].accessToken,
298 fixture: 'subtitle-good2.vtt'
303 await waitJobs(servers)
305 // Server 1 follows server 3
306 await follow(servers[ 0 ].url, [ servers[ 2 ].url ], servers[ 0 ].accessToken)
308 await waitJobs(servers)
311 it('Should have the correct follows counts 3', async function () {
312 await expectAccountFollows(servers[0].url, 'peertube@localhost:' + servers[0].port, 0, 2)
313 await expectAccountFollows(servers[0].url, 'peertube@localhost:' + servers[1].port, 1, 0)
314 await expectAccountFollows(servers[0].url, 'peertube@localhost:' + servers[2].port, 1, 0)
316 await expectAccountFollows(servers[1].url, 'peertube@localhost:' + servers[0].port, 0, 1)
317 await expectAccountFollows(servers[1].url, 'peertube@localhost:' + servers[1].port, 1, 0)
319 await expectAccountFollows(servers[2].url, 'peertube@localhost:' + servers[0].port, 0, 2)
320 await expectAccountFollows(servers[2].url, 'peertube@localhost:' + servers[2].port, 1, 0)
323 it('Should have propagated videos', async function () {
324 const res = await getVideosList(servers[ 0 ].url)
325 expect(res.body.total).to.equal(7)
327 const video2 = res.body.data.find(v => v.name === 'server3-2')
328 video4 = res.body.data.find(v => v.name === 'server3-4')
329 const video6 = res.body.data.find(v => v.name === 'server3-6')
331 expect(video2).to.not.be.undefined
332 expect(video4).to.not.be.undefined
333 expect(video6).to.not.be.undefined
335 const isLocal = false
336 const checkAttributes = {
342 description: 'my super description',
343 support: 'my super support text',
346 host: 'localhost:' + servers[2].port
349 commentsEnabled: true,
350 downloadEnabled: true,
352 tags: [ 'tag1', 'tag2', 'tag3' ],
353 privacy: VideoPrivacy.PUBLIC,
357 displayName: 'Main root channel',
358 name: 'root_channel',
362 fixture: 'video_short.webm',
370 await completeVideoCheck(servers[ 0 ].url, video4, checkAttributes)
373 it('Should have propagated comments', async function () {
374 const res1 = await getVideoCommentThreads(servers[0].url, video4.id, 0, 5)
376 expect(res1.body.total).to.equal(1)
377 expect(res1.body.data).to.be.an('array')
378 expect(res1.body.data).to.have.lengthOf(1)
380 const comment: VideoComment = res1.body.data[0]
381 expect(comment.inReplyToCommentId).to.be.null
382 expect(comment.text).equal('my super first comment')
383 expect(comment.videoId).to.equal(video4.id)
384 expect(comment.id).to.equal(comment.threadId)
385 expect(comment.account.name).to.equal('root')
386 expect(comment.account.host).to.equal('localhost:' + servers[2].port)
387 expect(comment.totalReplies).to.equal(3)
388 expect(dateIsValid(comment.createdAt as string)).to.be.true
389 expect(dateIsValid(comment.updatedAt as string)).to.be.true
391 const threadId = comment.threadId
393 const res2 = await getVideoThreadComments(servers[0].url, video4.id, threadId)
395 const tree: VideoCommentThreadTree = res2.body
396 expect(tree.comment.text).equal('my super first comment')
397 expect(tree.children).to.have.lengthOf(2)
399 const firstChild = tree.children[0]
400 expect(firstChild.comment.text).to.equal('my super answer to thread 1')
401 expect(firstChild.children).to.have.lengthOf(1)
403 const childOfFirstChild = firstChild.children[0]
404 expect(childOfFirstChild.comment.text).to.equal('my super answer to answer of thread 1')
405 expect(childOfFirstChild.children).to.have.lengthOf(0)
407 const secondChild = tree.children[1]
408 expect(secondChild.comment.text).to.equal('my second answer to thread 1')
409 expect(secondChild.children).to.have.lengthOf(0)
412 it('Should have propagated captions', async function () {
413 const res = await listVideoCaptions(servers[0].url, video4.id)
414 expect(res.body.total).to.equal(1)
415 expect(res.body.data).to.have.lengthOf(1)
417 const caption1: VideoCaption = res.body.data[0]
418 expect(caption1.language.id).to.equal('ar')
419 expect(caption1.language.label).to.equal('Arabic')
420 expect(caption1.captionPath).to.equal('/static/video-captions/' + video4.uuid + '-ar.vtt')
421 await testCaptionFile(servers[0].url, caption1.captionPath, 'Subtitle good 2.')
424 it('Should unfollow server 3 on server 1 and does not list server 3 videos', async function () {
427 await unfollow(servers[0].url, servers[0].accessToken, servers[2])
429 await waitJobs(servers)
431 let res = await getVideosList(servers[ 0 ].url)
432 expect(res.body.total).to.equal(1)
437 after(async function () {
438 await cleanupTests(servers)