2
1

video-blacklist.ts 16 KB

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