]>
Commit | Line | Data |
---|---|---|
a1587156 | 1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ |
0e1dc3e7 | 2 | |
b488ba1e | 3 | import 'mocha' |
0e1dc3e7 | 4 | import * as chai from 'chai' |
5abb9fbb | 5 | import { orderBy } from 'lodash' |
c55e3d72 C |
6 | import { FIXTURE_URLS } from '@server/tests/shared' |
7 | import { UserAdminFlag, UserRole, VideoBlacklist, VideoBlacklistType } from '@shared/models' | |
0e1dc3e7 | 8 | import { |
e3d15a6a | 9 | BlacklistCommand, |
03371ad9 | 10 | cleanupTests, |
254d3579 | 11 | createMultipleServers, |
59bbcced | 12 | doubleFollow, |
975e6e0e | 13 | killallServers, |
254d3579 | 14 | PeerTubeServer, |
975e6e0e | 15 | setAccessTokensToServers, |
af971e06 | 16 | waitJobs |
bf54587a | 17 | } from '@shared/server-commands' |
975e6e0e C |
18 | |
19 | const expect = chai.expect | |
0e1dc3e7 | 20 | |
1eddc9a7 | 21 | describe('Test video blacklist', function () { |
254d3579 | 22 | let servers: PeerTubeServer[] = [] |
5abb9fbb | 23 | let videoId: number |
e3d15a6a | 24 | let command: BlacklistCommand |
5abb9fbb | 25 | |
254d3579 | 26 | async function blacklistVideosOnServer (server: PeerTubeServer) { |
89d241a7 | 27 | const { data } = await server.videos.list() |
5abb9fbb | 28 | |
d23dd9fb | 29 | for (const video of data) { |
89d241a7 | 30 | await server.blacklist.add({ videoId: video.id, reason: 'super reason' }) |
5abb9fbb C |
31 | } |
32 | } | |
0e1dc3e7 C |
33 | |
34 | before(async function () { | |
572f8d3d | 35 | this.timeout(50000) |
0e1dc3e7 C |
36 | |
37 | // Run servers | |
254d3579 | 38 | servers = await createMultipleServers(2) |
0e1dc3e7 C |
39 | |
40 | // Get the access tokens | |
41 | await setAccessTokensToServers(servers) | |
42 | ||
975e6e0e C |
43 | // Server 1 and server 2 follow each other |
44 | await doubleFollow(servers[0], servers[1]) | |
0e1dc3e7 | 45 | |
5abb9fbb | 46 | // Upload 2 videos on server 2 |
89d241a7 C |
47 | await servers[1].videos.upload({ attributes: { name: 'My 1st video', description: 'A video on server 2' } }) |
48 | await servers[1].videos.upload({ attributes: { name: 'My 2nd video', description: 'A video on server 2' } }) | |
0e1dc3e7 | 49 | |
572f8d3d | 50 | // Wait videos propagation, server 2 has transcoding enabled |
3cd0734f | 51 | await waitJobs(servers) |
0e1dc3e7 | 52 | |
89d241a7 | 53 | command = servers[0].blacklist |
e3d15a6a | 54 | |
5abb9fbb C |
55 | // Blacklist the two videos on server 1 |
56 | await blacklistVideosOnServer(servers[0]) | |
57 | }) | |
58 | ||
59 | describe('When listing/searching videos', function () { | |
0e1dc3e7 | 60 | |
5abb9fbb C |
61 | it('Should not have the video blacklisted in videos list/search on server 1', async function () { |
62 | { | |
89d241a7 | 63 | const { total, data } = await servers[0].videos.list() |
0e1dc3e7 | 64 | |
d23dd9fb C |
65 | expect(total).to.equal(0) |
66 | expect(data).to.be.an('array') | |
67 | expect(data.length).to.equal(0) | |
5abb9fbb C |
68 | } |
69 | ||
70 | { | |
89d241a7 | 71 | const body = await servers[0].search.searchVideos({ search: 'video' }) |
5abb9fbb | 72 | |
af971e06 C |
73 | expect(body.total).to.equal(0) |
74 | expect(body.data).to.be.an('array') | |
75 | expect(body.data.length).to.equal(0) | |
5abb9fbb C |
76 | } |
77 | }) | |
78 | ||
79 | it('Should have the blacklisted video in videos list/search on server 2', async function () { | |
80 | { | |
89d241a7 | 81 | const { total, data } = await servers[1].videos.list() |
5abb9fbb | 82 | |
d23dd9fb C |
83 | expect(total).to.equal(2) |
84 | expect(data).to.be.an('array') | |
85 | expect(data.length).to.equal(2) | |
5abb9fbb C |
86 | } |
87 | ||
88 | { | |
89d241a7 | 89 | const body = await servers[1].search.searchVideos({ search: 'video' }) |
5abb9fbb | 90 | |
af971e06 C |
91 | expect(body.total).to.equal(2) |
92 | expect(body.data).to.be.an('array') | |
93 | expect(body.data.length).to.equal(2) | |
5abb9fbb C |
94 | } |
95 | }) | |
0e1dc3e7 C |
96 | }) |
97 | ||
7ccddd7b | 98 | describe('When listing manually blacklisted videos', function () { |
5abb9fbb | 99 | it('Should display all the blacklisted videos', async function () { |
e3d15a6a C |
100 | const body = await command.list() |
101 | expect(body.total).to.equal(2) | |
5abb9fbb | 102 | |
e3d15a6a | 103 | const blacklistedVideos = body.data |
5abb9fbb C |
104 | expect(blacklistedVideos).to.be.an('array') |
105 | expect(blacklistedVideos.length).to.equal(2) | |
106 | ||
107 | for (const blacklistedVideo of blacklistedVideos) { | |
108 | expect(blacklistedVideo.reason).to.equal('super reason') | |
109 | videoId = blacklistedVideo.video.id | |
110 | } | |
111 | }) | |
112 | ||
7ccddd7b | 113 | it('Should display all the blacklisted videos when applying manual type filter', async function () { |
e3d15a6a C |
114 | const body = await command.list({ type: VideoBlacklistType.MANUAL }) |
115 | expect(body.total).to.equal(2) | |
7ccddd7b | 116 | |
e3d15a6a | 117 | const blacklistedVideos = body.data |
7ccddd7b JM |
118 | expect(blacklistedVideos).to.be.an('array') |
119 | expect(blacklistedVideos.length).to.equal(2) | |
120 | }) | |
121 | ||
122 | it('Should display nothing when applying automatic type filter', async function () { | |
e3d15a6a C |
123 | const body = await command.list({ type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED }) |
124 | expect(body.total).to.equal(0) | |
7ccddd7b | 125 | |
e3d15a6a | 126 | const blacklistedVideos = body.data |
7ccddd7b JM |
127 | expect(blacklistedVideos).to.be.an('array') |
128 | expect(blacklistedVideos.length).to.equal(0) | |
129 | }) | |
130 | ||
5abb9fbb | 131 | it('Should get the correct sort when sorting by descending id', async function () { |
e3d15a6a C |
132 | const body = await command.list({ sort: '-id' }) |
133 | expect(body.total).to.equal(2) | |
5abb9fbb | 134 | |
e3d15a6a | 135 | const blacklistedVideos = body.data |
5abb9fbb C |
136 | expect(blacklistedVideos).to.be.an('array') |
137 | expect(blacklistedVideos.length).to.equal(2) | |
138 | ||
e3d15a6a | 139 | const result = orderBy(body.data, [ 'id' ], [ 'desc' ]) |
5abb9fbb C |
140 | expect(blacklistedVideos).to.deep.equal(result) |
141 | }) | |
142 | ||
143 | it('Should get the correct sort when sorting by descending video name', async function () { | |
e3d15a6a C |
144 | const body = await command.list({ sort: '-name' }) |
145 | expect(body.total).to.equal(2) | |
5abb9fbb | 146 | |
e3d15a6a | 147 | const blacklistedVideos = body.data |
5abb9fbb C |
148 | expect(blacklistedVideos).to.be.an('array') |
149 | expect(blacklistedVideos.length).to.equal(2) | |
150 | ||
e3d15a6a | 151 | const result = orderBy(body.data, [ 'name' ], [ 'desc' ]) |
5abb9fbb C |
152 | expect(blacklistedVideos).to.deep.equal(result) |
153 | }) | |
154 | ||
155 | it('Should get the correct sort when sorting by ascending creation date', async function () { | |
e3d15a6a C |
156 | const body = await command.list({ sort: 'createdAt' }) |
157 | expect(body.total).to.equal(2) | |
5abb9fbb | 158 | |
e3d15a6a | 159 | const blacklistedVideos = body.data |
5abb9fbb C |
160 | expect(blacklistedVideos).to.be.an('array') |
161 | expect(blacklistedVideos.length).to.equal(2) | |
162 | ||
e3d15a6a | 163 | const result = orderBy(body.data, [ 'createdAt' ]) |
5abb9fbb C |
164 | expect(blacklistedVideos).to.deep.equal(result) |
165 | }) | |
0e1dc3e7 C |
166 | }) |
167 | ||
5abb9fbb C |
168 | describe('When updating blacklisted videos', function () { |
169 | it('Should change the reason', async function () { | |
e3d15a6a | 170 | await command.update({ videoId, reason: 'my super reason updated' }) |
5abb9fbb | 171 | |
e3d15a6a C |
172 | const body = await command.list({ sort: '-name' }) |
173 | const video = body.data.find(b => b.video.id === videoId) | |
0e1dc3e7 | 174 | |
5abb9fbb C |
175 | expect(video.reason).to.equal('my super reason updated') |
176 | }) | |
0e1dc3e7 C |
177 | }) |
178 | ||
5abb9fbb C |
179 | describe('When listing my videos', function () { |
180 | it('Should display blacklisted videos', async function () { | |
181 | await blacklistVideosOnServer(servers[1]) | |
182 | ||
89d241a7 | 183 | const { total, data } = await servers[1].videos.listMyVideos() |
0e1dc3e7 | 184 | |
d23dd9fb C |
185 | expect(total).to.equal(2) |
186 | expect(data).to.have.lengthOf(2) | |
5abb9fbb | 187 | |
d23dd9fb | 188 | for (const video of data) { |
5abb9fbb C |
189 | expect(video.blacklisted).to.be.true |
190 | expect(video.blacklistedReason).to.equal('super reason') | |
191 | } | |
192 | }) | |
0e1dc3e7 C |
193 | }) |
194 | ||
5abb9fbb | 195 | describe('When removing a blacklisted video', function () { |
3487330d | 196 | let videoToRemove: VideoBlacklist |
5abb9fbb C |
197 | let blacklist = [] |
198 | ||
199 | it('Should not have any video in videos list on server 1', async function () { | |
89d241a7 | 200 | const { total, data } = await servers[0].videos.list() |
d23dd9fb C |
201 | expect(total).to.equal(0) |
202 | expect(data).to.be.an('array') | |
203 | expect(data.length).to.equal(0) | |
5abb9fbb C |
204 | }) |
205 | ||
206 | it('Should remove a video from the blacklist on server 1', async function () { | |
207 | // Get one video in the blacklist | |
e3d15a6a C |
208 | const body = await command.list({ sort: '-name' }) |
209 | videoToRemove = body.data[0] | |
210 | blacklist = body.data.slice(1) | |
5abb9fbb C |
211 | |
212 | // Remove it | |
e3d15a6a | 213 | await command.remove({ videoId: videoToRemove.video.id }) |
5abb9fbb C |
214 | }) |
215 | ||
216 | it('Should have the ex-blacklisted video in videos list on server 1', async function () { | |
89d241a7 | 217 | const { total, data } = await servers[0].videos.list() |
d23dd9fb | 218 | expect(total).to.equal(1) |
5abb9fbb | 219 | |
d23dd9fb C |
220 | expect(data).to.be.an('array') |
221 | expect(data.length).to.equal(1) | |
5abb9fbb | 222 | |
d23dd9fb C |
223 | expect(data[0].name).to.equal(videoToRemove.video.name) |
224 | expect(data[0].id).to.equal(videoToRemove.video.id) | |
5abb9fbb C |
225 | }) |
226 | ||
227 | it('Should not have the ex-blacklisted video in videos blacklist list on server 1', async function () { | |
e3d15a6a C |
228 | const body = await command.list({ sort: '-name' }) |
229 | expect(body.total).to.equal(1) | |
0e1dc3e7 | 230 | |
e3d15a6a | 231 | const videos = body.data |
5abb9fbb C |
232 | expect(videos).to.be.an('array') |
233 | expect(videos.length).to.equal(1) | |
234 | expect(videos).to.deep.equal(blacklist) | |
235 | }) | |
0e1dc3e7 C |
236 | }) |
237 | ||
5abb9fbb C |
238 | describe('When blacklisting local videos', function () { |
239 | let video3UUID: string | |
240 | let video4UUID: string | |
241 | ||
242 | before(async function () { | |
243 | this.timeout(10000) | |
244 | ||
245 | { | |
89d241a7 | 246 | const { uuid } = await servers[0].videos.upload({ attributes: { name: 'Video 3' } }) |
d23dd9fb | 247 | video3UUID = uuid |
5abb9fbb C |
248 | } |
249 | { | |
89d241a7 | 250 | const { uuid } = await servers[0].videos.upload({ attributes: { name: 'Video 4' } }) |
d23dd9fb | 251 | video4UUID = uuid |
5abb9fbb C |
252 | } |
253 | ||
254 | await waitJobs(servers) | |
255 | }) | |
256 | ||
257 | it('Should blacklist video 3 and keep it federated', async function () { | |
258 | this.timeout(10000) | |
259 | ||
e3d15a6a | 260 | await command.add({ videoId: video3UUID, reason: 'super reason', unfederate: false }) |
5abb9fbb C |
261 | |
262 | await waitJobs(servers) | |
263 | ||
264 | { | |
89d241a7 | 265 | const { data } = await servers[0].videos.list() |
d23dd9fb | 266 | expect(data.find(v => v.uuid === video3UUID)).to.be.undefined |
5abb9fbb C |
267 | } |
268 | ||
269 | { | |
89d241a7 | 270 | const { data } = await servers[1].videos.list() |
d23dd9fb | 271 | expect(data.find(v => v.uuid === video3UUID)).to.not.be.undefined |
5abb9fbb C |
272 | } |
273 | }) | |
274 | ||
275 | it('Should unfederate the video', async function () { | |
276 | this.timeout(10000) | |
277 | ||
e3d15a6a | 278 | await command.add({ videoId: video4UUID, reason: 'super reason', unfederate: true }) |
5abb9fbb C |
279 | |
280 | await waitJobs(servers) | |
281 | ||
282 | for (const server of servers) { | |
89d241a7 | 283 | const { data } = await server.videos.list() |
d23dd9fb | 284 | expect(data.find(v => v.uuid === video4UUID)).to.be.undefined |
5abb9fbb C |
285 | } |
286 | }) | |
287 | ||
288 | it('Should have the video unfederated even after an Update AP message', async function () { | |
289 | this.timeout(10000) | |
290 | ||
89d241a7 | 291 | await servers[0].videos.update({ id: video4UUID, attributes: { description: 'super description' } }) |
5abb9fbb C |
292 | |
293 | await waitJobs(servers) | |
294 | ||
295 | for (const server of servers) { | |
89d241a7 | 296 | const { data } = await server.videos.list() |
d23dd9fb | 297 | expect(data.find(v => v.uuid === video4UUID)).to.be.undefined |
5abb9fbb C |
298 | } |
299 | }) | |
300 | ||
301 | it('Should have the correct video blacklist unfederate attribute', async function () { | |
e3d15a6a | 302 | const body = await command.list({ sort: 'createdAt' }) |
5abb9fbb | 303 | |
e3d15a6a | 304 | const blacklistedVideos = body.data |
5abb9fbb C |
305 | const video3Blacklisted = blacklistedVideos.find(b => b.video.uuid === video3UUID) |
306 | const video4Blacklisted = blacklistedVideos.find(b => b.video.uuid === video4UUID) | |
307 | ||
308 | expect(video3Blacklisted.unfederated).to.be.false | |
309 | expect(video4Blacklisted.unfederated).to.be.true | |
310 | }) | |
311 | ||
312 | it('Should remove the video from blacklist and refederate the video', async function () { | |
313 | this.timeout(10000) | |
314 | ||
e3d15a6a | 315 | await command.remove({ videoId: video4UUID }) |
5abb9fbb C |
316 | |
317 | await waitJobs(servers) | |
318 | ||
319 | for (const server of servers) { | |
89d241a7 | 320 | const { data } = await server.videos.list() |
d23dd9fb | 321 | expect(data.find(v => v.uuid === video4UUID)).to.not.be.undefined |
5abb9fbb C |
322 | } |
323 | }) | |
0e1dc3e7 | 324 | |
0e1dc3e7 C |
325 | }) |
326 | ||
1eddc9a7 C |
327 | describe('When auto blacklist videos', function () { |
328 | let userWithoutFlag: string | |
329 | let userWithFlag: string | |
03371ad9 | 330 | let channelOfUserWithoutFlag: number |
1eddc9a7 C |
331 | |
332 | before(async function () { | |
333 | this.timeout(20000) | |
334 | ||
9293139f | 335 | await killallServers([ servers[0] ]) |
1eddc9a7 C |
336 | |
337 | const config = { | |
a1587156 | 338 | auto_blacklist: { |
1eddc9a7 | 339 | videos: { |
a1587156 | 340 | of_users: { |
1eddc9a7 C |
341 | enabled: true |
342 | } | |
343 | } | |
344 | } | |
345 | } | |
254d3579 | 346 | await servers[0].run(config) |
1eddc9a7 C |
347 | |
348 | { | |
349 | const user = { username: 'user_without_flag', password: 'password' } | |
89d241a7 | 350 | await servers[0].users.create({ |
1eddc9a7 C |
351 | username: user.username, |
352 | adminFlags: UserAdminFlag.NONE, | |
353 | password: user.password, | |
354 | role: UserRole.USER | |
355 | }) | |
356 | ||
89d241a7 | 357 | userWithoutFlag = await servers[0].login.getAccessToken(user) |
03371ad9 | 358 | |
89d241a7 | 359 | const { videoChannels } = await servers[0].users.getMyInfo({ token: userWithoutFlag }) |
7926c5f9 | 360 | channelOfUserWithoutFlag = videoChannels[0].id |
1eddc9a7 C |
361 | } |
362 | ||
363 | { | |
364 | const user = { username: 'user_with_flag', password: 'password' } | |
89d241a7 | 365 | await servers[0].users.create({ |
1eddc9a7 | 366 | username: user.username, |
3487330d | 367 | adminFlags: UserAdminFlag.BYPASS_VIDEO_AUTO_BLACKLIST, |
1eddc9a7 C |
368 | password: user.password, |
369 | role: UserRole.USER | |
370 | }) | |
371 | ||
89d241a7 | 372 | userWithFlag = await servers[0].login.getAccessToken(user) |
1eddc9a7 C |
373 | } |
374 | ||
375 | await waitJobs(servers) | |
376 | }) | |
377 | ||
03371ad9 | 378 | it('Should auto blacklist a video on upload', async function () { |
89d241a7 | 379 | await servers[0].videos.upload({ token: userWithoutFlag, attributes: { name: 'blacklisted' } }) |
1eddc9a7 | 380 | |
e3d15a6a C |
381 | const body = await command.list({ type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED }) |
382 | expect(body.total).to.equal(1) | |
383 | expect(body.data[0].video.name).to.equal('blacklisted') | |
1eddc9a7 C |
384 | }) |
385 | ||
03371ad9 | 386 | it('Should auto blacklist a video on URL import', async function () { |
109d893f C |
387 | this.timeout(15000) |
388 | ||
03371ad9 | 389 | const attributes = { |
59bbcced | 390 | targetUrl: FIXTURE_URLS.goodVideo, |
03371ad9 C |
391 | name: 'URL import', |
392 | channelId: channelOfUserWithoutFlag | |
393 | } | |
89d241a7 | 394 | await servers[0].imports.importVideo({ token: userWithoutFlag, attributes }) |
03371ad9 | 395 | |
e3d15a6a C |
396 | const body = await command.list({ sort: 'createdAt', type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED }) |
397 | expect(body.total).to.equal(2) | |
398 | expect(body.data[1].video.name).to.equal('URL import') | |
03371ad9 C |
399 | }) |
400 | ||
401 | it('Should auto blacklist a video on torrent import', async function () { | |
402 | const attributes = { | |
59bbcced | 403 | magnetUri: FIXTURE_URLS.magnet, |
03371ad9 C |
404 | name: 'Torrent import', |
405 | channelId: channelOfUserWithoutFlag | |
406 | } | |
89d241a7 | 407 | await servers[0].imports.importVideo({ token: userWithoutFlag, attributes }) |
03371ad9 | 408 | |
e3d15a6a C |
409 | const body = await command.list({ sort: 'createdAt', type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED }) |
410 | expect(body.total).to.equal(3) | |
411 | expect(body.data[2].video.name).to.equal('Torrent import') | |
03371ad9 C |
412 | }) |
413 | ||
414 | it('Should not auto blacklist a video on upload if the user has the bypass blacklist flag', async function () { | |
89d241a7 | 415 | await servers[0].videos.upload({ token: userWithFlag, attributes: { name: 'not blacklisted' } }) |
1eddc9a7 | 416 | |
e3d15a6a C |
417 | const body = await command.list({ type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED }) |
418 | expect(body.total).to.equal(3) | |
1eddc9a7 C |
419 | }) |
420 | }) | |
421 | ||
7c3b7976 C |
422 | after(async function () { |
423 | await cleanupTests(servers) | |
0e1dc3e7 C |
424 | }) |
425 | }) |