Add tests to handle down server
authorChocobozzz <me@florianbigard.com>
Thu, 11 Jan 2018 10:40:18 +0000 (11:40 +0100)
committerChocobozzz <me@florianbigard.com>
Thu, 11 Jan 2018 10:47:44 +0000 (11:47 +0100)
12 files changed:
server/controllers/api/server/follows.ts
server/initializers/constants.ts
server/lib/activitypub/process/process-accept.ts
server/lib/activitypub/process/process-create.ts
server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-job-scheduler.ts
server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-unicast-handler.ts
server/models/activitypub/actor.ts
server/tests/api/index-slow.ts
server/tests/api/server/handle-down.ts
server/tests/api/videos/video-channels.ts
server/tests/utils/server/servers.ts
server/tests/utils/videos/videos.ts

index c87107197e585f9313a3752882a3aac2d7396c3f..b93d8a816189c71ac57503dca20bbd32cabe99f3 100644 (file)
@@ -124,9 +124,7 @@ function follow (fromActor: ActorModel, targetActor: ActorModel) {
     actorFollow.ActorFollower = fromActor
 
     // Send a notification to remote server
-    if (actorFollow.state === 'pending') {
-      await sendFollow(actorFollow, t)
-    }
+    await sendFollow(actorFollow, t)
   })
 }
 
index 0c139912c5eead720c2211a82a23107d13adf660..a3c128d1fe744bd79917cccf20933d10497b95ce 100644 (file)
@@ -283,7 +283,7 @@ const ACTIVITY_PUB = {
     MAGNET: [ 'application/x-bittorrent;x-scheme-handler/magnet' ]
   },
   MAX_RECURSION_COMMENTS: 100,
