1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import { expect } from 'chai'
4 import { pathExists } from 'fs-extra'
5 import { HttpStatusCode, ThumbnailType } from '@shared/models'
15 setAccessTokensToServers,
17 } from '@shared/server-commands'
18 import { checkVideoFilesWereRemoved } from '../shared'
20 function postCommand (server: PeerTubeServer, command: string, bodyArg?: object) {
21 const body = { command }
22 if (bodyArg) Object.assign(body, bodyArg)
24 return makePostBodyRequest({
26 path: '/plugins/test-four/router/commander',
28 expectedStatus: HttpStatusCode.NO_CONTENT_204
32 describe('Test plugin helpers', function () {
33 let servers: PeerTubeServer[]
35 before(async function () {
38 servers = await createMultipleServers(2)
39 await setAccessTokensToServers(servers)
41 await doubleFollow(servers[0], servers[1])
43 await servers[0].plugins.install({ path: PluginsCommand.getPluginTestPath('-four') })
46 describe('Logger', function () {
48 it('Should have logged things', async function () {
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)
54 describe('Database', function () {
56 it('Should have made a query', async function () {
57 await servers[0].servers.waitUntilLog(`root email is admin${servers[0].internalServerNumber}@example.com`)
61 describe('Config', function () {
63 it('Should have the correct webserver url', async function () {
64 await servers[0].servers.waitUntilLog(`server url is http://localhost:${servers[0].port}`)
67 it('Should have the correct config', async function () {
68 const res = await makeGetRequest({
70 path: '/plugins/test-four/router/server-config',
71 expectedStatus: HttpStatusCode.OK_200
74 expect(res.body.serverConfig).to.exist
75 expect(res.body.serverConfig.instance.name).to.equal('PeerTube')
79 describe('Server', function () {
81 it('Should get the server actor', async function () {
82 await servers[0].servers.waitUntilLog('server actor name is peertube')
86 describe('Socket', function () {
88 it('Should sendNotification without any exceptions', async () => {
89 const user = await servers[0].users.create({ username: 'notis_redding', password: 'secret1234?' })
90 await makePostBodyRequest({
92 path: '/plugins/test-four/router/send-notification',
96 expectedStatus: HttpStatusCode.CREATED_201
100 it('Should sendVideoLiveNewState without any exceptions', async () => {
101 const res = await servers[0].videos.quickUpload({ name: 'video server 1' })
103 await makePostBodyRequest({
105 path: '/plugins/test-four/router/send-video-live-new-state/' + res.uuid,
106 expectedStatus: HttpStatusCode.CREATED_201
109 await servers[0].videos.remove({ id: res.uuid })
113 describe('Plugin', function () {
115 it('Should get the base static route', async function () {
116 const res = await makeGetRequest({
118 path: '/plugins/test-four/router/static-route',
119 expectedStatus: HttpStatusCode.OK_200
122 expect(res.body.staticRoute).to.equal('/plugins/test-four/0.0.1/static/')
125 it('Should get the base static route', async function () {
126 const baseRouter = '/plugins/test-four/0.0.1/router/'
128 const res = await makeGetRequest({
130 path: baseRouter + 'router-route',
131 expectedStatus: HttpStatusCode.OK_200
134 expect(res.body.routerRoute).to.equal(baseRouter)
138 describe('User', function () {
141 it('Should not get a user if not authenticated', async function () {
142 await makeGetRequest({
144 path: '/plugins/test-four/router/user',
145 expectedStatus: HttpStatusCode.NOT_FOUND_404
149 it('Should get a user if authenticated', async function () {
150 const res = await makeGetRequest({
152 token: servers[0].accessToken,
153 path: '/plugins/test-four/router/user',
154 expectedStatus: HttpStatusCode.OK_200
157 expect(res.body.username).to.equal('root')
158 expect(res.body.displayName).to.equal('root')
159 expect(res.body.isAdmin).to.be.true
160 expect(res.body.isModerator).to.be.false
161 expect(res.body.isUser).to.be.false
166 it('Should load a user by id', async function () {
168 const res = await makeGetRequest({
170 path: '/plugins/test-four/router/user/' + rootId,
171 expectedStatus: HttpStatusCode.OK_200
174 expect(res.body.username).to.equal('root')
178 await makeGetRequest({
180 path: '/plugins/test-four/router/user/42',
181 expectedStatus: HttpStatusCode.NOT_FOUND_404
187 describe('Moderation', function () {
188 let videoUUIDServer1: string
190 before(async function () {
194 const res = await servers[0].videos.quickUpload({ name: 'video server 1' })
195 videoUUIDServer1 = res.uuid
199 await servers[1].videos.quickUpload({ name: 'video server 2' })
202 await waitJobs(servers)
204 const { data } = await servers[0].videos.list()
206 expect(data).to.have.lengthOf(2)
209 it('Should mute server 2', async function () {
211 await postCommand(servers[0], 'blockServer', { hostToBlock: `localhost:${servers[1].port}` })
213 const { data } = await servers[0].videos.list()
215 expect(data).to.have.lengthOf(1)
216 expect(data[0].name).to.equal('video server 1')
219 it('Should unmute server 2', async function () {
220 await postCommand(servers[0], 'unblockServer', { hostToUnblock: `localhost:${servers[1].port}` })
222 const { data } = await servers[0].videos.list()
224 expect(data).to.have.lengthOf(2)
227 it('Should mute account of server 2', async function () {
228 await postCommand(servers[0], 'blockAccount', { handleToBlock: `root@localhost:${servers[1].port}` })
230 const { data } = await servers[0].videos.list()
232 expect(data).to.have.lengthOf(1)
233 expect(data[0].name).to.equal('video server 1')
236 it('Should unmute account of server 2', async function () {
237 await postCommand(servers[0], 'unblockAccount', { handleToUnblock: `root@localhost:${servers[1].port}` })
239 const { data } = await servers[0].videos.list()
241 expect(data).to.have.lengthOf(2)
244 it('Should blacklist video', async function () {
247 await postCommand(servers[0], 'blacklist', { videoUUID: videoUUIDServer1, unfederate: true })
249 await waitJobs(servers)
251 for (const server of servers) {
252 const { data } = await server.videos.list()
254 expect(data).to.have.lengthOf(1)
255 expect(data[0].name).to.equal('video server 2')
259 it('Should unblacklist video', async function () {
262 await postCommand(servers[0], 'unblacklist', { videoUUID: videoUUIDServer1 })
264 await waitJobs(servers)
266 for (const server of servers) {
267 const { data } = await server.videos.list()
269 expect(data).to.have.lengthOf(2)
274 describe('Videos', function () {
275 let videoUUID: string
276 let videoPath: string
281 await servers[0].config.enableTranscoding()
283 const res = await servers[0].videos.quickUpload({ name: 'video1' })
286 await waitJobs(servers)
289 it('Should get video files', async function () {
290 const { body } = await makeGetRequest({
292 path: '/plugins/test-four/router/video-files/' + videoUUID,
293 expectedStatus: HttpStatusCode.OK_200
298 expect(body.webtorrent.videoFiles).to.be.an('array')
299 expect(body.hls.videoFiles).to.be.an('array')
301 for (const resolution of [ 144, 240, 360, 480, 720 ]) {
302 for (const files of [ body.webtorrent.videoFiles, body.hls.videoFiles ]) {
303 const file = files.find(f => f.resolution === resolution)
304 expect(file).to.exist
306 expect(file.size).to.be.a('number')
307 expect(file.fps).to.equal(25)
309 expect(await pathExists(file.path)).to.be.true
310 await makeRawRequest({ url: file.url, expectedStatus: HttpStatusCode.OK_200 })
314 videoPath = body.webtorrent.videoFiles[0].path
319 expect(body.thumbnails).to.be.an('array')
321 const miniature = body.thumbnails.find(t => t.type === ThumbnailType.MINIATURE)
322 expect(miniature).to.exist
323 expect(await pathExists(miniature.path)).to.be.true
324 await makeRawRequest({ url: miniature.url, expectedStatus: HttpStatusCode.OK_200 })
326 const preview = body.thumbnails.find(t => t.type === ThumbnailType.PREVIEW)
327 expect(preview).to.exist
328 expect(await pathExists(preview.path)).to.be.true
329 await makeRawRequest({ url: preview.url, expectedStatus: HttpStatusCode.OK_200 })
333 it('Should probe a file', async function () {
334 const { body } = await makeGetRequest({
336 path: '/plugins/test-four/router/ffprobe',
340 expectedStatus: HttpStatusCode.OK_200
343 expect(body.streams).to.be.an('array')
344 expect(body.streams).to.have.lengthOf(2)
347 it('Should remove a video after a view', async function () {
350 // Should not throw -> video exists
351 const video = await servers[0].videos.get({ id: videoUUID })
352 // Should delete the video
353 await servers[0].views.simulateView({ id: videoUUID })
355 await servers[0].servers.waitUntilLog('Video deleted by plugin four.')
358 // Should throw because the video should have been deleted
359 await servers[0].videos.get({ id: videoUUID })
360 throw new Error('Video exists')
362 if (err.message.includes('exists')) throw err
365 await checkVideoFilesWereRemoved({ server: servers[0], video })
368 it('Should have fetched the video by URL', async function () {
369 await servers[0].servers.waitUntilLog(`video from DB uuid is ${videoUUID}`)
373 after(async function () {
374 await cleanupTests(servers)