]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/plugins/plugin-helpers.ts
c0d95e1e0cad3f251d8dae877e8376e79cfce66a
[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 } from '../../../shared/extra-utils'
17 import { cleanupTests, flushAndRunMultipleServers, ServerInfo, waitUntilLog } from '../../../shared/extra-utils/server/servers'
18 import { expect } from 'chai'
19 import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
20
21 function postCommand (server: ServerInfo, command: string, bodyArg?: object) {
22 const body = { command }
23 if (bodyArg) Object.assign(body, bodyArg)
24
25 return makePostBodyRequest({
26 url: server.url,
27 path: '/plugins/test-four/router/commander',
28 fields: body,
29 statusCodeExpected: HttpStatusCode.NO_CONTENT_204
30 })
31 }
32
33 describe('Test plugin helpers', function () {
34 let servers: ServerInfo[]
35
36 before(async function () {
37 this.timeout(60000)
38
39 servers = await flushAndRunMultipleServers(2)
40 await setAccessTokensToServers(servers)
41
42 await doubleFollow(servers[0], servers[1])
43
44 await installPlugin({
45 url: servers[0].url,
46 accessToken: servers[0].accessToken,
47 path: getPluginTestPath('-four')
48 })
49 })
50
51 describe('Logger', function () {
52
53 it('Should have logged things', async function () {
54 await waitUntilLog(servers[0], 'localhost:' + servers[0].port + ' peertube-plugin-test-four', 1, false)
55 await waitUntilLog(servers[0], 'Hello world from plugin four', 1)
56 })
57 })
58
59 describe('Database', function () {
60
61 it('Should have made a query', async function () {
62 await waitUntilLog(servers[0], `root email is admin${servers[0].internalServerNumber}@example.com`)
63 })
64 })
65
66 describe('Config', function () {
67
68 it('Should have the correct webserver url', async function () {
69 await waitUntilLog(servers[0], `server url is http://localhost:${servers[0].port}`)
70 })
71 })
72
73 describe('Server', function () {
74
75 it('Should get the server actor', async function () {
76 await waitUntilLog(servers[0], 'server actor name is peertube')
77 })
78 })
79
80 describe('Moderation', function () {
81 let videoUUIDServer1: string
82
83 before(async function () {
84 this.timeout(30000)
85
86 {
87 const res = await uploadVideoAndGetId({ server: servers[0], videoName: 'video server 1' })
88 videoUUIDServer1 = res.uuid
89 }
90
91 {
92 await uploadVideoAndGetId({ server: servers[1], videoName: 'video server 2' })
93 }
94
95 await waitJobs(servers)
96
97 const res = await getVideosList(servers[0].url)
98 const videos = res.body.data
99
100 expect(videos).to.have.lengthOf(2)
101 })
102
103 it('Should mute server 2', async function () {
104 this.timeout(10000)
105 await postCommand(servers[0], 'blockServer', { hostToBlock: `localhost:${servers[1].port}` })
106
107 const res = await getVideosList(servers[0].url)
108 const videos = res.body.data
109
110 expect(videos).to.have.lengthOf(1)
111 expect(videos[0].name).to.equal('video server 1')
112 })
113
114 it('Should unmute server 2', async function () {
115 await postCommand(servers[0], 'unblockServer', { hostToUnblock: `localhost:${servers[1].port}` })
116
117 const res = await getVideosList(servers[0].url)
118 const videos = res.body.data
119
120 expect(videos).to.have.lengthOf(2)
121 })
122
123 it('Should mute account of server 2', async function () {
124 await postCommand(servers[0], 'blockAccount', { handleToBlock: `root@localhost:${servers[1].port}` })
125
126 const res = await getVideosList(servers[0].url)
127 const videos = res.body.data
128
129 expect(videos).to.have.lengthOf(1)
130 expect(videos[0].name).to.equal('video server 1')
131 })
132
133 it('Should unmute account of server 2', async function () {
134 await postCommand(servers[0], 'unblockAccount', { handleToUnblock: `root@localhost:${servers[1].port}` })
135
136 const res = await getVideosList(servers[0].url)
137 const videos = res.body.data
138
139 expect(videos).to.have.lengthOf(2)
140 })
141
142 it('Should blacklist video', async function () {
143 this.timeout(10000)
144
145 await postCommand(servers[0], 'blacklist', { videoUUID: videoUUIDServer1, unfederate: true })
146
147 await waitJobs(servers)
148
149 for (const server of servers) {
150 const res = await getVideosList(server.url)
151 const videos = res.body.data
152
153 expect(videos).to.have.lengthOf(1)
154 expect(videos[0].name).to.equal('video server 2')
155 }
156 })
157
158 it('Should unblacklist video', async function () {
159 this.timeout(10000)
160
161 await postCommand(servers[0], 'unblacklist', { videoUUID: videoUUIDServer1 })
162
163 await waitJobs(servers)
164
165 for (const server of servers) {
166 const res = await getVideosList(server.url)
167 const videos = res.body.data
168
169 expect(videos).to.have.lengthOf(2)
170 }
171 })
172 })
173
174 describe('Videos', function () {
175 let videoUUID: string
176
177 before(async () => {
178 const res = await uploadVideoAndGetId({ server: servers[0], videoName: 'video1' })
179 videoUUID = res.uuid
180 })
181
182 it('Should remove a video after a view', async function () {
183 this.timeout(20000)
184
185 // Should not throw -> video exists
186 await getVideo(servers[0].url, videoUUID)
187 // Should delete the video
188 await viewVideo(servers[0].url, videoUUID)
189
190 await waitUntilLog(servers[0], 'Video deleted by plugin four.')
191
192 try {
193 // Should throw because the video should have been deleted
194 await getVideo(servers[0].url, videoUUID)
195 throw new Error('Video exists')
196 } catch (err) {
197 if (err.message.includes('exists')) throw err
198 }
199
200 await checkVideoFilesWereRemoved(videoUUID, servers[0].internalServerNumber)
201 })
202
203 it('Should have fetched the video by URL', async function () {
204 await waitUntilLog(servers[0], `video from DB uuid is ${videoUUID}`)
205 })
206 })
207
208 after(async function () {
209 await cleanupTests(servers)
210 })
211 })