EmailChangeHelpersTests.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. const SandboxedModule = require('sandboxed-module')
  2. const sinon = require('sinon')
  3. const { expect } = require('chai')
  4. describe('EmailChangeHelper', function () {
  5. let AnalyticsManager
  6. let UserGetter
  7. let EmailChangeHelpers
  8. const email = 'test@example.com'
  9. const userId = '507f1f77bcf86cd799439011'
  10. beforeEach(function () {
  11. UserGetter = {
  12. promises: {
  13. getUserFullEmails: sinon.stub().resolves([]),
  14. },
  15. }
  16. AnalyticsManager = {
  17. registerEmailChange: sinon.stub(),
  18. }
  19. EmailChangeHelpers = SandboxedModule.require(
  20. '../../../../app/src/Features/Analytics/EmailChangeHelper',
  21. {
  22. requires: {
  23. '../User/UserGetter': UserGetter,
  24. './AnalyticsManager': AnalyticsManager,
  25. },
  26. }
  27. )
  28. })
  29. describe('registerEmailUpdate', function () {
  30. describe('when the email cannot be matched', function () {
  31. beforeEach(function () {
  32. UserGetter.promises.getUserFullEmails.resolves([
  33. {
  34. email: 'test2@example.com',
  35. reversedHostname: 'moc.elpmaxe',
  36. createdAt: new Date('2023-01-01T00:00:00'),
  37. confirmedAt: new Date('2023-02-01T00:00:00'),
  38. default: false,
  39. },
  40. ])
  41. })
  42. it('calls registerEmailChange with the passed event data', async function () {
  43. const eventData = {
  44. emailCreatedAt: new Date('2024-01-01T00:00:00'),
  45. isPrimary: true,
  46. }
  47. await EmailChangeHelpers.registerEmailUpdate(userId, email, eventData)
  48. expect(AnalyticsManager.registerEmailChange).to.have.been.calledOnce
  49. const callArgs = AnalyticsManager.registerEmailChange.getCall(0).args[0]
  50. expect(callArgs).to.include({
  51. userId,
  52. email,
  53. action: 'updated',
  54. isPrimary: true,
  55. })
  56. expect(callArgs.emailCreatedAt).to.eql('2024-01-01T00:00:00.000Z')
  57. expect(callArgs.emailConfirmedAt).to.be.null
  58. })
  59. })
  60. describe('when the email can be matched', function () {
  61. beforeEach(function () {
  62. UserGetter.promises.getUserFullEmails.resolves([
  63. {
  64. email,
  65. reversedHostname: 'moc.elpmaxe',
  66. createdAt: new Date('2023-01-01T00:00:00'),
  67. confirmedAt: new Date('2023-02-01T00:00:00'),
  68. default: false,
  69. },
  70. ])
  71. })
  72. it('calls registerEmailChange with the email data', async function () {
  73. await EmailChangeHelpers.registerEmailUpdate(userId, email)
  74. expect(AnalyticsManager.registerEmailChange).to.have.been.calledOnce
  75. const callArgs = AnalyticsManager.registerEmailChange.getCall(0).args[0]
  76. expect(callArgs).to.include({
  77. userId,
  78. email,
  79. action: 'updated',
  80. isPrimary: false,
  81. })
  82. expect(callArgs.emailCreatedAt).to.eql('2023-01-01T00:00:00.000Z')
  83. expect(callArgs.emailConfirmedAt).to.eql('2023-02-01T00:00:00.000Z')
  84. expect(callArgs.emailDeletedAt).to.be.null
  85. })
  86. it('prefers supplied event data over fetched email data', async function () {
  87. const eventData = {
  88. emailCreatedAt: new Date('2024-01-01T00:00:00'),
  89. emailConfirmedAt: new Date('2024-02-01T00:00:00'),
  90. isPrimary: true,
  91. }
  92. await EmailChangeHelpers.registerEmailUpdate(userId, email, eventData)
  93. expect(AnalyticsManager.registerEmailChange).to.have.been.calledOnce
  94. const callArgs = AnalyticsManager.registerEmailChange.getCall(0).args[0]
  95. expect(callArgs).to.include({
  96. userId,
  97. email,
  98. action: 'updated',
  99. isPrimary: true,
  100. })
  101. expect(callArgs.emailCreatedAt).to.eql('2024-01-01T00:00:00.000Z')
  102. expect(callArgs.emailConfirmedAt).to.eql('2024-02-01T00:00:00.000Z')
  103. expect(callArgs.emailDeletedAt).to.be.null
  104. })
  105. })
  106. describe('when the user is not found', function () {
  107. beforeEach(function () {
  108. UserGetter.promises.getUserFullEmails.rejects(
  109. new Error('User not found')
  110. )
  111. })
  112. it('throws the error', async function () {
  113. await expect(
  114. EmailChangeHelpers.registerEmailUpdate(userId, email)
  115. ).to.eventually.be.rejectedWith('User not found')
  116. })
  117. })
  118. })
  119. describe('registerEmailCreation', function () {
  120. describe('when the email cannot be matched', function () {
  121. beforeEach(function () {
  122. UserGetter.promises.getUserFullEmails.resolves([
  123. {
  124. email: 'test2@example.com',
  125. reversedHostname: 'moc.elpmaxe',
  126. createdAt: new Date('2023-01-01T00:00:00'),
  127. confirmedAt: new Date('2023-02-01T00:00:00'),
  128. default: false,
  129. },
  130. ])
  131. })
  132. it('calls registerEmailChange with the passed event data', async function () {
  133. const eventData = {
  134. emailCreatedAt: new Date('2024-01-01T00:00:00'),
  135. isPrimary: true,
  136. }
  137. await EmailChangeHelpers.registerEmailCreation(userId, email, eventData)
  138. expect(AnalyticsManager.registerEmailChange).to.have.been.calledOnce
  139. const callArgs = AnalyticsManager.registerEmailChange.getCall(0).args[0]
  140. expect(callArgs).to.include({
  141. userId,
  142. email,
  143. action: 'created',
  144. isPrimary: true,
  145. })
  146. expect(callArgs.emailCreatedAt).to.eql('2024-01-01T00:00:00.000Z')
  147. expect(callArgs.emailConfirmedAt).to.be.null
  148. expect(callArgs.emailDeletedAt).to.be.null
  149. })
  150. })
  151. describe('when the email can be matched', function () {
  152. beforeEach(function () {
  153. UserGetter.promises.getUserFullEmails.resolves([
  154. {
  155. email,
  156. reversedHostname: 'moc.elpmaxe',
  157. createdAt: new Date('2023-01-01T00:00:00'),
  158. confirmedAt: new Date('2023-02-01T00:00:00'),
  159. default: false,
  160. },
  161. ])
  162. })
  163. it('calls registerEmailChange with the email data', async function () {
  164. await EmailChangeHelpers.registerEmailCreation(userId, email)
  165. expect(AnalyticsManager.registerEmailChange).to.have.been.calledOnce
  166. const callArgs = AnalyticsManager.registerEmailChange.getCall(0).args[0]
  167. expect(callArgs).to.include({
  168. userId,
  169. email,
  170. action: 'created',
  171. isPrimary: false,
  172. })
  173. expect(callArgs.emailCreatedAt).to.eql('2023-01-01T00:00:00.000Z')
  174. expect(callArgs.emailConfirmedAt).to.eql('2023-02-01T00:00:00.000Z')
  175. expect(callArgs.emailDeletedAt).to.be.null
  176. })
  177. it('prefers supplied event data over fetched email data', async function () {
  178. const eventData = {
  179. emailCreatedAt: new Date('2024-01-01T00:00:00'),
  180. emailConfirmedAt: new Date('2024-02-01T00:00:00'),
  181. isPrimary: true,
  182. }
  183. await EmailChangeHelpers.registerEmailCreation(userId, email, eventData)
  184. expect(AnalyticsManager.registerEmailChange).to.have.been.calledOnce
  185. const callArgs = AnalyticsManager.registerEmailChange.getCall(0).args[0]
  186. expect(callArgs).to.include({
  187. userId,
  188. email,
  189. action: 'created',
  190. isPrimary: true,
  191. })
  192. expect(callArgs.emailCreatedAt).to.eql('2024-01-01T00:00:00.000Z')
  193. expect(callArgs.emailConfirmedAt).to.eql('2024-02-01T00:00:00.000Z')
  194. expect(callArgs.emailDeletedAt).to.be.null
  195. })
  196. })
  197. describe('when the user is not found', function () {
  198. beforeEach(function () {
  199. UserGetter.promises.getUserFullEmails.rejects(
  200. new Error('User not found')
  201. )
  202. })
  203. it('throws the error', async function () {
  204. await expect(
  205. EmailChangeHelpers.registerEmailCreation(userId, email)
  206. ).to.eventually.be.rejectedWith('User not found')
  207. })
  208. })
  209. })
  210. describe('registerEmailDeletion', function () {
  211. describe('when the email cannot be matched', function () {
  212. beforeEach(function () {
  213. UserGetter.promises.getUserFullEmails.resolves([
  214. {
  215. email: 'test2@example.com',
  216. reversedHostname: 'moc.elpmaxe',
  217. createdAt: new Date('2023-01-01T00:00:00'),
  218. confirmedAt: new Date('2023-02-01T00:00:00'),
  219. default: false,
  220. },
  221. ])
  222. })
  223. it('calls registerEmailChange with the passed event data', async function () {
  224. const eventData = {
  225. emailCreatedAt: new Date('2024-01-01T00:00:00'),
  226. emailDeletedAt: new Date('2025-02-01T00:00:00'),
  227. isPrimary: true,
  228. }
  229. await EmailChangeHelpers.registerEmailDeletion(userId, email, eventData)
  230. expect(AnalyticsManager.registerEmailChange).to.have.been.calledOnce
  231. const callArgs = AnalyticsManager.registerEmailChange.getCall(0).args[0]
  232. expect(callArgs).to.include({
  233. userId,
  234. email,
  235. action: 'deleted',
  236. isPrimary: true,
  237. })
  238. expect(callArgs.emailCreatedAt).to.eql('2024-01-01T00:00:00.000Z')
  239. expect(callArgs.emailDeletedAt).to.eql('2025-02-01T00:00:00.000Z')
  240. })
  241. })
  242. describe('when the user is not found', function () {
  243. beforeEach(function () {
  244. UserGetter.promises.getUserFullEmails.rejects(
  245. new Error('User not found')
  246. )
  247. })
  248. it('throws the error', async function () {
  249. await expect(
  250. EmailChangeHelpers.registerEmailDeletion(userId, email)
  251. ).to.eventually.be.rejectedWith('User not found')
  252. })
  253. })
  254. })
  255. })