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