Add public settings endpoint
[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 { 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 followings on server 1', async function () {
101     {
102       const res = await getFollowingListPaginationAndSort(servers[ 0 ].url, 0, 1, 'createdAt', ':' + servers[1].port)
103       const follows = res.body.data
104
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)
108     }
109
110     {
111       const res = await getFollowingListPaginationAndSort(servers[ 0 ].url, 0, 1, 'createdAt', 'bla')
112       const follows = res.body.data
113
114       expect(res.body.total).to.equal(0)
115       expect(follows.length).to.equal(0)
116     }
117   })
118
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
123
124       expect(res.body.total).to.equal(0)
125       expect(follows).to.be.an('array')
126       expect(follows.length).to.equal(0)
127     }
128   })
129
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')
133
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)
139     }
140   })
141
142   it('Should search followers on server 2', async function () {
143     {
144       const res = await getFollowersListPaginationAndSort(servers[ 2 ].url, 0, 5, 'createdAt', servers[0].port + '')
145       const follows = res.body.data
146
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)
150     }
151
152     {
153       const res = await getFollowersListPaginationAndSort(servers[ 2 ].url, 0, 5, 'createdAt', 'bla')
154       const follows = res.body.data
155
156       expect(res.body.total).to.equal(0)
157       expect(follows.length).to.equal(0)
158     }
159   })
160
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
164
165     expect(res.body.total).to.equal(0)
166     expect(follows).to.be.an('array')
167     expect(follows.length).to.equal(0)
168   })
169
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)
174
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)
178
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)
181   })
182
183   it('Should unfollow server 3 on server 1', async function () {
184     this.timeout(5000)
185
186     await unfollow(servers[0].url, servers[0].accessToken, servers[2])
187
188     await waitJobs(servers)
189   })
190
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
194
195     expect(res.body.total).to.equal(1)
196     expect(follows).to.be.an('array')
197     expect(follows.length).to.equal(1)
198
199     expect(follows[0].following.host).to.equal('localhost:' + servers[1].port)
200   })
201
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')
204
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)
209   })
210
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)
214
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)
217
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)
220   })
221
222   it('Should upload a video on server 2 and 3 and propagate only the video of server 2', async function () {
223     this.timeout(35000)
224
225     await uploadVideo(servers[1].url, servers[1].accessToken, { name: 'server2' })
226     await uploadVideo(servers[2].url, servers[2].accessToken, { name: 'server3' })
227
228     await waitJobs(servers)
229
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')
233
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')
237
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')
241   })
242
243   describe('Should propagate data on a new following', function () {
244     let video4: Video
245
246     before(async function () {
247       this.timeout(20000)
248
249       const video4Attributes = {
250         name: 'server3-4',
251         category: 2,
252         nsfw: true,
253         licence: 6,
254         tags: [ 'tag1', 'tag2', 'tag3' ]
255       }
256
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' })
262
263       {
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)
267
268         const resVideos = await getVideosList(servers[ 2 ].url)
269         video4 = resVideos.body.data.find(v => v.name === 'server3-4')
270
271         {
272           await rateVideo(servers[ 2 ].url, servers[ 2 ].accessToken, video4.id, 'like')
273           await rateVideo(servers[ 2 ].url, userAccessToken, video4.id, 'dislike')
274         }
275
276         {
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
280
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
284
285           const text2 = 'my super answer to answer of thread 1'
286           await addVideoCommentReply(servers[ 2 ].url, servers[ 2 ].accessToken, video4.id, childCommentId, text2)
287
288           const text3 = 'my second answer to thread 1'
289           await addVideoCommentReply(servers[ 2 ].url, servers[ 2 ].accessToken, video4.id, threadId, text3)
290         }
291
292         {
293           await createVideoCaption({
294             url: servers[2].url,
295             accessToken: servers[2].accessToken,
296             language: 'ar',
297             videoId: video4.id,
298             fixture: 'subtitle-good2.vtt'
299           })
300         }
301       }
302
303       await waitJobs(servers)
304
305       // Server 1 follows server 3
306       await follow(servers[ 0 ].url, [ servers[ 2 ].url ], servers[ 0 ].accessToken)
307
308       await waitJobs(servers)
309     })
310
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)
315
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)
318
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)
321     })
322
323     it('Should have propagated videos', async function () {
324       const res = await getVideosList(servers[ 0 ].url)
325       expect(res.body.total).to.equal(7)
326
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')
330
331       expect(video2).to.not.be.undefined
332       expect(video4).to.not.be.undefined
333       expect(video6).to.not.be.undefined
334
335       const isLocal = false
336       const checkAttributes = {
337         name: 'server3-4',
338         category: 2,
339         licence: 6,
340         language: 'zh',
341         nsfw: true,
342         description: 'my super description',
343         support: 'my super support text',
344         account: {
345           name: 'root',
346           host: 'localhost:' + servers[2].port
347         },
348         isLocal,
349         commentsEnabled: true,
350         downloadEnabled: true,
351         duration: 5,
352         tags: [ 'tag1', 'tag2', 'tag3' ],
353         privacy: VideoPrivacy.PUBLIC,
354         likes: 1,
355         dislikes: 1,
356         channel: {
357           displayName: 'Main root channel',
358           name: 'root_channel',
359           description: '',
360           isLocal
361         },
362         fixture: 'video_short.webm',
363         files: [
364           {
365             resolution: 720,
366             size: 218910
367           }
368         ]
369       }
370       await completeVideoCheck(servers[ 0 ].url, video4, checkAttributes)
371     })
372
373     it('Should have propagated comments', async function () {
374       const res1 = await getVideoCommentThreads(servers[0].url, video4.id, 0, 5)
375
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)
379
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
390
391       const threadId = comment.threadId
392
393       const res2 = await getVideoThreadComments(servers[0].url, video4.id, threadId)
394
395       const tree: VideoCommentThreadTree = res2.body
396       expect(tree.comment.text).equal('my super first comment')
397       expect(tree.children).to.have.lengthOf(2)
398
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)
402
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)
406
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)
410     })
411
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)
416
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.')
422     })
423
424     it('Should unfollow server 3 on server 1 and does not list server 3 videos', async function () {
425       this.timeout(5000)
426
427       await unfollow(servers[0].url, servers[0].accessToken, servers[2])
428
429       await waitJobs(servers)
430
431       let res = await getVideosList(servers[ 0 ].url)
432       expect(res.body.total).to.equal(1)
433     })
434
435   })
436
437   after(async function () {
438     await cleanupTests(servers)
439   })
440 })