]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/tests/api/server/follows-moderation.ts
Cleanup tests imports
[github/Chocobozzz/PeerTube.git] / server / tests / api / server / follows-moderation.ts
index a82acdb34a4c8417e7e1e770337873f27259379a..deeaac85a0859b5b51d680f2ef26a2c9172dc531 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 { expect } from 'chai'
+import { expectStartWith } from '@server/tests/shared'
+import { ActorFollow, FollowState } from '@shared/models'
 import {
-  acceptFollower,
   cleanupTests,
-  flushAndRunMultipleServers,
-  ServerInfo,
+  createMultipleServers,
+  FollowsCommand,
+  PeerTubeServer,
   setAccessTokensToServers,
-  updateCustomSubConfig
-} from '../../../../shared/extra-utils/index'
-import {
-  follow,
-  getFollowersListPaginationAndSort,
-  getFollowingListPaginationAndSort,
-  rejectFollower,
-  removeFollower
-} from '../../../../shared/extra-utils/server/follows'
-import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
-import { ActorFollow } from '../../../../shared/models/actors'
-
-const expect = chai.expect
-
-async function checkServer1And2HasFollowers (servers: ServerInfo[], state = 'accepted') {
-  {
-    const res = await getFollowingListPaginationAndSort(servers[0].url, 0, 5, 'createdAt')
-    expect(res.body.total).to.equal(1)
+  waitJobs
+} from '@shared/server-commands'
 
-    const follow = res.body.data[0] as ActorFollow
-    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 checkServer1And2HasFollowers (servers: PeerTubeServer[], state = 'accepted') {
+  const fns = [
+    servers[0].follows.getFollowings.bind(servers[0].follows),
+    servers[1].follows.getFollowers.bind(servers[1].follows)
+  ]
 
-  {
-    const res = await getFollowersListPaginationAndSort(servers[1].url, 0, 5, 'createdAt')
-    expect(res.body.total).to.equal(1)
+  for (const fn of fns) {
+    const body = await fn({ start: 0, count: 5, sort: 'createdAt' })
+    expect(body.total).to.equal(1)
 
-    const follow = res.body.data[0] as ActorFollow
+    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')
+    expect(follow.follower.url).to.equal(servers[0].url + '/accounts/peertube')
+    expect(follow.following.url).to.equal(servers[1].url + '/accounts/peertube')
   }
 }
 
-async function checkNoFollowers (servers: ServerInfo[]) {
+async function checkFollows (options: {
+  follower: PeerTubeServer
+  followerState: FollowState | 'deleted'
+
+  following: PeerTubeServer
+  followingState: FollowState | 'deleted'
+}) {
+  const { follower, followerState, followingState, following } = options
+
+  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 res = await getFollowingListPaginationAndSort(servers[ 0 ].url, 0, 5, 'createdAt')
-    expect(res.body.total).to.equal(0)
+    const { data } = await follower.follows.getFollowings()
+    const follow = data.find(finder)
+
+    if (followerState === 'deleted') {
+      expect(follow).to.not.exist
+    } else {
+      expect(follow.state).to.equal(followerState)
+      expect(follow.follower.url).to.equal(followerUrl)
+      expect(follow.following.url).to.equal(followingUrl)
+    }
   }
 
   {
-    const res = await getFollowersListPaginationAndSort(servers[ 1 ].url, 0, 5, 'createdAt')
-    expect(res.body.total).to.equal(0)
+    const { data } = await following.follows.getFollowers()
+    const follow = data.find(finder)
+
+    if (followingState === 'deleted') {
+      expect(follow).to.not.exist
+    } else {
+      expect(follow.state).to.equal(followingState)
+      expect(follow.follower.url).to.equal(followerUrl)
+      expect(follow.following.url).to.equal(followingUrl)
+    }
+  }
+}
+
+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', state: 'accepted' })
+    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)
+    this.timeout(240000)
 
-    servers = await flushAndRunMultipleServers(3)
+    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)
   })
@@ -81,7 +109,9 @@ describe('Test follows moderation', function () {
   })
 
   it('Should remove follower on server 2', async function () {
-    await removeFollower(servers[1].url, servers[1].accessToken, servers[0])
+    this.timeout(10000)
+
+    await commands[1].removeFollower({ follower: servers[0] })
 
     await waitJobs(servers)
   })
