From 8bff1fe009b555434c41bde361fddbb484329bae Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 7 May 2020 16:32:54 +0200 Subject: [PATCH] Add auto mute plugin tests --- server/tests/external-plugins/auto-mute.ts | 120 +++++++++++++++++++ server/tests/external-plugins/index.ts | 1 + shared/extra-utils/index.ts | 1 + shared/extra-utils/plugins/mock-blocklist.ts | 28 +++++ 4 files changed, 150 insertions(+) create mode 100644 server/tests/external-plugins/auto-mute.ts create mode 100644 shared/extra-utils/plugins/mock-blocklist.ts diff --git a/server/tests/external-plugins/auto-mute.ts b/server/tests/external-plugins/auto-mute.ts new file mode 100644 index 000000000..49b104882 --- /dev/null +++ b/server/tests/external-plugins/auto-mute.ts @@ -0,0 +1,120 @@ +/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ + +import 'mocha' + + +import { installPlugin, MockBlocklist, setAccessTokensToServers, uploadVideoAndGetId, updatePluginSettings, doubleFollow, getVideosList, wait } from '../../../shared/extra-utils' +import { cleanupTests, flushAndRunMultipleServers, ServerInfo } from '../../../shared/extra-utils/server/servers' +import { expect } from 'chai' + +describe('Official plugin auto-mute', function () { + let servers: ServerInfo[] + let blocklistServer: MockBlocklist + + before(async function () { + this.timeout(30000) + + servers = await flushAndRunMultipleServers(2) + await setAccessTokensToServers(servers) + + await installPlugin({ + url: servers[0].url, + accessToken: servers[0].accessToken, + npmName: 'peertube-plugin-auto-mute' + }) + + blocklistServer = new MockBlocklist() + await blocklistServer.initialize() + + await uploadVideoAndGetId({ server: servers[0], videoName: 'video server 1' }) + await uploadVideoAndGetId({ server: servers[1], videoName: 'video server 2' }) + + await doubleFollow(servers[0], servers[1]) + }) + + it('Should update plugin settings', async function () { + await updatePluginSettings({ + url: servers[0].url, + accessToken: servers[0].accessToken, + npmName: 'peertube-plugin-auto-mute', + settings: { + 'blocklist-urls': 'http://localhost:42100/blocklist', + 'check-seconds-interval': 1 + } + }) + }) + + it('Should add a server blocklist', async function () { + this.timeout(10000) + + blocklistServer.replace({ + data: [ + { + value: 'localhost:' + servers[1].port + } + ] + }) + + await wait(2000) + + const res = await getVideosList(servers[0].url) + expect(res.body.total).to.equal(1) + }) + + it('Should remove a server blocklist', async function () { + this.timeout(10000) + + blocklistServer.replace({ + data: [ + { + value: 'localhost:' + servers[1].port, + action: 'remove' + } + ] + }) + + await wait(2000) + + const res = await getVideosList(servers[0].url) + expect(res.body.total).to.equal(2) + }) + + it('Should add an account blocklist', async function () { + this.timeout(10000) + + blocklistServer.replace({ + data: [ + { + value: 'root@localhost:' + servers[1].port + } + ] + }) + + await wait(2000) + + const res = await getVideosList(servers[0].url) + expect(res.body.total).to.equal(1) + }) + + it('Should remove an account blocklist', async function () { + this.timeout(10000) + + blocklistServer.replace({ + data: [ + { + value: 'root@localhost:' + servers[1].port, + action: 'remove' + } + ] + }) + + await wait(2000) + + const res = await getVideosList(servers[0].url) + expect(res.body.total).to.equal(2) + }) + + after(async function () { + await cleanupTests(servers) + }) +}) diff --git a/server/tests/external-plugins/index.ts b/server/tests/external-plugins/index.ts index 1f1236c69..352d38de9 100644 --- a/server/tests/external-plugins/index.ts +++ b/server/tests/external-plugins/index.ts @@ -1 +1,2 @@ export * from './auth-ldap' +export * from './auto-mute' diff --git a/shared/extra-utils/index.ts b/shared/extra-utils/index.ts index fd8fef5dc..d3f010b20 100644 --- a/shared/extra-utils/index.ts +++ b/shared/extra-utils/index.ts @@ -27,3 +27,4 @@ export * from './videos/video-change-ownership' export * from './feeds/feeds' export * from './instances-index/mock-instances-index' export * from './search/videos' +export * from './plugins/mock-blocklist' diff --git a/shared/extra-utils/plugins/mock-blocklist.ts b/shared/extra-utils/plugins/mock-blocklist.ts new file mode 100644 index 000000000..ef57d96a8 --- /dev/null +++ b/shared/extra-utils/plugins/mock-blocklist.ts @@ -0,0 +1,28 @@ +import * as express from 'express' + +type BlocklistResponse = { + data: { + value: string + action?: 'add' | 'remove' + }[] +} + +export class MockBlocklist { + private body: BlocklistResponse + + initialize () { + return new Promise(res => { + const app = express() + + app.get('/blocklist', (req: express.Request, res: express.Response) => { + return res.json(this.body) + }) + + app.listen(42100, () => res()) + }) + } + + replace (body: BlocklistResponse) { + this.body = body + } +} -- 2.25.1