]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/plugins/filter-hooks.ts
Increase notifications comment test timeout
[github/Chocobozzz/PeerTube.git] / server / tests / plugins / filter-hooks.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import 'mocha'
4 import * as chai from 'chai'
5 import { ServerConfig } from '@shared/models'
6 import {
7 addVideoCommentReply,
8 addVideoCommentThread,
9 createLive,
10 doubleFollow,
11 getAccountVideos,
12 getConfig,
13 getPluginTestPath,
14 getVideo,
15 getVideoChannelVideos,
16 getVideoCommentThreads,
17 getVideosList,
18 getVideosListPagination,
19 getVideoThreadComments,
20 getVideoWithToken,
21 installPlugin,
22 registerUser,
23 setAccessTokensToServers,
24 setDefaultVideoChannel,
25 updateCustomSubConfig,
26 updateVideo,
27 uploadVideo,
28 waitJobs
29 } from '../../../shared/extra-utils'
30 import { cleanupTests, flushAndRunMultipleServers, ServerInfo } from '../../../shared/extra-utils/server/servers'
31 import { getGoodVideoUrl, getMyVideoImports, importVideo } from '../../../shared/extra-utils/videos/video-imports'
32 import { VideoDetails, VideoImport, VideoImportState, VideoPrivacy } from '../../../shared/models/videos'
33 import { VideoCommentThreadTree } from '../../../shared/models/videos/video-comment.model'
34 import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
35
36 const expect = chai.expect
37
38 describe('Test plugin filter hooks', function () {
39 let servers: ServerInfo[]
40 let videoUUID: string
41 let threadId: number
42
43 before(async function () {
44 this.timeout(30000)
45
46 servers = await flushAndRunMultipleServers(2)
47 await setAccessTokensToServers(servers)
48 await setDefaultVideoChannel(servers)
49 await doubleFollow(servers[0], servers[1])
50
51 await installPlugin({
52 url: servers[0].url,
53 accessToken: servers[0].accessToken,
54 path: getPluginTestPath()
55 })
56
57 await installPlugin({
58 url: servers[0].url,
59 accessToken: servers[0].accessToken,
60 path: getPluginTestPath('-two')
61 })
62
63 for (let i = 0; i < 10; i++) {
64 await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'default video ' + i })
65 }
66
67 const res = await getVideosList(servers[0].url)
68 videoUUID = res.body.data[0].uuid
69
70 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, {
71 live: { enabled: true },
72 signup: { enabled: true },
73 import: {
74 videos: {
75 http: { enabled: true },
76 torrent: { enabled: true }
77 }
78 }
79 })
80 })
81
82 it('Should run filter:api.videos.list.params', async function () {
83 const res = await getVideosListPagination(servers[0].url, 0, 2)
84
85 // 2 plugins do +1 to the count parameter
86 expect(res.body.data).to.have.lengthOf(4)
87 })
88
89 it('Should run filter:api.videos.list.result', async function () {
90 const res = await getVideosListPagination(servers[0].url, 0, 0)
91
92 // Plugin do +1 to the total result
93 expect(res.body.total).to.equal(11)
94 })
95
96 it('Should run filter:api.accounts.videos.list.params', async function () {
97 const res = await getAccountVideos(servers[0].url, servers[0].accessToken, 'root', 0, 2)
98
99 // 1 plugin do +1 to the count parameter
100 expect(res.body.data).to.have.lengthOf(3)
101 })
102
103 it('Should run filter:api.accounts.videos.list.result', async function () {
104 const res = await getAccountVideos(servers[0].url, servers[0].accessToken, 'root', 0, 2)
105
106 // Plugin do +2 to the total result
107 expect(res.body.total).to.equal(12)
108 })
109
110 it('Should run filter:api.video-channels.videos.list.params', async function () {
111 const res = await getVideoChannelVideos(servers[0].url, servers[0].accessToken, 'root_channel', 0, 2)
112
113 // 1 plugin do +3 to the count parameter
114 expect(res.body.data).to.have.lengthOf(5)
115 })
116
117 it('Should run filter:api.video-channels.videos.list.result', async function () {
118 const res = await getVideoChannelVideos(servers[0].url, servers[0].accessToken, 'root_channel', 0, 2)
119
120 // Plugin do +3 to the total result
121 expect(res.body.total).to.equal(13)
122 })
123
124 it('Should run filter:api.video.get.result', async function () {
125 const res = await getVideo(servers[0].url, videoUUID)
126
127 expect(res.body.name).to.contain('<3')
128 })
129
130 it('Should run filter:api.video.upload.accept.result', async function () {
131 await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video with bad word' }, HttpStatusCode.FORBIDDEN_403)
132 })
133
134 it('Should run filter:api.live-video.create.accept.result', async function () {
135 const attributes = {
136 name: 'video with bad word',
137 privacy: VideoPrivacy.PUBLIC,
138 channelId: servers[0].videoChannel.id
139 }
140
141 await createLive(servers[0].url, servers[0].accessToken, attributes, HttpStatusCode.FORBIDDEN_403)
142 })
143
144 it('Should run filter:api.video.pre-import-url.accept.result', async function () {
145 const baseAttributes = {
146 name: 'normal title',
147 privacy: VideoPrivacy.PUBLIC,
148 channelId: servers[0].videoChannel.id,
149 targetUrl: getGoodVideoUrl() + 'bad'
150 }
151 await importVideo(servers[0].url, servers[0].accessToken, baseAttributes, HttpStatusCode.FORBIDDEN_403)
152 })
153
154 it('Should run filter:api.video.pre-import-torrent.accept.result', async function () {
155 const baseAttributes = {
156 name: 'bad torrent',
157 privacy: VideoPrivacy.PUBLIC,
158 channelId: servers[0].videoChannel.id,
159 torrentfile: 'video-720p.torrent' as any
160 }
161 await importVideo(servers[0].url, servers[0].accessToken, baseAttributes, HttpStatusCode.FORBIDDEN_403)
162 })
163
164 it('Should run filter:api.video.post-import-url.accept.result', async function () {
165 this.timeout(60000)
166
167 let videoImportId: number
168
169 {
170 const baseAttributes = {
171 name: 'title with bad word',
172 privacy: VideoPrivacy.PUBLIC,
173 channelId: servers[0].videoChannel.id,
174 targetUrl: getGoodVideoUrl()
175 }
176 const res = await importVideo(servers[0].url, servers[0].accessToken, baseAttributes)
177 videoImportId = res.body.id
178 }
179
180 await waitJobs(servers)
181
182 {
183 const res = await getMyVideoImports(servers[0].url, servers[0].accessToken)
184 const videoImports = res.body.data as VideoImport[]
185
186 const videoImport = videoImports.find(i => i.id === videoImportId)
187
188 expect(videoImport.state.id).to.equal(VideoImportState.REJECTED)
189 expect(videoImport.state.label).to.equal('Rejected')
190 }
191 })
192
193 it('Should run filter:api.video.post-import-torrent.accept.result', async function () {
194 this.timeout(60000)
195
196 let videoImportId: number
197
198 {
199 const baseAttributes = {
200 name: 'title with bad word',
201 privacy: VideoPrivacy.PUBLIC,
202 channelId: servers[0].videoChannel.id,
203 torrentfile: 'video-720p.torrent' as any
204 }
205 const res = await importVideo(servers[0].url, servers[0].accessToken, baseAttributes)
206 videoImportId = res.body.id
207 }
208
209 await waitJobs(servers)
210
211 {
212 const res = await getMyVideoImports(servers[0].url, servers[0].accessToken)
213 const videoImports = res.body.data as VideoImport[]
214
215 const videoImport = videoImports.find(i => i.id === videoImportId)
216
217 expect(videoImport.state.id).to.equal(VideoImportState.REJECTED)
218 expect(videoImport.state.label).to.equal('Rejected')
219 }
220 })
221
222 it('Should run filter:api.video-thread.create.accept.result', async function () {
223 await addVideoCommentThread(servers[0].url, servers[0].accessToken, videoUUID, 'comment with bad word', HttpStatusCode.FORBIDDEN_403)
224 })
225
226 it('Should run filter:api.video-comment-reply.create.accept.result', async function () {
227 const res = await addVideoCommentThread(servers[0].url, servers[0].accessToken, videoUUID, 'thread')
228 threadId = res.body.comment.id
229
230 await addVideoCommentReply(
231 servers[0].url,
232 servers[0].accessToken,
233 videoUUID,
234 threadId,
235 'comment with bad word',
236 HttpStatusCode.FORBIDDEN_403
237 )
238 await addVideoCommentReply(servers[0].url, servers[0].accessToken, videoUUID, threadId, 'comment with good word', HttpStatusCode.OK_200)
239 })
240
241 it('Should run filter:api.video-threads.list.params', async function () {
242 const res = await getVideoCommentThreads(servers[0].url, videoUUID, 0, 0)
243
244 // our plugin do +1 to the count parameter
245 expect(res.body.data).to.have.lengthOf(1)
246 })
247
248 it('Should run filter:api.video-threads.list.result', async function () {
249 const res = await getVideoCommentThreads(servers[0].url, videoUUID, 0, 0)
250
251 // Plugin do +1 to the total result
252 expect(res.body.total).to.equal(2)
253 })
254
255 it('Should run filter:api.video-thread-comments.list.params')
256
257 it('Should run filter:api.video-thread-comments.list.result', async function () {
258 const res = await getVideoThreadComments(servers[0].url, videoUUID, threadId)
259
260 const thread = res.body as VideoCommentThreadTree
261 expect(thread.comment.text.endsWith(' <3')).to.be.true
262 })
263
264 describe('Should run filter:video.auto-blacklist.result', function () {
265
266 async function checkIsBlacklisted (oldRes: any, value: boolean) {
267 const videoId = oldRes.body.video.uuid
268
269 const res = await getVideoWithToken(servers[0].url, servers[0].accessToken, videoId)
270 const video: VideoDetails = res.body
271 expect(video.blacklisted).to.equal(value)
272 }
273
274 it('Should blacklist on upload', async function () {
275 const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video please blacklist me' })
276 await checkIsBlacklisted(res, true)
277 })
278
279 it('Should blacklist on import', async function () {
280 this.timeout(15000)
281
282 const attributes = {
283 name: 'video please blacklist me',
284 targetUrl: getGoodVideoUrl(),
285 channelId: servers[0].videoChannel.id
286 }
287 const res = await importVideo(servers[0].url, servers[0].accessToken, attributes)
288 await checkIsBlacklisted(res, true)
289 })
290
291 it('Should blacklist on update', async function () {
292 const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video' })
293 const videoId = res.body.video.uuid
294 await checkIsBlacklisted(res, false)
295
296 await updateVideo(servers[0].url, servers[0].accessToken, videoId, { name: 'please blacklist me' })
297 await checkIsBlacklisted(res, true)
298 })
299
300 it('Should blacklist on remote upload', async function () {
301 this.timeout(45000)
302
303 const res = await uploadVideo(servers[1].url, servers[1].accessToken, { name: 'remote please blacklist me' })
304 await waitJobs(servers)
305
306 await checkIsBlacklisted(res, true)
307 })
308
309 it('Should blacklist on remote update', async function () {
310 this.timeout(45000)
311
312 const res = await uploadVideo(servers[1].url, servers[1].accessToken, { name: 'video' })
313 await waitJobs(servers)
314
315 const videoId = res.body.video.uuid
316 await checkIsBlacklisted(res, false)
317
318 await updateVideo(servers[1].url, servers[1].accessToken, videoId, { name: 'please blacklist me' })
319 await waitJobs(servers)
320
321 await checkIsBlacklisted(res, true)
322 })
323 })
324
325 describe('Should run filter:api.user.signup.allowed.result', function () {
326
327 it('Should run on config endpoint', async function () {
328 const res = await getConfig(servers[0].url)
329 expect((res.body as ServerConfig).signup.allowed).to.be.true
330 })
331
332 it('Should allow a signup', async function () {
333 await registerUser(servers[0].url, 'john', 'password')
334 })
335
336 it('Should not allow a signup', async function () {
337 const res = await registerUser(servers[0].url, 'jma', 'password', HttpStatusCode.FORBIDDEN_403)
338
339 expect(res.body.error).to.equal('No jma')
340 })
341 })
342
343 after(async function () {
344 await cleanupTests(servers)
345 })
346 })