]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/tests/api/server/follows-moderation.ts
Add ability for instances to follow any actor
[github/Chocobozzz/PeerTube.git] / server / tests / api / server / follows-moderation.ts
index b1cbfb62ca07e8190b1249f74168c3912da6c1cd..921f510435cf3131e6bd138b4481c79f24660ce9 100644 (file)
-/* tslint:disable:no-unused-expression */
+/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
 
-import * as chai from 'chai'
 import 'mocha'
-import { flushAndRunMultipleServers, killallServers, ServerInfo, setAccessTokensToServers } from '../../../../shared/utils/index'
+import * as chai from 'chai'
 import {
-  follow,
-  getFollowersListPaginationAndSort,
-  getFollowingListPaginationAndSort,
-  removeFollower
-} from '../../../../shared/utils/server/follows'
-import { waitJobs } from '../../../../shared/utils/server/jobs'
-import { ActorFollow } from '../../../../shared/models/actors'
+  cleanupTests,
+  createMultipleServers,
+  FollowsCommand,
+  PeerTubeServer,
+  setAccessTokensToServers,
+  waitJobs
+} from '@shared/extra-utils'
 
 const expect = chai.expect
 
+async function checkServer1And2HasFollowers (servers: PeerTubeServer[], state = 'accepted') {
+  const fns = [
+    servers[0].follows.getFollowings.bind(servers[0].follows),
+    servers[1].follows.getFollowers.bind(servers[1].follows)
+  ]
+
+  for (const fn of fns) {
+    const body = await fn({ start: 0, count: 5, sort: 'createdAt' })
+    expect(body.total).to.equal(1)
+
+    const follow = body.data[0]
+    expect(follow.state).to.equal(state)
+    expect(follow.follower.url).to.equal('http://localhost:' + servers[0].port + '/accounts/peertube')
+    expect(follow.following.url).to.equal('http://localhost:' + servers[1].port + '/accounts/peertube')
+  }
+}
+
+async function checkNoFollowers (servers: PeerTubeServer[]) {
+  const fns = [
+    servers[0].follows.getFollowings.bind(servers[0].follows),
+    servers[1].follows.getFollowers.bind(servers[1].follows)
+  ]
+
+  for (const fn of fns) {
+    const body = await fn({ start: 0, count: 5, sort: 'createdAt' })
+    expect(body.total).to.equal(0)
+  }
+}
+
 describe('Test follows moderation', function () {
-  let servers: ServerInfo[] = []
+  let servers: PeerTubeServer[] = []
+  let commands: FollowsCommand[]
 
   before(async function () {
     this.timeout(30000)
 
-    servers = await flushAndRunMultipleServers(2)
+    servers = await createMultipleServers(3)
 
     // Get the access tokens
     await setAccessTokensToServers(servers)
+
+    commands = servers.map(s => s.follows)
   })
 
   it('Should have server 1 following server 2', async function () {
     this.timeout(30000)
 
-    await follow(servers[0].url, [ servers[1].url ], servers[0].accessToken)
+    await commands[0].follow({ hosts: [ servers[1].url ] })
 
     await waitJobs(servers)
   })
 
   it('Should have correct follows', async function () {
-    {
-      const res = await getFollowingListPaginationAndSort(servers[0].url, 0, 5, 'createdAt')
-      expect(res.body.total).to.equal(1)
+    await checkServer1And2HasFollowers(servers)
+  })
+
+  it('Should remove follower on server 2', async function () {
+    this.timeout(10000)
+
+    await commands[1].removeFollower({ follower: servers[0] })
+
+    await waitJobs(servers)
+  })
+
+  it('Should not not have follows anymore', async function () {
+    await checkNoFollowers(servers)
+  })
 
-      const follow = res.body.data[0] as ActorFollow
-      expect(follow.follower.url).to.equal('http://localhost:9001/accounts/peertube')
-      expect(follow.following.url).to.equal('http://localhost:9002/accounts/peertube')
+  it('Should disable followers on server 2', async function () {
+    this.timeout(10000)
+
+    const subConfig = {
+      followers: {
+        instance: {
+          enabled: false,
+          manualApproval: false
+        }
+      }
     }
 
-    {
-      const res = await getFollowersListPaginationAndSort(servers[1].url, 0, 5, 'createdAt')
-      expect(res.body.total).to.equal(1)
+    await servers[1].config.updateCustomSubConfig({ newConfig: subConfig })
 
-      const follow = res.body.data[0] as ActorFollow
-      expect(follow.follower.url).to.equal('http://localhost:9001/accounts/peertube')
-      expect(follow.following.url).to.equal('http://localhost:9002/accounts/peertube')
+    await commands[0].follow({ hosts: [ servers[1].url ] })
+    await waitJobs(servers)
+
+    await checkNoFollowers(servers)
+  })
+
+  it('Should re enable followers on server 2', async function () {
+    this.timeout(10000)
+
+    const subConfig = {
+      followers: {
+        instance: {
+          enabled: true,
+          manualApproval: false
+        }
+      }
     }
+
+    await servers[1].config.updateCustomSubConfig({ newConfig: subConfig })
+
+    await commands[0].follow({ hosts: [ servers[1].url ] })
+    await waitJobs(servers)
+
+    await checkServer1And2HasFollowers(servers)
   })
 
-  it('Should remove follower on server 2', async function () {
-    await removeFollower(servers[1].url, servers[1].accessToken, servers[0])
+  it('Should manually approve followers', async function () {
+    this.timeout(20000)
 
+    await commands[1].removeFollower({ follower: servers[0] })
     await waitJobs(servers)
+
+    const subConfig = {
+      followers: {
+        instance: {
+          enabled: true,
+          manualApproval: true
+        }
+      }
+    }
+
+    await servers[1].config.updateCustomSubConfig({ newConfig: subConfig })
+    await servers[2].config.updateCustomSubConfig({ newConfig: subConfig })
+
+    await commands[0].follow({ hosts: [ servers[1].url ] })
+    await waitJobs(servers)
+
+    await checkServer1And2HasFollowers(servers, 'pending')
   })
 
-  it('Should not not have follows anymore', async function () {
+  it('Should accept a follower', async function () {
+    this.timeout(10000)
+
+    await commands[1].acceptFollower({ follower: 'peertube@localhost:' + servers[0].port })
+    await waitJobs(servers)
+
+    await checkServer1And2HasFollowers(servers)
+  })
+
+  it('Should reject another follower', async function () {
+    this.timeout(20000)
+
+    await commands[0].follow({ hosts: [ servers[2].url ] })
+    await waitJobs(servers)
+
+    {
+      const body = await commands[0].getFollowings({ start: 0, count: 5, sort: 'createdAt' })
+      expect(body.total).to.equal(2)
+    }
+
     {
-      const res = await getFollowingListPaginationAndSort(servers[ 0 ].url, 0, 1, 'createdAt')
-      expect(res.body.total).to.equal(0)
+      const body = await commands[1].getFollowers({ start: 0, count: 5, sort: 'createdAt' })
+      expect(body.total).to.equal(1)
     }
 
     {
-      const res = await getFollowingListPaginationAndSort(servers[ 0 ].url, 0, 1, 'createdAt')
-      expect(res.body.total).to.equal(0)
+      const body = await commands[2].getFollowers({ start: 0, count: 5, sort: 'createdAt' })
+      expect(body.total).to.equal(1)
+    }
+
+    await commands[2].rejectFollower({ follower: 'peertube@localhost:' + servers[0].port })
+    await waitJobs(servers)
+
+    await checkServer1And2HasFollowers(servers)
+
+    {
+      const body = await commands[2].getFollowers({ start: 0, count: 5, sort: 'createdAt' })
+      expect(body.total).to.equal(0)
     }
   })
 
   after(async function () {
-    killallServers(servers)
+    await cleanupTests(servers)
   })
 })