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,
11 import { follow, getFollowersListPaginationAndSort, getFollowingListPaginationAndSort, unfollow } from '../utils/follows'
12 import { getUserAccessToken } from '../utils/login'
13 import { dateIsValid, webtorrentAdd } from '../utils/miscs'
14 import { createUser } from '../utils/users'
15 import { addVideoCommentReply, addVideoCommentThread, getVideoCommentThreads, getVideoThreadComments } from '../utils/video-comments'
16 import { getVideo, rateVideo, testVideoImage } from '../utils/videos'
18 const expect = chai.expect
20 describe('Test follows', function () {
21 let servers: ServerInfo[] = []
23 before(async function () {
26 servers = await flushAndRunMultipleServers(3)
28 // Get the access tokens
29 await setAccessTokensToServers(servers)
32 it('Should not have followers', async function () {
33 for (const server of servers) {
34 const res = await getFollowersListPaginationAndSort(server.url, 0, 5, 'createdAt')
35 const follows = res.body.data
37 expect(res.body.total).to.equal(0)
38 expect(follows).to.be.an('array')
39 expect(follows.length).to.equal(0)
43 it('Should not have following', async function () {
44 for (const server of servers) {
45 const res = await getFollowingListPaginationAndSort(server.url, 0, 5, 'createdAt')
46 const follows = res.body.data
48 expect(res.body.total).to.equal(0)
49 expect(follows).to.be.an('array')
50 expect(follows.length).to.equal(0)
54 it('Should have server 1 following server 2 and 3', async function () {
57 await follow(servers[0].url, [ servers[1].url, servers[2].url ], servers[0].accessToken)
62 it('Should have 2 followings on server 1', async function () {
63 let res = await getFollowingListPaginationAndSort(servers[0].url, 0, 1, 'createdAt')
64 let follows = res.body.data
66 expect(res.body.total).to.equal(2)
67 expect(follows).to.be.an('array')
68 expect(follows.length).to.equal(1)
70 res = await getFollowingListPaginationAndSort(servers[0].url, 1, 1, 'createdAt')
71 follows = follows.concat(res.body.data)
73 const server2Follow = follows.find(f => f.following.host === 'localhost:9002')
74 const server3Follow = follows.find(f => f.following.host === 'localhost:9003')
76 expect(server2Follow).to.not.be.undefined
77 expect(server3Follow).to.not.be.undefined
78 expect(server2Follow.state).to.equal('accepted')
79 expect(server3Follow.state).to.equal('accepted')
82 it('Should have 0 followings on server 1 and 2', async function () {
83 for (const server of [ servers[1], servers[2] ]) {
84 const res = await getFollowingListPaginationAndSort(server.url, 0, 5, 'createdAt')
85 const follows = res.body.data
87 expect(res.body.total).to.equal(0)
88 expect(follows).to.be.an('array')
89 expect(follows.length).to.equal(0)
93 it('Should have 1 followers on server 2 and 3', async function () {
94 for (const server of [ servers[1], servers[2] ]) {
95 let res = await getFollowersListPaginationAndSort(server.url, 0, 1, 'createdAt')
97 let follows = res.body.data
98 expect(res.body.total).to.equal(1)
99 expect(follows).to.be.an('array')
100 expect(follows.length).to.equal(1)
101 expect(follows[0].follower.host).to.equal('localhost:9001')
105 it('Should have 0 followers on server 1', async function () {
106 const res = await getFollowersListPaginationAndSort(servers[0].url, 0, 5, 'createdAt')
107 const follows = res.body.data
109 expect(res.body.total).to.equal(0)
110 expect(follows).to.be.an('array')
111 expect(follows.length).to.equal(0)
114 it('Should unfollow server 3 on server 1', async function () {
117 await unfollow(servers[0].url, servers[0].accessToken, servers[2])
122 it('Should not follow server 3 on server 1 anymore', async function () {
123 const res = await getFollowingListPaginationAndSort(servers[0].url, 0, 2, 'createdAt')
124 let follows = res.body.data
126 expect(res.body.total).to.equal(1)
127 expect(follows).to.be.an('array')
128 expect(follows.length).to.equal(1)
130 expect(follows[0].following.host).to.equal('localhost:9002')
133 it('Should not have server 1 as follower on server 3 anymore', async function () {
134 const res = await getFollowersListPaginationAndSort(servers[2].url, 0, 1, 'createdAt')
136 let follows = res.body.data
137 expect(res.body.total).to.equal(0)
138 expect(follows).to.be.an('array')
139 expect(follows.length).to.equal(0)
142 it('Should upload a video on server 2 ans 3 and propagate only the video of server 2', async function () {
145 await uploadVideo(servers[1].url, servers[1].accessToken, { name: 'server2' })
146 await uploadVideo(servers[2].url, servers[2].accessToken, { name: 'server3' })
150 let res = await getVideosList(servers[0].url)
151 expect(res.body.total).to.equal(1)
152 expect(res.body.data[0].name).to.equal('server2')
154 res = await getVideosList(servers[1].url)
155 expect(res.body.total).to.equal(1)
156 expect(res.body.data[0].name).to.equal('server2')
158 res = await getVideosList(servers[2].url)
159 expect(res.body.total).to.equal(1)
160 expect(res.body.data[0].name).to.equal('server3')
163 it('Should propagate previous uploaded videos on a new following', async function () {
166 const video4Attributes = {
171 tags: [ 'tag1', 'tag2', 'tag3' ]
174 await uploadVideo(servers[2].url, servers[2].accessToken, { name: 'server3-2' })
175 await uploadVideo(servers[2].url, servers[2].accessToken, { name: 'server3-3' })
176 await uploadVideo(servers[2].url, servers[2].accessToken, video4Attributes)
177 await uploadVideo(servers[2].url, servers[2].accessToken, { name: 'server3-5' })
178 await uploadVideo(servers[2].url, servers[2].accessToken, { name: 'server3-6' })
181 const user = { username: 'captain', password: 'password' }
182 await createUser(servers[2].url, servers[2].accessToken, user.username, user.password)
183 const userAccessToken = await getUserAccessToken(servers[2], user)
185 const resVideos = await getVideosList(servers[ 2 ].url)
186 const video4 = resVideos.body.data.find(v => v.name === 'server3-4')
189 await rateVideo(servers[ 2 ].url, servers[ 2 ].accessToken, video4.id, 'like')
190 await rateVideo(servers[ 2 ].url, userAccessToken, video4.id, 'dislike')
194 const text = 'my super first comment'
195 const res = await addVideoCommentThread(servers[ 2 ].url, servers[ 2 ].accessToken, video4.id, text)
196 const threadId = res.body.comment.id
198 const text1 = 'my super answer to thread 1'
199 const childCommentRes = await addVideoCommentReply(servers[ 2 ].url, servers[ 2 ].accessToken, video4.id, threadId, text1)
200 const childCommentId = childCommentRes.body.comment.id
202 const text2 = 'my super answer to answer of thread 1'
203 await addVideoCommentReply(servers[ 2 ].url, servers[ 2 ].accessToken, video4.id, childCommentId, text2)
205 const text3 = 'my second answer to thread 1'
206 await addVideoCommentReply(servers[ 2 ].url, servers[ 2 ].accessToken, video4.id, threadId, text3)
212 // Server 1 follows server 3
213 await follow(servers[0].url, [ servers[2].url ], servers[0].accessToken)
217 let res = await getVideosList(servers[0].url)
218 expect(res.body.total).to.equal(7)
220 const video2 = res.body.data.find(v => v.name === 'server3-2')
221 const video4 = res.body.data.find(v => v.name === 'server3-4')
222 const video6 = res.body.data.find(v => v.name === 'server3-6')
224 expect(video2).to.not.be.undefined
225 expect(video4).to.not.be.undefined
226 expect(video6).to.not.be.undefined
228 const res2 = await getVideo(servers[0].url, video4.id)
229 const videoDetails = res2.body
231 expect(videoDetails.name).to.equal('server3-4')
232 expect(videoDetails.category).to.equal(2)
233 expect(videoDetails.categoryLabel).to.equal('Films')
234 expect(videoDetails.licence).to.equal(6)
235 expect(videoDetails.licenceLabel).to.equal('Attribution - Non Commercial - No Derivatives')
236 expect(videoDetails.language).to.equal(3)
237 expect(videoDetails.languageLabel).to.equal('Mandarin')
238 expect(videoDetails.nsfw).to.be.ok
239 expect(videoDetails.description).to.equal('my super description')
240 expect(videoDetails.serverHost).to.equal('localhost:9003')
241 expect(videoDetails.accountName).to.equal('root')
242 expect(videoDetails.likes).to.equal(1)
243 expect(videoDetails.dislikes).to.equal(1)
244 expect(videoDetails.isLocal).to.be.false
245 expect(videoDetails.tags).to.deep.equal([ 'tag1', 'tag2', 'tag3' ])
246 expect(dateIsValid(videoDetails.createdAt)).to.be.true
247 expect(dateIsValid(videoDetails.updatedAt)).to.be.true
248 expect(videoDetails.files).to.have.lengthOf(1)
250 const file = videoDetails.files[0]
251 const magnetUri = file.magnetUri
252 expect(file.magnetUri).to.have.lengthOf.above(2)
253 expect(file.torrentUrl).to.equal(`${servers[2].url}/static/torrents/${videoDetails.uuid}-${file.resolution}.torrent`)
254 expect(file.fileUrl).to.equal(`${servers[2].url}/static/webseed/${videoDetails.uuid}-${file.resolution}.webm`)
255 expect(file.resolution).to.equal(720)
256 expect(file.resolutionLabel).to.equal('720p')
257 expect(file.size).to.equal(218910)
259 const test = await testVideoImage(servers[2].url, 'video_short.webm', videoDetails.thumbnailPath)
260 expect(test).to.equal(true)
262 const torrent = await webtorrentAdd(magnetUri)
263 expect(torrent.files).to.be.an('array')
264 expect(torrent.files.length).to.equal(1)
265 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
268 const res1 = await getVideoCommentThreads(servers[0].url, video4.id, 0, 5)
270 expect(res1.body.total).to.equal(1)
271 expect(res1.body.data).to.be.an('array')
272 expect(res1.body.data).to.have.lengthOf(1)
274 const comment: VideoComment = res1.body.data[0]
275 expect(comment.inReplyToCommentId).to.be.null
276 expect(comment.text).equal('my super first comment')
277 expect(comment.videoId).to.equal(video4.id)
278 expect(comment.id).to.equal(comment.threadId)
279 expect(comment.account.name).to.equal('root')
280 expect(comment.account.host).to.equal('localhost:9003')
281 expect(comment.totalReplies).to.equal(3)
282 expect(dateIsValid(comment.createdAt as string)).to.be.true
283 expect(dateIsValid(comment.updatedAt as string)).to.be.true
285 const threadId = comment.threadId
287 const res2 = await getVideoThreadComments(servers[0].url, video4.id, threadId)
289 const tree: VideoCommentThreadTree = res2.body
290 expect(tree.comment.text).equal('my super first comment')
291 expect(tree.children).to.have.lengthOf(2)
293 const firstChild = tree.children[0]
294 expect(firstChild.comment.text).to.equal('my super answer to thread 1')
295 expect(firstChild.children).to.have.lengthOf(1)
297 const childOfFirstChild = firstChild.children[0]
298 expect(childOfFirstChild.comment.text).to.equal('my super answer to answer of thread 1')
299 expect(childOfFirstChild.children).to.have.lengthOf(0)
301 const secondChild = tree.children[1]
302 expect(secondChild.comment.text).to.equal('my second answer to thread 1')
303 expect(secondChild.children).to.have.lengthOf(0)
307 after(async function () {
308 killallServers(servers)
310 // Keep the logs if the test failed