1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import { expect } from 'chai'
5 import { pathExists } from 'fs-extra'
6 import { HttpStatusCode, ThumbnailType } from '@shared/models'
16 setAccessTokensToServers,
18 } from '@shared/server-commands'
19 import { checkVideoFilesWereRemoved } from '../shared'
21 function postCommand (server: PeerTubeServer, command: string, bodyArg?: object) {
22 const body = { command }
23 if (bodyArg) Object.assign(body, bodyArg)
25 return makePostBodyRequest({
27 path: '/plugins/test-four/router/commander',
29 expectedStatus: HttpStatusCode.NO_CONTENT_204
33 describe('Test plugin helpers', function () {
34 let servers: PeerTubeServer[]
36 before(async function () {
39 servers = await createMultipleServers(2)
40 await setAccessTokensToServers(servers)
42 await doubleFollow(servers[0], servers[1])
44 await servers[0].plugins.install({ path: PluginsCommand.getPluginTestPath('-four') })
47 describe('Logger', function () {
49 it('Should have logged things', async function () {
50 await servers[0].servers.waitUntilLog('localhost:' + servers[0].port + ' peertube-plugin-test-four', 1, false)
51 await servers[0].servers.waitUntilLog('Hello world from plugin four', 1)
55 describe('Database', function () {
57 it('Should have made a query', async function () {
58 await servers[0].servers.waitUntilLog(`root email is admin${servers[0].internalServerNumber}@example.com`)
62 describe('Config', function () {
64 it('Should have the correct webserver url', async function () {
65 await servers[0].servers.waitUntilLog(`server url is http://localhost:${servers[0].port}`)
68 it('Should have the correct config', async function () {
69 const res = await makeGetRequest({
71 path: '/plugins/test-four/router/server-config',
72 expectedStatus: HttpStatusCode.OK_200
75 expect(res.body.serverConfig).to.exist
76 expect(res.body.serverConfig.instance.name).to.equal('PeerTube')
80 describe('Server', function () {
82 it('Should get the server actor', async function () {
83 await servers[0].servers.waitUntilLog('server actor name is peertube')
87 describe('Plugin', function () {
89 it('Should get the base static route', async function () {
90 const res = await makeGetRequest({
92 path: '/plugins/test-four/router/static-route',
93 expectedStatus: HttpStatusCode.OK_200
96 expect(res.body.staticRoute).to.equal('/plugins/test-four/0.0.1/static/')
99 it('Should get the base static route', async function () {
100 const baseRouter = '/plugins/test-four/0.0.1/router/'
102 const res = await makeGetRequest({
104 path: baseRouter + 'router-route',
105 expectedStatus: HttpStatusCode.OK_200
108 expect(res.body.routerRoute).to.equal(baseRouter)
112 describe('User', function () {
115 it('Should not get a user if not authenticated', async function () {
116 await makeGetRequest({
118 path: '/plugins/test-four/router/user',
119 expectedStatus: HttpStatusCode.NOT_FOUND_404
123 it('Should get a user if authenticated', async function () {
124 const res = await makeGetRequest({
126 token: servers[0].accessToken,
127 path: '/plugins/test-four/router/user',
128 expectedStatus: HttpStatusCode.OK_200
131 expect(res.body.username).to.equal('root')
132 expect(res.body.displayName).to.equal('root')
133 expect(res.body.isAdmin).to.be.true
134 expect(res.body.isModerator).to.be.false
135 expect(res.body.isUser).to.be.false
140 it('Should load a user by id', async function () {
142 const res = await makeGetRequest({
144 path: '/plugins/test-four/router/user/' + rootId,
145 expectedStatus: HttpStatusCode.OK_200
148 expect(res.body.username).to.equal('root')
152 await makeGetRequest({
154 path: '/plugins/test-four/router/user/42',
155 expectedStatus: HttpStatusCode.NOT_FOUND_404
161 describe('Moderation', function () {
162 let videoUUIDServer1: string
164 before(async function () {
168 const res = await servers[0].videos.quickUpload({ name: 'video server 1' })
169 videoUUIDServer1 = res.uuid
173 await servers[1].videos.quickUpload({ name: 'video server 2' })
176 await waitJobs(servers)
178 const { data } = await servers[0].videos.list()
180 expect(data).to.have.lengthOf(2)
183 it('Should mute server 2', async function () {
185 await postCommand(servers[0], 'blockServer', { hostToBlock: `localhost:${servers[1].port}` })
187 const { data } = await servers[0].videos.list()
189 expect(data).to.have.lengthOf(1)
190 expect(data[0].name).to.equal('video server 1')
193 it('Should unmute server 2', async function () {
194 await postCommand(servers[0], 'unblockServer', { hostToUnblock: `localhost:${servers[1].port}` })
196 const { data } = await servers[0].videos.list()
198 expect(data).to.have.lengthOf(2)
201 it('Should mute account of server 2', async function () {
202 await postCommand(servers[0], 'blockAccount', { handleToBlock: `root@localhost:${servers[1].port}` })
204 const { data } = await servers[0].videos.list()
206 expect(data).to.have.lengthOf(1)
207 expect(data[0].name).to.equal('video server 1')
210 it('Should unmute account of server 2', async function () {
211 await postCommand(servers[0], 'unblockAccount', { handleToUnblock: `root@localhost:${servers[1].port}` })
213 const { data } = await servers[0].videos.list()
215 expect(data).to.have.lengthOf(2)
218 it('Should blacklist video', async function () {
221 await postCommand(servers[0], 'blacklist', { videoUUID: videoUUIDServer1, unfederate: true })
223 await waitJobs(servers)
225 for (const server of servers) {
226 const { data } = await server.videos.list()
228 expect(data).to.have.lengthOf(1)
229 expect(data[0].name).to.equal('video server 2')
233 it('Should unblacklist video', async function () {
236 await postCommand(servers[0], 'unblacklist', { videoUUID: videoUUIDServer1 })
238 await waitJobs(servers)
240 for (const server of servers) {
241 const { data } = await server.videos.list()
243 expect(data).to.have.lengthOf(2)
248 describe('Videos', function () {
249 let videoUUID: string
250 let videoPath: string
255 await servers[0].config.enableTranscoding()
257 const res = await servers[0].videos.quickUpload({ name: 'video1' })
260 await waitJobs(servers)
263 it('Should get video files', async function () {
264 const { body } = await makeGetRequest({
266 path: '/plugins/test-four/router/video-files/' + videoUUID,
267 expectedStatus: HttpStatusCode.OK_200
272 expect(body.webtorrent.videoFiles).to.be.an('array')
273 expect(body.hls.videoFiles).to.be.an('array')
275 for (const resolution of [ 144, 240, 360, 480, 720 ]) {
276 for (const files of [ body.webtorrent.videoFiles, body.hls.videoFiles ]) {
277 const file = files.find(f => f.resolution === resolution)
278 expect(file).to.exist
280 expect(file.size).to.be.a('number')
281 expect(file.fps).to.equal(25)
283 expect(await pathExists(file.path)).to.be.true
284 await makeRawRequest(file.url, HttpStatusCode.OK_200)
288 videoPath = body.webtorrent.videoFiles[0].path
293 expect(body.thumbnails).to.be.an('array')
295 const miniature = body.thumbnails.find(t => t.type === ThumbnailType.MINIATURE)
296 expect(miniature).to.exist
297 expect(await pathExists(miniature.path)).to.be.true
298 await makeRawRequest(miniature.url, HttpStatusCode.OK_200)
300 const preview = body.thumbnails.find(t => t.type === ThumbnailType.PREVIEW)
301 expect(preview).to.exist
302 expect(await pathExists(preview.path)).to.be.true
303 await makeRawRequest(preview.url, HttpStatusCode.OK_200)
307 it('Should probe a file', async function () {
308 const { body } = await makeGetRequest({
310 path: '/plugins/test-four/router/ffprobe',
314 expectedStatus: HttpStatusCode.OK_200
317 expect(body.streams).to.be.an('array')
318 expect(body.streams).to.have.lengthOf(2)
321 it('Should remove a video after a view', async function () {
324 // Should not throw -> video exists
325 const video = await servers[0].videos.get({ id: videoUUID })
326 // Should delete the video
327 await servers[0].views.simulateView({ id: videoUUID })
329 await servers[0].servers.waitUntilLog('Video deleted by plugin four.')
332 // Should throw because the video should have been deleted
333 await servers[0].videos.get({ id: videoUUID })
334 throw new Error('Video exists')
336 if (err.message.includes('exists')) throw err
339 await checkVideoFilesWereRemoved({ server: servers[0], video })
342 it('Should have fetched the video by URL', async function () {
343 await servers[0].servers.waitUntilLog(`video from DB uuid is ${videoUUID}`)
347 after(async function () {
348 await cleanupTests(servers)