email.ts 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /* tslint:disable:no-unused-expression */
  2. import * as chai from 'chai'
  3. import 'mocha'
  4. import {
  5. addVideoToBlacklist,
  6. askResetPassword,
  7. askSendVerifyEmail,
  8. blockUser,
  9. cleanupTests,
  10. createUser,
  11. flushAndRunServer,
  12. removeVideoFromBlacklist,
  13. reportVideoAbuse,
  14. resetPassword,
  15. ServerInfo,
  16. setAccessTokensToServers,
  17. unblockUser,
  18. uploadVideo,
  19. userLogin,
  20. verifyEmail
  21. } from '../../../../shared/extra-utils'
  22. import { MockSmtpServer } from '../../../../shared/extra-utils/miscs/email'
  23. import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
  24. const expect = chai.expect
  25. describe('Test emails', function () {
  26. let server: ServerInfo
  27. let userId: number
  28. let userAccessToken: string
  29. let videoUUID: string
  30. let videoUserUUID: string
  31. let verificationString: string
  32. const emails: object[] = []
  33. const user = {
  34. username: 'user_1',
  35. password: 'super_password'
  36. }
  37. let emailPort: number
  38. before(async function () {
  39. this.timeout(50000)
  40. emailPort = await MockSmtpServer.Instance.collectEmails(emails)
  41. const overrideConfig = {
  42. smtp: {
  43. hostname: 'localhost',
  44. port: emailPort
  45. }
  46. }
  47. server = await flushAndRunServer(1, overrideConfig)
  48. await setAccessTokensToServers([ server ])
  49. {
  50. const res = await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
  51. userId = res.body.user.id
  52. userAccessToken = await userLogin(server, user)
  53. }
  54. {
  55. const attributes = {
  56. name: 'my super user video'
  57. }
  58. const res = await uploadVideo(server.url, userAccessToken, attributes)
  59. videoUserUUID = res.body.video.uuid
  60. }
  61. {
  62. const attributes = {
  63. name: 'my super name'
  64. }
  65. const res = await uploadVideo(server.url, server.accessToken, attributes)
  66. videoUUID = res.body.video.uuid
  67. }
  68. })
  69. describe('When resetting user password', function () {
  70. it('Should ask to reset the password', async function () {
  71. this.timeout(10000)
  72. await askResetPassword(server.url, 'user_1@example.com')
  73. await waitJobs(server)
  74. expect(emails).to.have.lengthOf(1)
  75. const email = emails[0]
  76. expect(email['from'][0]['name']).equal('localhost:' + server.port)
  77. expect(email['from'][0]['address']).equal('test-admin@localhost')
  78. expect(email['to'][0]['address']).equal('user_1@example.com')
  79. expect(email['subject']).contains('password')
  80. const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text'])
  81. expect(verificationStringMatches).not.to.be.null
  82. verificationString = verificationStringMatches[1]
  83. expect(verificationString).to.have.length.above(2)
  84. const userIdMatches = /userId=([0-9]+)/.exec(email['text'])
  85. expect(userIdMatches).not.to.be.null
  86. userId = parseInt(userIdMatches[1], 10)
  87. expect(verificationString).to.not.be.undefined
  88. })
  89. it('Should not reset the password with an invalid verification string', async function () {
  90. await resetPassword(server.url, userId, verificationString + 'b', 'super_password2', 403)
  91. })
  92. it('Should reset the password', async function () {
  93. await resetPassword(server.url, userId, verificationString, 'super_password2')
  94. })
  95. it('Should login with this new password', async function () {
  96. user.password = 'super_password2'
  97. await userLogin(server, user)
  98. })
  99. })
  100. describe('When creating a video abuse', function () {
  101. it('Should send the notification email', async function () {
  102. this.timeout(10000)
  103. const reason = 'my super bad reason'
  104. await reportVideoAbuse(server.url, server.accessToken, videoUUID, reason)
  105. await waitJobs(server)
  106. expect(emails).to.have.lengthOf(2)
  107. const email = emails[1]
  108. expect(email['from'][0]['name']).equal('localhost:' + server.port)
  109. expect(email['from'][0]['address']).equal('test-admin@localhost')
  110. expect(email['to'][0]['address']).equal('admin' + server.internalServerNumber + '@example.com')
  111. expect(email['subject']).contains('abuse')
  112. expect(email['text']).contains(videoUUID)
  113. })
  114. })
  115. describe('When blocking/unblocking user', function () {
  116. it('Should send the notification email when blocking a user', async function () {
  117. this.timeout(10000)
  118. const reason = 'my super bad reason'
  119. await blockUser(server.url, userId, server.accessToken, 204, reason)
  120. await waitJobs(server)
  121. expect(emails).to.have.lengthOf(3)
  122. const email = emails[2]
  123. expect(email['from'][0]['name']).equal('localhost:' + server.port)
  124. expect(email['from'][0]['address']).equal('test-admin@localhost')
  125. expect(email['to'][0]['address']).equal('user_1@example.com')
  126. expect(email['subject']).contains(' blocked')
  127. expect(email['text']).contains(' blocked')
  128. expect(email['text']).contains(reason)
  129. })
  130. it('Should send the notification email when unblocking a user', async function () {
  131. this.timeout(10000)
  132. await unblockUser(server.url, userId, server.accessToken, 204)
  133. await waitJobs(server)
  134. expect(emails).to.have.lengthOf(4)
  135. const email = emails[3]
  136. expect(email['from'][0]['name']).equal('localhost:' + server.port)
  137. expect(email['from'][0]['address']).equal('test-admin@localhost')
  138. expect(email['to'][0]['address']).equal('user_1@example.com')
  139. expect(email['subject']).contains(' unblocked')
  140. expect(email['text']).contains(' unblocked')
  141. })
  142. })
  143. describe('When blacklisting a video', function () {
  144. it('Should send the notification email', async function () {
  145. this.timeout(10000)
  146. const reason = 'my super reason'
  147. await addVideoToBlacklist(server.url, server.accessToken, videoUserUUID, reason)
  148. await waitJobs(server)
  149. expect(emails).to.have.lengthOf(5)
  150. const email = emails[4]
  151. expect(email['from'][0]['name']).equal('localhost:' + server.port)
  152. expect(email['from'][0]['address']).equal('test-admin@localhost')
  153. expect(email['to'][0]['address']).equal('user_1@example.com')
  154. expect(email['subject']).contains(' blacklisted')
  155. expect(email['text']).contains('my super user video')
  156. expect(email['text']).contains('my super reason')
  157. })
  158. it('Should send the notification email', async function () {
  159. this.timeout(10000)
  160. await removeVideoFromBlacklist(server.url, server.accessToken, videoUserUUID)
  161. await waitJobs(server)
  162. expect(emails).to.have.lengthOf(6)
  163. const email = emails[5]
  164. expect(email['from'][0]['name']).equal('localhost:' + server.port)
  165. expect(email['from'][0]['address']).equal('test-admin@localhost')
  166. expect(email['to'][0]['address']).equal('user_1@example.com')
  167. expect(email['subject']).contains(' unblacklisted')
  168. expect(email['text']).contains('my super user video')
  169. })
  170. })
  171. describe('When verifying a user email', function () {
  172. it('Should ask to send the verification email', async function () {
  173. this.timeout(10000)
  174. await askSendVerifyEmail(server.url, 'user_1@example.com')
  175. await waitJobs(server)
  176. expect(emails).to.have.lengthOf(7)
  177. const email = emails[6]
  178. expect(email['from'][0]['name']).equal('localhost:' + server.port)
  179. expect(email['from'][0]['address']).equal('test-admin@localhost')
  180. expect(email['to'][0]['address']).equal('user_1@example.com')
  181. expect(email['subject']).contains('Verify')
  182. const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text'])
  183. expect(verificationStringMatches).not.to.be.null
  184. verificationString = verificationStringMatches[1]
  185. expect(verificationString).to.not.be.undefined
  186. expect(verificationString).to.have.length.above(2)
  187. const userIdMatches = /userId=([0-9]+)/.exec(email['text'])
  188. expect(userIdMatches).not.to.be.null
  189. userId = parseInt(userIdMatches[1], 10)
  190. })
  191. it('Should not verify the email with an invalid verification string', async function () {
  192. await verifyEmail(server.url, userId, verificationString + 'b', false, 403)
  193. })
  194. it('Should verify the email', async function () {
  195. await verifyEmail(server.url, userId, verificationString)
  196. })
  197. })
  198. after(async function () {
  199. MockSmtpServer.Instance.kill()
  200. await cleanupTests([ server ])
  201. })
  202. })