]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/tests/api/server/auto-follows.ts
Deprecate old static routes
[github/Chocobozzz/PeerTube.git] / server / tests / api / server / auto-follows.ts
index 32ad259c9cc3227bad1c074c543a936cd9ea47cd..e04d70af4a44dc77d6ca3576181566d0e97ed3af 100644 (file)
@@ -1,4 +1,4 @@
-/* tslint:disable:no-unused-expression */
+/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
 
 import * as chai from 'chai'
 import 'mocha'
@@ -6,10 +6,12 @@ import {
   acceptFollower,
   cleanupTests,
   flushAndRunMultipleServers,
+  MockInstancesIndex,
   ServerInfo,
   setAccessTokensToServers,
   unfollow,
-  updateCustomSubConfig
+  updateCustomSubConfig,
+  wait
 } from '../../../../shared/extra-utils/index'
 import { follow, getFollowersListPaginationAndSort, getFollowingListPaginationAndSort } from '../../../../shared/extra-utils/server/follows'
 import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
@@ -19,30 +21,32 @@ const expect = chai.expect
 
 async function checkFollow (follower: ServerInfo, following: ServerInfo, exists: boolean) {
   {
-    const res = await getFollowersListPaginationAndSort(following.url, 0, 5, '-createdAt')
+    const res = await getFollowersListPaginationAndSort({ url: following.url, start: 0, count: 5, sort: '-createdAt' })
     const follows = res.body.data as ActorFollow[]
 
-    if (exists === true) {
-      expect(res.body.total).to.equal(1)
+    const follow = follows.find(f => {
+      return f.follower.host === follower.host && f.state === 'accepted'
+    })
 
-      expect(follows[ 0 ].follower.host).to.equal(follower.host)
-      expect(follows[ 0 ].state).to.equal('accepted')
+    if (exists === true) {
+      expect(follow).to.exist
     } else {
-      expect(follows.filter(f => f.state === 'accepted')).to.have.lengthOf(0)
+      expect(follow).to.be.undefined
     }
   }
 
   {
-    const res = await getFollowingListPaginationAndSort(follower.url, 0, 5, '-createdAt')
+    const res = await getFollowingListPaginationAndSort({ url: follower.url, start: 0, count: 5, sort: '-createdAt' })
     const follows = res.body.data as ActorFollow[]
 
-    if (exists === true) {
-      expect(res.body.total).to.equal(1)
+    const follow = follows.find(f => {
+      return f.following.host === following.host && f.state === 'accepted'
+    })
 
-      expect(follows[ 0 ].following.host).to.equal(following.host)
-      expect(follows[ 0 ].state).to.equal('accepted')
+    if (exists === true) {
+      expect(follow).to.exist
     } else {
-      expect(follows.filter(f => f.state === 'accepted')).to.have.lengthOf(0)
+      expect(follow).to.be.undefined
     }
   }
 }
@@ -55,9 +59,10 @@ async function server1Follows2 (servers: ServerInfo[]) {
 
 async function resetFollows (servers: ServerInfo[]) {
   try {
-    await unfollow(servers[ 0 ].url, servers[ 0 ].accessToken, servers[ 1 ])
-    await unfollow(servers[ 1 ].url, servers[ 1 ].accessToken, servers[ 0 ])
-  } catch { /* empty */ }
+    await unfollow(servers[0].url, servers[0].accessToken, servers[1])
+    await unfollow(servers[1].url, servers[1].accessToken, servers[0])
+  } catch { /* empty */
+  }
 
   await waitJobs(servers)
 
@@ -71,7 +76,7 @@ describe('Test auto follows', function () {
   before(async function () {
     this.timeout(30000)
 
-    servers = await flushAndRunMultipleServers(2)
+    servers = await flushAndRunMultipleServers(3)
 
     // Get the access tokens
     await setAccessTokensToServers(servers)
@@ -139,6 +144,65 @@ describe('Test auto follows', function () {
       await checkFollow(servers[1], servers[0], true)
 
       await resetFollows(servers)
+
+      config.followings.instance.autoFollowBack.enabled = false
+      config.followers.instance.manualApproval = false
+      await updateCustomSubConfig(servers[1].url, servers[1].accessToken, config)
+    })
+  })
+
+  describe('Auto follow index', function () {
+    const instanceIndexServer = new MockInstancesIndex()
+
+    before(async () => {
+      await instanceIndexServer.initialize()
+    })
+
+    it('Should not auto follow index if the option is not enabled', async function () {
+      this.timeout(30000)
+
+      await wait(5000)
+      await waitJobs(servers)
+
+      await checkFollow(servers[0], servers[1], false)
+      await checkFollow(servers[1], servers[0], false)
+    })
+
+    it('Should auto follow the index', async function () {
+      this.timeout(30000)
+
+      instanceIndexServer.addInstance(servers[1].host)
+
+      const config = {
+        followings: {
+          instance: {
+            autoFollowIndex: {
+              indexUrl: 'http://localhost:42101/api/v1/instances/hosts',
+              enabled: true
+            }
+          }
+        }
+      }
+      await updateCustomSubConfig(servers[0].url, servers[0].accessToken, config)
+
+      await wait(5000)
+      await waitJobs(servers)
+
+      await checkFollow(servers[0], servers[1], true)
+
+      await resetFollows(servers)
+    })
+
+    it('Should follow new added instances in the index but not old ones', async function () {
+      this.timeout(30000)
+
+      instanceIndexServer.addInstance(servers[2].host)
+
+      await wait(5000)
+      await waitJobs(servers)
+
+      await checkFollow(servers[0], servers[1], false)
+      await checkFollow(servers[0], servers[2], true)
     })
   })