Add comments federation tests
[oweals/peertube.git] / server / tests / api / multiple-servers.ts
index cdbd24f5641e95ca7ccee21e95f79ab83713196e..06274d4cc11dba5530979003f0675400377a130a 100644 (file)
@@ -1,30 +1,19 @@
 /* tslint:disable:no-unused-expression */
 
-import 'mocha'
 import * as chai from 'chai'
+import 'mocha'
+import { join } from 'path'
+import * as request from 'supertest'
+import { VideoComment, VideoCommentThreadTree } from '../../../shared/models/videos/video-comment.model'
 
 import {
-  dateIsValid,
-  flushAndRunMultipleServers,
-  flushTests,
-  getVideo,
-  getVideosList,
-  killallServers,
-  rateVideo,
-  removeVideo,
-  ServerInfo,
-  setAccessTokensToServers,
-  testVideoImage,
-  updateVideo,
-  uploadVideo,
-  wait,
-  webtorrentAdd,
-  addVideoChannel,
-  getVideoChannelsList,
-  getUserAccessToken,
-  doubleFollow
+  addVideoChannel, dateIsValid, doubleFollow, flushAndRunMultipleServers, flushTests, getUserAccessToken, getVideo,
+  getVideoChannelsList, getVideosList, killallServers, rateVideo, removeVideo, ServerInfo, setAccessTokensToServers, testVideoImage,
+  updateVideo, uploadVideo, wait, webtorrentAdd
 } from '../utils'
 import { createUser } from '../utils/users'
+import { addVideoCommentReply, addVideoCommentThread, getVideoCommentThreads, getVideoThreadComments } from '../utils/video-comments'
+import { viewVideo } from '../utils/videos'
 
 const expect = chai.expect
 
