]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/server/follows.ts
Only display accepted followers/followings in about page
[github/Chocobozzz/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 { cleanupTests, completeVideoCheck } from '../../../../shared/extra-utils'
8 import {
9 flushAndRunMultipleServers,
10 getVideosList,
11 ServerInfo,
12 setAccessTokensToServers,
13 uploadVideo
14 } from '../../../../shared/extra-utils/index'
15 import { dateIsValid } from '../../../../shared/extra-utils/miscs/miscs'
16 import {
17 follow,
18 getFollowersListPaginationAndSort,
19 getFollowingListPaginationAndSort,
20 unfollow
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'
25 import {
26 addVideoCommentReply,
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'
35
36 const expect = chai.expect
37
38 describe('Test follows', function () {
39 let servers: ServerInfo[] = []
40
41 before(async function () {
42 this.timeout(30000)
43
44 servers = await flushAndRunMultipleServers(3)
45
46 // Get the access tokens
47 await setAccessTokensToServers(servers)
48 })
49
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
54
55 expect(res.body.total).to.equal(0)
56 expect(follows).to.be.an('array')
57 expect(follows.length).to.equal(0)
58 }
59 })
60
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
65
66 expect(res.body.total).to.equal(0)
67 expect(follows).to.be.an('array')
68 expect(follows.length).to.equal(0)
69 }
70 })
71
72 it('Should have server 1 following server 2 and 3', async function () {
73 this.timeout(30000)
74
75 await follow(servers[0].url, [ servers[1].url, servers[2].url ], servers[0].accessToken)
76
77 await waitJobs(servers)
78 })
79
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
83
84 expect(res.body.total).to.equal(2)
85 expect(follows).to.be.an('array')
86 expect(follows.length).to.equal(1)
87
88 res = await getFollowingListPaginationAndSort(servers[0].url, 1, 1, 'createdAt')
89 follows = follows.concat(res.body.data)
90
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)
93
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')
98 })
99
100 it('Should search/filter followings on server 1', async function () {
101 {
102 const search = ':' + servers[1].port
103 const res = await getFollowingListPaginationAndSort(servers[ 0 ].url, 0, 1, 'createdAt', search)
104 const follows = res.body.data
105
106 expect(res.body.total).to.equal(1)
107 expect(follows.length).to.equal(1)
108 expect(follows[ 0 ].following.host).to.equal('localhost:' + servers[1].port)
109
110 const res2 = await getFollowingListPaginationAndSort(servers[ 0 ].url, 0, 1, 'createdAt', search, 'accepted')
111 expect(res2.body.total).to.equal(1)
112 expect(res2.body.data).to.have.lengthOf(1)
113
114 const res3 = await getFollowingListPaginationAndSort(servers[ 0 ].url, 0, 1, 'createdAt', search, 'pending')
115 expect(res3.body.total).to.equal(0)
116 expect(res3.body.data).to.have.lengthOf(0)
117 }
118
119 {
120 const res = await getFollowingListPaginationAndSort(servers[ 0 ].url, 0, 1, 'createdAt', 'bla')
121 const follows = res.body.data
122
123 expect(res.body.total).to.equal(0)
124 expect(follows.length).to.equal(0)
125 }
126 })
127
128 it('Should have 0 followings on server 2 and 3', async function () {
129 for (const server of [ servers[1], servers[2] ]) {
130 const res = await getFollowingListPaginationAndSort(server.url, 0, 5, 'createdAt')
131 const follows = res.body.data
132
133 expect(res.body.total).to.equal(0)
134 expect(follows).to.be.an('array')
135 expect(follows.length).to.equal(0)
136 }
137 })
138
139 it('Should have 1 followers on server 2 and 3', async function () {
140 for (const server of [ servers[1], servers[2] ]) {
141 let res = await getFollowersListPaginationAndSort(server.url, 0, 1, 'createdAt')
142
143 let follows = res.body.data
144 expect(res.body.total).to.equal(1)
145 expect(follows).to.be.an('array')
146 expect(follows.length).to.equal(1)
147 expect(follows[0].follower.host).to.equal('localhost:' + servers[0].port)
148 }
149 })
150
151 it('Should search/filter followers on server 2', async function () {
152 {
153 const search = servers[0].port + ''
154 const res = await getFollowersListPaginationAndSort(servers[ 2 ].url, 0, 5, 'createdAt', search)
155 const follows = res.body.data
156
157 expect(res.body.total).to.equal(1)
158 expect(follows.length).to.equal(1)
159 expect(follows[ 0 ].following.host).to.equal('localhost:' + servers[2].port)
160
161 const res2 = await getFollowersListPaginationAndSort(servers[ 2 ].url, 0, 5, 'createdAt', search, 'accepted')
162 expect(res2.body.total).to.equal(1)
163 expect(res2.body.data).to.have.lengthOf(1)
164
165 const res3 = await getFollowersListPaginationAndSort(servers[ 2 ].url, 0, 5, 'createdAt', search, 'pending')
166 expect(res3.body.total).to.equal(0)
167 expect(res3.body.data).to.have.lengthOf(0)
168 }
169
170 {
171 const res = await getFollowersListPaginationAndSort(servers[ 2 ].url, 0, 5, 'createdAt', 'bla')
172 const follows = res.body.data
173
174 expect(res.body.total).to.equal(0)
175 expect(follows.length).to.equal(0)
176 }
177 })
178
179 it('Should have 0 followers on server 1', async function () {
180 const res = await getFollowersListPaginationAndSort(servers[0].url, 0, 5, 'createdAt')
181 const follows = res.body.data
182
183 expect(res.body.total).to.equal(0)
184 expect(follows).to.be.an('array')
185 expect(follows.length).to.equal(0)
186 })
187
188 it('Should have the correct follows counts', async function () {
189 await expectAccountFollows(servers[0].url, 'peertube@localhost:' + servers[0].port, 0, 2)
190 await expectAccountFollows(servers[0].url, 'peertube@localhost:' + servers[1].port, 1, 0)
191 await expectAccountFollows(servers[0].url, 'peertube@localhost:' + servers[2].port, 1, 0)
192
193 // Server 2 and 3 does not know server 1 follow another server (there was not a refresh)
194 await expectAccountFollows(servers[1].url, 'peertube@localhost:' + servers[0].port, 0, 1)
195 await expectAccountFollows(servers[1].url, 'peertube@localhost:' + servers[1].port, 1, 0)
196
197 await expectAccountFollows(servers[2].url, 'peertube@localhost:' + servers[0].port, 0, 1)
198 await expectAccountFollows(servers[2].url, 'peertube@localhost:' + servers[2].port, 1, 0)
199 })
200
201 it('Should unfollow server 3 on server 1', async function () {
202 this.timeout(5000)
203
204 await unfollow(servers[0].url, servers[0].accessToken, servers[2])
205
206 await waitJobs(servers)
207 })
208
209 it('Should not follow server 3 on server 1 anymore', async function () {
210 const res = await getFollowingListPaginationAndSort(servers[0].url, 0, 2, 'createdAt')
211 let follows = res.body.data
212
213 expect(res.body.total).to.equal(1)
214 expect(follows).to.be.an('array')
215 expect(follows.length).to.equal(1)
216
217 expect(follows[0].following.host).to.equal('localhost:' + servers[1].port)
218 })
219
220 it('Should not have server 1 as follower on server 3 anymore', async function () {
221 const res = await getFollowersListPaginationAndSort(servers[2].url, 0, 1, 'createdAt')
222
223 let follows = res.body.data
224 expect(res.body.total).to.equal(0)
225 expect(follows).to.be.an('array')
226 expect(follows.length).to.equal(0)
227 })
228
229 it('Should have the correct follows counts 2', async function () {
230 await expectAccountFollows(servers[0].url, 'peertube@localhost:' + servers[0].port, 0, 1)
231 await expectAccountFollows(servers[0].url, 'peertube@localhost:' + servers[1].port, 1, 0)
232
233 await expectAccountFollows(servers[1].url, 'peertube@localhost:' + servers[0].port, 0, 1)
234 await expectAccountFollows(servers[1].url, 'peertube@localhost:' + servers[1].port, 1, 0)
235
236 await expectAccountFollows(servers[2].url, 'peertube@localhost:' + servers[0].port, 0, 0)
237 await expectAccountFollows(servers[2].url, 'peertube@localhost:' + servers[2].port, 0, 0)
238 })
239
240 it('Should upload a video on server 2 and 3 and propagate only the video of server 2', async function () {
241 this.timeout(35000)
242
243 await uploadVideo(servers[1].url, servers[1].accessToken, { name: 'server2' })
244 await uploadVideo(servers[2].url, servers[2].accessToken, { name: 'server3' })
245
246 await waitJobs(servers)
247
248 let res = await getVideosList(servers[0].url)
249 expect(res.body.total).to.equal(1)
250 expect(res.body.data[0].name).to.equal('server2')
251
252 res = await getVideosList(servers[1].url)
253 expect(res.body.total).to.equal(1)
254 expect(res.body.data[0].name).to.equal('server2')
255
256 res = await getVideosList(servers[2].url)
257 expect(res.body.total).to.equal(1)
258 expect(res.body.data[0].name).to.equal('server3')
259 })
260
261 describe('Should propagate data on a new following', function () {
262 let video4: Video
263
264 before(async function () {
265 this.timeout(20000)
266
267 const video4Attributes = {
268 name: 'server3-4',
269 category: 2,
270 nsfw: true,
271 licence: 6,
272 tags: [ 'tag1', 'tag2', 'tag3' ]
273 }
274
275 await uploadVideo(servers[ 2 ].url, servers[ 2 ].accessToken, { name: 'server3-2' })
276 await uploadVideo(servers[ 2 ].url, servers[ 2 ].accessToken, { name: 'server3-3' })
277 await uploadVideo(servers[ 2 ].url, servers[ 2 ].accessToken, video4Attributes)
278 await uploadVideo(servers[ 2 ].url, servers[ 2 ].accessToken, { name: 'server3-5' })
279 await uploadVideo(servers[ 2 ].url, servers[ 2 ].accessToken, { name: 'server3-6' })
280
281 {
282 const user = { username: 'captain', password: 'password' }
283 await createUser({ url: servers[ 2 ].url, accessToken: servers[ 2 ].accessToken, username: user.username, password: user.password })
284 const userAccessToken = await userLogin(servers[ 2 ], user)
285
286 const resVideos = await getVideosList(servers[ 2 ].url)
287 video4 = resVideos.body.data.find(v => v.name === 'server3-4')
288
289 {
290 await rateVideo(servers[ 2 ].url, servers[ 2 ].accessToken, video4.id, 'like')
291 await rateVideo(servers[ 2 ].url, userAccessToken, video4.id, 'dislike')
292 }
293
294 {
295 const text = 'my super first comment'
296 const res = await addVideoCommentThread(servers[ 2 ].url, servers[ 2 ].accessToken, video4.id, text)
297 const threadId = res.body.comment.id
298
299 const text1 = 'my super answer to thread 1'
300 const childCommentRes = await addVideoCommentReply(servers[ 2 ].url, servers[ 2 ].accessToken, video4.id, threadId, text1)
301 const childCommentId = childCommentRes.body.comment.id
302
303 const text2 = 'my super answer to answer of thread 1'
304 await addVideoCommentReply(servers[ 2 ].url, servers[ 2 ].accessToken, video4.id, childCommentId, text2)
305
306 const text3 = 'my second answer to thread 1'
307 await addVideoCommentReply(servers[ 2 ].url, servers[ 2 ].accessToken, video4.id, threadId, text3)
308 }
309
310 {
311 await createVideoCaption({
312 url: servers[2].url,
313 accessToken: servers[2].accessToken,
314 language: 'ar',
315 videoId: video4.id,
316 fixture: 'subtitle-good2.vtt'
317 })
318 }
319 }
320
321 await waitJobs(servers)
322
323 // Server 1 follows server 3
324 await follow(servers[ 0 ].url, [ servers[ 2 ].url ], servers[ 0 ].accessToken)
325
326 await waitJobs(servers)
327 })
328
329 it('Should have the correct follows counts 3', async function () {
330 await expectAccountFollows(servers[0].url, 'peertube@localhost:' + servers[0].port, 0, 2)
331 await expectAccountFollows(servers[0].url, 'peertube@localhost:' + servers[1].port, 1, 0)
332 await expectAccountFollows(servers[0].url, 'peertube@localhost:' + servers[2].port, 1, 0)
333
334 await expectAccountFollows(servers[1].url, 'peertube@localhost:' + servers[0].port, 0, 1)
335 await expectAccountFollows(servers[1].url, 'peertube@localhost:' + servers[1].port, 1, 0)
336
337 await expectAccountFollows(servers[2].url, 'peertube@localhost:' + servers[0].port, 0, 2)
338 await expectAccountFollows(servers[2].url, 'peertube@localhost:' + servers[2].port, 1, 0)
339 })
340
341 it('Should have propagated videos', async function () {
342 const res = await getVideosList(servers[ 0 ].url)
343 expect(res.body.total).to.equal(7)
344
345 const video2 = res.body.data.find(v => v.name === 'server3-2')
346 video4 = res.body.data.find(v => v.name === 'server3-4')
347 const video6 = res.body.data.find(v => v.name === 'server3-6')
348
349 expect(video2).to.not.be.undefined
350 expect(video4).to.not.be.undefined
351 expect(video6).to.not.be.undefined
352
353 const isLocal = false
354 const checkAttributes = {
355 name: 'server3-4',
356 category: 2,
357 licence: 6,
358 language: 'zh',
359 nsfw: true,
360 description: 'my super description',
361 support: 'my super support text',
362 account: {
363 name: 'root',
364 host: 'localhost:' + servers[2].port
365 },
366 isLocal,
367 commentsEnabled: true,
368 downloadEnabled: true,
369 duration: 5,
370 tags: [ 'tag1', 'tag2', 'tag3' ],
371 privacy: VideoPrivacy.PUBLIC,
372 likes: 1,
373 dislikes: 1,
374 channel: {
375 displayName: 'Main root channel',
376 name: 'root_channel',
377 description: '',
378 isLocal
379 },
380 fixture: 'video_short.webm',
381 files: [
382 {
383 resolution: 720,
384 size: 218910
385 }
386 ]
387 }
388 await completeVideoCheck(servers[ 0 ].url, video4, checkAttributes)
389 })
390
391 it('Should have propagated comments', async function () {
392 const res1 = await getVideoCommentThreads(servers[0].url, video4.id, 0, 5)
393
394 expect(res1.body.total).to.equal(1)
395 expect(res1.body.data).to.be.an('array')
396 expect(res1.body.data).to.have.lengthOf(1)
397
398 const comment: VideoComment = res1.body.data[0]
399 expect(comment.inReplyToCommentId).to.be.null
400 expect(comment.text).equal('my super first comment')
401 expect(comment.videoId).to.equal(video4.id)
402 expect(comment.id).to.equal(comment.threadId)
403 expect(comment.account.name).to.equal('root')
404 expect(comment.account.host).to.equal('localhost:' + servers[2].port)
405 expect(comment.totalReplies).to.equal(3)
406 expect(dateIsValid(comment.createdAt as string)).to.be.true
407 expect(dateIsValid(comment.updatedAt as string)).to.be.true
408
409 const threadId = comment.threadId
410
411 const res2 = await getVideoThreadComments(servers[0].url, video4.id, threadId)
412
413 const tree: VideoCommentThreadTree = res2.body
414 expect(tree.comment.text).equal('my super first comment')
415 expect(tree.children).to.have.lengthOf(2)
416
417 const firstChild = tree.children[0]
418 expect(firstChild.comment.text).to.equal('my super answer to thread 1')
419 expect(firstChild.children).to.have.lengthOf(1)
420
421 const childOfFirstChild = firstChild.children[0]
422 expect(childOfFirstChild.comment.text).to.equal('my super answer to answer of thread 1')
423 expect(childOfFirstChild.children).to.have.lengthOf(0)
424
425 const secondChild = tree.children[1]
426 expect(secondChild.comment.text).to.equal('my second answer to thread 1')
427 expect(secondChild.children).to.have.lengthOf(0)
428 })
429
430 it('Should have propagated captions', async function () {
431 const res = await listVideoCaptions(servers[0].url, video4.id)
432 expect(res.body.total).to.equal(1)
433 expect(res.body.data).to.have.lengthOf(1)
434
435 const caption1: VideoCaption = res.body.data[0]
436 expect(caption1.language.id).to.equal('ar')
437 expect(caption1.language.label).to.equal('Arabic')
438 expect(caption1.captionPath).to.equal('/static/video-captions/' + video4.uuid + '-ar.vtt')
439 await testCaptionFile(servers[0].url, caption1.captionPath, 'Subtitle good 2.')
440 })
441
442 it('Should unfollow server 3 on server 1 and does not list server 3 videos', async function () {
443 this.timeout(5000)
444
445 await unfollow(servers[0].url, servers[0].accessToken, servers[2])
446
447 await waitJobs(servers)
448
449 let res = await getVideosList(servers[ 0 ].url)
450 expect(res.body.total).to.equal(1)
451 })
452
453 })
454
455 after(async function () {
456 await cleanupTests(servers)
457 })
458 })