]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/plugins/plugin-helpers.ts
Introduce server commands
[github/Chocobozzz/PeerTube.git] / server / tests / plugins / plugin-helpers.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import 'mocha'
4 import { expect } from 'chai'
5 import { HttpStatusCode } from '@shared/core-utils'
6 import {
7 checkVideoFilesWereRemoved,
8 cleanupTests,
9 doubleFollow,
10 flushAndRunMultipleServers,
11 getVideo,
12 getVideosList,
13 makeGetRequest,
14 makePostBodyRequest,
15 PluginsCommand,
16 ServerInfo,
17 setAccessTokensToServers,
18 uploadVideoAndGetId,
19 viewVideo,
20 waitJobs
21 } from '@shared/extra-utils'
22
23 function postCommand (server: ServerInfo, command: string, bodyArg?: object) {
24 const body = { command }
25 if (bodyArg) Object.assign(body, bodyArg)
26
27 return makePostBodyRequest({
28 url: server.url,
29 path: '/plugins/test-four/router/commander',
30 fields: body,
31 statusCodeExpected: HttpStatusCode.NO_CONTENT_204
32 })
33 }
34
35 describe('Test plugin helpers', function () {
36 let servers: ServerInfo[]
37
38 before(async function () {
39 this.timeout(60000)
40
41 servers = await flushAndRunMultipleServers(2)
42 await setAccessTokensToServers(servers)
43
44 await doubleFollow(servers[0], servers[1])
45
46 await servers[0].pluginsCommand.install({ path: PluginsCommand.getPluginTestPath('-four') })
47 })
48
49 describe('Logger', function () {
50
51 it('Should have logged things', async function () {
52 await servers[0].serversCommand.waitUntilLog('localhost:' + servers[0].port + ' peertube-plugin-test-four', 1, false)
53 await servers[0].serversCommand.waitUntilLog('Hello world from plugin four', 1)
54 })
55 })
56
57 describe('Database', function () {
58
59 it('Should have made a query', async function () {
60 await servers[0].serversCommand.waitUntilLog(`root email is admin${servers[0].internalServerNumber}@example.com`)
61 })
62 })
63
64 describe('Config', function () {
65
66 it('Should have the correct webserver url', async function () {
67 await servers[0].serversCommand.waitUntilLog(`server url is http://localhost:${servers[0].port}`)
68 })
69
70 it('Should have the correct config', async function () {
71 const res = await makeGetRequest({
72 url: servers[0].url,
73 path: '/plugins/test-four/router/server-config',
74 statusCodeExpected: HttpStatusCode.OK_200
75 })
76
77 expect(res.body.serverConfig).to.exist
78 expect(res.body.serverConfig.instance.name).to.equal('PeerTube')
79 })
80 })
81
82 describe('Server', function () {
83
84 it('Should get the server actor', async function () {
85 await servers[0].serversCommand.waitUntilLog('server actor name is peertube')
86 })
87 })
88
89 describe('Plugin', function () {
90
91 it('Should get the base static route', async function () {
92 const res = await makeGetRequest({
93 url: servers[0].url,
94 path: '/plugins/test-four/router/static-route',
95 statusCodeExpected: HttpStatusCode.OK_200
96 })
97
98 expect(res.body.staticRoute).to.equal('/plugins/test-four/0.0.1/static/')
99 })
100
101 it('Should get the base static route', async function () {
102 const baseRouter = '/plugins/test-four/0.0.1/router/'
103
104 const res = await makeGetRequest({
105 url: servers[0].url,
106 path: baseRouter + 'router-route',
107 statusCodeExpected: HttpStatusCode.OK_200
108 })
109
110 expect(res.body.routerRoute).to.equal(baseRouter)
111 })
112 })
113
114 describe('User', function () {
115
116 it('Should not get a user if not authenticated', async function () {
117 await makeGetRequest({
118 url: servers[0].url,
119 path: '/plugins/test-four/router/user',
120 statusCodeExpected: HttpStatusCode.NOT_FOUND_404
121 })
122 })
123
124 it('Should get a user if authenticated', async function () {
125 const res = await makeGetRequest({
126 url: servers[0].url,
127 token: servers[0].accessToken,
128 path: '/plugins/test-four/router/user',
129 statusCodeExpected: HttpStatusCode.OK_200
130 })
131
132 expect(res.body.username).to.equal('root')
133 expect(res.body.displayName).to.equal('root')
134 expect(res.body.isAdmin).to.be.true
135 expect(res.body.isModerator).to.be.false
136 expect(res.body.isUser).to.be.false
137 })
138 })
139
140 describe('Moderation', function () {
141 let videoUUIDServer1: string
142
143 before(async function () {
144 this.timeout(60000)
145
146 {
147 const res = await uploadVideoAndGetId({ server: servers[0], videoName: 'video server 1' })
148 videoUUIDServer1 = res.uuid
149 }
150
151 {
152 await uploadVideoAndGetId({ server: servers[1], videoName: 'video server 2' })
153 }
154
155 await waitJobs(servers)
156
157 const res = await getVideosList(servers[0].url)
158 const videos = res.body.data
159
160 expect(videos).to.have.lengthOf(2)
161 })
162
163 it('Should mute server 2', async function () {
164 this.timeout(10000)
165 await postCommand(servers[0], 'blockServer', { hostToBlock: `localhost:${servers[1].port}` })
166
167 const res = await getVideosList(servers[0].url)
168 const videos = res.body.data
169
170 expect(videos).to.have.lengthOf(1)
171 expect(videos[0].name).to.equal('video server 1')
172 })
173
174 it('Should unmute server 2', async function () {
175 await postCommand(servers[0], 'unblockServer', { hostToUnblock: `localhost:${servers[1].port}` })
176
177 const res = await getVideosList(servers[0].url)
178 const videos = res.body.data
179
180 expect(videos).to.have.lengthOf(2)
181 })
182
183 it('Should mute account of server 2', async function () {
184 await postCommand(servers[0], 'blockAccount', { handleToBlock: `root@localhost:${servers[1].port}` })
185
186 const res = await getVideosList(servers[0].url)
187 const videos = res.body.data
188
189 expect(videos).to.have.lengthOf(1)
190 expect(videos[0].name).to.equal('video server 1')
191 })
192
193 it('Should unmute account of server 2', async function () {
194 await postCommand(servers[0], 'unblockAccount', { handleToUnblock: `root@localhost:${servers[1].port}` })
195
196 const res = await getVideosList(servers[0].url)
197 const videos = res.body.data
198
199 expect(videos).to.have.lengthOf(2)
200 })
201
202 it('Should blacklist video', async function () {
203 this.timeout(10000)
204
205 await postCommand(servers[0], 'blacklist', { videoUUID: videoUUIDServer1, unfederate: true })
206
207 await waitJobs(servers)
208
209 for (const server of servers) {
210 const res = await getVideosList(server.url)
211 const videos = res.body.data
212
213 expect(videos).to.have.lengthOf(1)
214 expect(videos[0].name).to.equal('video server 2')
215 }
216 })
217
218 it('Should unblacklist video', async function () {
219 this.timeout(10000)
220
221 await postCommand(servers[0], 'unblacklist', { videoUUID: videoUUIDServer1 })
222
223 await waitJobs(servers)
224
225 for (const server of servers) {
226 const res = await getVideosList(server.url)
227 const videos = res.body.data
228
229 expect(videos).to.have.lengthOf(2)
230 }
231 })
232 })
233
234 describe('Videos', function () {
235 let videoUUID: string
236
237 before(async () => {
238 const res = await uploadVideoAndGetId({ server: servers[0], videoName: 'video1' })
239 videoUUID = res.uuid
240 })
241
242 it('Should remove a video after a view', async function () {
243 this.timeout(40000)
244
245 // Should not throw -> video exists
246 await getVideo(servers[0].url, videoUUID)
247 // Should delete the video
248 await viewVideo(servers[0].url, videoUUID)
249
250 await servers[0].serversCommand.waitUntilLog('Video deleted by plugin four.')
251
252 try {
253 // Should throw because the video should have been deleted
254 await getVideo(servers[0].url, videoUUID)
255 throw new Error('Video exists')
256 } catch (err) {
257 if (err.message.includes('exists')) throw err
258 }
259
260 await checkVideoFilesWereRemoved(videoUUID, servers[0])
261 })
262
263 it('Should have fetched the video by URL', async function () {
264 await servers[0].serversCommand.waitUntilLog(`video from DB uuid is ${videoUUID}`)
265 })
266 })
267
268 after(async function () {
269 await cleanupTests(servers)
270 })
271 })