video-blacklist.ts 14 KB

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