1 /* tslint:disable:no-unused-expression */
3 import * as chai from 'chai'
5 import { VideoComment, VideoCommentThreadTree } from '../../../../shared/models/videos/video-comment.model'
8 flushAndRunMultipleServers, flushTests, getVideosList, killallServers, ServerInfo, setAccessTokensToServers, uploadVideo,
10 } from '../../utils/index'
11 import { dateIsValid, webtorrentAdd } from '../../utils/miscs/miscs'
12 import { follow, getFollowersListPaginationAndSort, getFollowingListPaginationAndSort, unfollow } from '../../utils/server/follows'
13 import { getUserAccessToken } from '../../utils/users/login'
14 import { createUser } from '../../utils/users/users'
16 addVideoCommentReply, addVideoCommentThread, getVideoCommentThreads,
17 getVideoThreadComments
18 } from '../../utils/videos/video-comments'
19 import { getVideo, rateVideo, testVideoImage } from '../../utils/videos/videos'
21 const expect = chai.expect
23 describe('Test follows', function () {
24 let servers: ServerInfo[] = []
26 before(async function () {
29 servers = await flushAndRunMultipleServers(3)
31 // Get the access tokens
32 await setAccessTokensToServers(servers)
35 it('Should not have followers', async function () {
36 for (const server of servers) {
37 const res = await getFollowersListPaginationAndSort(server.url, 0, 5, 'createdAt')
38 const follows = res.body.data
40 expect(res.body.total).to.equal(0)
41 expect(follows).to.be.an('array')
42 expect(follows.length).to.equal(0)
46 it('Should not have following', async function () {
47 for (const server of servers) {
48 const res = await getFollowingListPaginationAndSort(server.url, 0, 5, 'createdAt')
49 const follows = res.body.data
51 expect(res.body.total).to.equal(0)
52 expect(follows).to.be.an('array')
53 expect(follows.length).to.equal(0)
57 it('Should have server 1 following server 2 and 3', async function () {
60 await follow(servers[0].url, [ servers[1].url, servers[2].url ], servers[0].accessToken)
65 it('Should have 2 followings on server 1', async function () {
66 let res = await getFollowingListPaginationAndSort(servers[0].url, 0, 1, 'createdAt')
67 let follows = res.body.data
69 expect(res.body.total).to.equal(2)
70 expect(follows).to.be.an('array')
71 expect(follows.length).to.equal(1)
73 res = await getFollowingListPaginationAndSort(servers[0].url, 1, 1, 'createdAt')
74 follows = follows.concat(res.body.data)
76 const server2Follow = follows.find(f => f.following.host === 'localhost:9002')
77 const server3Follow = follows.find(f => f.following.host === 'localhost:9003')
79 expect(server2Follow).to.not.be.undefined
80 expect(server3Follow).to.not.be.undefined
81 expect(server2Follow.state).to.equal('accepted')
82 expect(server3Follow.state).to.equal('accepted')
85 it('Should have 0 followings on server 1 and 2', async function () {
86 for (const server of [ servers[1], servers[2] ]) {
87 const res = await getFollowingListPaginationAndSort(server.url, 0, 5, 'createdAt')
88 const follows = res.body.data
90 expect(res.body.total).to.equal(0)
91 expect(follows).to.be.an('array')
92 expect(follows.length).to.equal(0)
96 it('Should have 1 followers on server 2 and 3', async function () {
97 for (const server of [ servers[1], servers[2] ]) {
98 let res = await getFollowersListPaginationAndSort(server.url, 0, 1, 'createdAt')
100 let follows = res.body.data
101 expect(res.body.total).to.equal(1)
102 expect(follows).to.be.an('array')
103 expect(follows.length).to.equal(1)
104 expect(follows[0].follower.host).to.equal('localhost:9001')
108 it('Should have 0 followers on server 1', async function () {
109 const res = await getFollowersListPaginationAndSort(servers[0].url, 0, 5, 'createdAt')
110 const follows = res.body.data
112 expect(res.body.total).to.equal(0)
113 expect(follows).to.be.an('array')
114 expect(follows.length).to.equal(0)
117 it('Should unfollow server 3 on server 1', async function () {
120 await unfollow(servers[0].url, servers[0].accessToken, servers[2])
125 it('Should not follow server 3 on server 1 anymore', async function () {
126 const res = await getFollowingListPaginationAndSort(servers[0].url, 0, 2, 'createdAt')
127 let follows = res.body.data
129 expect(res.body.total).to.equal(1)
130 expect(follows).to.be.an('array')
131 expect(follows.length).to.equal(1)
133 expect(follows[0].following.host).to.equal('localhost:9002')
136 it('Should not have server 1 as follower on server 3 anymore', async function () {
137 const res = await getFollowersListPaginationAndSort(servers[2].url, 0, 1, 'createdAt')
139 let follows = res.body.data
140 expect(res.body.total).to.equal(0)
141 expect(follows).to.be.an('array')
142 expect(follows.length).to.equal(0)
145 it('Should upload a video on server 2 ans 3 and propagate only the video of server 2', async function () {
148 await uploadVideo(servers[1].url, servers[1].accessToken, { name: 'server2' })
149 await uploadVideo(servers[2].url, servers[2].accessToken, { name: 'server3' })
153 let res = await getVideosList(servers[0].url)
154 expect(res.body.total).to.equal(1)
155 expect(res.body.data[0].name).to.equal('server2')
157 res = await getVideosList(servers[1].url)
158 expect(res.body.total).to.equal(1)
159 expect(res.body.data[0].name).to.equal('server2')
161 res = await getVideosList(servers[2].url)
162 expect(res.body.total).to.equal(1)
163 expect(res.body.data[0].name).to.equal('server3')
166 it('Should propagate previous uploaded videos on a new following', async function () {
169 const video4Attributes = {
174 tags: [ 'tag1', 'tag2', 'tag3' ]
177 await uploadVideo(servers[2].url, servers[2].accessToken, { name: 'server3-2' })
178 await uploadVideo(servers[2].url, servers[2].accessToken, { name: 'server3-3' })
179 await uploadVideo(servers[2].url, servers[2].accessToken, video4Attributes)
180 await uploadVideo(servers[2].url, servers[2].accessToken, { name: 'server3-5' })
181 await uploadVideo(servers[2].url, servers[2].accessToken, { name: 'server3-6' })
184 const user = { username: 'captain', password: 'password' }
185 await createUser(servers[2].url, servers[2].accessToken, user.username, user.password)
186 const userAccessToken = await getUserAccessToken(servers[2], user)
188 const resVideos = await getVideosList(servers[ 2 ].url)
189 const video4 = resVideos.body.data.find(v => v.name === 'server3-4')
192 await rateVideo(servers[ 2 ].url, servers[ 2 ].accessToken, video4.id, 'like')
193 await rateVideo(servers[ 2 ].url, userAccessToken, video4.id, 'dislike')
197 const text = 'my super first comment'
198 const res = await addVideoCommentThread(servers[ 2 ].url, servers[ 2 ].accessToken, video4.id, text)
199 const threadId = res.body.comment.id
201 const text1 = 'my super answer to thread 1'
202 const childCommentRes = await addVideoCommentReply(servers[ 2 ].url, servers[ 2 ].accessToken, video4.id, threadId, text1)
203 const childCommentId = childCommentRes.body.comment.id
205 const text2 = 'my super answer to answer of thread 1'
206 await addVideoCommentReply(servers[ 2 ].url, servers[ 2 ].accessToken, video4.id, childCommentId, text2)
208 const text3 = 'my second answer to thread 1'
209 await addVideoCommentReply(servers[ 2 ].url, servers[ 2 ].accessToken, video4.id, threadId, text3)
215 // Server 1 follows server 3
216 await follow(servers[0].url, [ servers[2].url ], servers[0].accessToken)
220 let res = await getVideosList(servers[0].url)
221 expect(res.body.total).to.equal(7)
223 const video2 = res.body.data.find(v => v.name === 'server3-2')
224 const video4 = res.body.data.find(v => v.name === 'server3-4')
225 const video6 = res.body.data.find(v => v.name === 'server3-6')
227 expect(video2).to.not.be.undefined
228 expect(video4).to.not.be.undefined
229 expect(video6).to.not.be.undefined
231 const res2 = await getVideo(servers[0].url, video4.id)
232 const videoDetails = res2.body
234 expect(videoDetails.name).to.equal('server3-4')
235 expect(videoDetails.category).to.equal(2)
236 expect(videoDetails.categoryLabel).to.equal('Films')
237 expect(videoDetails.licence).to.equal(6)
238 expect(videoDetails.licenceLabel).to.equal('Attribution - Non Commercial - No Derivatives')
239 expect(videoDetails.language).to.equal(3)
240 expect(videoDetails.languageLabel).to.equal('Mandarin')
241 expect(videoDetails.nsfw).to.be.ok
242 expect(videoDetails.description).to.equal('my super description')
243 expect(videoDetails.serverHost).to.equal('localhost:9003')
244 expect(videoDetails.accountName).to.equal('root')
245 expect(videoDetails.likes).to.equal(1)
246 expect(videoDetails.dislikes).to.equal(1)
247 expect(videoDetails.isLocal).to.be.false
248 expect(videoDetails.tags).to.deep.equal([ 'tag1', 'tag2', 'tag3' ])
249 expect(dateIsValid(videoDetails.createdAt)).to.be.true
250 expect(dateIsValid(videoDetails.updatedAt)).to.be.true
251 expect(videoDetails.files).to.have.lengthOf(1)
253 const file = videoDetails.files[0]
254 const magnetUri = file.magnetUri
255 expect(file.magnetUri).to.have.lengthOf.above(2)
256 expect(file.torrentUrl).to.equal(`${servers[2].url}/static/torrents/${videoDetails.uuid}-${file.resolution}.torrent`)
257 expect(file.fileUrl).to.equal(`${servers[2].url}/static/webseed/${videoDetails.uuid}-${file.resolution}.webm`)
258 expect(file.resolution).to.equal(720)
259 expect(file.resolutionLabel).to.equal('720p')
260 expect(file.size).to.equal(218910)
262 const test = await testVideoImage(servers[2].url, 'video_short.webm', videoDetails.thumbnailPath)
263 expect(test).to.equal(true)
265 const torrent = await webtorrentAdd(magnetUri)
266 expect(torrent.files).to.be.an('array')
267 expect(torrent.files.length).to.equal(1)
268 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
271 const res1 = await getVideoCommentThreads(servers[0].url, video4.id, 0, 5)
273 expect(res1.body.total).to.equal(1)
274 expect(res1.body.data).to.be.an('array')
275 expect(res1.body.data).to.have.lengthOf(1)
277 const comment: VideoComment = res1.body.data[0]
278 expect(comment.inReplyToCommentId).to.be.null
279 expect(comment.text).equal('my super first comment')
280 expect(comment.videoId).to.equal(video4.id)
281 expect(comment.id).to.equal(comment.threadId)
282 expect(comment.account.name).to.equal('root')
283 expect(comment.account.host).to.equal('localhost:9003')
284 expect(comment.totalReplies).to.equal(3)
285 expect(dateIsValid(comment.createdAt as string)).to.be.true
286 expect(dateIsValid(comment.updatedAt as string)).to.be.true
288 const threadId = comment.threadId
290 const res2 = await getVideoThreadComments(servers[0].url, video4.id, threadId)
292 const tree: VideoCommentThreadTree = res2.body
293 expect(tree.comment.text).equal('my super first comment')
294 expect(tree.children).to.have.lengthOf(2)
296 const firstChild = tree.children[0]
297 expect(firstChild.comment.text).to.equal('my super answer to thread 1')
298 expect(firstChild.children).to.have.lengthOf(1)
300 const childOfFirstChild = firstChild.children[0]
301 expect(childOfFirstChild.comment.text).to.equal('my super answer to answer of thread 1')
302 expect(childOfFirstChild.children).to.have.lengthOf(0)
304 const secondChild = tree.children[1]
305 expect(secondChild.comment.text).to.equal('my second answer to thread 1')
306 expect(secondChild.children).to.have.lengthOf(0)
310 after(async function () {
311 killallServers(servers)
313 // Keep the logs if the test failed