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(servers[0].host + ' 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 ${servers[0].url}`)
67 it('Should have the correct listening config', async function () {
68 const res = await makeGetRequest({
70 path: '/plugins/test-four/router/server-listening-config',
71 expectedStatus: HttpStatusCode.OK_200
74 expect(res.body.config).to.exist
75 expect(res.body.config.hostname).to.equal('::')
76 expect(res.body.config.port).to.equal(servers[0].port)
79 it('Should have the correct config', async function () {
80 const res = await makeGetRequest({
82 path: '/plugins/test-four/router/server-config',
83 expectedStatus: HttpStatusCode.OK_200
86 expect(res.body.serverConfig).to.exist
87 expect(res.body.serverConfig.instance.name).to.equal('PeerTube')
91 describe('Server', function () {
93 it('Should get the server actor', async function () {
94 await servers[0].servers.waitUntilLog('server actor name is peertube')
98 describe('Socket', function () {
100 it('Should sendNotification without any exceptions', async () => {
101 const user = await servers[0].users.create({ username: 'notis_redding', password: 'secret1234?' })
102 await makePostBodyRequest({
104 path: '/plugins/test-four/router/send-notification',
108 expectedStatus: HttpStatusCode.CREATED_201
112 it('Should sendVideoLiveNewState without any exceptions', async () => {
113 const res = await servers[0].videos.quickUpload({ name: 'video server 1' })
115 await makePostBodyRequest({
117 path: '/plugins/test-four/router/send-video-live-new-state/' + res.uuid,
118 expectedStatus: HttpStatusCode.CREATED_201
121 await servers[0].videos.remove({ id: res.uuid })
125 describe('Plugin', function () {
127 it('Should get the base static route', async function () {
128 const res = await makeGetRequest({
130 path: '/plugins/test-four/router/static-route',
131 expectedStatus: HttpStatusCode.OK_200
134 expect(res.body.staticRoute).to.equal('/plugins/test-four/0.0.1/static/')
137 it('Should get the base static route', async function () {
138 const baseRouter = '/plugins/test-four/0.0.1/router/'
140 const res = await makeGetRequest({
142 path: baseRouter + 'router-route',
143 expectedStatus: HttpStatusCode.OK_200
146 expect(res.body.routerRoute).to.equal(baseRouter)
150 describe('User', function () {
153 it('Should not get a user if not authenticated', async function () {
154 await makeGetRequest({
156 path: '/plugins/test-four/router/user',
157 expectedStatus: HttpStatusCode.NOT_FOUND_404
161 it('Should get a user if authenticated', async function () {
162 const res = await makeGetRequest({
164 token: servers[0].accessToken,
165 path: '/plugins/test-four/router/user',
166 expectedStatus: HttpStatusCode.OK_200
169 expect(res.body.username).to.equal('root')
170 expect(res.body.displayName).to.equal('root')
171 expect(res.body.isAdmin).to.be.true
172 expect(res.body.isModerator).to.be.false
173 expect(res.body.isUser).to.be.false
178 it('Should load a user by id', async function () {
180 const res = await makeGetRequest({
182 path: '/plugins/test-four/router/user/' + rootId,
183 expectedStatus: HttpStatusCode.OK_200
186 expect(res.body.username).to.equal('root')
190 await makeGetRequest({
192 path: '/plugins/test-four/router/user/42',
193 expectedStatus: HttpStatusCode.NOT_FOUND_404
199 describe('Moderation', function () {
200 let videoUUIDServer1: string
202 before(async function () {
206 const res = await servers[0].videos.quickUpload({ name: 'video server 1' })
207 videoUUIDServer1 = res.uuid
211 await servers[1].videos.quickUpload({ name: 'video server 2' })
214 await waitJobs(servers)
216 const { data } = await servers[0].videos.list()
218 expect(data).to.have.lengthOf(2)
221 it('Should mute server 2', async function () {
222 await postCommand(servers[0], 'blockServer', { hostToBlock: servers[1].host })
224 const { data } = await servers[0].videos.list()
226 expect(data).to.have.lengthOf(1)
227 expect(data[0].name).to.equal('video server 1')
230 it('Should unmute server 2', async function () {
231 await postCommand(servers[0], 'unblockServer', { hostToUnblock: servers[1].host })
233 const { data } = await servers[0].videos.list()
235 expect(data).to.have.lengthOf(2)
238 it('Should mute account of server 2', async function () {
239 await postCommand(servers[0], 'blockAccount', { handleToBlock: `root@${servers[1].host}` })
241 const { data } = await servers[0].videos.list()
243 expect(data).to.have.lengthOf(1)
244 expect(data[0].name).to.equal('video server 1')
247 it('Should unmute account of server 2', async function () {
248 await postCommand(servers[0], 'unblockAccount', { handleToUnblock: `root@${servers[1].host}` })
250 const { data } = await servers[0].videos.list()
252 expect(data).to.have.lengthOf(2)
255 it('Should blacklist video', async function () {
256 await postCommand(servers[0], 'blacklist', { videoUUID: videoUUIDServer1, unfederate: true })
258 await waitJobs(servers)
260 for (const server of servers) {
261 const { data } = await server.videos.list()
263 expect(data).to.have.lengthOf(1)
264 expect(data[0].name).to.equal('video server 2')
268 it('Should unblacklist video', async function () {
269 await postCommand(servers[0], 'unblacklist', { videoUUID: videoUUIDServer1 })
271 await waitJobs(servers)
273 for (const server of servers) {
274 const { data } = await server.videos.list()
276 expect(data).to.have.lengthOf(2)
281 describe('Videos', function () {
282 let videoUUID: string
283 let videoPath: string
285 before(async function () {
288 await servers[0].config.enableTranscoding()
290 const res = await servers[0].videos.quickUpload({ name: 'video1' })
293 await waitJobs(servers)
296 it('Should get video files', async function () {
297 const { body } = await makeGetRequest({
299 path: '/plugins/test-four/router/video-files/' + videoUUID,
300 expectedStatus: HttpStatusCode.OK_200
305 expect(body.webtorrent.videoFiles).to.be.an('array')
306 expect(body.hls.videoFiles).to.be.an('array')
308 for (const resolution of [ 144, 240, 360, 480, 720 ]) {
309 for (const files of [ body.webtorrent.videoFiles, body.hls.videoFiles ]) {
310 const file = files.find(f => f.resolution === resolution)
311 expect(file).to.exist
313 expect(file.size).to.be.a('number')
314 expect(file.fps).to.equal(25)
316 expect(await pathExists(file.path)).to.be.true
317 await makeRawRequest({ url: file.url, expectedStatus: HttpStatusCode.OK_200 })
321 videoPath = body.webtorrent.videoFiles[0].path
326 expect(body.thumbnails).to.be.an('array')
328 const miniature = body.thumbnails.find(t => t.type === ThumbnailType.MINIATURE)
329 expect(miniature).to.exist
330 expect(await pathExists(miniature.path)).to.be.true
331 await makeRawRequest({ url: miniature.url, expectedStatus: HttpStatusCode.OK_200 })
333 const preview = body.thumbnails.find(t => t.type === ThumbnailType.PREVIEW)
334 expect(preview).to.exist
335 expect(await pathExists(preview.path)).to.be.true
336 await makeRawRequest({ url: preview.url, expectedStatus: HttpStatusCode.OK_200 })
340 it('Should probe a file', async function () {
341 const { body } = await makeGetRequest({
343 path: '/plugins/test-four/router/ffprobe',
347 expectedStatus: HttpStatusCode.OK_200
350 expect(body.streams).to.be.an('array')
351 expect(body.streams).to.have.lengthOf(2)
354 it('Should remove a video after a view', async function () {
357 // Should not throw -> video exists
358 const video = await servers[0].videos.get({ id: videoUUID })
359 // Should delete the video
360 await servers[0].views.simulateView({ id: videoUUID })
362 await servers[0].servers.waitUntilLog('Video deleted by plugin four.')
365 // Should throw because the video should have been deleted
366 await servers[0].videos.get({ id: videoUUID })
367 throw new Error('Video exists')
369 if (err.message.includes('exists')) throw err
372 await checkVideoFilesWereRemoved({ server: servers[0], video })
375 it('Should have fetched the video by URL', async function () {
376 await servers[0].servers.waitUntilLog(`video from DB uuid is ${videoUUID}`)
380 after(async function () {
381 await cleanupTests(servers)