action-hooks.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
  2. import 'mocha'
  3. import { ServerHookName, VideoPlaylistPrivacy, VideoPrivacy } from '@shared/models'
  4. import {
  5. cleanupTests,
  6. createMultipleServers,
  7. killallServers,
  8. PeerTubeServer,
  9. PluginsCommand,
  10. setAccessTokensToServers,
  11. setDefaultVideoChannel
  12. } from '@shared/server-commands'
  13. describe('Test plugin action hooks', function () {
  14. let servers: PeerTubeServer[]
  15. let videoUUID: string
  16. let threadId: number
  17. function checkHook (hook: ServerHookName) {
  18. return servers[0].servers.waitUntilLog('Run hook ' + hook)
  19. }
  20. before(async function () {
  21. this.timeout(30000)
  22. servers = await createMultipleServers(2)
  23. await setAccessTokensToServers(servers)
  24. await setDefaultVideoChannel(servers)
  25. await servers[0].plugins.install({ path: PluginsCommand.getPluginTestPath() })
  26. await killallServers([ servers[0] ])
  27. await servers[0].run({
  28. live: {
  29. enabled: true
  30. }
  31. })
  32. })
  33. describe('Application hooks', function () {
  34. it('Should run action:application.listening', async function () {
  35. await checkHook('action:application.listening')
  36. })
  37. })
  38. describe('Videos hooks', function () {
  39. it('Should run action:api.video.uploaded', async function () {
  40. const { uuid } = await servers[0].videos.upload({ attributes: { name: 'video' } })
  41. videoUUID = uuid
  42. await checkHook('action:api.video.uploaded')
  43. })
  44. it('Should run action:api.video.updated', async function () {
  45. await servers[0].videos.update({ id: videoUUID, attributes: { name: 'video updated' } })
  46. await checkHook('action:api.video.updated')
  47. })
  48. it('Should run action:api.video.viewed', async function () {
  49. await servers[0].views.simulateView({ id: videoUUID })
  50. await checkHook('action:api.video.viewed')
  51. })
  52. })
  53. describe('Live hooks', function () {
  54. it('Should run action:api.live-video.created', async function () {
  55. const attributes = {
  56. name: 'live',
  57. privacy: VideoPrivacy.PUBLIC,
  58. channelId: servers[0].store.channel.id
  59. }
  60. await servers[0].live.create({ fields: attributes })
  61. await checkHook('action:api.live-video.created')
  62. })
  63. })
  64. describe('Comments hooks', function () {
  65. it('Should run action:api.video-thread.created', async function () {
  66. const created = await servers[0].comments.createThread({ videoId: videoUUID, text: 'thread' })
  67. threadId = created.id
  68. await checkHook('action:api.video-thread.created')
  69. })
  70. it('Should run action:api.video-comment-reply.created', async function () {
  71. await servers[0].comments.addReply({ videoId: videoUUID, toCommentId: threadId, text: 'reply' })
  72. await checkHook('action:api.video-comment-reply.created')
  73. })
  74. it('Should run action:api.video-comment.deleted', async function () {
  75. await servers[0].comments.delete({ videoId: videoUUID, commentId: threadId })
  76. await checkHook('action:api.video-comment.deleted')
  77. })
  78. })
  79. describe('Captions hooks', function () {
  80. it('Should run action:api.video-caption.created', async function () {
  81. await servers[0].captions.add({ videoId: videoUUID, language: 'en', fixture: 'subtitle-good.srt' })
  82. await checkHook('action:api.video-caption.created')
  83. })
  84. it('Should run action:api.video-caption.deleted', async function () {
  85. await servers[0].captions.delete({ videoId: videoUUID, language: 'en' })
  86. await checkHook('action:api.video-caption.deleted')
  87. })
  88. })
  89. describe('Users hooks', function () {
  90. let userId: number
  91. it('Should run action:api.user.registered', async function () {
  92. await servers[0].users.register({ username: 'registered_user' })
  93. await checkHook('action:api.user.registered')
  94. })
  95. it('Should run action:api.user.created', async function () {
  96. const user = await servers[0].users.create({ username: 'created_user' })
  97. userId = user.id
  98. await checkHook('action:api.user.created')
  99. })
  100. it('Should run action:api.user.oauth2-got-token', async function () {
  101. await servers[0].login.login({ user: { username: 'created_user' } })
  102. await checkHook('action:api.user.oauth2-got-token')
  103. })
  104. it('Should run action:api.user.blocked', async function () {
  105. await servers[0].users.banUser({ userId })
  106. await checkHook('action:api.user.blocked')
  107. })
  108. it('Should run action:api.user.unblocked', async function () {
  109. await servers[0].users.unbanUser({ userId })
  110. await checkHook('action:api.user.unblocked')
  111. })
  112. it('Should run action:api.user.updated', async function () {
  113. await servers[0].users.update({ userId, videoQuota: 50 })
  114. await checkHook('action:api.user.updated')
  115. })
  116. it('Should run action:api.user.deleted', async function () {
  117. await servers[0].users.remove({ userId })
  118. await checkHook('action:api.user.deleted')
  119. })
  120. })
  121. describe('Playlist hooks', function () {
  122. let playlistId: number
  123. let videoId: number
  124. before(async function () {
  125. {
  126. const { id } = await servers[0].playlists.create({
  127. attributes: {
  128. displayName: 'My playlist',
  129. privacy: VideoPlaylistPrivacy.PRIVATE
  130. }
  131. })
  132. playlistId = id
  133. }
  134. {
  135. const { id } = await servers[0].videos.upload({ attributes: { name: 'my super name' } })
  136. videoId = id
  137. }
  138. })
  139. it('Should run action:api.video-playlist-element.created', async function () {
  140. await servers[0].playlists.addElement({ playlistId, attributes: { videoId } })
  141. await checkHook('action:api.video-playlist-element.created')
  142. })
  143. })
  144. after(async function () {
  145. await cleanupTests(servers)
  146. })
  147. })