From 8424c4026afd7304880a4ce8138a04ffb3d8c938 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 30 Aug 2019 16:50:12 +0200 Subject: Add auto follow back support for instances --- server/tests/api/check-params/config.ts | 23 +++- .../tests/api/check-params/user-notifications.ts | 3 +- .../tests/api/notifications/user-notifications.ts | 40 +++++- server/tests/api/server/auto-follows.ts | 148 +++++++++++++++++++++ server/tests/api/server/config.ts | 19 +++ server/tests/api/server/index.ts | 1 + 6 files changed, 227 insertions(+), 7 deletions(-) create mode 100644 server/tests/api/server/auto-follows.ts (limited to 'server/tests/api') diff --git a/server/tests/api/check-params/config.ts b/server/tests/api/check-params/config.ts index 7773ae1e7..1221735c5 100644 --- a/server/tests/api/check-params/config.ts +++ b/server/tests/api/check-params/config.ts @@ -5,8 +5,16 @@ import 'mocha' import { CustomConfig } from '../../../../shared/models/server/custom-config.model' import { - createUser, flushTests, killallServers, makeDeleteRequest, makeGetRequest, makePutBodyRequest, flushAndRunServer, ServerInfo, - setAccessTokensToServers, userLogin, immutableAssign, cleanupTests + cleanupTests, + createUser, + flushAndRunServer, + immutableAssign, + makeDeleteRequest, + makeGetRequest, + makePutBodyRequest, + ServerInfo, + setAccessTokensToServers, + userLogin } from '../../../../shared/extra-utils' describe('Test config API validators', function () { @@ -98,6 +106,17 @@ describe('Test config API validators', function () { enabled: false, manualApproval: true } + }, + followings: { + instance: { + autoFollowBack: { + enabled: true + }, + autoFollowIndex: { + enabled: true, + indexUrl: 'https://index.example.com' + } + } } } diff --git a/server/tests/api/check-params/user-notifications.ts b/server/tests/api/check-params/user-notifications.ts index 14ee20d45..3b06be7ef 100644 --- a/server/tests/api/check-params/user-notifications.ts +++ b/server/tests/api/check-params/user-notifications.ts @@ -172,7 +172,8 @@ describe('Test user notifications API validators', function () { commentMention: UserNotificationSettingValue.WEB, newFollow: UserNotificationSettingValue.WEB, newUserRegistration: UserNotificationSettingValue.WEB, - newInstanceFollower: UserNotificationSettingValue.WEB + newInstanceFollower: UserNotificationSettingValue.WEB, + autoInstanceFollowing: UserNotificationSettingValue.WEB } it('Should fail with missing fields', async function () { diff --git a/server/tests/api/notifications/user-notifications.ts b/server/tests/api/notifications/user-notifications.ts index 6fa630562..62b797b47 100644 --- a/server/tests/api/notifications/user-notifications.ts +++ b/server/tests/api/notifications/user-notifications.ts @@ -16,8 +16,8 @@ import { immutableAssign, registerUser, removeVideoFromBlacklist, - reportVideoAbuse, - updateCustomConfig, + reportVideoAbuse, unfollow, + updateCustomConfig, updateCustomSubConfig, updateMyUser, updateVideo, updateVideoChannel, @@ -45,7 +45,8 @@ import { getUserNotifications, markAsReadAllNotifications, markAsReadNotifications, - updateMyNotificationSettings + updateMyNotificationSettings, + checkAutoInstanceFollowing } from '../../../../shared/extra-utils/users/user-notifications' import { User, @@ -108,7 +109,8 @@ describe('Test users notifications', function () { commentMention: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, newFollow: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, newUserRegistration: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, - newInstanceFollower: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL + newInstanceFollower: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, + autoInstanceFollowing: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL } before(async function () { @@ -897,6 +899,36 @@ describe('Test users notifications', function () { const userOverride = { socketNotifications: userNotifications, token: userAccessToken, check: { web: true, mail: false } } await checkNewInstanceFollower(immutableAssign(baseParams, userOverride), 'localhost:' + servers[2].port, 'absence') }) + + it('Should send a notification on auto follow back', async function () { + this.timeout(40000) + + await unfollow(servers[2].url, servers[2].accessToken, servers[0]) + await waitJobs(servers) + + const config = { + followings: { + instance: { + autoFollowBack: { enabled: true } + } + } + } + await updateCustomSubConfig(servers[0].url, servers[0].accessToken, config) + + await follow(servers[2].url, [ servers[0].url ], servers[2].accessToken) + + await waitJobs(servers) + + const followerHost = servers[0].host + const followingHost = servers[2].host + await checkAutoInstanceFollowing(baseParams, followerHost, followingHost, 'presence') + + const userOverride = { socketNotifications: userNotifications, token: userAccessToken, check: { web: true, mail: false } } + await checkAutoInstanceFollowing(immutableAssign(baseParams, userOverride), followerHost, followingHost, 'absence') + + config.followings.instance.autoFollowBack.enabled = false + await updateCustomSubConfig(servers[0].url, servers[0].accessToken, config) + }) }) describe('New actor follow', function () { diff --git a/server/tests/api/server/auto-follows.ts b/server/tests/api/server/auto-follows.ts new file mode 100644 index 000000000..32ad259c9 --- /dev/null +++ b/server/tests/api/server/auto-follows.ts @@ -0,0 +1,148 @@ +/* tslint:disable:no-unused-expression */ + +import * as chai from 'chai' +import 'mocha' +import { + acceptFollower, + cleanupTests, + flushAndRunMultipleServers, + ServerInfo, + setAccessTokensToServers, + unfollow, + updateCustomSubConfig +} from '../../../../shared/extra-utils/index' +import { follow, getFollowersListPaginationAndSort, getFollowingListPaginationAndSort } 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 checkFollow (follower: ServerInfo, following: ServerInfo, exists: boolean) { + { + const res = await getFollowersListPaginationAndSort(following.url, 0, 5, '-createdAt') + const follows = res.body.data as ActorFollow[] + + if (exists === true) { + expect(res.body.total).to.equal(1) + + expect(follows[ 0 ].follower.host).to.equal(follower.host) + expect(follows[ 0 ].state).to.equal('accepted') + } else { + expect(follows.filter(f => f.state === 'accepted')).to.have.lengthOf(0) + } + } + + { + const res = await getFollowingListPaginationAndSort(follower.url, 0, 5, '-createdAt') + const follows = res.body.data as ActorFollow[] + + if (exists === true) { + expect(res.body.total).to.equal(1) + + expect(follows[ 0 ].following.host).to.equal(following.host) + expect(follows[ 0 ].state).to.equal('accepted') + } else { + expect(follows.filter(f => f.state === 'accepted')).to.have.lengthOf(0) + } + } +} + +async function server1Follows2 (servers: ServerInfo[]) { + await follow(servers[0].url, [ servers[1].host ], servers[0].accessToken) + + await waitJobs(servers) +} + +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 waitJobs(servers) + + await checkFollow(servers[0], servers[1], false) + await checkFollow(servers[1], servers[0], false) +} + +describe('Test auto follows', function () { + let servers: ServerInfo[] = [] + + before(async function () { + this.timeout(30000) + + servers = await flushAndRunMultipleServers(2) + + // Get the access tokens + await setAccessTokensToServers(servers) + }) + + describe('Auto follow back', function () { + + it('Should not auto follow back if the option is not enabled', async function () { + this.timeout(15000) + + await server1Follows2(servers) + + await checkFollow(servers[0], servers[1], true) + await checkFollow(servers[1], servers[0], false) + + await resetFollows(servers) + }) + + it('Should auto follow back on auto accept if the option is enabled', async function () { + this.timeout(15000) + + const config = { + followings: { + instance: { + autoFollowBack: { enabled: true } + } + } + } + await updateCustomSubConfig(servers[1].url, servers[1].accessToken, config) + + await server1Follows2(servers) + + await checkFollow(servers[0], servers[1], true) + await checkFollow(servers[1], servers[0], true) + + await resetFollows(servers) + }) + + it('Should wait the acceptation before auto follow back', async function () { + this.timeout(30000) + + const config = { + followings: { + instance: { + autoFollowBack: { enabled: true } + } + }, + followers: { + instance: { + manualApproval: true + } + } + } + await updateCustomSubConfig(servers[1].url, servers[1].accessToken, config) + + await server1Follows2(servers) + + await checkFollow(servers[0], servers[1], false) + await checkFollow(servers[1], servers[0], false) + + await acceptFollower(servers[1].url, servers[1].accessToken, 'peertube@' + servers[0].host) + await waitJobs(servers) + + await checkFollow(servers[0], servers[1], true) + await checkFollow(servers[1], servers[0], true) + + await resetFollows(servers) + }) + }) + + after(async function () { + await cleanupTests(servers) + }) +}) diff --git a/server/tests/api/server/config.ts b/server/tests/api/server/config.ts index 78fdc9cc0..b2f1933d1 100644 --- a/server/tests/api/server/config.ts +++ b/server/tests/api/server/config.ts @@ -68,6 +68,10 @@ function checkInitialConfig (server: ServerInfo, data: CustomConfig) { expect(data.followers.instance.enabled).to.be.true expect(data.followers.instance.manualApproval).to.be.false + + expect(data.followings.instance.autoFollowBack.enabled).to.be.false + expect(data.followings.instance.autoFollowIndex.enabled).to.be.false + expect(data.followings.instance.autoFollowIndex.indexUrl).to.equal('https://instances.joinpeertube.org') } function checkUpdatedConfig (data: CustomConfig) { @@ -119,6 +123,10 @@ function checkUpdatedConfig (data: CustomConfig) { expect(data.followers.instance.enabled).to.be.false expect(data.followers.instance.manualApproval).to.be.true + + expect(data.followings.instance.autoFollowBack.enabled).to.be.true + expect(data.followings.instance.autoFollowIndex.enabled).to.be.true + expect(data.followings.instance.autoFollowIndex.indexUrl).to.equal('https://updated.example.com') } describe('Test config', function () { @@ -261,6 +269,17 @@ describe('Test config', function () { enabled: false, manualApproval: true } + }, + followings: { + instance: { + autoFollowBack: { + enabled: true + }, + autoFollowIndex: { + enabled: true, + indexUrl: 'https://updated.example.com' + } + } } } await updateCustomConfig(server.url, server.accessToken, newCustomConfig) diff --git a/server/tests/api/server/index.ts b/server/tests/api/server/index.ts index 3daeeb49a..08205b2c8 100644 --- a/server/tests/api/server/index.ts +++ b/server/tests/api/server/index.ts @@ -1,3 +1,4 @@ +import './auto-follows' import './config' import './contact-form' import './email' -- cgit v1.2.3