auto-mute.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
  2. import 'mocha'
  3. import { expect } from 'chai'
  4. import {
  5. addAccountToServerBlocklist,
  6. addServerToAccountBlocklist,
  7. removeAccountFromServerBlocklist
  8. } from '@shared/extra-utils/users/blocklist'
  9. import {
  10. doubleFollow,
  11. getVideosList,
  12. installPlugin,
  13. makeGetRequest,
  14. MockBlocklist,
  15. setAccessTokensToServers,
  16. updatePluginSettings,
  17. uploadVideoAndGetId,
  18. wait
  19. } from '../../../shared/extra-utils'
  20. import {
  21. cleanupTests,
  22. flushAndRunMultipleServers,
  23. killallServers,
  24. reRunServer,
  25. ServerInfo
  26. } from '../../../shared/extra-utils/server/servers'
  27. import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
  28. describe('Official plugin auto-mute', function () {
  29. const autoMuteListPath = '/plugins/auto-mute/router/api/v1/mute-list'
  30. let servers: ServerInfo[]
  31. let blocklistServer: MockBlocklist
  32. before(async function () {
  33. this.timeout(30000)
  34. servers = await flushAndRunMultipleServers(2)
  35. await setAccessTokensToServers(servers)
  36. for (const server of servers) {
  37. await installPlugin({
  38. url: server.url,
  39. accessToken: server.accessToken,
  40. npmName: 'peertube-plugin-auto-mute'
  41. })
  42. }
  43. blocklistServer = new MockBlocklist()
  44. await blocklistServer.initialize()
  45. await uploadVideoAndGetId({ server: servers[0], videoName: 'video server 1' })
  46. await uploadVideoAndGetId({ server: servers[1], videoName: 'video server 2' })
  47. await doubleFollow(servers[0], servers[1])
  48. })
  49. it('Should update plugin settings', async function () {
  50. await updatePluginSettings({
  51. url: servers[0].url,
  52. accessToken: servers[0].accessToken,
  53. npmName: 'peertube-plugin-auto-mute',
  54. settings: {
  55. 'blocklist-urls': 'http://localhost:42100/blocklist',
  56. 'check-seconds-interval': 1
  57. }
  58. })
  59. })
  60. it('Should add a server blocklist', async function () {
  61. this.timeout(10000)
  62. blocklistServer.replace({
  63. data: [
  64. {
  65. value: 'localhost:' + servers[1].port
  66. }
  67. ]
  68. })
  69. await wait(2000)
  70. const res = await getVideosList(servers[0].url)
  71. expect(res.body.total).to.equal(1)
  72. })
  73. it('Should remove a server blocklist', async function () {
  74. this.timeout(10000)
  75. blocklistServer.replace({
  76. data: [
  77. {
  78. value: 'localhost:' + servers[1].port,
  79. action: 'remove'
  80. }
  81. ]
  82. })
  83. await wait(2000)
  84. const res = await getVideosList(servers[0].url)
  85. expect(res.body.total).to.equal(2)
  86. })
  87. it('Should add an account blocklist', async function () {
  88. this.timeout(10000)
  89. blocklistServer.replace({
  90. data: [
  91. {
  92. value: 'root@localhost:' + servers[1].port
  93. }
  94. ]
  95. })
  96. await wait(2000)
  97. const res = await getVideosList(servers[0].url)
  98. expect(res.body.total).to.equal(1)
  99. })
  100. it('Should remove an account blocklist', async function () {
  101. this.timeout(10000)
  102. blocklistServer.replace({
  103. data: [
  104. {
  105. value: 'root@localhost:' + servers[1].port,
  106. action: 'remove'
  107. }
  108. ]
  109. })
  110. await wait(2000)
  111. const res = await getVideosList(servers[0].url)
  112. expect(res.body.total).to.equal(2)
  113. })
  114. it('Should auto mute an account, manually unmute it and do not remute it automatically', async function () {
  115. this.timeout(20000)
  116. const account = 'root@localhost:' + servers[1].port
  117. blocklistServer.replace({
  118. data: [
  119. {
  120. value: account,
  121. updatedAt: new Date().toISOString()
  122. }
  123. ]
  124. })
  125. await wait(2000)
  126. {
  127. const res = await getVideosList(servers[0].url)
  128. expect(res.body.total).to.equal(1)
  129. }
  130. await removeAccountFromServerBlocklist(servers[0].url, servers[0].accessToken, account)
  131. {
  132. const res = await getVideosList(servers[0].url)
  133. expect(res.body.total).to.equal(2)
  134. }
  135. killallServers([ servers[0] ])
  136. await reRunServer(servers[0])
  137. await wait(2000)
  138. {
  139. const res = await getVideosList(servers[0].url)
  140. expect(res.body.total).to.equal(2)
  141. }
  142. })
  143. it('Should not expose the auto mute list', async function () {
  144. await makeGetRequest({
  145. url: servers[0].url,
  146. path: '/plugins/auto-mute/router/api/v1/mute-list',
  147. statusCodeExpected: HttpStatusCode.FORBIDDEN_403
  148. })
  149. })
  150. it('Should enable auto mute list', async function () {
  151. await updatePluginSettings({
  152. url: servers[0].url,
  153. accessToken: servers[0].accessToken,
  154. npmName: 'peertube-plugin-auto-mute',
  155. settings: {
  156. 'blocklist-urls': '',
  157. 'check-seconds-interval': 1,
  158. 'expose-mute-list': true
  159. }
  160. })
  161. await makeGetRequest({
  162. url: servers[0].url,
  163. path: '/plugins/auto-mute/router/api/v1/mute-list',
  164. statusCodeExpected: HttpStatusCode.OK_200
  165. })
  166. })
  167. it('Should mute an account on server 1, and server 2 auto mutes it', async function () {
  168. this.timeout(20000)
  169. await updatePluginSettings({
  170. url: servers[1].url,
  171. accessToken: servers[1].accessToken,
  172. npmName: 'peertube-plugin-auto-mute',
  173. settings: {
  174. 'blocklist-urls': 'http://localhost:' + servers[0].port + autoMuteListPath,
  175. 'check-seconds-interval': 1,
  176. 'expose-mute-list': false
  177. }
  178. })
  179. await addAccountToServerBlocklist(servers[0].url, servers[0].accessToken, 'root@localhost:' + servers[1].port)
  180. await addServerToAccountBlocklist(servers[0].url, servers[0].accessToken, 'localhost:' + servers[1].port)
  181. const res = await makeGetRequest({
  182. url: servers[0].url,
  183. path: '/plugins/auto-mute/router/api/v1/mute-list',
  184. statusCodeExpected: HttpStatusCode.OK_200
  185. })
  186. const data = res.body.data
  187. expect(data).to.have.lengthOf(1)
  188. expect(data[0].updatedAt).to.exist
  189. expect(data[0].value).to.equal('root@localhost:' + servers[1].port)
  190. await wait(2000)
  191. for (const server of servers) {
  192. const res = await getVideosList(server.url)
  193. expect(res.body.total).to.equal(1)
  194. }
  195. })
  196. after(async function () {
  197. await blocklistServer.terminate()
  198. await cleanupTests(servers)
  199. })
  200. })