]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/tests/api/server/follows-moderation.ts
Fix tests
[github/Chocobozzz/PeerTube.git] / server / tests / api / server / follows-moderation.ts
index a0e94c10e9f011de68e8108d38bcd671bdd1f296..d145dd9d2c9b27c33d521e0f3d517d1771348a6a 100644 (file)
@@ -1,7 +1,6 @@
 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
 
-import 'mocha'
-import * as chai from 'chai'
+import { expect } from 'chai'
 import { expectStartWith } from '@server/tests/shared'
 import { ActorFollow, FollowState } from '@shared/models'
 import {
@@ -13,8 +12,6 @@ import {
   waitJobs
 } from '@shared/server-commands'
 
-const expect = chai.expect
-
 async function checkServer1And2HasFollowers (servers: PeerTubeServer[], state = 'accepted') {
   const fns = [
     servers[0].follows.getFollowings.bind(servers[0].follows),
@@ -33,42 +30,39 @@ async function checkServer1And2HasFollowers (servers: PeerTubeServer[], state =
 }
 
 async function checkFollows (options: {
-  follower: {
-    server: PeerTubeServer
-    state?: FollowState // if not provided, it means it does not exist
-  }
-  following: {
-    server: PeerTubeServer
-    state?: FollowState // if not provided, it means it does not exist
-  }
+  follower: PeerTubeServer
+  followerState: FollowState | 'deleted'
+
+  following: PeerTubeServer
+  followingState: FollowState | 'deleted'
 }) {
-  const { follower, following } = options
+  const { follower, followerState, followingState, following } = options
 
-  const followerUrl = follower.server.url + '/accounts/peertube'
-  const followingUrl = following.server.url + '/accounts/peertube'
+  const followerUrl = follower.url + '/accounts/peertube'
+  const followingUrl = following.url + '/accounts/peertube'
   const finder = (d: ActorFollow) => d.follower.url === followerUrl && d.following.url === followingUrl
 
   {
-    const { data } = await follower.server.follows.getFollowings()
+    const { data } = await follower.follows.getFollowings()
     const follow = data.find(finder)
 
-    if (!follower.state) {
+    if (followerState === 'deleted') {
       expect(follow).to.not.exist
     } else {
-      expect(follow.state).to.equal(follower.state)
+      expect(follow.state).to.equal(followerState)
       expect(follow.follower.url).to.equal(followerUrl)
       expect(follow.following.url).to.equal(followingUrl)
     }
   }
 
   {
-    const { data } = await following.server.follows.getFollowers()
+    const { data } = await following.follows.getFollowers()
     const follow = data.find(finder)
 
-    if (!following.state) {
+    if (followingState === 'deleted') {
       expect(follow).to.not.exist
     } else {
-      expect(follow.state).to.equal(following.state)
+      expect(follow.state).to.equal(followingState)
       expect(follow.follower.url).to.equal(followerUrl)
       expect(follow.following.url).to.equal(followingUrl)
     }
@@ -92,7 +86,7 @@ describe('Test follows moderation', function () {
   let commands: FollowsCommand[]
 
   before(async function () {
-    this.timeout(30000)
+    this.timeout(240000)
 
     servers = await createMultipleServers(3)
 
@@ -102,246 +96,266 @@ describe('Test follows moderation', function () {
     commands = servers.map(s => s.follows)
   })
 
-  it('Should have server 1 following server 2', async function () {
-    this.timeout(30000)
+  describe('Default behaviour', function () {
 
-    await commands[0].follow({ hosts: [ servers[1].url ] })
+    it('Should have server 1 following server 2', async function () {
+      this.timeout(30000)
 
-    await waitJobs(servers)
-  })
+      await commands[0].follow({ hosts: [ servers[1].url ] })
 
-  it('Should have correct follows', async function () {
-    await checkServer1And2HasFollowers(servers)
-  })
+      await waitJobs(servers)
+    })
 
-  it('Should remove follower on server 2', async function () {
-    this.timeout(10000)
+    it('Should have correct follows', async function () {
+      await checkServer1And2HasFollowers(servers)
+    })
 
-    await commands[1].removeFollower({ follower: servers[0] })
+    it('Should remove follower on server 2', async function () {
+      await commands[1].removeFollower({ follower: servers[0] })
 
-    await waitJobs(servers)
-  })
+      await waitJobs(servers)
+    })
 
-  it('Should not not have follows anymore', async function () {
-    await checkNoFollowers(servers)
+    it('Should not not have follows anymore', async function () {
+      await checkNoFollowers(servers)
+    })
   })
 
-  it('Should disable followers on server 2', async function () {
-    this.timeout(10000)
+  describe('Disabled/Enabled followers', function () {
 
-    const subConfig = {
-      followers: {
-        instance: {
-          enabled: false,
-          manualApproval: false
+    it('Should disable followers on server 2', async function () {
+      const subConfig = {
+        followers: {
+          instance: {
+            enabled: false,
+            manualApproval: false
+          }
         }
       }
-    }
 
-    await servers[1].config.updateCustomSubConfig({ newConfig: subConfig })
+      await servers[1].config.updateCustomSubConfig({ newConfig: subConfig })
 
-    await commands[0].follow({ hosts: [ servers[1].url ] })
-    await waitJobs(servers)
-
-    await checkNoFollowers(servers)
-  })
+      await commands[0].follow({ hosts: [ servers[1].url ] })
+      await waitJobs(servers)
 
-  it('Should re enable followers on server 2', async function () {
-    this.timeout(10000)
+      await checkNoFollowers(servers)
+    })
 
-    const subConfig = {
-      followers: {
-        instance: {
-          enabled: true,
-          manualApproval: false
+    it('Should re enable followers on server 2', async function () {
+      const subConfig = {
+        followers: {
+          instance: {
+            enabled: true,
+            manualApproval: false
+          }
         }
       }
-    }
 
-    await servers[1].config.updateCustomSubConfig({ newConfig: subConfig })
+      await servers[1].config.updateCustomSubConfig({ newConfig: subConfig })
 
-    await commands[0].follow({ hosts: [ servers[1].url ] })
-    await waitJobs(servers)
+      await commands[0].follow({ hosts: [ servers[1].url ] })
+      await waitJobs(servers)
 
-    await checkServer1And2HasFollowers(servers)
+      await checkServer1And2HasFollowers(servers)
+    })
   })
 
-  it('Should manually approve followers', async function () {
-    this.timeout(20000)
+  describe('Manual approbation', function () {
 
-    await commands[0].unfollow({ target: servers[1] })
-    await waitJobs(servers)
+    it('Should manually approve followers', async function () {
+      this.timeout(20000)
 
-    const subConfig = {
-      followers: {
-        instance: {
-          enabled: true,
-          manualApproval: true
+      await commands[0].unfollow({ target: servers[1] })
+      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')
-  })
+      await servers[1].config.updateCustomSubConfig({ newConfig: subConfig })
+      await servers[2].config.updateCustomSubConfig({ newConfig: subConfig })
 
-  it('Should accept a follower', async function () {
-    this.timeout(10000)
-
-    await commands[1].acceptFollower({ follower: 'peertube@' + servers[0].host })
-    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)
+      await commands[0].follow({ hosts: [ servers[1].url ] })
+      await waitJobs(servers)
 
-    {
-      const body = await commands[0].getFollowings()
-      expect(body.total).to.equal(2)
-    }
+      await checkServer1And2HasFollowers(servers, 'pending')
+    })
 
-    {
-      const body = await commands[1].getFollowers()
-      expect(body.total).to.equal(1)
-    }
+    it('Should accept a follower', async function () {
+      await commands[1].acceptFollower({ follower: 'peertube@' + servers[0].host })
+      await waitJobs(servers)
 
-    {
-      const body = await commands[2].getFollowers()
-      expect(body.total).to.equal(1)
-    }
+      await checkServer1And2HasFollowers(servers)
+    })
 
-    await commands[2].rejectFollower({ follower: 'peertube@' + servers[0].host })
-    await waitJobs(servers)
+    it('Should reject another follower', async function () {
+      this.timeout(20000)
 
-    { // server 1
-      {
-        const { data } = await commands[0].getFollowings({ state: 'accepted' })
-        expect(data).to.have.lengthOf(1)
-      }
+      await commands[0].follow({ hosts: [ servers[2].url ] })
+      await waitJobs(servers)
 
       {
-        const { data } = await commands[0].getFollowings({ state: 'rejected' })
-        expect(data).to.have.lengthOf(1)
-        expectStartWith(data[0].following.url, servers[2].url)
+        const body = await commands[0].getFollowings()
+        expect(body.total).to.equal(2)
       }
-    }
 
-    { // server 3
       {
-        const { data } = await commands[2].getFollowers({ state: 'accepted' })
-        expect(data).to.have.lengthOf(0)
+        const body = await commands[1].getFollowers()
+        expect(body.total).to.equal(1)
       }
 
       {
-        const { data } = await commands[2].getFollowers({ state: 'rejected' })
-        expect(data).to.have.lengthOf(1)
-        expectStartWith(data[0].follower.url, servers[0].url)
+        const body = await commands[2].getFollowers()
+        expect(body.total).to.equal(1)
       }
-    }
-  })
 
-  it('Should not change the follow on refollow with and without auto accept', async function () {
-    const run = async () => {
-      await commands[0].follow({ hosts: [ servers[2].url ] })
+      await commands[2].rejectFollower({ follower: 'peertube@' + servers[0].host })
       await waitJobs(servers)
 
-      await checkFollows({
-        follower: {
-          server: servers[0],
-          state: 'rejected'
-        },
-        following: {
-          server: servers[2],
-          state: 'rejected'
+      { // server 1
+        {
+          const { data } = await commands[0].getFollowings({ state: 'accepted' })
+          expect(data).to.have.lengthOf(1)
         }
-      })
-    }
 
-    await servers[2].config.updateExistingSubConfig({ newConfig: { followers: { instance: { manualApproval: false } } } })
-    await run()
+        {
+          const { data } = await commands[0].getFollowings({ state: 'rejected' })
+          expect(data).to.have.lengthOf(1)
+          expectStartWith(data[0].following.url, servers[2].url)
+        }
+      }
 
-    await servers[2].config.updateExistingSubConfig({ newConfig: { followers: { instance: { manualApproval: true } } } })
-    await run()
-  })
+      { // server 3
+        {
+          const { data } = await commands[2].getFollowers({ state: 'accepted' })
+          expect(data).to.have.lengthOf(0)
+        }
 
-  it('Should not change the rejected status on unfollow', async function () {
-    await commands[0].unfollow({ target: servers[2] })
-    await waitJobs(servers)
-
-    await checkFollows({
-      follower: {
-        server: servers[0]
-      },
-      following: {
-        server: servers[2],
-        state: 'rejected'
+        {
+          const { data } = await commands[2].getFollowers({ state: 'rejected' })
+          expect(data).to.have.lengthOf(1)
+          expectStartWith(data[0].follower.url, servers[0].url)
+        }
       }
     })
-  })
 
-  it('Should delete the follower and add again the follower', async function () {
-    await commands[2].removeFollower({ follower: servers[0] })
-    await waitJobs(servers)
-
-    await commands[0].follow({ hosts: [ servers[2].url ] })
-    await waitJobs(servers)
-
-    await checkFollows({
-      follower: {
-        server: servers[0],
-        state: 'pending'
-      },
-      following: {
-        server: servers[2],
-        state: 'pending'
-      }
+    it('Should still auto accept channel followers', async function () {
+      await commands[0].follow({ handles: [ 'root_channel@' + servers[1].host ] })
+
+      await waitJobs(servers)
+
+      const body = await commands[0].getFollowings()
+      const follow = body.data[0]
+      expect(follow.following.name).to.equal('root_channel')
+      expect(follow.state).to.equal('accepted')
     })
   })
 
-  it('Should be able to reject a previously accepted follower', async function () {
-    await commands[1].rejectFollower({ follower: 'peertube@' + servers[0].host })
-    await waitJobs(servers)
-
-    await checkFollows({
-      follower: {
-        server: servers[0],
-        state: 'rejected'
-      },
-      following: {
-        server: servers[1],
-        state: 'rejected'
+  describe('Accept/reject state', function () {
+
+    it('Should not change the follow on refollow with and without auto accept', async function () {
+      const run = async () => {
+        await commands[0].follow({ hosts: [ servers[2].url ] })
+        await waitJobs(servers)
+
+        await checkFollows({
+          follower: servers[0],
+          followerState: 'rejected',
+          following: servers[2],
+          followingState: 'rejected'
+        })
       }
+
+      await servers[2].config.updateExistingSubConfig({ newConfig: { followers: { instance: { manualApproval: false } } } })
+      await run()
+
+      await servers[2].config.updateExistingSubConfig({ newConfig: { followers: { instance: { manualApproval: true } } } })
+      await run()
     })
-  })
 
-  it('Should be able to re accept a previously rejected follower', async function () {
-    await commands[1].acceptFollower({ follower: 'peertube@' + servers[0].host })
-    await waitJobs(servers)
-
-    await checkFollows({
-      follower: {
-        server: servers[0],
-        state: 'accepted'
-      },
-      following: {
-        server: servers[1],
-        state: 'accepted'
-      }
+    it('Should not change the rejected status on unfollow', async function () {
+      await commands[0].unfollow({ target: servers[2] })
+      await waitJobs(servers)
+
+      await checkFollows({
+        follower: servers[0],
+        followerState: 'deleted',
+        following: servers[2],
+        followingState: 'rejected'
+      })
+    })
+
+    it('Should delete the follower and add again the follower', async function () {
+      await commands[2].removeFollower({ follower: servers[0] })
+      await waitJobs(servers)
+
+      await commands[0].follow({ hosts: [ servers[2].url ] })
+      await waitJobs(servers)
+
+      await checkFollows({
+        follower: servers[0],
+        followerState: 'pending',
+        following: servers[2],
+        followingState: 'pending'
+      })
+    })
+
+    it('Should be able to reject a previously accepted follower', async function () {
+      await commands[1].rejectFollower({ follower: 'peertube@' + servers[0].host })
+      await waitJobs(servers)
+
+      await checkFollows({
+        follower: servers[0],
+        followerState: 'rejected',
+        following: servers[1],
+        followingState: 'rejected'
+      })
+    })
+
+    it('Should be able to re accept a previously rejected follower', async function () {
+      await commands[1].acceptFollower({ follower: 'peertube@' + servers[0].host })
+      await waitJobs(servers)
+
+      await checkFollows({
+        follower: servers[0],
+        followerState: 'accepted',
+        following: servers[1],
+        followingState: 'accepted'
+      })
     })
   })
 
-  it('Should ignore follow requests of muted servers', async function () {
+  describe('Muted servers', function () {
+
+    it('Should ignore follow requests of muted servers', async function () {
+      await servers[1].blocklist.addToServerBlocklist({ server: servers[0].host })
 
+      await commands[0].unfollow({ target: servers[1] })
+
+      await waitJobs(servers)
+
+      await checkFollows({
+        follower: servers[0],
+        followerState: 'deleted',
+        following: servers[1],
+        followingState: 'deleted'
+      })
+
+      await commands[0].follow({ hosts: [ servers[1].host ] })
+      await waitJobs(servers)
+
+      await checkFollows({
+        follower: servers[0],
+        followerState: 'rejected',
+        following: servers[1],
+        followingState: 'deleted'
+      })
+    })
   })
 
   after(async function () {