From 14893eb71cb2d4ca47e07589c81958863603aba4 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 8 Apr 2019 15:18:04 +0200 Subject: Add ability to manually approves instance followers in REST API --- server/tests/api/check-params/config.ts | 3 +- server/tests/api/check-params/follows.ts | 80 ++++++++++++++++++++++++++ server/tests/api/server/config.ts | 5 +- server/tests/api/server/follows-moderation.ts | 83 ++++++++++++++++++++++++--- 4 files changed, 162 insertions(+), 9 deletions(-) (limited to 'server/tests') diff --git a/server/tests/api/check-params/config.ts b/server/tests/api/check-params/config.ts index d117f26e6..01ab84584 100644 --- a/server/tests/api/check-params/config.ts +++ b/server/tests/api/check-params/config.ts @@ -90,7 +90,8 @@ describe('Test config API validators', function () { }, followers: { instance: { - enabled: false + enabled: false, + manualApproval: true } } } diff --git a/server/tests/api/check-params/follows.ts b/server/tests/api/check-params/follows.ts index 67fa43778..ed1d2db59 100644 --- a/server/tests/api/check-params/follows.ts +++ b/server/tests/api/check-params/follows.ts @@ -184,6 +184,86 @@ describe('Test server follows API validators', function () { }) }) + describe('When accepting a follower', function () { + const path = '/api/v1/server/followers' + + it('Should fail with an invalid token', async function () { + await makePostBodyRequest({ + url: server.url, + path: path + '/toto@localhost:9002/accept', + token: 'fake_token', + statusCodeExpected: 401 + }) + }) + + it('Should fail if the user is not an administrator', async function () { + await makePostBodyRequest({ + url: server.url, + path: path + '/toto@localhost:9002/accept', + token: userAccessToken, + statusCodeExpected: 403 + }) + }) + + it('Should fail with an invalid follower', async function () { + await makePostBodyRequest({ + url: server.url, + path: path + '/toto/accept', + token: server.accessToken, + statusCodeExpected: 400 + }) + }) + + it('Should fail with an unknown follower', async function () { + await makePostBodyRequest({ + url: server.url, + path: path + '/toto@localhost:9003/accept', + token: server.accessToken, + statusCodeExpected: 404 + }) + }) + }) + + describe('When rejecting a follower', function () { + const path = '/api/v1/server/followers' + + it('Should fail with an invalid token', async function () { + await makePostBodyRequest({ + url: server.url, + path: path + '/toto@localhost:9002/reject', + token: 'fake_token', + statusCodeExpected: 401 + }) + }) + + it('Should fail if the user is not an administrator', async function () { + await makePostBodyRequest({ + url: server.url, + path: path + '/toto@localhost:9002/reject', + token: userAccessToken, + statusCodeExpected: 403 + }) + }) + + it('Should fail with an invalid follower', async function () { + await makePostBodyRequest({ + url: server.url, + path: path + '/toto/reject', + token: server.accessToken, + statusCodeExpected: 400 + }) + }) + + it('Should fail with an unknown follower', async function () { + await makePostBodyRequest({ + url: server.url, + path: path + '/toto@localhost:9003/reject', + token: server.accessToken, + statusCodeExpected: 404 + }) + }) + }) + describe('When removing following', function () { const path = '/api/v1/server/following' diff --git a/server/tests/api/server/config.ts b/server/tests/api/server/config.ts index cb2700f29..5373d02f2 100644 --- a/server/tests/api/server/config.ts +++ b/server/tests/api/server/config.ts @@ -65,6 +65,7 @@ function checkInitialConfig (data: CustomConfig) { expect(data.autoBlacklist.videos.ofUsers.enabled).to.be.false expect(data.followers.instance.enabled).to.be.true + expect(data.followers.instance.manualApproval).to.be.false } function checkUpdatedConfig (data: CustomConfig) { @@ -109,6 +110,7 @@ function checkUpdatedConfig (data: CustomConfig) { expect(data.autoBlacklist.videos.ofUsers.enabled).to.be.true expect(data.followers.instance.enabled).to.be.false + expect(data.followers.instance.manualApproval).to.be.true } describe('Test config', function () { @@ -241,7 +243,8 @@ describe('Test config', function () { }, followers: { instance: { - enabled: false + enabled: false, + manualApproval: true } } } diff --git a/server/tests/api/server/follows-moderation.ts b/server/tests/api/server/follows-moderation.ts index a360706f2..0bb3aa866 100644 --- a/server/tests/api/server/follows-moderation.ts +++ b/server/tests/api/server/follows-moderation.ts @@ -3,6 +3,7 @@ import * as chai from 'chai' import 'mocha' import { + acceptFollower, flushAndRunMultipleServers, killallServers, ServerInfo, @@ -13,19 +14,21 @@ import { follow, getFollowersListPaginationAndSort, getFollowingListPaginationAndSort, - removeFollower + removeFollower, + rejectFollower } from '../../../../shared/utils/server/follows' import { waitJobs } from '../../../../shared/utils/server/jobs' import { ActorFollow } from '../../../../shared/models/actors' const expect = chai.expect -async function checkHasFollowers (servers: ServerInfo[]) { +async function checkServer1And2HasFollowers (servers: ServerInfo[], state = 'accepted') { { const res = await getFollowingListPaginationAndSort(servers[0].url, 0, 5, 'createdAt') expect(res.body.total).to.equal(1) const follow = res.body.data[0] as ActorFollow + expect(follow.state).to.equal(state) expect(follow.follower.url).to.equal('http://localhost:9001/accounts/peertube') expect(follow.following.url).to.equal('http://localhost:9002/accounts/peertube') } @@ -35,6 +38,7 @@ async function checkHasFollowers (servers: ServerInfo[]) { expect(res.body.total).to.equal(1) const follow = res.body.data[0] as ActorFollow + expect(follow.state).to.equal(state) expect(follow.follower.url).to.equal('http://localhost:9001/accounts/peertube') expect(follow.following.url).to.equal('http://localhost:9002/accounts/peertube') } @@ -58,7 +62,7 @@ describe('Test follows moderation', function () { before(async function () { this.timeout(30000) - servers = await flushAndRunMultipleServers(2) + servers = await flushAndRunMultipleServers(3) // Get the access tokens await setAccessTokensToServers(servers) @@ -73,7 +77,7 @@ describe('Test follows moderation', function () { }) it('Should have correct follows', async function () { - await checkHasFollowers(servers) + await checkServer1And2HasFollowers(servers) }) it('Should remove follower on server 2', async function () { @@ -90,7 +94,8 @@ describe('Test follows moderation', function () { const subConfig = { followers: { instance: { - enabled: false + enabled: false, + manualApproval: false } } } @@ -107,7 +112,8 @@ describe('Test follows moderation', function () { const subConfig = { followers: { instance: { - enabled: true + enabled: true, + manualApproval: false } } } @@ -117,7 +123,70 @@ describe('Test follows moderation', function () { await follow(servers[0].url, [ servers[1].url ], servers[0].accessToken) await waitJobs(servers) - await checkHasFollowers(servers) + await checkServer1And2HasFollowers(servers) + }) + + it('Should manually approve followers', async function () { + this.timeout(20000) + + await removeFollower(servers[1].url, servers[1].accessToken, servers[0]) + await waitJobs(servers) + + const subConfig = { + followers: { + instance: { + enabled: true, + manualApproval: true + } + } + } + + await updateCustomSubConfig(servers[1].url, servers[1].accessToken, subConfig) + await updateCustomSubConfig(servers[2].url, servers[2].accessToken, subConfig) + + await follow(servers[0].url, [ servers[1].url ], servers[0].accessToken) + await waitJobs(servers) + + await checkServer1And2HasFollowers(servers, 'pending') + }) + + it('Should accept a follower', async function () { + await acceptFollower(servers[1].url, servers[1].accessToken, 'peertube@localhost:9001') + await waitJobs(servers) + + await checkServer1And2HasFollowers(servers) + }) + + it('Should reject another follower', async function () { + this.timeout(20000) + + await follow(servers[0].url, [ servers[2].url ], servers[0].accessToken) + await waitJobs(servers) + + { + const res = await getFollowingListPaginationAndSort(servers[0].url, 0, 5, 'createdAt') + expect(res.body.total).to.equal(2) + } + + { + const res = await getFollowersListPaginationAndSort(servers[1].url, 0, 5, 'createdAt') + expect(res.body.total).to.equal(1) + } + + { + const res = await getFollowersListPaginationAndSort(servers[2].url, 0, 5, 'createdAt') + expect(res.body.total).to.equal(1) + } + + await rejectFollower(servers[2].url, servers[2].accessToken, 'peertube@localhost:9001') + await waitJobs(servers) + + await checkServer1And2HasFollowers(servers) + + { + const res = await getFollowersListPaginationAndSort(servers[ 2 ].url, 0, 5, 'createdAt') + expect(res.body.total).to.equal(0) + } }) after(async function () { -- cgit v1.2.3