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