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,
15 setAccessTokensToServers,
17 } from '@shared/extra-utils'
19 function postCommand (server: ServerInfo, command: string, bodyArg?: object) {
20 const body = { command }
21 if (bodyArg) Object.assign(body, bodyArg)
23 return makePostBodyRequest({
25 path: '/plugins/test-four/router/commander',
27 statusCodeExpected: HttpStatusCode.NO_CONTENT_204
31 describe('Test plugin helpers', function () {
32 let servers: ServerInfo[]
34 before(async function () {
37 servers = await flushAndRunMultipleServers(2)
38 await setAccessTokensToServers(servers)
40 await doubleFollow(servers[0], servers[1])
42 await servers[0].pluginsCommand.install({ path: PluginsCommand.getPluginTestPath('-four') })
45 describe('Logger', function () {
47 it('Should have logged things', async function () {
48 await servers[0].serversCommand.waitUntilLog('localhost:' + servers[0].port + ' peertube-plugin-test-four', 1, false)
49 await servers[0].serversCommand.waitUntilLog('Hello world from plugin four', 1)
53 describe('Database', function () {
55 it('Should have made a query', async function () {
56 await servers[0].serversCommand.waitUntilLog(`root email is admin${servers[0].internalServerNumber}@example.com`)
60 describe('Config', function () {
62 it('Should have the correct webserver url', async function () {
63 await servers[0].serversCommand.waitUntilLog(`server url is http://localhost:${servers[0].port}`)
66 it('Should have the correct config', async function () {
67 const res = await makeGetRequest({
69 path: '/plugins/test-four/router/server-config',
70 statusCodeExpected: HttpStatusCode.OK_200
73 expect(res.body.serverConfig).to.exist
74 expect(res.body.serverConfig.instance.name).to.equal('PeerTube')
78 describe('Server', function () {
80 it('Should get the server actor', async function () {
81 await servers[0].serversCommand.waitUntilLog('server actor name is peertube')
85 describe('Plugin', function () {
87 it('Should get the base static route', async function () {
88 const res = await makeGetRequest({
90 path: '/plugins/test-four/router/static-route',
91 statusCodeExpected: HttpStatusCode.OK_200
94 expect(res.body.staticRoute).to.equal('/plugins/test-four/0.0.1/static/')
97 it('Should get the base static route', async function () {
98 const baseRouter = '/plugins/test-four/0.0.1/router/'
100 const res = await makeGetRequest({
102 path: baseRouter + 'router-route',
103 statusCodeExpected: HttpStatusCode.OK_200
106 expect(res.body.routerRoute).to.equal(baseRouter)
110 describe('User', function () {
112 it('Should not get a user if not authenticated', async function () {
113 await makeGetRequest({
115 path: '/plugins/test-four/router/user',
116 statusCodeExpected: HttpStatusCode.NOT_FOUND_404
120 it('Should get a user if authenticated', async function () {
121 const res = await makeGetRequest({
123 token: servers[0].accessToken,
124 path: '/plugins/test-four/router/user',
125 statusCodeExpected: HttpStatusCode.OK_200
128 expect(res.body.username).to.equal('root')
129 expect(res.body.displayName).to.equal('root')
130 expect(res.body.isAdmin).to.be.true
131 expect(res.body.isModerator).to.be.false
132 expect(res.body.isUser).to.be.false
136 describe('Moderation', function () {
137 let videoUUIDServer1: string
139 before(async function () {
143 const res = await await servers[0].videosCommand.quickUpload({ name: 'video server 1' })
144 videoUUIDServer1 = res.uuid
148 await await servers[1].videosCommand.quickUpload({ name: 'video server 2' })
151 await waitJobs(servers)
153 const { data } = await servers[0].videosCommand.list()
155 expect(data).to.have.lengthOf(2)
158 it('Should mute server 2', async function () {
160 await postCommand(servers[0], 'blockServer', { hostToBlock: `localhost:${servers[1].port}` })
162 const { data } = await servers[0].videosCommand.list()
164 expect(data).to.have.lengthOf(1)
165 expect(data[0].name).to.equal('video server 1')
168 it('Should unmute server 2', async function () {
169 await postCommand(servers[0], 'unblockServer', { hostToUnblock: `localhost:${servers[1].port}` })
171 const { data } = await servers[0].videosCommand.list()
173 expect(data).to.have.lengthOf(2)
176 it('Should mute account of server 2', async function () {
177 await postCommand(servers[0], 'blockAccount', { handleToBlock: `root@localhost:${servers[1].port}` })
179 const { data } = await servers[0].videosCommand.list()
181 expect(data).to.have.lengthOf(1)
182 expect(data[0].name).to.equal('video server 1')
185 it('Should unmute account of server 2', async function () {
186 await postCommand(servers[0], 'unblockAccount', { handleToUnblock: `root@localhost:${servers[1].port}` })
188 const { data } = await servers[0].videosCommand.list()
190 expect(data).to.have.lengthOf(2)
193 it('Should blacklist video', async function () {
196 await postCommand(servers[0], 'blacklist', { videoUUID: videoUUIDServer1, unfederate: true })
198 await waitJobs(servers)
200 for (const server of servers) {
201 const { data } = await server.videosCommand.list()
203 expect(data).to.have.lengthOf(1)
204 expect(data[0].name).to.equal('video server 2')
208 it('Should unblacklist video', async function () {
211 await postCommand(servers[0], 'unblacklist', { videoUUID: videoUUIDServer1 })
213 await waitJobs(servers)
215 for (const server of servers) {
216 const { data } = await server.videosCommand.list()
218 expect(data).to.have.lengthOf(2)
223 describe('Videos', function () {
224 let videoUUID: string
227 const res = await await servers[0].videosCommand.quickUpload({ name: 'video1' })
231 it('Should remove a video after a view', async function () {
234 // Should not throw -> video exists
235 await servers[0].videosCommand.get({ id: videoUUID })
236 // Should delete the video
237 await servers[0].videosCommand.view({ id: videoUUID })
239 await servers[0].serversCommand.waitUntilLog('Video deleted by plugin four.')
242 // Should throw because the video should have been deleted
243 await servers[0].videosCommand.get({ id: videoUUID })
244 throw new Error('Video exists')
246 if (err.message.includes('exists')) throw err
249 await checkVideoFilesWereRemoved(videoUUID, servers[0])
252 it('Should have fetched the video by URL', async function () {
253 await servers[0].serversCommand.waitUntilLog(`video from DB uuid is ${videoUUID}`)
257 after(async function () {
258 await cleanupTests(servers)