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