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('Plugin', function () {
88 it('Should get the base static route', async function () {
89 const res = await makeGetRequest({
91 path: '/plugins/test-four/router/static-route',
92 expectedStatus: HttpStatusCode.OK_200
95 expect(res.body.staticRoute).to.equal('/plugins/test-four/0.0.1/static/')
98 it('Should get the base static route', async function () {
99 const baseRouter = '/plugins/test-four/0.0.1/router/'
101 const res = await makeGetRequest({
103 path: baseRouter + 'router-route',
104 expectedStatus: HttpStatusCode.OK_200
107 expect(res.body.routerRoute).to.equal(baseRouter)
111 describe('User', function () {
114 it('Should not get a user if not authenticated', async function () {
115 await makeGetRequest({
117 path: '/plugins/test-four/router/user',
118 expectedStatus: HttpStatusCode.NOT_FOUND_404
122 it('Should get a user if authenticated', async function () {
123 const res = await makeGetRequest({
125 token: servers[0].accessToken,
126 path: '/plugins/test-four/router/user',
127 expectedStatus: HttpStatusCode.OK_200
130 expect(res.body.username).to.equal('root')
131 expect(res.body.displayName).to.equal('root')
132 expect(res.body.isAdmin).to.be.true
133 expect(res.body.isModerator).to.be.false
134 expect(res.body.isUser).to.be.false
139 it('Should load a user by id', async function () {
141 const res = await makeGetRequest({
143 path: '/plugins/test-four/router/user/' + rootId,
144 expectedStatus: HttpStatusCode.OK_200
147 expect(res.body.username).to.equal('root')
151 await makeGetRequest({
153 path: '/plugins/test-four/router/user/42',
154 expectedStatus: HttpStatusCode.NOT_FOUND_404
160 describe('Moderation', function () {
161 let videoUUIDServer1: string
163 before(async function () {
167 const res = await servers[0].videos.quickUpload({ name: 'video server 1' })
168 videoUUIDServer1 = res.uuid
172 await servers[1].videos.quickUpload({ name: 'video server 2' })
175 await waitJobs(servers)
177 const { data } = await servers[0].videos.list()
179 expect(data).to.have.lengthOf(2)
182 it('Should mute server 2', async function () {
184 await postCommand(servers[0], 'blockServer', { hostToBlock: `localhost:${servers[1].port}` })
186 const { data } = await servers[0].videos.list()
188 expect(data).to.have.lengthOf(1)
189 expect(data[0].name).to.equal('video server 1')
192 it('Should unmute server 2', async function () {
193 await postCommand(servers[0], 'unblockServer', { hostToUnblock: `localhost:${servers[1].port}` })
195 const { data } = await servers[0].videos.list()
197 expect(data).to.have.lengthOf(2)
200 it('Should mute account of server 2', async function () {
201 await postCommand(servers[0], 'blockAccount', { handleToBlock: `root@localhost:${servers[1].port}` })
203 const { data } = await servers[0].videos.list()
205 expect(data).to.have.lengthOf(1)
206 expect(data[0].name).to.equal('video server 1')
209 it('Should unmute account of server 2', async function () {
210 await postCommand(servers[0], 'unblockAccount', { handleToUnblock: `root@localhost:${servers[1].port}` })
212 const { data } = await servers[0].videos.list()
214 expect(data).to.have.lengthOf(2)
217 it('Should blacklist video', async function () {
220 await postCommand(servers[0], 'blacklist', { videoUUID: videoUUIDServer1, unfederate: true })
222 await waitJobs(servers)
224 for (const server of servers) {
225 const { data } = await server.videos.list()
227 expect(data).to.have.lengthOf(1)
228 expect(data[0].name).to.equal('video server 2')
232 it('Should unblacklist video', async function () {
235 await postCommand(servers[0], 'unblacklist', { videoUUID: videoUUIDServer1 })
237 await waitJobs(servers)
239 for (const server of servers) {
240 const { data } = await server.videos.list()
242 expect(data).to.have.lengthOf(2)
247 describe('Videos', function () {
248 let videoUUID: string
249 let videoPath: string
254 await servers[0].config.enableTranscoding()
256 const res = await servers[0].videos.quickUpload({ name: 'video1' })
259 await waitJobs(servers)
262 it('Should get video files', async function () {
263 const { body } = await makeGetRequest({
265 path: '/plugins/test-four/router/video-files/' + videoUUID,
266 expectedStatus: HttpStatusCode.OK_200
271 expect(body.webtorrent.videoFiles).to.be.an('array')
272 expect(body.hls.videoFiles).to.be.an('array')
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
279 expect(file.size).to.be.a('number')
280 expect(file.fps).to.equal(25)
282 expect(await pathExists(file.path)).to.be.true
283 await makeRawRequest(file.url, HttpStatusCode.OK_200)
287 videoPath = body.webtorrent.videoFiles[0].path
292 expect(body.thumbnails).to.be.an('array')
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)
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)
306 it('Should probe a file', async function () {
307 const { body } = await makeGetRequest({
309 path: '/plugins/test-four/router/ffprobe',
313 expectedStatus: HttpStatusCode.OK_200
316 expect(body.streams).to.be.an('array')
317 expect(body.streams).to.have.lengthOf(2)
320 it('Should remove a video after a view', async function () {
323 // Should not throw -> video exists
324 const video = await servers[0].videos.get({ id: videoUUID })
325 // Should delete the video
326 await servers[0].views.simulateView({ id: videoUUID })
328 await servers[0].servers.waitUntilLog('Video deleted by plugin four.')
331 // Should throw because the video should have been deleted
332 await servers[0].videos.get({ id: videoUUID })
333 throw new Error('Video exists')
335 if (err.message.includes('exists')) throw err
338 await checkVideoFilesWereRemoved({ server: servers[0], video })
341 it('Should have fetched the video by URL', async function () {
342 await servers[0].servers.waitUntilLog(`video from DB uuid is ${videoUUID}`)
346 after(async function () {
347 await cleanupTests(servers)