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