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