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,
22 } from '@shared/extra-utils'
24 function postCommand (server: ServerInfo, command: string, bodyArg?: object) {
25 const body = { command }
26 if (bodyArg) Object.assign(body, bodyArg)
28 return makePostBodyRequest({
30 path: '/plugins/test-four/router/commander',
32 statusCodeExpected: HttpStatusCode.NO_CONTENT_204
36 describe('Test plugin helpers', function () {
37 let servers: ServerInfo[]
39 before(async function () {
42 servers = await flushAndRunMultipleServers(2)
43 await setAccessTokensToServers(servers)
45 await doubleFollow(servers[0], servers[1])
47 await servers[0].pluginsCommand.install({ path: PluginsCommand.getPluginTestPath('-four') })
50 describe('Logger', function () {
52 it('Should have logged things', async function () {
53 await waitUntilLog(servers[0], 'localhost:' + servers[0].port + ' peertube-plugin-test-four', 1, false)
54 await waitUntilLog(servers[0], 'Hello world from plugin four', 1)
58 describe('Database', function () {
60 it('Should have made a query', async function () {
61 await waitUntilLog(servers[0], `root email is admin${servers[0].internalServerNumber}@example.com`)
65 describe('Config', function () {
67 it('Should have the correct webserver url', async function () {
68 await waitUntilLog(servers[0], `server url is http://localhost:${servers[0].port}`)
71 it('Should have the correct config', async function () {
72 const res = await makeGetRequest({
74 path: '/plugins/test-four/router/server-config',
75 statusCodeExpected: HttpStatusCode.OK_200
78 expect(res.body.serverConfig).to.exist
79 expect(res.body.serverConfig.instance.name).to.equal('PeerTube')
83 describe('Server', function () {
85 it('Should get the server actor', async function () {
86 await waitUntilLog(servers[0], 'server actor name is peertube')
90 describe('Plugin', function () {
92 it('Should get the base static route', async function () {
93 const res = await makeGetRequest({
95 path: '/plugins/test-four/router/static-route',
96 statusCodeExpected: HttpStatusCode.OK_200
99 expect(res.body.staticRoute).to.equal('/plugins/test-four/0.0.1/static/')
102 it('Should get the base static route', async function () {
103 const baseRouter = '/plugins/test-four/0.0.1/router/'
105 const res = await makeGetRequest({
107 path: baseRouter + 'router-route',
108 statusCodeExpected: HttpStatusCode.OK_200
111 expect(res.body.routerRoute).to.equal(baseRouter)
115 describe('User', function () {
117 it('Should not get a user if not authenticated', async function () {
118 await makeGetRequest({
120 path: '/plugins/test-four/router/user',
121 statusCodeExpected: HttpStatusCode.NOT_FOUND_404
125 it('Should get a user if authenticated', async function () {
126 const res = await makeGetRequest({
128 token: servers[0].accessToken,
129 path: '/plugins/test-four/router/user',
130 statusCodeExpected: HttpStatusCode.OK_200
133 expect(res.body.username).to.equal('root')
134 expect(res.body.displayName).to.equal('root')
135 expect(res.body.isAdmin).to.be.true
136 expect(res.body.isModerator).to.be.false
137 expect(res.body.isUser).to.be.false
141 describe('Moderation', function () {
142 let videoUUIDServer1: string
144 before(async function () {
148 const res = await uploadVideoAndGetId({ server: servers[0], videoName: 'video server 1' })
149 videoUUIDServer1 = res.uuid
153 await uploadVideoAndGetId({ server: servers[1], videoName: 'video server 2' })
156 await waitJobs(servers)
158 const res = await getVideosList(servers[0].url)
159 const videos = res.body.data
161 expect(videos).to.have.lengthOf(2)
164 it('Should mute server 2', async function () {
166 await postCommand(servers[0], 'blockServer', { hostToBlock: `localhost:${servers[1].port}` })
168 const res = await getVideosList(servers[0].url)
169 const videos = res.body.data
171 expect(videos).to.have.lengthOf(1)
172 expect(videos[0].name).to.equal('video server 1')
175 it('Should unmute server 2', async function () {
176 await postCommand(servers[0], 'unblockServer', { hostToUnblock: `localhost:${servers[1].port}` })
178 const res = await getVideosList(servers[0].url)
179 const videos = res.body.data
181 expect(videos).to.have.lengthOf(2)
184 it('Should mute account of server 2', async function () {
185 await postCommand(servers[0], 'blockAccount', { handleToBlock: `root@localhost:${servers[1].port}` })
187 const res = await getVideosList(servers[0].url)
188 const videos = res.body.data
190 expect(videos).to.have.lengthOf(1)
191 expect(videos[0].name).to.equal('video server 1')
194 it('Should unmute account of server 2', async function () {
195 await postCommand(servers[0], 'unblockAccount', { handleToUnblock: `root@localhost:${servers[1].port}` })
197 const res = await getVideosList(servers[0].url)
198 const videos = res.body.data
200 expect(videos).to.have.lengthOf(2)
203 it('Should blacklist video', async function () {
206 await postCommand(servers[0], 'blacklist', { videoUUID: videoUUIDServer1, unfederate: true })
208 await waitJobs(servers)
210 for (const server of servers) {
211 const res = await getVideosList(server.url)
212 const videos = res.body.data
214 expect(videos).to.have.lengthOf(1)
215 expect(videos[0].name).to.equal('video server 2')
219 it('Should unblacklist video', async function () {
222 await postCommand(servers[0], 'unblacklist', { videoUUID: videoUUIDServer1 })
224 await waitJobs(servers)
226 for (const server of servers) {
227 const res = await getVideosList(server.url)
228 const videos = res.body.data
230 expect(videos).to.have.lengthOf(2)
235 describe('Videos', function () {
236 let videoUUID: string
239 const res = await uploadVideoAndGetId({ server: servers[0], videoName: 'video1' })
243 it('Should remove a video after a view', async function () {
246 // Should not throw -> video exists
247 await getVideo(servers[0].url, videoUUID)
248 // Should delete the video
249 await viewVideo(servers[0].url, videoUUID)
251 await waitUntilLog(servers[0], 'Video deleted by plugin four.')
254 // Should throw because the video should have been deleted
255 await getVideo(servers[0].url, videoUUID)
256 throw new Error('Video exists')
258 if (err.message.includes('exists')) throw err
261 await checkVideoFilesWereRemoved(videoUUID, servers[0].internalServerNumber)
264 it('Should have fetched the video by URL', async function () {
265 await waitUntilLog(servers[0], `video from DB uuid is ${videoUUID}`)
269 after(async function () {
270 await cleanupTests(servers)