]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/plugins/plugin-helpers.ts
Search channels against handles and not names
[github/Chocobozzz/PeerTube.git] / server / tests / plugins / plugin-helpers.ts
CommitLineData
1b05d82d
C
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
1b05d82d 3import 'mocha'
ae2abfd3 4import { expect } from 'chai'
ab3ead3a
C
5import {
6 checkVideoFilesWereRemoved,
ae2abfd3 7 cleanupTests,
254d3579 8 createMultipleServers,
4c7e60bc 9 doubleFollow,
ae2abfd3 10 makeGetRequest,
80fdaf06 11 makePostBodyRequest,
254d3579 12 PeerTubeServer,
4c7e60bc 13 PluginsCommand,
ab3ead3a 14 setAccessTokensToServers,
6c5065a0 15 waitJobs
ae2abfd3 16} from '@shared/extra-utils'
4c7e60bc 17import { HttpStatusCode } from '@shared/models'
80fdaf06 18
254d3579 19function postCommand (server: PeerTubeServer, command: string, bodyArg?: object) {
80fdaf06
C
20 const body = { command }
21 if (bodyArg) Object.assign(body, bodyArg)
22
23 return makePostBodyRequest({
24 url: server.url,
25 path: '/plugins/test-four/router/commander',
26 fields: body,
c0e8b12e 27 expectedStatus: HttpStatusCode.NO_CONTENT_204
80fdaf06
C
28 })
29}
1b05d82d
C
30
31describe('Test plugin helpers', function () {
254d3579 32 let servers: PeerTubeServer[]
1b05d82d
C
33
34 before(async function () {
80fdaf06
C
35 this.timeout(60000)
36
254d3579 37 servers = await createMultipleServers(2)
80fdaf06 38 await setAccessTokensToServers(servers)
1b05d82d 39
80fdaf06 40 await doubleFollow(servers[0], servers[1])
1b05d82d 41
89d241a7 42 await servers[0].plugins.install({ path: PluginsCommand.getPluginTestPath('-four') })
1b05d82d
C
43 })
44
80fdaf06
C
45 describe('Logger', function () {
46
47 it('Should have logged things', async function () {
89d241a7
C
48 await servers[0].servers.waitUntilLog('localhost:' + servers[0].port + ' peertube-plugin-test-four', 1, false)
49 await servers[0].servers.waitUntilLog('Hello world from plugin four', 1)
80fdaf06 50 })
1b05d82d
C
51 })
52
80fdaf06
C
53 describe('Database', function () {
54
55 it('Should have made a query', async function () {
89d241a7 56 await servers[0].servers.waitUntilLog(`root email is admin${servers[0].internalServerNumber}@example.com`)
80fdaf06
C
57 })
58 })
59
60 describe('Config', function () {
61
62 it('Should have the correct webserver url', async function () {
89d241a7 63 await servers[0].servers.waitUntilLog(`server url is http://localhost:${servers[0].port}`)
80fdaf06 64 })
22820226
C
65
66 it('Should have the correct config', async function () {
67 const res = await makeGetRequest({
68 url: servers[0].url,
69 path: '/plugins/test-four/router/server-config',
c0e8b12e 70 expectedStatus: HttpStatusCode.OK_200
22820226
C
71 })
72
73 expect(res.body.serverConfig).to.exist
74 expect(res.body.serverConfig.instance.name).to.equal('PeerTube')
75 })
80fdaf06
C
76 })
77
78 describe('Server', function () {
79
80 it('Should get the server actor', async function () {
89d241a7 81 await servers[0].servers.waitUntilLog('server actor name is peertube')
80fdaf06
C
82 })
83 })
84
22820226
C
85 describe('Plugin', function () {
86
87 it('Should get the base static route', async function () {
88 const res = await makeGetRequest({
89 url: servers[0].url,
90 path: '/plugins/test-four/router/static-route',
c0e8b12e 91 expectedStatus: HttpStatusCode.OK_200
22820226
C
92 })
93
94 expect(res.body.staticRoute).to.equal('/plugins/test-four/0.0.1/static/')
95 })
302eba0d
C
96
97 it('Should get the base static route', async function () {
98 const baseRouter = '/plugins/test-four/0.0.1/router/'
99
100 const res = await makeGetRequest({
101 url: servers[0].url,
102 path: baseRouter + 'router-route',
c0e8b12e 103 expectedStatus: HttpStatusCode.OK_200
302eba0d
C
104 })
105
106 expect(res.body.routerRoute).to.equal(baseRouter)
107 })
108 })
109
110 describe('User', function () {
111
112 it('Should not get a user if not authenticated', async function () {
62906990 113 await makeGetRequest({
302eba0d
C
114 url: servers[0].url,
115 path: '/plugins/test-four/router/user',
c0e8b12e 116 expectedStatus: HttpStatusCode.NOT_FOUND_404
302eba0d 117 })
302eba0d
C
118 })
119
120 it('Should get a user if authenticated', async function () {
121 const res = await makeGetRequest({
122 url: servers[0].url,
123 token: servers[0].accessToken,
124 path: '/plugins/test-four/router/user',
c0e8b12e 125 expectedStatus: HttpStatusCode.OK_200
302eba0d
C
126 })
127
302eba0d 128 expect(res.body.username).to.equal('root')
b31d7262 129 expect(res.body.displayName).to.equal('root')
302eba0d
C
130 expect(res.body.isAdmin).to.be.true
131 expect(res.body.isModerator).to.be.false
132 expect(res.body.isUser).to.be.false
133 })
22820226
C
134 })
135
80fdaf06
C
136 describe('Moderation', function () {
137 let videoUUIDServer1: string
138
139 before(async function () {
75e12406 140 this.timeout(60000)
80fdaf06
C
141
142 {
c0e8b12e 143 const res = await servers[0].videos.quickUpload({ name: 'video server 1' })
80fdaf06
C
144 videoUUIDServer1 = res.uuid
145 }
146
147 {
c0e8b12e 148 await servers[1].videos.quickUpload({ name: 'video server 2' })
80fdaf06
C
149 }
150
151 await waitJobs(servers)
152
89d241a7 153 const { data } = await servers[0].videos.list()
80fdaf06 154
d23dd9fb 155 expect(data).to.have.lengthOf(2)
80fdaf06
C
156 })
157
158 it('Should mute server 2', async function () {
159 this.timeout(10000)
160 await postCommand(servers[0], 'blockServer', { hostToBlock: `localhost:${servers[1].port}` })
161
89d241a7 162 const { data } = await servers[0].videos.list()
80fdaf06 163
d23dd9fb
C
164 expect(data).to.have.lengthOf(1)
165 expect(data[0].name).to.equal('video server 1')
80fdaf06
C
166 })
167
168 it('Should unmute server 2', async function () {
169 await postCommand(servers[0], 'unblockServer', { hostToUnblock: `localhost:${servers[1].port}` })
170
89d241a7 171 const { data } = await servers[0].videos.list()
80fdaf06 172
d23dd9fb 173 expect(data).to.have.lengthOf(2)
80fdaf06
C
174 })
175
176 it('Should mute account of server 2', async function () {
177 await postCommand(servers[0], 'blockAccount', { handleToBlock: `root@localhost:${servers[1].port}` })
178
89d241a7 179 const { data } = await servers[0].videos.list()
80fdaf06 180
d23dd9fb
C
181 expect(data).to.have.lengthOf(1)
182 expect(data[0].name).to.equal('video server 1')
80fdaf06
C
183 })
184
185 it('Should unmute account of server 2', async function () {
186 await postCommand(servers[0], 'unblockAccount', { handleToUnblock: `root@localhost:${servers[1].port}` })
187
89d241a7 188 const { data } = await servers[0].videos.list()
80fdaf06 189
d23dd9fb 190 expect(data).to.have.lengthOf(2)
80fdaf06
C
191 })
192
193 it('Should blacklist video', async function () {
194 this.timeout(10000)
195
196 await postCommand(servers[0], 'blacklist', { videoUUID: videoUUIDServer1, unfederate: true })
197
198 await waitJobs(servers)
199
200 for (const server of servers) {
89d241a7 201 const { data } = await server.videos.list()
80fdaf06 202
d23dd9fb
C
203 expect(data).to.have.lengthOf(1)
204 expect(data[0].name).to.equal('video server 2')
80fdaf06
C
205 }
206 })
207
208 it('Should unblacklist video', async function () {
209 this.timeout(10000)
210
211 await postCommand(servers[0], 'unblacklist', { videoUUID: videoUUIDServer1 })
212
213 await waitJobs(servers)
214
215 for (const server of servers) {
89d241a7 216 const { data } = await server.videos.list()
80fdaf06 217
d23dd9fb 218 expect(data).to.have.lengthOf(2)
80fdaf06
C
219 }
220 })
1b05d82d
C
221 })
222
80fdaf06
C
223 describe('Videos', function () {
224 let videoUUID: string
225
226 before(async () => {
c0e8b12e 227 const res = await servers[0].videos.quickUpload({ name: 'video1' })
80fdaf06
C
228 videoUUID = res.uuid
229 })
ab3ead3a 230
80fdaf06 231 it('Should remove a video after a view', async function () {
59fd824c 232 this.timeout(40000)
ab3ead3a 233
80fdaf06 234 // Should not throw -> video exists
83903cb6 235 const video = await servers[0].videos.get({ id: videoUUID })
80fdaf06 236 // Should delete the video
89d241a7 237 await servers[0].videos.view({ id: videoUUID })
ab3ead3a 238
89d241a7 239 await servers[0].servers.waitUntilLog('Video deleted by plugin four.')
ab3ead3a 240
80fdaf06
C
241 try {
242 // Should throw because the video should have been deleted
89d241a7 243 await servers[0].videos.get({ id: videoUUID })
80fdaf06
C
244 throw new Error('Video exists')
245 } catch (err) {
246 if (err.message.includes('exists')) throw err
247 }
ab3ead3a 248
83903cb6 249 await checkVideoFilesWereRemoved({ server: servers[0], video })
80fdaf06
C
250 })
251
252 it('Should have fetched the video by URL', async function () {
89d241a7 253 await servers[0].servers.waitUntilLog(`video from DB uuid is ${videoUUID}`)
80fdaf06 254 })
ab3ead3a
C
255 })
256
1b05d82d 257 after(async function () {
80fdaf06 258 await cleanupTests(servers)
1b05d82d
C
259 })
260})