1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import { expect } from 'chai'
5 import { HttpStatusCode } from '@shared/core-utils'
7 checkVideoFilesWereRemoved,
10 flushAndRunMultipleServers,
17 setAccessTokensToServers,
21 } from '@shared/extra-utils'
23 function postCommand (server: ServerInfo, command: string, bodyArg?: object) {
24 const body = { command }
25 if (bodyArg) Object.assign(body, bodyArg)
27 return makePostBodyRequest({
29 path: '/plugins/test-four/router/commander',
31 statusCodeExpected: HttpStatusCode.NO_CONTENT_204
35 describe('Test plugin helpers', function () {
36 let servers: ServerInfo[]
38 before(async function () {
41 servers = await flushAndRunMultipleServers(2)
42 await setAccessTokensToServers(servers)
44 await doubleFollow(servers[0], servers[1])
46 await servers[0].pluginsCommand.install({ path: PluginsCommand.getPluginTestPath('-four') })
49 describe('Logger', function () {
51 it('Should have logged things', async function () {
52 await servers[0].serversCommand.waitUntilLog('localhost:' + servers[0].port + ' peertube-plugin-test-four', 1, false)
53 await servers[0].serversCommand.waitUntilLog('Hello world from plugin four', 1)
57 describe('Database', function () {
59 it('Should have made a query', async function () {
60 await servers[0].serversCommand.waitUntilLog(`root email is admin${servers[0].internalServerNumber}@example.com`)
64 describe('Config', function () {
66 it('Should have the correct webserver url', async function () {
67 await servers[0].serversCommand.waitUntilLog(`server url is http://localhost:${servers[0].port}`)
70 it('Should have the correct config', async function () {
71 const res = await makeGetRequest({
73 path: '/plugins/test-four/router/server-config',
74 statusCodeExpected: HttpStatusCode.OK_200
77 expect(res.body.serverConfig).to.exist
78 expect(res.body.serverConfig.instance.name).to.equal('PeerTube')
82 describe('Server', function () {
84 it('Should get the server actor', async function () {
85 await servers[0].serversCommand.waitUntilLog('server actor name is peertube')
89 describe('Plugin', function () {
91 it('Should get the base static route', async function () {
92 const res = await makeGetRequest({
94 path: '/plugins/test-four/router/static-route',
95 statusCodeExpected: HttpStatusCode.OK_200
98 expect(res.body.staticRoute).to.equal('/plugins/test-four/0.0.1/static/')
101 it('Should get the base static route', async function () {
102 const baseRouter = '/plugins/test-four/0.0.1/router/'
104 const res = await makeGetRequest({
106 path: baseRouter + 'router-route',
107 statusCodeExpected: HttpStatusCode.OK_200
110 expect(res.body.routerRoute).to.equal(baseRouter)
114 describe('User', function () {
116 it('Should not get a user if not authenticated', async function () {
117 await makeGetRequest({
119 path: '/plugins/test-four/router/user',
120 statusCodeExpected: HttpStatusCode.NOT_FOUND_404
124 it('Should get a user if authenticated', async function () {
125 const res = await makeGetRequest({
127 token: servers[0].accessToken,
128 path: '/plugins/test-four/router/user',
129 statusCodeExpected: HttpStatusCode.OK_200
132 expect(res.body.username).to.equal('root')
133 expect(res.body.displayName).to.equal('root')
134 expect(res.body.isAdmin).to.be.true
135 expect(res.body.isModerator).to.be.false
136 expect(res.body.isUser).to.be.false
140 describe('Moderation', function () {
141 let videoUUIDServer1: string
143 before(async function () {
147 const res = await uploadVideoAndGetId({ server: servers[0], videoName: 'video server 1' })
148 videoUUIDServer1 = res.uuid
152 await uploadVideoAndGetId({ server: servers[1], videoName: 'video server 2' })
155 await waitJobs(servers)
157 const res = await getVideosList(servers[0].url)
158 const videos = res.body.data
160 expect(videos).to.have.lengthOf(2)
163 it('Should mute server 2', async function () {
165 await postCommand(servers[0], 'blockServer', { hostToBlock: `localhost:${servers[1].port}` })
167 const res = await getVideosList(servers[0].url)
168 const videos = res.body.data
170 expect(videos).to.have.lengthOf(1)
171 expect(videos[0].name).to.equal('video server 1')
174 it('Should unmute server 2', async function () {
175 await postCommand(servers[0], 'unblockServer', { hostToUnblock: `localhost:${servers[1].port}` })
177 const res = await getVideosList(servers[0].url)
178 const videos = res.body.data
180 expect(videos).to.have.lengthOf(2)
183 it('Should mute account of server 2', async function () {
184 await postCommand(servers[0], 'blockAccount', { handleToBlock: `root@localhost:${servers[1].port}` })
186 const res = await getVideosList(servers[0].url)
187 const videos = res.body.data
189 expect(videos).to.have.lengthOf(1)
190 expect(videos[0].name).to.equal('video server 1')
193 it('Should unmute account of server 2', async function () {
194 await postCommand(servers[0], 'unblockAccount', { handleToUnblock: `root@localhost:${servers[1].port}` })
196 const res = await getVideosList(servers[0].url)
197 const videos = res.body.data
199 expect(videos).to.have.lengthOf(2)
202 it('Should blacklist video', async function () {
205 await postCommand(servers[0], 'blacklist', { videoUUID: videoUUIDServer1, unfederate: true })
207 await waitJobs(servers)
209 for (const server of servers) {
210 const res = await getVideosList(server.url)
211 const videos = res.body.data
213 expect(videos).to.have.lengthOf(1)
214 expect(videos[0].name).to.equal('video server 2')
218 it('Should unblacklist video', async function () {
221 await postCommand(servers[0], 'unblacklist', { videoUUID: videoUUIDServer1 })
223 await waitJobs(servers)
225 for (const server of servers) {
226 const res = await getVideosList(server.url)
227 const videos = res.body.data
229 expect(videos).to.have.lengthOf(2)
234 describe('Videos', function () {
235 let videoUUID: string
238 const res = await uploadVideoAndGetId({ server: servers[0], videoName: 'video1' })
242 it('Should remove a video after a view', async function () {
245 // Should not throw -> video exists
246 await getVideo(servers[0].url, videoUUID)
247 // Should delete the video
248 await viewVideo(servers[0].url, videoUUID)
250 await servers[0].serversCommand.waitUntilLog('Video deleted by plugin four.')
253 // Should throw because the video should have been deleted
254 await getVideo(servers[0].url, videoUUID)
255 throw new Error('Video exists')
257 if (err.message.includes('exists')) throw err
260 await checkVideoFilesWereRemoved(videoUUID, servers[0])
263 it('Should have fetched the video by URL', async function () {
264 await servers[0].serversCommand.waitUntilLog(`video from DB uuid is ${videoUUID}`)
268 after(async function () {
269 await cleanupTests(servers)