auto-block-videos.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
  2. import 'mocha'
  3. import { expect } from 'chai'
  4. import { wait } from '@shared/core-utils'
  5. import { Video } from '@shared/models'
  6. import {
  7. cleanupTests,
  8. createMultipleServers,
  9. doubleFollow,
  10. killallServers,
  11. PeerTubeServer,
  12. setAccessTokensToServers
  13. } from '@shared/server-commands'
  14. import { MockBlocklist } from '../shared'
  15. async function check (server: PeerTubeServer, videoUUID: string, exists = true) {
  16. const { data } = await server.videos.list()
  17. const video = data.find(v => v.uuid === videoUUID)
  18. if (exists) expect(video).to.not.be.undefined
  19. else expect(video).to.be.undefined
  20. }
  21. describe('Official plugin auto-block videos', function () {
  22. let servers: PeerTubeServer[]
  23. let blocklistServer: MockBlocklist
  24. let server1Videos: Video[] = []
  25. let server2Videos: Video[] = []
  26. let port: number
  27. before(async function () {
  28. this.timeout(60000)
  29. servers = await createMultipleServers(2)
  30. await setAccessTokensToServers(servers)
  31. for (const server of servers) {
  32. await server.plugins.install({ npmName: 'peertube-plugin-auto-block-videos' })
  33. }
  34. blocklistServer = new MockBlocklist()
  35. port = await blocklistServer.initialize()
  36. await servers[0].videos.quickUpload({ name: 'video server 1' })
  37. await servers[1].videos.quickUpload({ name: 'video server 2' })
  38. await servers[1].videos.quickUpload({ name: 'video 2 server 2' })
  39. await servers[1].videos.quickUpload({ name: 'video 3 server 2' })
  40. {
  41. const { data } = await servers[0].videos.list()
  42. server1Videos = data.map(v => Object.assign(v, { url: servers[0].url + '/videos/watch/' + v.uuid }))
  43. }
  44. {
  45. const { data } = await servers[1].videos.list()
  46. server2Videos = data.map(v => Object.assign(v, { url: servers[1].url + '/videos/watch/' + v.uuid }))
  47. }
  48. await doubleFollow(servers[0], servers[1])
  49. })
  50. it('Should update plugin settings', async function () {
  51. await servers[0].plugins.updateSettings({
  52. npmName: 'peertube-plugin-auto-block-videos',
  53. settings: {
  54. 'blocklist-urls': `http://localhost:${port}/blocklist`,
  55. 'check-seconds-interval': 1
  56. }
  57. })
  58. })
  59. it('Should auto block a video', async function () {
  60. this.timeout(10000)
  61. await check(servers[0], server2Videos[0].uuid, true)
  62. blocklistServer.replace({
  63. data: [
  64. {
  65. value: server2Videos[0].url
  66. }
  67. ]
  68. })
  69. await wait(2000)
  70. await check(servers[0], server2Videos[0].uuid, false)
  71. })
  72. it('Should have video in blacklists', async function () {
  73. const body = await servers[0].blacklist.list()
  74. const videoBlacklists = body.data
  75. expect(videoBlacklists).to.have.lengthOf(1)
  76. expect(videoBlacklists[0].reason).to.contains('Automatically blocked from auto block plugin')
  77. expect(videoBlacklists[0].video.name).to.equal(server2Videos[0].name)
  78. })
  79. it('Should not block a local video', async function () {
  80. this.timeout(10000)
  81. await check(servers[0], server1Videos[0].uuid, true)
  82. blocklistServer.replace({
  83. data: [
  84. {
  85. value: server1Videos[0].url
  86. }
  87. ]
  88. })
  89. await wait(2000)
  90. await check(servers[0], server1Videos[0].uuid, true)
  91. })
  92. it('Should remove a video block', async function () {
  93. this.timeout(10000)
  94. await check(servers[0], server2Videos[0].uuid, false)
  95. blocklistServer.replace({
  96. data: [
  97. {
  98. value: server2Videos[0].url,
  99. action: 'remove'
  100. }
  101. ]
  102. })
  103. await wait(2000)
  104. await check(servers[0], server2Videos[0].uuid, true)
  105. })
  106. it('Should auto block a video, manually unblock it and do not reblock it automatically', async function () {
  107. this.timeout(20000)
  108. const video = server2Videos[1]
  109. await check(servers[0], video.uuid, true)
  110. blocklistServer.replace({
  111. data: [
  112. {
  113. value: video.url,
  114. updatedAt: new Date().toISOString()
  115. }
  116. ]
  117. })
  118. await wait(2000)
  119. await check(servers[0], video.uuid, false)
  120. await servers[0].blacklist.remove({ videoId: video.uuid })
  121. await check(servers[0], video.uuid, true)
  122. await killallServers([ servers[0] ])
  123. await servers[0].run()
  124. await wait(2000)
  125. await check(servers[0], video.uuid, true)
  126. })
  127. after(async function () {
  128. await blocklistServer.terminate()
  129. await cleanupTests(servers)
  130. })
  131. })