Add comments federation tests
authorChocobozzz <me@florianbigard.com>
Wed, 27 Dec 2017 19:03:37 +0000 (20:03 +0100)
committerChocobozzz <me@florianbigard.com>
Wed, 27 Dec 2017 19:03:37 +0000 (20:03 +0100)
client/src/app/videos/+video-watch/comment/video-comment-add.component.ts
server/lib/activitypub/videos.ts
server/lib/cache/videos-preview-cache.ts
server/middlewares/validators/video-channels.ts
server/models/video/video-comment.ts
server/tests/api/check-params/video-channels.ts
server/tests/api/check-params/video-comments.ts
server/tests/api/multiple-servers.ts
server/tests/api/video-comments.ts
server/tests/utils/video-comments.ts

index 5ad83fc47623fb0b116aeb76a63b061525bb3000..d05232202e32a5b756050870080f8483f2bc96a0 100644 (file)
@@ -66,7 +66,7 @@ export class VideoCommentAddComponent extends FormReactive implements OnInit {
 
       err => this.notificationsService.error('Error', err.text)
     )
-}
+  }
 
   isAddButtonDisplayed () {
     return this.form.value['text']
index fab43757a443a1fa713fd9e722ed4b0e2700d276..ded854ee135034c8110d6915e3b73d07eff1c0fe 100644 (file)
@@ -17,12 +17,12 @@ import {
   sendUndoLikeToVideoFollowers
 } from './send'
 
-function fetchRemoteVideoPreview (video: VideoModel) {
+function fetchRemoteVideoPreview (video: VideoModel, reject: Function) {
   // FIXME: use url
   const host = video.VideoChannel.Account.Actor.Server.host
   const path = join(STATIC_PATHS.PREVIEWS, video.getPreviewName())
 
-  return request.get(REMOTE_SCHEME.HTTP + '://' + host + path)
+  return request.get(REMOTE_SCHEME.HTTP + '://' + host + path, err => reject(err))
 }
 
 async function fetchRemoteVideoDescription (video: VideoModel) {
index 0eb43efcc03dc10c7aab65a929b1e64bfa2f1734..ea959076d88c7614b0e4e7dac4f981029bf1eea0 100644 (file)
@@ -52,21 +52,19 @@ class VideosPreviewCache {
 
     if (video.isOwned()) throw new Error('Cannot load preview of owned video.')
 
-    const res = await this.saveRemotePreviewAndReturnPath(video)
-
-    return res
+    return this.saveRemotePreviewAndReturnPath(video)
   }
 
   private saveRemotePreviewAndReturnPath (video: VideoModel) {
-    const req = fetchRemoteVideoPreview(video)
 
     return new Promise<string>((res, rej) => {
+      const req = fetchRemoteVideoPreview(video, rej)
       const path = join(CACHE.DIRECTORIES.PREVIEWS, video.getPreviewName())
       const stream = createWriteStream(path)
 
       req.pipe(stream)
-         .on('finish', () => res(path))
-         .on('error', (err) => rej(err))
+        .on('error', (err) => rej(err))
+        .on('finish', () => res(path))
     })
   }
 }
index cc7d54c0672d6baa264de4b949272c65a2fe745b..0e6eff493d71e7003c7452ae08ed21e8881952c5 100644 (file)
@@ -51,7 +51,7 @@ const videoChannelsUpdateValidator = [
     if (!await isVideoChannelExist(req.params.id, res)) return
 
     // We need to make additional checks
-    if (res.locals.videoChannel.isOwned() === false) {
+    if (res.locals.videoChannel.Actor.isOwned() === false) {
       return res.status(403)
         .json({ error: 'Cannot update video channel of another server' })
         .end()
index a3e8c48d4eb464bfaa104e96715a4c850f82da1c..8ceeb563add9748866355f0dfa06779bdc21c7c3 100644 (file)
@@ -212,7 +212,7 @@ export class VideoCommentModel extends Model<VideoCommentModel> {
       url: this.url,
       text: this.text,
       threadId: this.originCommentId || this.id,
-      inReplyToCommentId: this.inReplyToCommentId,
+      inReplyToCommentId: this.inReplyToCommentId || null,
       videoId: this.videoId,
       createdAt: this.createdAt,
       updatedAt: this.updatedAt,
index e72b2cb6c20ec24a65c9f1ba22f7cc09fa823e48..7103aec25700f1dc26d031e0c4c107be36b98890 100644 (file)
@@ -69,9 +69,9 @@ describe('Test videos API validator', function () {
     })
   })
 
-  describe('When listing author video channels', function () {
-    it('Should fail with bad author', async function () {
-      const path = '/api/v1/videos/authors/hello/channels'
+  describe('When listing account video channels', function () {
+    it('Should fail with bad account', async function () {
+      const path = '/api/v1/videos/accounts/hello/channels'
 
       await request(server.url)
         .get(path)
@@ -79,8 +79,8 @@ describe('Test videos API validator', function () {
         .expect(400)
     })
 
-    it('Should fail with a unknown author', async function () {
-      const path = '/api/v1/videos/authors/156/channels'
+    it('Should fail with a unknown account', async function () {
+      const path = '/api/v1/videos/accounts/156/channels'
 
       await request(server.url)
         .get(path)
index 5112274f0f8372b12ee1c7c0eda955682d93f207..e8d7ddf382362df6a384b1acd72fb4230c851f9d 100644 (file)
@@ -2,17 +2,13 @@
 
 import 'mocha'
 import * as request from 'supertest'
-import {
-  createUser, flushTests, getUserAccessToken, killallServers, makePostBodyRequest, runServer, ServerInfo, setAccessTokensToServers,
-  uploadVideo
-} from '../../utils'
+import { flushTests, killallServers, makePostBodyRequest, runServer, ServerInfo, setAccessTokensToServers, uploadVideo } from '../../utils'
 import { addVideoCommentThread } from '../../utils/video-comments'
 
 describe('Test video comments API validator', function () {
   let pathThread: string
   let pathComment: string
   let server: ServerInfo
-  let accessTokenUser: string
   let videoUUID: string
   let commentId: number
 
@@ -27,16 +23,6 @@ describe('Test video comments API validator', function () {
 
     await setAccessTokensToServers([ server ])
 
-    {
-      const user = {
-        username: 'fake',
-        password: 'fake_password'
-      }
-      await createUser(server.url, server.accessToken, user.username, user.password)
-
-      accessTokenUser = await getUserAccessToken(server, user)
-    }
-
     {
       const res = await uploadVideo(server.url, server.accessToken, {})
       videoUUID = res.body.video.uuid
index 6fe1fb651a154b1f92113dbecd458a3f13c5afa1..06274d4cc11dba5530979003f0675400377a130a 100644 (file)
@@ -1,32 +1,18 @@
 /* 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
@@ -709,6 +695,115 @@ 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)
index 2c7d1c6e2d9ea16a795c15c0f64734204def2c1d..f05ca5e817a24c3c4a26cb2977ffacbe57622df9 100644 (file)
@@ -40,7 +40,7 @@ describe('Test video comments', function () {
     const text = 'my super first comment'
 
     const res = await addVideoCommentThread(server.url, server.accessToken, videoUUID, text)
-    const comment = res.body
+    const comment = res.body.comment
 
     expect(comment.inReplyToCommentId).to.be.null
     expect(comment.text).equal('my super first comment')
@@ -133,9 +133,9 @@ describe('Test video comments', function () {
     expect(res.body.data).to.have.lengthOf(3)
 
     expect(res.body.data[0].text).to.equal('my super first comment')
-    expect(res.body.data[0].totalReplies).to.equal(2)
+    expect(res.body.data[0].totalReplies).to.equal(3)
     expect(res.body.data[1].text).to.equal('super thread 2')
-    expect(res.body.data[1].totalReplies).to.equal(1)
+    expect(res.body.data[1].totalReplies).to.equal(0)
     expect(res.body.data[2].text).to.equal('super thread 3')
     expect(res.body.data[2].totalReplies).to.equal(0)
   })
index 6a6e225f33fa0b63efc6957d439740bd1daa43ad..8781470496d6d091267c900cd5ca952c0dc85673 100644 (file)
@@ -1,6 +1,6 @@
 import * as request from 'supertest'
 
-function getVideoCommentThreads (url: string, videoId: number, start: number, count: number, sort?: string) {
+function getVideoCommentThreads (url: string, videoId: number | string, start: number, count: number, sort?: string) {
   const path = '/api/v1/videos/' + videoId + '/comment-threads'
 
   const req = request(url)
@@ -15,7 +15,7 @@ function getVideoCommentThreads (url: string, videoId: number, start: number, co
     .expect('Content-Type', /json/)
 }
 
-function getVideoThreadComments (url: string, videoId: number, threadId: number) {
+function getVideoThreadComments (url: string, videoId: number | string, threadId: number) {
   const path = '/api/v1/videos/' + videoId + '/comment-threads/' + threadId
 
   return request(url)
@@ -39,7 +39,7 @@ function addVideoCommentThread (url: string, token: string, videoId: number | st
 function addVideoCommentReply (
   url: string,
   token: string,
-  videoId: number,
+  videoId: number | string,
   inReplyToCommentId: number,
   text: string,
   expectedStatus = 200