diff options
author | Chocobozzz <me@florianbigard.com> | 2020-05-07 14:58:24 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2020-05-07 15:07:01 +0200 |
commit | 80fdaf064562aff968f4c9cea1cf220bc12a70da (patch) | |
tree | a0ec49f433b828660f741b394dfd87056cd2c6ef /server/tests | |
parent | 3cc665f48fd233d09f778d7e887488dde6f03ef6 (diff) | |
download | PeerTube-80fdaf064562aff968f4c9cea1cf220bc12a70da.tar.gz PeerTube-80fdaf064562aff968f4c9cea1cf220bc12a70da.tar.zst PeerTube-80fdaf064562aff968f4c9cea1cf220bc12a70da.zip |
Add moderation helpers to plugins
Diffstat (limited to 'server/tests')
-rw-r--r-- | server/tests/fixtures/peertube-plugin-test-four/main.js | 107 | ||||
-rw-r--r-- | server/tests/plugins/plugin-helpers.ts | 204 |
2 files changed, 265 insertions, 46 deletions
diff --git a/server/tests/fixtures/peertube-plugin-test-four/main.js b/server/tests/fixtures/peertube-plugin-test-four/main.js index 2e81550c1..067c3fe15 100644 --- a/server/tests/fixtures/peertube-plugin-test-four/main.js +++ b/server/tests/fixtures/peertube-plugin-test-four/main.js | |||
@@ -1,30 +1,70 @@ | |||
1 | async function register ({ | 1 | async function register ({ |
2 | peertubeHelpers, | 2 | peertubeHelpers, |
3 | registerHook | 3 | registerHook, |
4 | getRouter | ||
4 | }) { | 5 | }) { |
5 | const logger = peertubeHelpers.logger | 6 | const logger = peertubeHelpers.logger |
6 | 7 | ||
7 | logger.info('Hello world from plugin four') | 8 | logger.info('Hello world from plugin four') |
8 | 9 | ||
9 | const username = 'root' | 10 | { |
10 | const results = await peertubeHelpers.database.query( | 11 | const username = 'root' |
11 | 'SELECT "email" from "user" WHERE "username" = $username', | 12 | const results = await peertubeHelpers.database.query( |
12 | { | 13 | 'SELECT "email" from "user" WHERE "username" = $username', |
13 | type: 'SELECT', | 14 | { |
14 | bind: { username } | 15 | type: 'SELECT', |
15 | } | 16 | bind: { username } |
16 | ) | 17 | } |
18 | ) | ||
19 | |||
20 | logger.info('root email is ' + results[0]['email']) | ||
21 | } | ||
22 | |||
23 | { | ||
24 | registerHook({ | ||
25 | target: 'action:api.video.viewed', | ||
26 | handler: async ({ video }) => { | ||
27 | const videoFromDB = await peertubeHelpers.videos.loadByUrl(video.url) | ||
28 | logger.info('video from DB uuid is %s.', videoFromDB.uuid) | ||
29 | |||
30 | await peertubeHelpers.videos.removeVideo(video.id) | ||
17 | 31 | ||
18 | logger.info('root email is ' + results[0]['email']) | 32 | logger.info('Video deleted by plugin four.') |
33 | } | ||
34 | }) | ||
35 | } | ||
19 | 36 | ||
20 | registerHook({ | 37 | { |
21 | target: 'action:api.video.viewed', | 38 | const serverActor = await peertubeHelpers.server.getServerActor() |
22 | handler: async ({ video }) => { | 39 | logger.info('server actor name is %s', serverActor.preferredUsername) |
23 | await peertubeHelpers.videos.removeVideo(video.id) | 40 | } |
24 | 41 | ||
25 | logger.info('Video deleted by plugin four.') | 42 | { |
43 | logger.info('server url is %s', peertubeHelpers.config.getWebserverUrl()) | ||
44 | } | ||
45 | |||
46 | { | ||
47 | const actions = { | ||
48 | blockServer, | ||
49 | unblockServer, | ||
50 | blockAccount, | ||
51 | unblockAccount, | ||
52 | blacklist, | ||
53 | unblacklist | ||
26 | } | 54 | } |
27 | }) | 55 | |
56 | const router = getRouter() | ||
57 | router.post('/commander', async (req, res) => { | ||
58 | try { | ||
59 | await actions[req.body.command](peertubeHelpers, req.body) | ||
60 | |||
61 | res.sendStatus(204) | ||
62 | } catch (err) { | ||
63 | logger.error('Error in commander.', { err }) | ||
64 | res.sendStatus(500) | ||
65 | } | ||
66 | }) | ||
67 | } | ||
28 | } | 68 | } |
29 | 69 | ||
30 | async function unregister () { | 70 | async function unregister () { |
@@ -37,3 +77,38 @@ module.exports = { | |||
37 | } | 77 | } |
38 | 78 | ||
39 | // ########################################################################### | 79 | // ########################################################################### |
80 | |||
81 | async function blockServer (peertubeHelpers, body) { | ||
82 | const serverActor = await peertubeHelpers.server.getServerActor() | ||
83 | |||
84 | await peertubeHelpers.moderation.blockServer({ byAccountId: serverActor.Account.id, hostToBlock: body.hostToBlock }) | ||
85 | } | ||
86 | |||
87 | async function unblockServer (peertubeHelpers, body) { | ||
88 | const serverActor = await peertubeHelpers.server.getServerActor() | ||
89 | |||
90 | await peertubeHelpers.moderation.unblockServer({ byAccountId: serverActor.Account.id, hostToUnblock: body.hostToUnblock }) | ||
91 | } | ||
92 | |||
93 | async function blockAccount (peertubeHelpers, body) { | ||
94 | const serverActor = await peertubeHelpers.server.getServerActor() | ||
95 | |||
96 | await peertubeHelpers.moderation.blockAccount({ byAccountId: serverActor.Account.id, handleToBlock: body.handleToBlock }) | ||
97 | } | ||
98 | |||
99 | async function unblockAccount (peertubeHelpers, body) { | ||
100 | const serverActor = await peertubeHelpers.server.getServerActor() | ||
101 | |||
102 | await peertubeHelpers.moderation.unblockAccount({ byAccountId: serverActor.Account.id, handleToUnblock: body.handleToUnblock }) | ||
103 | } | ||
104 | |||
105 | async function blacklist (peertubeHelpers, body) { | ||
106 | await peertubeHelpers.moderation.blacklistVideo({ | ||
107 | videoIdOrUUID: body.videoUUID, | ||
108 | createOptions: body | ||
109 | }) | ||
110 | } | ||
111 | |||
112 | async function unblacklist (peertubeHelpers, body) { | ||
113 | await peertubeHelpers.moderation.unblacklistVideo({ videoIdOrUUID: body.videoUUID }) | ||
114 | } | ||
diff --git a/server/tests/plugins/plugin-helpers.ts b/server/tests/plugins/plugin-helpers.ts index dfe8ebe55..0915603d0 100644 --- a/server/tests/plugins/plugin-helpers.ts +++ b/server/tests/plugins/plugin-helpers.ts | |||
@@ -1,66 +1,210 @@ | |||
1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ | 1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ |
2 | 2 | ||
3 | import 'mocha' | 3 | import 'mocha' |
4 | import { cleanupTests, flushAndRunServer, ServerInfo, waitUntilLog } from '../../../shared/extra-utils/server/servers' | ||
5 | import { | 4 | import { |
6 | checkVideoFilesWereRemoved, | 5 | checkVideoFilesWereRemoved, |
6 | doubleFollow, | ||
7 | getPluginTestPath, | 7 | getPluginTestPath, |
8 | getVideo, | 8 | getVideo, |
9 | installPlugin, | 9 | installPlugin, |
10 | makePostBodyRequest, | ||
10 | setAccessTokensToServers, | 11 | setAccessTokensToServers, |
11 | uploadVideoAndGetId, | 12 | uploadVideoAndGetId, |
12 | viewVideo | 13 | viewVideo, |
14 | getVideosList, | ||
15 | waitJobs | ||
13 | } from '../../../shared/extra-utils' | 16 | } from '../../../shared/extra-utils' |
17 | import { cleanupTests, flushAndRunMultipleServers, ServerInfo, waitUntilLog } from '../../../shared/extra-utils/server/servers' | ||
18 | import { expect } from 'chai' | ||
19 | |||
20 | function postCommand (server: ServerInfo, command: string, bodyArg?: object) { | ||
21 | const body = { command } | ||
22 | if (bodyArg) Object.assign(body, bodyArg) | ||
23 | |||
24 | return makePostBodyRequest({ | ||
25 | url: server.url, | ||
26 | path: '/plugins/test-four/router/commander', | ||
27 | fields: body, | ||
28 | statusCodeExpected: 204 | ||
29 | }) | ||
30 | } | ||
14 | 31 | ||
15 | describe('Test plugin helpers', function () { | 32 | describe('Test plugin helpers', function () { |
16 | let server: ServerInfo | 33 | let servers: ServerInfo[] |
17 | 34 | ||
18 | before(async function () { | 35 | before(async function () { |
19 | this.timeout(30000) | 36 | this.timeout(60000) |
37 | |||
38 | servers = await flushAndRunMultipleServers(2) | ||
39 | await setAccessTokensToServers(servers) | ||
20 | 40 | ||
21 | server = await flushAndRunServer(1) | 41 | await doubleFollow(servers[0], servers[1]) |
22 | await setAccessTokensToServers([ server ]) | ||
23 | 42 | ||
24 | await installPlugin({ | 43 | await installPlugin({ |
25 | url: server.url, | 44 | url: servers[0].url, |
26 | accessToken: server.accessToken, | 45 | accessToken: servers[0].accessToken, |
27 | path: getPluginTestPath('-four') | 46 | path: getPluginTestPath('-four') |
28 | }) | 47 | }) |
29 | }) | 48 | }) |
30 | 49 | ||
31 | it('Should have logged things', async function () { | 50 | describe('Logger', function () { |
32 | await waitUntilLog(server, 'localhost:' + server.port + ' peertube-plugin-test-four', 1, false) | 51 | |
33 | await waitUntilLog(server, 'Hello world from plugin four', 1) | 52 | it('Should have logged things', async function () { |
53 | await waitUntilLog(servers[0], 'localhost:' + servers[0].port + ' peertube-plugin-test-four', 1, false) | ||
54 | await waitUntilLog(servers[0], 'Hello world from plugin four', 1) | ||
55 | }) | ||
34 | }) | 56 | }) |
35 | 57 | ||
36 | it('Should have made a query', async function () { | 58 | describe('Database', function () { |
37 | await waitUntilLog(server, `root email is admin${server.internalServerNumber}@example.com`, 1) | 59 | |
60 | it('Should have made a query', async function () { | ||
61 | await waitUntilLog(servers[0], `root email is admin${servers[0].internalServerNumber}@example.com`) | ||
62 | }) | ||
63 | }) | ||
64 | |||
65 | describe('Config', function () { | ||
66 | |||
67 | it('Should have the correct webserver url', async function () { | ||
68 | await waitUntilLog(servers[0], `server url is http://localhost:${servers[0].port}`) | ||
69 | }) | ||
70 | }) | ||
71 | |||
72 | describe('Server', function () { | ||
73 | |||
74 | it('Should get the server actor', async function () { | ||
75 | await waitUntilLog(servers[0], 'server actor name is peertube') | ||
76 | }) | ||
77 | }) | ||
78 | |||
79 | describe('Moderation', function () { | ||
80 | let videoUUIDServer1: string | ||
81 | |||
82 | before(async function () { | ||
83 | this.timeout(15000) | ||
84 | |||
85 | { | ||
86 | const res = await uploadVideoAndGetId({ server: servers[0], videoName: 'video server 1' }) | ||
87 | videoUUIDServer1 = res.uuid | ||
88 | } | ||
89 | |||
90 | { | ||
91 | await uploadVideoAndGetId({ server: servers[1], videoName: 'video server 2' }) | ||
92 | } | ||
93 | |||
94 | await waitJobs(servers) | ||
95 | |||
96 | const res = await getVideosList(servers[0].url) | ||
97 | const videos = res.body.data | ||
98 | |||
99 | expect(videos).to.have.lengthOf(2) | ||
100 | }) | ||
101 | |||
102 | it('Should mute server 2', async function () { | ||
103 | this.timeout(10000) | ||
104 | await postCommand(servers[0], 'blockServer', { hostToBlock: `localhost:${servers[1].port}` }) | ||
105 | |||
106 | const res = await getVideosList(servers[0].url) | ||
107 | const videos = res.body.data | ||
108 | |||
109 | expect(videos).to.have.lengthOf(1) | ||
110 | expect(videos[0].name).to.equal('video server 1') | ||
111 | }) | ||
112 | |||
113 | it('Should unmute server 2', async function () { | ||
114 | await postCommand(servers[0], 'unblockServer', { hostToUnblock: `localhost:${servers[1].port}` }) | ||
115 | |||
116 | const res = await getVideosList(servers[0].url) | ||
117 | const videos = res.body.data | ||
118 | |||
119 | expect(videos).to.have.lengthOf(2) | ||
120 | }) | ||
121 | |||
122 | it('Should mute account of server 2', async function () { | ||
123 | await postCommand(servers[0], 'blockAccount', { handleToBlock: `root@localhost:${servers[1].port}` }) | ||
124 | |||
125 | const res = await getVideosList(servers[0].url) | ||
126 | const videos = res.body.data | ||
127 | |||
128 | expect(videos).to.have.lengthOf(1) | ||
129 | expect(videos[0].name).to.equal('video server 1') | ||
130 | }) | ||
131 | |||
132 | it('Should unmute account of server 2', async function () { | ||
133 | await postCommand(servers[0], 'unblockAccount', { handleToUnblock: `root@localhost:${servers[1].port}` }) | ||
134 | |||
135 | const res = await getVideosList(servers[0].url) | ||
136 | const videos = res.body.data | ||
137 | |||
138 | expect(videos).to.have.lengthOf(2) | ||
139 | }) | ||
140 | |||
141 | it('Should blacklist video', async function () { | ||
142 | this.timeout(10000) | ||
143 | |||
144 | await postCommand(servers[0], 'blacklist', { videoUUID: videoUUIDServer1, unfederate: true }) | ||
145 | |||
146 | await waitJobs(servers) | ||
147 | |||
148 | for (const server of servers) { | ||
149 | const res = await getVideosList(server.url) | ||
150 | const videos = res.body.data | ||
151 | |||
152 | expect(videos).to.have.lengthOf(1) | ||
153 | expect(videos[0].name).to.equal('video server 2') | ||
154 | } | ||
155 | }) | ||
156 | |||
157 | it('Should unblacklist video', async function () { | ||
158 | this.timeout(10000) | ||
159 | |||
160 | await postCommand(servers[0], 'unblacklist', { videoUUID: videoUUIDServer1 }) | ||
161 | |||
162 | await waitJobs(servers) | ||
163 | |||
164 | for (const server of servers) { | ||
165 | const res = await getVideosList(server.url) | ||
166 | const videos = res.body.data | ||
167 | |||
168 | expect(videos).to.have.lengthOf(2) | ||
169 | } | ||
170 | }) | ||
38 | }) | 171 | }) |
39 | 172 | ||
40 | it('Should remove a video after a view', async function () { | 173 | describe('Videos', function () { |
41 | this.timeout(20000) | 174 | let videoUUID: string |
175 | |||
176 | before(async () => { | ||
177 | const res = await uploadVideoAndGetId({ server: servers[0], videoName: 'video1' }) | ||
178 | videoUUID = res.uuid | ||
179 | }) | ||
42 | 180 | ||
43 | const videoUUID = (await uploadVideoAndGetId({ server: server, videoName: 'video1' })).uuid | 181 | it('Should remove a video after a view', async function () { |
182 | this.timeout(20000) | ||
44 | 183 | ||
45 | // Should not throw -> video exists | 184 | // Should not throw -> video exists |
46 | await getVideo(server.url, videoUUID) | 185 | await getVideo(servers[0].url, videoUUID) |
47 | // Should delete the video | 186 | // Should delete the video |
48 | await viewVideo(server.url, videoUUID) | 187 | await viewVideo(servers[0].url, videoUUID) |
49 | 188 | ||
50 | await waitUntilLog(server, 'Video deleted by plugin four.', 1) | 189 | await waitUntilLog(servers[0], 'Video deleted by plugin four.') |
51 | 190 | ||
52 | try { | 191 | try { |
53 | // Should throw because the video should have been deleted | 192 | // Should throw because the video should have been deleted |
54 | await getVideo(server.url, videoUUID) | 193 | await getVideo(servers[0].url, videoUUID) |
55 | throw new Error('Video exists') | 194 | throw new Error('Video exists') |
56 | } catch (err) { | 195 | } catch (err) { |
57 | if (err.message.includes('exists')) throw err | 196 | if (err.message.includes('exists')) throw err |
58 | } | 197 | } |
59 | 198 | ||
60 | await checkVideoFilesWereRemoved(videoUUID, server.internalServerNumber) | 199 | await checkVideoFilesWereRemoved(videoUUID, servers[0].internalServerNumber) |
200 | }) | ||
201 | |||
202 | it('Should have fetched the video by URL', async function () { | ||
203 | await waitUntilLog(servers[0], `video from DB uuid is ${videoUUID}`) | ||
204 | }) | ||
61 | }) | 205 | }) |
62 | 206 | ||
63 | after(async function () { | 207 | after(async function () { |
64 | await cleanupTests([ server ]) | 208 | await cleanupTests(servers) |
65 | }) | 209 | }) |
66 | }) | 210 | }) |