@@ -69,8 +58,7 @@ describe('Test multiple servers', function () {
 
   describe('Should upload the video and propagate on each server', function () {
     it('Should upload the video on server 1 and propagate on each server', async function () {
-      // Server 1 has video transcoding activated
-      this.timeout(15000)
+      this.timeout(25000)
 
       const videoAttributes = {
         name: 'my super name for server 1',
@@ -85,7 +73,7 @@ describe('Test multiple servers', function () {
       }
       await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes)
 
-      await wait(5000)
+      await wait(10000)
 
       // All servers should have this video
       for (const server of servers) {
@@ -108,19 +96,20 @@ describe('Test multiple servers', function () {
         expect(video.description).to.equal('my super description for server 1')
         expect(video.serverHost).to.equal('localhost:9001')
         expect(video.duration).to.equal(10)
-        expect(video.tags).to.deep.equal([ 'tag1p1', 'tag2p1' ])
         expect(dateIsValid(video.createdAt)).to.be.true
         expect(dateIsValid(video.updatedAt)).to.be.true
-        expect(video.account).to.equal('root')
+        expect(video.accountName).to.equal('root')
 
         const res2 = await getVideo(server.url, video.uuid)
         const videoDetails = res2.body
 
         expect(videoDetails.channel.name).to.equal('my channel')
         expect(videoDetails.channel.description).to.equal('super channel')
+        expect(videoDetails.account.name).to.equal('root')
         expect(dateIsValid(videoDetails.channel.createdAt)).to.be.true
         expect(dateIsValid(videoDetails.channel.updatedAt)).to.be.true
         expect(videoDetails.files).to.have.lengthOf(1)
+        expect(videoDetails.tags).to.deep.equal([ 'tag1p1', 'tag2p1' ])
 
         const file = videoDetails.files[0]
         const magnetUri = file.magnetUri
@@ -175,7 +164,7 @@ describe('Test multiple servers', function () {
       await uploadVideo(servers[1].url, userAccessToken, videoAttributes)
 
       // Transcoding
-      await wait(10000)
+      await wait(30000)
 
       // All servers should have this video
       for (const server of servers) {
@@ -198,10 +187,9 @@ describe('Test multiple servers', function () {
         expect(video.description).to.equal('my super description for server 2')
         expect(video.serverHost).to.equal('localhost:9002')
         expect(video.duration).to.equal(5)
-        expect(video.tags).to.deep.equal([ 'tag1p2', 'tag2p2', 'tag3p2' ])
         expect(dateIsValid(video.createdAt)).to.be.true
         expect(dateIsValid(video.updatedAt)).to.be.true
-        expect(video.account).to.equal('user1')
+        expect(video.accountName).to.equal('user1')
 
         if (server.url !== 'http://localhost:9002') {
           expect(video.isLocal).to.be.false
@@ -215,6 +203,7 @@ describe('Test multiple servers', function () {
         expect(videoDetails.channel.name).to.equal('Default user1 channel')
         expect(dateIsValid(videoDetails.channel.createdAt)).to.be.true
         expect(dateIsValid(videoDetails.channel.updatedAt)).to.be.true
+        expect(videoDetails.tags).to.deep.equal([ 'tag1p2', 'tag2p2', 'tag3p2' ])
 
         expect(videoDetails.files).to.have.lengthOf(4)
 
@@ -315,14 +304,14 @@ describe('Test multiple servers', function () {
         expect(video1.description).to.equal('my super description for server 3')
         expect(video1.serverHost).to.equal('localhost:9003')
         expect(video1.duration).to.equal(5)
-        expect(video1.tags).to.deep.equal([ 'tag1p3' ])
-        expect(video1.account).to.equal('root')
+        expect(video1.accountName).to.equal('root')
         expect(dateIsValid(video1.createdAt)).to.be.true
         expect(dateIsValid(video1.updatedAt)).to.be.true
 
         const res2 = await getVideo(server.url, video1.id)
         const video1Details = res2.body
         expect(video1Details.files).to.have.lengthOf(1)
+        expect(video1Details.tags).to.deep.equal([ 'tag1p3' ])
 
         const file1 = video1Details.files[0]
         expect(file1.magnetUri).to.have.lengthOf.above(2)
@@ -341,13 +330,13 @@ describe('Test multiple servers', function () {
         expect(video2.description).to.equal('my super description for server 3-2')
         expect(video2.serverHost).to.equal('localhost:9003')
         expect(video2.duration).to.equal(5)
-        expect(video2.tags).to.deep.equal([ 'tag2p3', 'tag3p3', 'tag4p3' ])
-        expect(video2.account).to.equal('root')
+        expect(video2.accountName).to.equal('root')
         expect(dateIsValid(video2.createdAt)).to.be.true
         expect(dateIsValid(video2.updatedAt)).to.be.true
 
         const res3 = await getVideo(server.url, video2.id)
         const video2Details = res3.body
+        expect(video2Details.tags).to.deep.equal([ 'tag2p3', 'tag3p3', 'tag4p3' ])
 
         expect(video2Details.files).to.have.lengthOf(1)
 
@@ -487,10 +476,10 @@ describe('Test multiple servers', function () {
       this.timeout(10000)
 
       const tasks: Promise<any>[] = []
-      tasks.push(getVideo(servers[2].url, localVideosServer3[0]))
-      tasks.push(getVideo(servers[2].url, localVideosServer3[0]))
-      tasks.push(getVideo(servers[2].url, localVideosServer3[0]))
-      tasks.push(getVideo(servers[2].url, localVideosServer3[1]))
+      tasks.push(viewVideo(servers[2].url, localVideosServer3[0]))
+      tasks.push(viewVideo(servers[2].url, localVideosServer3[0]))
+      tasks.push(viewVideo(servers[2].url, localVideosServer3[0]))
+      tasks.push(viewVideo(servers[2].url, localVideosServer3[1]))
 
       await Promise.all(tasks)
 
@@ -503,8 +492,8 @@ describe('Test multiple servers', function () {
         const video0 = videos.find(v => v.uuid === localVideosServer3[0])
         const video1 = videos.find(v => v.uuid === localVideosServer3[1])
 
-        expect(video0.views).to.equal(7)
-        expect(video1.views).to.equal(5)
+        expect(video0.views).to.equal(3)
+        expect(video1.views).to.equal(1)
       }
     })
 
@@ -512,16 +501,16 @@ describe('Test multiple servers', function () {
       this.timeout(15000)
 
       const tasks: Promise<any>[] = []
-      tasks.push(getVideo(servers[0].url, remoteVideosServer1[0]))
-      tasks.push(getVideo(servers[1].url, remoteVideosServer2[0]))
-      tasks.push(getVideo(servers[1].url, remoteVideosServer2[0]))
-      tasks.push(getVideo(servers[2].url, remoteVideosServer3[0]))
-      tasks.push(getVideo(servers[2].url, remoteVideosServer3[1]))
-      tasks.push(getVideo(servers[2].url, remoteVideosServer3[1]))
-      tasks.push(getVideo(servers[2].url, remoteVideosServer3[1]))
-      tasks.push(getVideo(servers[2].url, localVideosServer3[1]))
-      tasks.push(getVideo(servers[2].url, localVideosServer3[1]))
-      tasks.push(getVideo(servers[2].url, localVideosServer3[1]))
+      tasks.push(viewVideo(servers[0].url, remoteVideosServer1[0]))
+      tasks.push(viewVideo(servers[1].url, remoteVideosServer2[0]))
+      tasks.push(viewVideo(servers[1].url, remoteVideosServer2[0]))
+      tasks.push(viewVideo(servers[2].url, remoteVideosServer3[0]))
+      tasks.push(viewVideo(servers[2].url, remoteVideosServer3[1]))
+      tasks.push(viewVideo(servers[2].url, remoteVideosServer3[1]))
+      tasks.push(viewVideo(servers[2].url, remoteVideosServer3[1]))
+      tasks.push(viewVideo(servers[2].url, localVideosServer3[1]))
+      tasks.push(viewVideo(servers[2].url, localVideosServer3[1]))
+      tasks.push(viewVideo(servers[2].url, localVideosServer3[1]))
 
       await Promise.all(tasks)
 
@@ -537,7 +526,7 @@ describe('Test multiple servers', function () {
         // Initialize base videos for future comparisons
         if (baseVideos === null) {
           baseVideos = videos
-          return
+          continue
         }
 
         for (const baseVideo of baseVideos) {
@@ -572,14 +561,14 @@ describe('Test multiple servers', function () {
         // Initialize base videos for future comparisons
         if (baseVideos === null) {
           baseVideos = videos
-          return
+          continue
         }
 
-        baseVideos.forEach(baseVideo => {
+        for (const baseVideo of baseVideos) {
           const sameVideo = videos.find(video => video.name === baseVideo.name)
           expect(baseVideo.likes).to.equal(sameVideo.likes)
           expect(baseVideo.dislikes).to.equal(sameVideo.dislikes)
-        })
+        }
       }
     })
   })
@@ -621,13 +610,13 @@ describe('Test multiple servers', function () {
         expect(videoUpdated.languageLabel).to.equal('French')
         expect(videoUpdated.nsfw).to.be.ok
         expect(videoUpdated.description).to.equal('my super description updated')
-        expect(videoUpdated.tags).to.deep.equal([ 'tag_up_1', 'tag_up_2' ])
         expect(dateIsValid(videoUpdated.updatedAt, 20000)).to.be.true
 
         const res2 = await getVideo(server.url, videoUpdated.uuid)
         const videoUpdatedDetails = res2.body
+        expect(videoUpdatedDetails.tags).to.deep.equal([ 'tag_up_1', 'tag_up_2' ])
 
-        const file = videoUpdatedDetails .files[0]
+        const file = videoUpdatedDetails.files[0]
         expect(file.magnetUri).to.have.lengthOf.above(2)
         expect(file.resolution).to.equal(720)
         expect(file.resolutionLabel).to.equal('720p')
@@ -680,7 +669,7 @@ describe('Test multiple servers', function () {
 
         if (baseVideo === null) {
           baseVideo = video
-          return
+          continue
         }
 
         expect(baseVideo.name).to.equal(video.name)
@@ -690,7 +679,7 @@ describe('Test multiple servers', function () {
         expect(baseVideo.licence).to.equal(video.licence)
         expect(baseVideo.category).to.equal(video.category)
         expect(baseVideo.nsfw).to.equal(video.nsfw)
-        expect(baseVideo.account).to.equal(video.account)
+        expect(baseVideo.accountName).to.equal(video.accountName)
         expect(baseVideo.tags).to.deep.equal(video.tags)
       }
     })
@@ -706,6 +695,158 @@ describe('Test multiple servers', function () {
     })
   })
 
+  describe('Should comment these videos', function () {
+    it('Should add comment (threads and replies)', async function () {
+      this.timeout(25000)
+
+      {
+        const text = 'my super first comment'
+        await addVideoCommentThread(servers[ 0 ].url, servers[ 0 ].accessToken, videoUUID, text)
+      }
+
+      {
+        const text = 'my super second comment'
+        await addVideoCommentThread(servers[ 2 ].url, servers[ 2 ].accessToken, videoUUID, text)
+      }
+
+      await wait(5000)
+
+      {
+        const res = await getVideoCommentThreads(servers[1].url, videoUUID, 0, 5)
+        const threadId = res.body.data.find(c => c.text === 'my super first comment').id
+
+        const text = 'my super answer to thread 1'
+        await addVideoCommentReply(servers[ 1 ].url, servers[ 1 ].accessToken, videoUUID, threadId, text)
+      }
+
+      await wait(5000)
+
+      {
+        const res1 = await getVideoCommentThreads(servers[2].url, videoUUID, 0, 5)
+        const threadId = res1.body.data.find(c => c.text === 'my super first comment').id
+
+        const res2 = await getVideoThreadComments(servers[2].url, videoUUID, threadId)
+        const childCommentId = res2.body.children[0].comment.id
+
+        const text3 = 'my second answer to thread 1'
+        await addVideoCommentReply(servers[ 2 ].url, servers[ 2 ].accessToken, videoUUID, threadId, text3)
+
+        const text2 = 'my super answer to answer of thread 1'
+        await addVideoCommentReply(servers[ 2 ].url, servers[ 2 ].accessToken, videoUUID, childCommentId, text2)
+      }
+
+      await wait(5000)
+    })
+
+    it('Should have these threads', async function () {
+      for (const server of servers) {
+        const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5)
+
+        expect(res.body.total).to.equal(2)
+        expect(res.body.data).to.be.an('array')
+        expect(res.body.data).to.have.lengthOf(2)
+
+        {
+          const comment: VideoComment = res.body.data.find(c => c.text === 'my super first comment')
+          expect(comment).to.not.be.undefined
+          expect(comment.inReplyToCommentId).to.be.null
+          expect(comment.account.name).to.equal('root')
+          expect(comment.account.host).to.equal('localhost:9001')
+          expect(comment.totalReplies).to.equal(3)
+          expect(dateIsValid(comment.createdAt as string)).to.be.true
+          expect(dateIsValid(comment.updatedAt as string)).to.be.true
+        }
+
+        {
+          const comment: VideoComment = res.body.data.find(c => c.text === 'my super second comment')
+          expect(comment).to.not.be.undefined
+          expect(comment.inReplyToCommentId).to.be.null
+          expect(comment.account.name).to.equal('root')
+          expect(comment.account.host).to.equal('localhost:9003')
+          expect(comment.totalReplies).to.equal(0)
+          expect(dateIsValid(comment.createdAt as string)).to.be.true
+          expect(dateIsValid(comment.updatedAt as string)).to.be.true
+        }
+      }
+    })
+
+    it('Should have these comments', async function () {
+      for (const server of servers) {
+        const res1 = await getVideoCommentThreads(server.url, videoUUID, 0, 5)
+        const threadId = res1.body.data.find(c => c.text === 'my super first comment').id
+
+        const res2 = await getVideoThreadComments(server.url, videoUUID, threadId)
+
+        const tree: VideoCommentThreadTree = res2.body
+        expect(tree.comment.text).equal('my super first comment')
+        expect(tree.comment.account.name).equal('root')
+        expect(tree.comment.account.host).equal('localhost:9001')
+        expect(tree.children).to.have.lengthOf(2)
+
+        const firstChild = tree.children[0]
+        expect(firstChild.comment.text).to.equal('my super answer to thread 1')
+        expect(firstChild.comment.account.name).equal('root')
+        expect(firstChild.comment.account.host).equal('localhost:9002')
+        expect(firstChild.children).to.have.lengthOf(1)
+
+        const childOfFirstChild = firstChild.children[0]
+        expect(childOfFirstChild.comment.text).to.equal('my super answer to answer of thread 1')
+        expect(childOfFirstChild.comment.account.name).equal('root')
+        expect(childOfFirstChild.comment.account.host).equal('localhost:9003')
+        expect(childOfFirstChild.children).to.have.lengthOf(0)
+
+        const secondChild = tree.children[1]
+        expect(secondChild.comment.text).to.equal('my second answer to thread 1')
+        expect(secondChild.comment.account.name).equal('root')
+        expect(secondChild.comment.account.host).equal('localhost:9003')
+        expect(secondChild.children).to.have.lengthOf(0)
+      }
+    })
+  })
+
+  describe('With minimum parameters', function () {
+    it('Should upload and propagate the video', async function () {
+      this.timeout(50000)
+
+      const path = '/api/v1/videos/upload'
+
+      const req = request(servers[1].url)
+        .post(path)
+        .set('Accept', 'application/json')
+        .set('Authorization', 'Bearer ' + servers[1].accessToken)
+        .field('name', 'minimum parameters')
+        .field('privacy', '1')
+        .field('nsfw', 'false')
+        .field('channelId', '1')
+
+      const filePath = join(__dirname, '..', 'api', 'fixtures', 'video_short.webm')
+
+      await req.attach('videofile', filePath)
+        .expect(200)
+
+      await wait(25000)
+
+      for (const server of servers) {
+        const res = await getVideosList(server.url)
+        const video = res.body.data.find(v => v.name === 'minimum parameters')
+
+        expect(video.name).to.equal('minimum parameters')
+        expect(video.category).to.equal(null)
+        expect(video.categoryLabel).to.equal('Misc')
+        expect(video.licence).to.equal(null)
+        expect(video.licenceLabel).to.equal('Unknown')
+        expect(video.language).to.equal(null)
+        expect(video.languageLabel).to.equal('Unknown')
+        expect(video.nsfw).to.not.be.ok
+        expect(video.description).to.equal(null)
+        expect(video.serverHost).to.equal('localhost:9002')
+        expect(video.accountName).to.equal('root')
+        expect(dateIsValid(video.createdAt)).to.be.true
+        expect(dateIsValid(video.updatedAt)).to.be.true
+      }
+    })
+  })
+
   after(async function () {
     killallServers(servers)