]>
Commit | Line | Data |
---|---|---|
1b05d82d C |
1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ |
2 | ||
ae2abfd3 | 3 | import { expect } from 'chai' |
2e9c7877 | 4 | import { pathExists } from 'fs-extra' |
c55e3d72 | 5 | import { HttpStatusCode, ThumbnailType } from '@shared/models' |
ab3ead3a | 6 | import { |
ae2abfd3 | 7 | cleanupTests, |
254d3579 | 8 | createMultipleServers, |
4c7e60bc | 9 | doubleFollow, |
ae2abfd3 | 10 | makeGetRequest, |
80fdaf06 | 11 | makePostBodyRequest, |
2e9c7877 | 12 | makeRawRequest, |
254d3579 | 13 | PeerTubeServer, |
4c7e60bc | 14 | PluginsCommand, |
ab3ead3a | 15 | setAccessTokensToServers, |
6c5065a0 | 16 | waitJobs |
bf54587a | 17 | } from '@shared/server-commands' |
c55e3d72 | 18 | import { checkVideoFilesWereRemoved } from '../shared' |
80fdaf06 | 19 | |
254d3579 | 20 | function postCommand (server: PeerTubeServer, command: string, bodyArg?: object) { |
80fdaf06 C |
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, | |
c0e8b12e | 28 | expectedStatus: HttpStatusCode.NO_CONTENT_204 |
80fdaf06 C |
29 | }) |
30 | } | |
1b05d82d C |
31 | |
32 | describe('Test plugin helpers', function () { | |
254d3579 | 33 | let servers: PeerTubeServer[] |
1b05d82d C |
34 | |
35 | before(async function () { | |
80fdaf06 C |
36 | this.timeout(60000) |
37 | ||
254d3579 | 38 | servers = await createMultipleServers(2) |
80fdaf06 | 39 | await setAccessTokensToServers(servers) |
1b05d82d | 40 | |
80fdaf06 | 41 | await doubleFollow(servers[0], servers[1]) |
1b05d82d | 42 | |
89d241a7 | 43 | await servers[0].plugins.install({ path: PluginsCommand.getPluginTestPath('-four') }) |
1b05d82d C |
44 | }) |
45 | ||
80fdaf06 C |
46 | describe('Logger', function () { |
47 | ||
48 | it('Should have logged things', async function () { | |
89d241a7 C |
49 | await servers[0].servers.waitUntilLog('localhost:' + servers[0].port + ' peertube-plugin-test-four', 1, false) |
50 | await servers[0].servers.waitUntilLog('Hello world from plugin four', 1) | |
80fdaf06 | 51 | }) |
1b05d82d C |
52 | }) |
53 | ||
80fdaf06 C |
54 | describe('Database', function () { |
55 | ||
56 | it('Should have made a query', async function () { | |
89d241a7 | 57 | await servers[0].servers.waitUntilLog(`root email is admin${servers[0].internalServerNumber}@example.com`) |
80fdaf06 C |
58 | }) |
59 | }) | |
60 | ||
61 | describe('Config', function () { | |
62 | ||
63 | it('Should have the correct webserver url', async function () { | |
89d241a7 | 64 | await servers[0].servers.waitUntilLog(`server url is http://localhost:${servers[0].port}`) |
80fdaf06 | 65 | }) |
22820226 C |
66 | |
67 | it('Should have the correct config', async function () { | |
68 | const res = await makeGetRequest({ | |
69 | url: servers[0].url, | |
70 | path: '/plugins/test-four/router/server-config', | |
c0e8b12e | 71 | expectedStatus: HttpStatusCode.OK_200 |
22820226 C |
72 | }) |
73 | ||
74 | expect(res.body.serverConfig).to.exist | |
75 | expect(res.body.serverConfig.instance.name).to.equal('PeerTube') | |
76 | }) | |
80fdaf06 C |
77 | }) |
78 | ||
79 | describe('Server', function () { | |
80 | ||
81 | it('Should get the server actor', async function () { | |
89d241a7 | 82 | await servers[0].servers.waitUntilLog('server actor name is peertube') |
80fdaf06 C |
83 | }) |
84 | }) | |
85 | ||
22820226 C |
86 | describe('Plugin', function () { |
87 | ||
88 | it('Should get the base static route', async function () { | |
89 | const res = await makeGetRequest({ | |
90 | url: servers[0].url, | |
91 | path: '/plugins/test-four/router/static-route', | |
c0e8b12e | 92 | expectedStatus: HttpStatusCode.OK_200 |
22820226 C |
93 | }) |
94 | ||
95 | expect(res.body.staticRoute).to.equal('/plugins/test-four/0.0.1/static/') | |
96 | }) | |
302eba0d C |
97 | |
98 | it('Should get the base static route', async function () { | |
99 | const baseRouter = '/plugins/test-four/0.0.1/router/' | |
100 | ||
101 | const res = await makeGetRequest({ | |
102 | url: servers[0].url, | |
103 | path: baseRouter + 'router-route', | |
c0e8b12e | 104 | expectedStatus: HttpStatusCode.OK_200 |
302eba0d C |
105 | }) |
106 | ||
107 | expect(res.body.routerRoute).to.equal(baseRouter) | |
108 | }) | |
109 | }) | |
110 | ||
111 | describe('User', function () { | |
22df69fd | 112 | let rootId: number |
302eba0d C |
113 | |
114 | it('Should not get a user if not authenticated', async function () { | |
62906990 | 115 | await makeGetRequest({ |
302eba0d C |
116 | url: servers[0].url, |
117 | path: '/plugins/test-four/router/user', | |
c0e8b12e | 118 | expectedStatus: HttpStatusCode.NOT_FOUND_404 |
302eba0d | 119 | }) |
302eba0d C |
120 | }) |
121 | ||
122 | it('Should get a user if authenticated', async function () { | |
123 | const res = await makeGetRequest({ | |
124 | url: servers[0].url, | |
125 | token: servers[0].accessToken, | |
126 | path: '/plugins/test-four/router/user', | |
c0e8b12e | 127 | expectedStatus: HttpStatusCode.OK_200 |
302eba0d C |
128 | }) |
129 | ||
302eba0d | 130 | expect(res.body.username).to.equal('root') |
b31d7262 | 131 | expect(res.body.displayName).to.equal('root') |
302eba0d C |
132 | expect(res.body.isAdmin).to.be.true |
133 | expect(res.body.isModerator).to.be.false | |
134 | expect(res.body.isUser).to.be.false | |
22df69fd C |
135 | |
136 | rootId = res.body.id | |
137 | }) | |
138 | ||
139 | it('Should load a user by id', async function () { | |
140 | { | |
141 | const res = await makeGetRequest({ | |
142 | url: servers[0].url, | |
143 | path: '/plugins/test-four/router/user/' + rootId, | |
144 | expectedStatus: HttpStatusCode.OK_200 | |
145 | }) | |
146 | ||
147 | expect(res.body.username).to.equal('root') | |
148 | } | |
149 | ||
150 | { | |
151 | await makeGetRequest({ | |
152 | url: servers[0].url, | |
153 | path: '/plugins/test-four/router/user/42', | |
154 | expectedStatus: HttpStatusCode.NOT_FOUND_404 | |
155 | }) | |
156 | } | |
302eba0d | 157 | }) |
22820226 C |
158 | }) |
159 | ||
80fdaf06 C |
160 | describe('Moderation', function () { |
161 | let videoUUIDServer1: string | |
162 | ||
163 | before(async function () { | |
75e12406 | 164 | this.timeout(60000) |
80fdaf06 C |
165 | |
166 | { | |
c0e8b12e | 167 | const res = await servers[0].videos.quickUpload({ name: 'video server 1' }) |
80fdaf06 C |
168 | videoUUIDServer1 = res.uuid |
169 | } | |
170 | ||
171 | { | |
c0e8b12e | 172 | await servers[1].videos.quickUpload({ name: 'video server 2' }) |
80fdaf06 C |
173 | } |
174 | ||
175 | await waitJobs(servers) | |
176 | ||
89d241a7 | 177 | const { data } = await servers[0].videos.list() |
80fdaf06 | 178 | |
d23dd9fb | 179 | expect(data).to.have.lengthOf(2) |
80fdaf06 C |
180 | }) |
181 | ||
182 | it('Should mute server 2', async function () { | |
183 | this.timeout(10000) | |
184 | await postCommand(servers[0], 'blockServer', { hostToBlock: `localhost:${servers[1].port}` }) | |
185 | ||
89d241a7 | 186 | const { data } = await servers[0].videos.list() |
80fdaf06 | 187 | |
d23dd9fb C |
188 | expect(data).to.have.lengthOf(1) |
189 | expect(data[0].name).to.equal('video server 1') | |
80fdaf06 C |
190 | }) |
191 | ||
192 | it('Should unmute server 2', async function () { | |
193 | await postCommand(servers[0], 'unblockServer', { hostToUnblock: `localhost:${servers[1].port}` }) | |
194 | ||
89d241a7 | 195 | const { data } = await servers[0].videos.list() |
80fdaf06 | 196 | |
d23dd9fb | 197 | expect(data).to.have.lengthOf(2) |
80fdaf06 C |
198 | }) |
199 | ||
200 | it('Should mute account of server 2', async function () { | |
201 | await postCommand(servers[0], 'blockAccount', { handleToBlock: `root@localhost:${servers[1].port}` }) | |
202 | ||
89d241a7 | 203 | const { data } = await servers[0].videos.list() |
80fdaf06 | 204 | |
d23dd9fb C |
205 | expect(data).to.have.lengthOf(1) |
206 | expect(data[0].name).to.equal('video server 1') | |
80fdaf06 C |
207 | }) |
208 | ||
209 | it('Should unmute account of server 2', async function () { | |
210 | await postCommand(servers[0], 'unblockAccount', { handleToUnblock: `root@localhost:${servers[1].port}` }) | |
211 | ||
89d241a7 | 212 | const { data } = await servers[0].videos.list() |
80fdaf06 | 213 | |
d23dd9fb | 214 | expect(data).to.have.lengthOf(2) |
80fdaf06 C |
215 | }) |
216 | ||
217 | it('Should blacklist video', async function () { | |
218 | this.timeout(10000) | |
219 | ||
220 | await postCommand(servers[0], 'blacklist', { videoUUID: videoUUIDServer1, unfederate: true }) | |
221 | ||
222 | await waitJobs(servers) | |
223 | ||
224 | for (const server of servers) { | |
89d241a7 | 225 | const { data } = await server.videos.list() |
80fdaf06 | 226 | |
d23dd9fb C |
227 | expect(data).to.have.lengthOf(1) |
228 | expect(data[0].name).to.equal('video server 2') | |
80fdaf06 C |
229 | } |
230 | }) | |
231 | ||
232 | it('Should unblacklist video', async function () { | |
233 | this.timeout(10000) | |
234 | ||
235 | await postCommand(servers[0], 'unblacklist', { videoUUID: videoUUIDServer1 }) | |
236 | ||
237 | await waitJobs(servers) | |
238 | ||
239 | for (const server of servers) { | |
89d241a7 | 240 | const { data } = await server.videos.list() |
80fdaf06 | 241 | |
d23dd9fb | 242 | expect(data).to.have.lengthOf(2) |
80fdaf06 C |
243 | } |
244 | }) | |
1b05d82d C |
245 | }) |
246 | ||
80fdaf06 C |
247 | describe('Videos', function () { |
248 | let videoUUID: string | |
754c52b9 | 249 | let videoPath: string |
80fdaf06 C |
250 | |
251 | before(async () => { | |
2e9c7877 C |
252 | this.timeout(240000) |
253 | ||
254 | await servers[0].config.enableTranscoding() | |
255 | ||
c0e8b12e | 256 | const res = await servers[0].videos.quickUpload({ name: 'video1' }) |
80fdaf06 | 257 | videoUUID = res.uuid |
2e9c7877 C |
258 | |
259 | await waitJobs(servers) | |
260 | }) | |
261 | ||
262 | it('Should get video files', async function () { | |
263 | const { body } = await makeGetRequest({ | |
264 | url: servers[0].url, | |
265 | path: '/plugins/test-four/router/video-files/' + videoUUID, | |
266 | expectedStatus: HttpStatusCode.OK_200 | |
267 | }) | |
268 | ||
269 | // Video files check | |
270 | { | |
271 | expect(body.webtorrent.videoFiles).to.be.an('array') | |
272 | expect(body.hls.videoFiles).to.be.an('array') | |
273 | ||
274 | for (const resolution of [ 144, 240, 360, 480, 720 ]) { | |
275 | for (const files of [ body.webtorrent.videoFiles, body.hls.videoFiles ]) { | |
276 | const file = files.find(f => f.resolution === resolution) | |
277 | expect(file).to.exist | |
278 | ||
279 | expect(file.size).to.be.a('number') | |
280 | expect(file.fps).to.equal(25) | |
281 | ||
282 | expect(await pathExists(file.path)).to.be.true | |
283 | await makeRawRequest(file.url, HttpStatusCode.OK_200) | |
284 | } | |
285 | } | |
754c52b9 C |
286 | |
287 | videoPath = body.webtorrent.videoFiles[0].path | |
2e9c7877 C |
288 | } |
289 | ||
290 | // Thumbnails check | |
291 | { | |
292 | expect(body.thumbnails).to.be.an('array') | |
293 | ||
294 | const miniature = body.thumbnails.find(t => t.type === ThumbnailType.MINIATURE) | |
295 | expect(miniature).to.exist | |
296 | expect(await pathExists(miniature.path)).to.be.true | |
297 | await makeRawRequest(miniature.url, HttpStatusCode.OK_200) | |
298 | ||
299 | const preview = body.thumbnails.find(t => t.type === ThumbnailType.PREVIEW) | |
300 | expect(preview).to.exist | |
301 | expect(await pathExists(preview.path)).to.be.true | |
302 | await makeRawRequest(preview.url, HttpStatusCode.OK_200) | |
303 | } | |
80fdaf06 | 304 | }) |
ab3ead3a | 305 | |
754c52b9 C |
306 | it('Should probe a file', async function () { |
307 | const { body } = await makeGetRequest({ | |
308 | url: servers[0].url, | |
309 | path: '/plugins/test-four/router/ffprobe', | |
310 | query: { | |
311 | path: videoPath | |
312 | }, | |
313 | expectedStatus: HttpStatusCode.OK_200 | |
314 | }) | |
315 | ||
316 | expect(body.streams).to.be.an('array') | |
317 | expect(body.streams).to.have.lengthOf(2) | |
318 | }) | |
319 | ||
80fdaf06 | 320 | it('Should remove a video after a view', async function () { |
59fd824c | 321 | this.timeout(40000) |
ab3ead3a | 322 | |
80fdaf06 | 323 | // Should not throw -> video exists |
83903cb6 | 324 | const video = await servers[0].videos.get({ id: videoUUID }) |
80fdaf06 | 325 | // Should delete the video |
b2111066 | 326 | await servers[0].views.simulateView({ id: videoUUID }) |
ab3ead3a | 327 | |
89d241a7 | 328 | await servers[0].servers.waitUntilLog('Video deleted by plugin four.') |
ab3ead3a | 329 | |
80fdaf06 C |
330 | try { |
331 | // Should throw because the video should have been deleted | |
89d241a7 | 332 | await servers[0].videos.get({ id: videoUUID }) |
80fdaf06 C |
333 | throw new Error('Video exists') |
334 | } catch (err) { | |
335 | if (err.message.includes('exists')) throw err | |
336 | } | |
ab3ead3a | 337 | |
83903cb6 | 338 | await checkVideoFilesWereRemoved({ server: servers[0], video }) |
80fdaf06 C |
339 | }) |
340 | ||
341 | it('Should have fetched the video by URL', async function () { | |
89d241a7 | 342 | await servers[0].servers.waitUntilLog(`video from DB uuid is ${videoUUID}`) |
80fdaf06 | 343 | }) |
ab3ead3a C |
344 | }) |
345 | ||
1b05d82d | 346 | after(async function () { |
80fdaf06 | 347 | await cleanupTests(servers) |
1b05d82d C |
348 | }) |
349 | }) |