@@ -91,6 +121,8 @@ describe('Test follows moderation', function () {
   })
 
   it('Should disable followers on server 2', async function () {
+    this.timeout(10000)
+
     const subConfig = {
       followers: {
         instance: {
@@ -100,15 +132,17 @@ describe('Test follows moderation', function () {
       }
     }
 
-    await updateCustomSubConfig(servers[1].url, servers[1].accessToken, subConfig)
+    await servers[1].config.updateCustomSubConfig({ newConfig: subConfig })
 
-    await follow(servers[0].url, [ servers[1].url ], servers[0].accessToken)
+    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: {
@@ -118,9 +152,9 @@ describe('Test follows moderation', function () {
       }
     }
 
-    await updateCustomSubConfig(servers[1].url, servers[1].accessToken, subConfig)
+    await servers[1].config.updateCustomSubConfig({ newConfig: subConfig })
 
-    await follow(servers[0].url, [ servers[1].url ], servers[0].accessToken)
+    await commands[0].follow({ hosts: [ servers[1].url ] })
     await waitJobs(servers)
 
     await checkServer1And2HasFollowers(servers)
@@ -129,7 +163,7 @@ describe('Test follows moderation', function () {
   it('Should manually approve followers', async function () {
     this.timeout(20000)
 
-    await removeFollower(servers[1].url, servers[1].accessToken, servers[0])
+    await commands[0].unfollow({ target: servers[1] })
     await waitJobs(servers)
 
     const subConfig = {
@@ -141,17 +175,19 @@ describe('Test follows moderation', function () {
       }
     }
 
-    await updateCustomSubConfig(servers[1].url, servers[1].accessToken, subConfig)
-    await updateCustomSubConfig(servers[2].url, servers[2].accessToken, subConfig)
+    await servers[1].config.updateCustomSubConfig({ newConfig: subConfig })
+    await servers[2].config.updateCustomSubConfig({ newConfig: subConfig })
 
-    await follow(servers[0].url, [ servers[1].url ], servers[0].accessToken)
+    await commands[0].follow({ hosts: [ servers[1].url ] })
     await waitJobs(servers)
 
     await checkServer1And2HasFollowers(servers, 'pending')
   })
 
   it('Should accept a follower', async function () {
-    await acceptFollower(servers[1].url, servers[1].accessToken, 'peertube@localhost:' + servers[0].port)
+    this.timeout(10000)
+
+    await commands[1].acceptFollower({ follower: 'peertube@' + servers[0].host })
     await waitJobs(servers)
 
     await checkServer1And2HasFollowers(servers)
@@ -160,33 +196,148 @@ describe('Test follows moderation', function () {
   it('Should reject another follower', async function () {
     this.timeout(20000)
 
-    await follow(servers[0].url, [ servers[2].url ], servers[0].accessToken)
+    await commands[0].follow({ hosts: [ servers[2].url ] })
     await waitJobs(servers)
 
     {
-      const res = await getFollowingListPaginationAndSort(servers[0].url, 0, 5, 'createdAt')
-      expect(res.body.total).to.equal(2)
+      const body = await commands[0].getFollowings()
+      expect(body.total).to.equal(2)
     }
 
     {
-      const res = await getFollowersListPaginationAndSort(servers[1].url, 0, 5, 'createdAt')
-      expect(res.body.total).to.equal(1)
+      const body = await commands[1].getFollowers()
+      expect(body.total).to.equal(1)
     }
 
     {
-      const res = await getFollowersListPaginationAndSort(servers[2].url, 0, 5, 'createdAt')
-      expect(res.body.total).to.equal(1)
+      const body = await commands[2].getFollowers()
+      expect(body.total).to.equal(1)
     }
 
-    await rejectFollower(servers[2].url, servers[2].accessToken, 'peertube@localhost:' + servers[0].port)
+    await commands[2].rejectFollower({ follower: 'peertube@' + servers[0].host })
     await waitJobs(servers)
 
-    await checkServer1And2HasFollowers(servers)
+    { // server 1
+      {
+        const { data } = await commands[0].getFollowings({ state: 'accepted' })
+        expect(data).to.have.lengthOf(1)
+      }
 
-    {
-      const res = await getFollowersListPaginationAndSort(servers[ 2 ].url, 0, 5, 'createdAt')
-      expect(res.body.total).to.equal(0)
+      {
+        const { data } = await commands[0].getFollowings({ state: 'rejected' })
+        expect(data).to.have.lengthOf(1)
+        expectStartWith(data[0].following.url, servers[2].url)
+      }
     }
+
+    { // server 3
+      {
+        const { data } = await commands[2].getFollowers({ state: 'accepted' })
+        expect(data).to.have.lengthOf(0)
+      }
+
+      {
+        const { data } = await commands[2].getFollowers({ state: 'rejected' })
+        expect(data).to.have.lengthOf(1)
+        expectStartWith(data[0].follower.url, servers[0].url)
+      }
+    }
+  })
+
+  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 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 () {
+    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 () {