-  ACTOR_REFRESH_INTERVAL: 3600 * 24 // 1 day
+  ACTOR_REFRESH_INTERVAL: 3600 * 24 * 1000 // 1 day
 }
 
 const ACTIVITY_PUB_ACTOR_TYPES: { [ id: string ]: ActivityPubActorType } = {
@@ -355,7 +355,7 @@ if (isTestInstance() === true) {
   REMOTE_SCHEME.WS = 'ws'
   STATIC_MAX_AGE = '0'
   ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE = 2
-  ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL = 60 // 1 minute
+  ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL = 10 * 1000 // 10 seconds
   CONSTRAINTS_FIELDS.ACTORS.AVATAR.FILE_SIZE.max = 100 * 1024 // 100KB
   SCHEDULER_INTERVAL = 10000
 }
index b9d906ec94c7afe65e9747509ae672e32e444753..551f09ea73394fa4210c5faa590e2e93755e8ea6 100644 (file)
@@ -23,7 +23,9 @@ async function processAccept (actor: ActorModel, targetActor: ActorModel) {
   const follow = await ActorFollowModel.loadByActorAndTarget(actor.id, targetActor.id)
   if (!follow) throw new Error('Cannot find associated follow.')
 
-  follow.set('state', 'accepted')
-  await follow.save()
-  await addFetchOutboxJob(targetActor, undefined)
+  if (follow.state !== 'accepted') {
+    follow.set('state', 'accepted')
+    await follow.save()
+    await addFetchOutboxJob(targetActor, undefined)
+  }
 }
index 08d61996a2ff7bac2bfe08cb689b8b037f33c158..442cd5c0b8797f9f8c6c73b7b5842740d316ea1e 100644 (file)
@@ -147,8 +147,8 @@ async function processCreateView (byActor: ActorModel, activity: ActivityCreate)
 
   const { video } = await getOrCreateAccountAndVideoAndChannel(view.object)
 
-  const account = await ActorModel.loadByUrl(view.actor)
-  if (!account) throw new Error('Unknown account ' + view.actor)
+  const actor = await ActorModel.loadByUrl(view.actor)
+  if (!actor) throw new Error('Unknown actor ' + view.actor)
 
   await video.increment('views')
 
index 884ede5a37e1d07fb94218647cdba530898f7d3f..10423a7df52b828ac98bdf659113a62b1b200504 100644 (file)
@@ -4,7 +4,6 @@ import { logger } from '../../../helpers/logger'
 import { getServerActor } from '../../../helpers/utils'
 import { ACTIVITY_PUB } from '../../../initializers'
 import { ActorModel } from '../../../models/activitypub/actor'
-import { ActorFollowModel } from '../../../models/activitypub/actor-follow'
 import { JobHandler, JobScheduler } from '../job-scheduler'
 
 import * as activitypubHttpBroadcastHandler from './activitypub-http-broadcast-handler'
index e02bd698e53bbcddcfb035db5aa80986e28a67fc..deedf8402aad1dfcf644dc3284cd9acb4db60685 100644 (file)
@@ -19,11 +19,11 @@ async function process (payload: ActivityPubHttpPayload, jobId: number) {
 
   try {
     await doRequest(options)
-    await ActorFollowModel.updateActorFollowsScoreAndRemoveBadOnes([ uri ], [], undefined)
+    ActorFollowModel.updateActorFollowsScoreAndRemoveBadOnes([ uri ], [], undefined)
   } catch (err) {
     const isRetryingLater = await maybeRetryRequestLater(err, payload, uri)
     if (isRetryingLater === false) {
-      await ActorFollowModel.updateActorFollowsScoreAndRemoveBadOnes([], [ uri ], undefined)
+      ActorFollowModel.updateActorFollowsScoreAndRemoveBadOnes([], [ uri ], undefined)
     }
 
     throw err
index 912d8d748b97b1319ce85530efb2aec7723da7d4..6c74aa61b38ec295bccb7c4482a2d3185c55db06 100644 (file)
@@ -41,11 +41,11 @@ enum ScopeNames {
   [ScopeNames.FULL]: {
     include: [
       {
-        model: () => AccountModel,
+        model: () => AccountModel.unscoped(),
         required: false
       },
       {
-        model: () => VideoChannelModel,
+        model: () => VideoChannelModel.unscoped(),
         required: false
       },
       {
index fe86fc01859fc617cd90a817afd0a058919190f1..0082bcb561bf1aa7785dbcc73918a478d924f8a0 100644 (file)
@@ -6,3 +6,4 @@ import './server/follows'
 import './server/jobs'
 import './videos/video-comments'
 import './users/users-multiple-servers'
+import './server/handle-down'
index f8ba8bd6648252ea911f3d4f097dc3f607a58a02..cc1ff9a9fd799ffe4926a83a71cd222b8d4340cd 100644 (file)
@@ -3,7 +3,8 @@
 import * as chai from 'chai'
 import 'mocha'
 import { VideoPrivacy } from '../../../../shared/models/videos'
-import { completeVideoCheck, runServer, viewVideo } from '../../utils'
+import { VideoCommentThreadTree } from '../../../../shared/models/videos/video-comment.model'
+import { completeVideoCheck, getVideo, immutableAssign, reRunServer, viewVideo } from '../../utils'
 
 import {
   flushAndRunMultipleServers, flushTests, getVideosList, killallServers, ServerInfo, setAccessTokensToServers, uploadVideo,
@@ -11,11 +12,20 @@ import {
 } from '../../utils/index'
 import { follow, getFollowersListPaginationAndSort } from '../../utils/server/follows'
 import { getJobsListPaginationAndSort } from '../../utils/server/jobs'
+import {
+  addVideoCommentReply, addVideoCommentThread, getVideoCommentThreads,
+  getVideoThreadComments
+} from '../../utils/videos/video-comments'
 
 const expect = chai.expect
 
 describe('Test handle downs', function () {
   let servers: ServerInfo[] = []
+  const videos = []
+  let threadIdServer1: number
+  let threadIdServer2: number
+  let commentIdServer1: number
+  let commentIdServer2: number
 
   const videoAttributes = {
     name: 'my super name for server 1',
@@ -23,11 +33,16 @@ describe('Test handle downs', function () {
     licence: 4,
     language: 9,
     nsfw: true,
+    privacy: VideoPrivacy.PUBLIC,
     description: 'my super description for server 1',
     tags: [ 'tag1p1', 'tag2p1' ],
     fixture: 'video_short1.webm'
   }
 
+  const unlistedVideoAttributes = immutableAssign(videoAttributes, {
+    privacy: VideoPrivacy.UNLISTED
+  })
+
   const checkAttributes = {
     name: 'my super name for server 1',
     category: 5,
@@ -56,6 +71,10 @@ describe('Test handle downs', function () {
     ]
   }
 
+  const unlistedCheckAttributes = immutableAssign(checkAttributes, {
+    privacy: VideoPrivacy.UNLISTED
+  })
+
   before(async function () {
     this.timeout(20000)
 
@@ -85,9 +104,27 @@ describe('Test handle downs', function () {
     // Kill server 1
     killallServers([ servers[1] ])
 
+    let resVideo = await uploadVideo(servers[ 0 ].url, servers[ 0 ].accessToken, unlistedVideoAttributes)
+    videos.push(resVideo.body.video)
+
     // Remove server 2 follower
     for (let i = 0; i < 10; i++) {
-      await uploadVideo(servers[ 0 ].url, servers[ 0 ].accessToken, videoAttributes)
+      resVideo = await uploadVideo(servers[ 0 ].url, servers[ 0 ].accessToken, videoAttributes)
+      videos.push(resVideo.body.video)
+    }
+
+    // Add comments to video 2
+    {
+      const text = 'thread 1'
+      let resComment = await addVideoCommentThread(servers[0].url, servers[0].accessToken, videos[1].uuid, text)
+      let comment = resComment.body.comment
+      threadIdServer1 = comment.id
+
+      resComment = await addVideoCommentReply(servers[0].url, servers[0].accessToken, videos[1].uuid, comment.id, 'comment 1-1')
+      comment = resComment.body.comment
+
+      resComment = await addVideoCommentReply(servers[0].url, servers[0].accessToken, videos[1].uuid, comment.id, 'comment 1-2')
+      commentIdServer1 = resComment.body.comment.id
     }
 
     await wait(10000)
@@ -108,7 +145,9 @@ describe('Test handle downs', function () {
   })
 
   it('Should follow server 1', async function () {
-    servers[1] = await runServer(2)
+    this.timeout(15000)
+
+    await reRunServer(servers[1])
 
     await follow(servers[1].url, [ servers[0].url ], servers[1].accessToken)
 
@@ -120,19 +159,98 @@ describe('Test handle downs', function () {
   })
 
   it('Should send a view to server 2, and automatically fetch the video', async function () {
-    const resVideo = await getVideosList(servers[0].url)
-    const videoServer1 = resVideo.body.data[0]
+    this.timeout(15000)
 
-    await viewVideo(servers[0].url, videoServer1.uuid)
+    await viewVideo(servers[0].url, videos[0].uuid)
 
     await wait(5000)
 
     const res = await getVideosList(servers[1].url)
-    const videoServer2 = res.body.data.find(v => v.url === videoServer1.url)
+    expect(res.body.data).to.be.an('array')
+    // Video is unlisted
+    expect(res.body.data).to.have.lengthOf(1)
+
+    const resVideo = await getVideo(servers[1].url, videos[0].uuid)
+    expect(resVideo.body).not.to.be.undefined
+
+    await completeVideoCheck(servers[1].url, resVideo.body, unlistedCheckAttributes)
+  })
+
+  it('Should send comments on a video to server 2, and automatically fetch the video', async function () {
+    this.timeout(25000)
 
-    expect(videoServer2).not.to.be.undefined
+    await addVideoCommentReply(servers[0].url, servers[0].accessToken, videos[1].uuid, commentIdServer1, 'comment 1-3')
+
+    await wait(5000)
+
+    const res = await getVideosList(servers[1].url)
+    expect(res.body.data).to.be.an('array')
+    expect(res.body.data).to.have.lengthOf(2)
 
-    await completeVideoCheck(servers[1].url, videoServer2, checkAttributes)
+    const resVideo = await getVideo(servers[1].url, videos[0].uuid)
+    expect(resVideo.body).not.to.be.undefined
+
+    await completeVideoCheck(servers[1].url, resVideo.body, unlistedCheckAttributes)
+
+    {
+      let resComment = await getVideoCommentThreads(servers[1].url, videos[1].uuid, 0, 5)
+      expect(resComment.body.data).to.be.an('array')
+      expect(resComment.body.data).to.have.lengthOf(1)
+
+      threadIdServer2 = resComment.body.data[0].id
+
+      resComment = await getVideoThreadComments(servers[1].url, videos[1].uuid, threadIdServer2)
+
+      const tree: VideoCommentThreadTree = resComment.body
+      expect(tree.comment.text).equal('thread 1')
+      expect(tree.children).to.have.lengthOf(1)
+
+      const firstChild = tree.children[0]
+      expect(firstChild.comment.text).to.equal('comment 1-1')
+      expect(firstChild.children).to.have.lengthOf(1)
+
+      const childOfFirstChild = firstChild.children[0]
+      expect(childOfFirstChild.comment.text).to.equal('comment 1-2')
+      expect(childOfFirstChild.children).to.have.lengthOf(1)
+
+      const childOfChildFirstChild = childOfFirstChild.children[0]
+      expect(childOfChildFirstChild.comment.text).to.equal('comment 1-3')
+      expect(childOfChildFirstChild.children).to.have.lengthOf(0)
+
+      commentIdServer2 = childOfChildFirstChild.comment.id
+    }
+  })
+
+  it('Should correctly reply to the comment', async function () {
+    this.timeout(15000)
+
+    await addVideoCommentReply(servers[1].url, servers[1].accessToken, videos[1].uuid, commentIdServer2, 'comment 1-4')
+
+    await wait(5000)
+
+    {
+      const resComment = await getVideoThreadComments(servers[0].url, videos[1].uuid, threadIdServer1)
+
+      const tree: VideoCommentThreadTree = resComment.body
+      expect(tree.comment.text).equal('thread 1')
+      expect(tree.children).to.have.lengthOf(1)
+
+      const firstChild = tree.children[0]
+      expect(firstChild.comment.text).to.equal('comment 1-1')
+      expect(firstChild.children).to.have.lengthOf(1)
+
+      const childOfFirstChild = firstChild.children[0]
+      expect(childOfFirstChild.comment.text).to.equal('comment 1-2')
+      expect(childOfFirstChild.children).to.have.lengthOf(1)
+
+      const childOfChildFirstChild = childOfFirstChild.children[0]
+      expect(childOfChildFirstChild.comment.text).to.equal('comment 1-3')
+      expect(childOfChildFirstChild.children).to.have.lengthOf(1)
+
+      const childOfChildOfChildOfFirstChild = childOfChildFirstChild.children[0]
+      expect(childOfChildOfChildOfFirstChild.comment.text).to.equal('comment 1-4')
+      expect(childOfChildOfChildOfFirstChild.children).to.have.lengthOf(0)
+    }
   })
 
   after(async function () {
index 454a96da6973b29149fa32c68d358250fbcc0a04..8d33c602590c6e4f4856644cd2b30d33571872ed 100644 (file)
@@ -59,8 +59,8 @@ describe('Test a video channels', function () {
     expect(userInfo.videoChannels).to.have.lengthOf(2)
 
     const videoChannels = userInfo.videoChannels
-    expect(videoChannels[0].name).to.equal('Default root channel')
-    expect(videoChannels[1].name).to.equal('second video channel')
+    expect(videoChannels[0].displayName).to.equal('Default root channel')
+    expect(videoChannels[1].displayName).to.equal('second video channel')
     expect(videoChannels[1].description).to.equal('super video channel description')
   })
 
@@ -72,8 +72,8 @@ describe('Test a video channels', function () {
     expect(res.body.data).to.have.lengthOf(2)
 
     const videoChannels = res.body.data
-    expect(videoChannels[0].name).to.equal('Default root channel')
-    expect(videoChannels[1].name).to.equal('second video channel')
+    expect(videoChannels[0].displayName).to.equal('Default root channel')
+    expect(videoChannels[1].displayName).to.equal('second video channel')
     expect(videoChannels[1].description).to.equal('super video channel description')
 
     videoChannelId = videoChannels[1].id
@@ -85,7 +85,7 @@ describe('Test a video channels', function () {
     expect(res.body.total).to.equal(2)
     expect(res.body.data).to.be.an('array')
     expect(res.body.data).to.have.lengthOf(1)
-    expect(res.body.data[0].name).to.equal('Default root channel')
+    expect(res.body.data[0].displayName).to.equal('Default root channel')
   })
 
   it('Should update video channel', async () => {
@@ -103,7 +103,7 @@ describe('Test a video channels', function () {
     expect(res.body.total).to.equal(2)
     expect(res.body.data).to.be.an('array')
     expect(res.body.data).to.have.lengthOf(1)
-    expect(res.body.data[0].name).to.equal('video channel updated')
+    expect(res.body.data[0].displayName).to.equal('video channel updated')
     expect(res.body.data[0].description).to.equal('video channel description updated')
   })
 
@@ -111,7 +111,7 @@ describe('Test a video channels', function () {
     const res = await getVideoChannel(server.url, videoChannelId)
 
     const videoChannel = res.body
-    expect(videoChannel.name).to.equal('video channel updated')
+    expect(videoChannel.displayName).to.equal('video channel updated')
     expect(videoChannel.description).to.equal('video channel description updated')
   })
 
@@ -125,7 +125,7 @@ describe('Test a video channels', function () {
     expect(res.body.total).to.equal(1)
     expect(res.body.data).to.be.an('array')
     expect(res.body.data).to.have.lengthOf(1)
-    expect(res.body.data[0].name).to.equal('Default root channel')
+    expect(res.body.data[0].displayName).to.equal('Default root channel')
   })
 
   after(async function () {
index 4add2f69a2eb73414587c5837ec720b7633c6b38..878efe91a6f02e8af58f2432372a66e759ca3c2b 100644 (file)
@@ -145,6 +145,13 @@ function runServer (serverNumber: number, configOverride?: Object) {
   })
 }
 
+async function reRunServer (server: ServerInfo) {
+  const newServer = await runServer(server.serverNumber)
+  server.app = newServer.app
+
+  return server
+}
+
 function killallServers (servers: ServerInfo[]) {
   for (const server of servers) {
     process.kill(-server.app.pid)
@@ -158,5 +165,6 @@ export {
   flushAndRunMultipleServers,
   flushTests,
   runServer,
-  killallServers
+  killallServers,
+  reRunServer
 }
index c437c21b2e471f5017ac7cccc2a4da0d6d4162df..dc132721550c55fa0c508971d54b5018e5a23512 100644 (file)
@@ -383,7 +383,8 @@ async function completeVideoCheck (
   expect(videoDetails.account.name).to.equal(attributes.account)
   expect(videoDetails.commentsEnabled).to.equal(attributes.commentsEnabled)
 
-  expect(videoDetails.channel.name).to.equal(attributes.channel.name)
+  expect(videoDetails.channel.displayName).to.equal(attributes.channel.name)
+  expect(videoDetails.channel.name).to.have.lengthOf(36)
   expect(videoDetails.channel.isLocal).to.equal(attributes.channel.isLocal)
   expect(dateIsValid(videoDetails.channel.createdAt)).to.be.true
   expect(dateIsValid(videoDetails.channel.updatedAt)).to.be.true