auto-mute.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 { HttpStatusCode } from '@shared/models'
  6. import {
  7. cleanupTests,
  8. createMultipleServers,
  9. doubleFollow,
  10. killallServers,
  11. makeGetRequest,
  12. PeerTubeServer,
  13. setAccessTokensToServers
  14. } from '@shared/server-commands'
  15. import { MockBlocklist } from '../shared'
  16. describe('Official plugin auto-mute', function () {
  17. const autoMuteListPath = '/plugins/auto-mute/router/api/v1/mute-list'
  18. let servers: PeerTubeServer[]
  19. let blocklistServer: MockBlocklist
  20. let port: number
  21. before(async function () {
  22. this.timeout(30000)
  23. servers = await createMultipleServers(2)
  24. await setAccessTokensToServers(servers)
  25. for (const server of servers) {
  26. await server.plugins.install({ npmName: 'peertube-plugin-auto-mute' })
  27. }
  28. blocklistServer = new MockBlocklist()
  29. port = await blocklistServer.initialize()
  30. await servers[0].videos.quickUpload({ name: 'video server 1' })
  31. await servers[1].videos.quickUpload({ name: 'video server 2' })
  32. await doubleFollow(servers[0], servers[1])
  33. })
  34. it('Should update plugin settings', async function () {
  35. await servers[0].plugins.updateSettings({
  36. npmName: 'peertube-plugin-auto-mute',
  37. settings: {
  38. 'blocklist-urls': `http://localhost:${port}/blocklist`,
  39. 'check-seconds-interval': 1
  40. }
  41. })
  42. })
  43. it('Should add a server blocklist', async function () {
  44. this.timeout(10000)
  45. blocklistServer.replace({
  46. data: [
  47. {
  48. value: 'localhost:' + servers[1].port
  49. }
  50. ]
  51. })
  52. await wait(2000)
  53. const { total } = await servers[0].videos.list()
  54. expect(total).to.equal(1)
  55. })
  56. it('Should remove a server blocklist', async function () {
  57. this.timeout(10000)
  58. blocklistServer.replace({
  59. data: [
  60. {
  61. value: 'localhost:' + servers[1].port,
  62. action: 'remove'
  63. }
  64. ]
  65. })
  66. await wait(2000)
  67. const { total } = await servers[0].videos.list()
  68. expect(total).to.equal(2)
  69. })
  70. it('Should add an account blocklist', async function () {
  71. this.timeout(10000)
  72. blocklistServer.replace({
  73. data: [
  74. {
  75. value: 'root@localhost:' + servers[1].port
  76. }
  77. ]
  78. })
  79. await wait(2000)
  80. const { total } = await servers[0].videos.list()
  81. expect(total).to.equal(1)
  82. })
  83. it('Should remove an account blocklist', async function () {
  84. this.timeout(10000)
  85. blocklistServer.replace({
  86. data: [
  87. {
  88. value: 'root@localhost:' + servers[1].port,
  89. action: 'remove'
  90. }
  91. ]
  92. })
  93. await wait(2000)
  94. const { total } = await servers[0].videos.list()
  95. expect(total).to.equal(2)
  96. })
  97. it('Should auto mute an account, manually unmute it and do not remute it automatically', async function () {
  98. this.timeout(20000)
  99. const account = 'root@localhost:' + servers[1].port
  100. blocklistServer.replace({
  101. data: [
  102. {
  103. value: account,
  104. updatedAt: new Date().toISOString()
  105. }
  106. ]
  107. })
  108. await wait(2000)
  109. {
  110. const { total } = await servers[0].videos.list()
  111. expect(total).to.equal(1)
  112. }
  113. await servers[0].blocklist.removeFromServerBlocklist({ account })
  114. {
  115. const { total } = await servers[0].videos.list()
  116. expect(total).to.equal(2)
  117. }
  118. await killallServers([ servers[0] ])
  119. await servers[0].run()
  120. await wait(2000)
  121. {
  122. const { total } = await servers[0].videos.list()
  123. expect(total).to.equal(2)
  124. }
  125. })
  126. it('Should not expose the auto mute list', async function () {
  127. await makeGetRequest({
  128. url: servers[0].url,
  129. path: '/plugins/auto-mute/router/api/v1/mute-list',
  130. expectedStatus: HttpStatusCode.FORBIDDEN_403
  131. })
  132. })
  133. it('Should enable auto mute list', async function () {
  134. await servers[0].plugins.updateSettings({
  135. npmName: 'peertube-plugin-auto-mute',
  136. settings: {
  137. 'blocklist-urls': '',
  138. 'check-seconds-interval': 1,
  139. 'expose-mute-list': true
  140. }
  141. })
  142. await makeGetRequest({
  143. url: servers[0].url,
  144. path: '/plugins/auto-mute/router/api/v1/mute-list',
  145. expectedStatus: HttpStatusCode.OK_200
  146. })
  147. })
  148. it('Should mute an account on server 1, and server 2 auto mutes it', async function () {
  149. this.timeout(20000)
  150. await servers[1].plugins.updateSettings({
  151. npmName: 'peertube-plugin-auto-mute',
  152. settings: {
  153. 'blocklist-urls': 'http://localhost:' + servers[0].port + autoMuteListPath,
  154. 'check-seconds-interval': 1,
  155. 'expose-mute-list': false
  156. }
  157. })
  158. await servers[0].blocklist.addToServerBlocklist({ account: 'root@localhost:' + servers[1].port })
  159. await servers[0].blocklist.addToMyBlocklist({ server: 'localhost:' + servers[1].port })
  160. const res = await makeGetRequest({
  161. url: servers[0].url,
  162. path: '/plugins/auto-mute/router/api/v1/mute-list',
  163. expectedStatus: HttpStatusCode.OK_200
  164. })
  165. const data = res.body.data
  166. expect(data).to.have.lengthOf(1)
  167. expect(data[0].updatedAt).to.exist
  168. expect(data[0].value).to.equal('root@localhost:' + servers[1].port)
  169. await wait(2000)
  170. for (const server of servers) {
  171. const { total } = await server.videos.list()
  172. expect(total).to.equal(1)
  173. }
  174. })
  175. after(async function () {
  176. await blocklistServer.terminate()
  177. await cleanupTests(servers)
  178. })
  179. })