| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273 |
- import { vi, expect } from 'vitest'
- import sinon from 'sinon'
- describe('EmailChangeHelper', function () {
- let AnalyticsManager
- let UserGetter
- let EmailChangeHelpers
- const email = 'test@example.com'
- const userId = '507f1f77bcf86cd799439011'
- beforeEach(async function () {
- UserGetter = {
- promises: {
- getUserFullEmails: sinon.stub().resolves([]),
- },
- }
- AnalyticsManager = {
- registerEmailChange: sinon.stub(),
- }
- vi.doMock('../../../../app/src/Features/User/UserGetter', () => ({
- default: UserGetter,
- }))
- vi.doMock(
- '../../../../app/src/Features/Analytics/AnalyticsManager',
- () => ({
- default: AnalyticsManager,
- })
- )
- EmailChangeHelpers = (
- await import('../../../../app/src/Features/Analytics/EmailChangeHelper.mjs')
- ).default
- })
- describe('registerEmailUpdate', function () {
- describe('when the email cannot be matched', function () {
- beforeEach(function () {
- UserGetter.promises.getUserFullEmails.resolves([
- {
- email: 'test2@example.com',
- reversedHostname: 'moc.elpmaxe',
- createdAt: new Date('2023-01-01T00:00:00'),
- confirmedAt: new Date('2023-02-01T00:00:00'),
- default: false,
- },
- ])
- })
- it('calls registerEmailChange with the passed event data', async function () {
- const eventData = {
- emailCreatedAt: new Date('2024-01-01T00:00:00'),
- isPrimary: true,
- }
- await EmailChangeHelpers.registerEmailUpdate(userId, email, eventData)
- expect(AnalyticsManager.registerEmailChange).to.have.been.calledOnce
- const callArgs = AnalyticsManager.registerEmailChange.getCall(0).args[0]
- expect(callArgs).to.include({
- userId,
- email,
- action: 'updated',
- isPrimary: true,
- })
- expect(callArgs.emailCreatedAt).to.eql('2024-01-01T00:00:00.000Z')
- expect(callArgs.emailConfirmedAt).to.be.null
- })
- })
- describe('when the email can be matched', function () {
- beforeEach(function () {
- UserGetter.promises.getUserFullEmails.resolves([
- {
- email,
- reversedHostname: 'moc.elpmaxe',
- createdAt: new Date('2023-01-01T00:00:00'),
- confirmedAt: new Date('2023-02-01T00:00:00'),
- default: false,
- },
- ])
- })
- it('calls registerEmailChange with the email data', async function () {
- await EmailChangeHelpers.registerEmailUpdate(userId, email)
- expect(AnalyticsManager.registerEmailChange).to.have.been.calledOnce
- const callArgs = AnalyticsManager.registerEmailChange.getCall(0).args[0]
- expect(callArgs).to.include({
- userId,
- email,
- action: 'updated',
- isPrimary: false,
- })
- expect(callArgs.emailCreatedAt).to.eql('2023-01-01T00:00:00.000Z')
- expect(callArgs.emailConfirmedAt).to.eql('2023-02-01T00:00:00.000Z')
- expect(callArgs.emailDeletedAt).to.be.null
- })
- it('prefers supplied event data over fetched email data', async function () {
- const eventData = {
- emailCreatedAt: new Date('2024-01-01T00:00:00'),
- emailConfirmedAt: new Date('2024-02-01T00:00:00'),
- isPrimary: true,
- }
- await EmailChangeHelpers.registerEmailUpdate(userId, email, eventData)
- expect(AnalyticsManager.registerEmailChange).to.have.been.calledOnce
- const callArgs = AnalyticsManager.registerEmailChange.getCall(0).args[0]
- expect(callArgs).to.include({
- userId,
- email,
- action: 'updated',
- isPrimary: true,
- })
- expect(callArgs.emailCreatedAt).to.eql('2024-01-01T00:00:00.000Z')
- expect(callArgs.emailConfirmedAt).to.eql('2024-02-01T00:00:00.000Z')
- expect(callArgs.emailDeletedAt).to.be.null
- })
- })
- describe('when the user is not found', function () {
- beforeEach(function () {
- UserGetter.promises.getUserFullEmails.rejects(
- new Error('User not found')
- )
- })
- it('throws the error', async function () {
- await expect(
- EmailChangeHelpers.registerEmailUpdate(userId, email)
- ).to.eventually.be.rejectedWith('User not found')
- })
- })
- })
- describe('registerEmailCreation', function () {
- describe('when the email cannot be matched', function () {
- beforeEach(function () {
- UserGetter.promises.getUserFullEmails.resolves([
- {
- email: 'test2@example.com',
- reversedHostname: 'moc.elpmaxe',
- createdAt: new Date('2023-01-01T00:00:00'),
- confirmedAt: new Date('2023-02-01T00:00:00'),
- default: false,
- },
- ])
- })
- it('calls registerEmailChange with the passed event data', async function () {
- const eventData = {
- emailCreatedAt: new Date('2024-01-01T00:00:00'),
- isPrimary: true,
- }
- await EmailChangeHelpers.registerEmailCreation(userId, email, eventData)
- expect(AnalyticsManager.registerEmailChange).to.have.been.calledOnce
- const callArgs = AnalyticsManager.registerEmailChange.getCall(0).args[0]
- expect(callArgs).to.include({
- userId,
- email,
- action: 'created',
- isPrimary: true,
- })
- expect(callArgs.emailCreatedAt).to.eql('2024-01-01T00:00:00.000Z')
- expect(callArgs.emailConfirmedAt).to.be.null
- expect(callArgs.emailDeletedAt).to.be.null
- })
- })
- describe('when the email can be matched', function () {
- beforeEach(function () {
- UserGetter.promises.getUserFullEmails.resolves([
- {
- email,
- reversedHostname: 'moc.elpmaxe',
- createdAt: new Date('2023-01-01T00:00:00'),
- confirmedAt: new Date('2023-02-01T00:00:00'),
- default: false,
- },
- ])
- })
- it('calls registerEmailChange with the email data', async function () {
- await EmailChangeHelpers.registerEmailCreation(userId, email)
- expect(AnalyticsManager.registerEmailChange).to.have.been.calledOnce
- const callArgs = AnalyticsManager.registerEmailChange.getCall(0).args[0]
- expect(callArgs).to.include({
- userId,
- email,
- action: 'created',
- isPrimary: false,
- })
- expect(callArgs.emailCreatedAt).to.eql('2023-01-01T00:00:00.000Z')
- expect(callArgs.emailConfirmedAt).to.eql('2023-02-01T00:00:00.000Z')
- expect(callArgs.emailDeletedAt).to.be.null
- })
- it('prefers supplied event data over fetched email data', async function () {
- const eventData = {
- emailCreatedAt: new Date('2024-01-01T00:00:00'),
- emailConfirmedAt: new Date('2024-02-01T00:00:00'),
- isPrimary: true,
- }
- await EmailChangeHelpers.registerEmailCreation(userId, email, eventData)
- expect(AnalyticsManager.registerEmailChange).to.have.been.calledOnce
- const callArgs = AnalyticsManager.registerEmailChange.getCall(0).args[0]
- expect(callArgs).to.include({
- userId,
- email,
- action: 'created',
- isPrimary: true,
- })
- expect(callArgs.emailCreatedAt).to.eql('2024-01-01T00:00:00.000Z')
- expect(callArgs.emailConfirmedAt).to.eql('2024-02-01T00:00:00.000Z')
- expect(callArgs.emailDeletedAt).to.be.null
- })
- })
- describe('when the user is not found', function () {
- beforeEach(function () {
- UserGetter.promises.getUserFullEmails.rejects(
- new Error('User not found')
- )
- })
- it('throws the error', async function () {
- await expect(
- EmailChangeHelpers.registerEmailCreation(userId, email)
- ).to.eventually.be.rejectedWith('User not found')
- })
- })
- })
- describe('registerEmailDeletion', function () {
- describe('when the email cannot be matched', function () {
- beforeEach(function () {
- UserGetter.promises.getUserFullEmails.resolves([
- {
- email: 'test2@example.com',
- reversedHostname: 'moc.elpmaxe',
- createdAt: new Date('2023-01-01T00:00:00'),
- confirmedAt: new Date('2023-02-01T00:00:00'),
- default: false,
- },
- ])
- })
- it('calls registerEmailChange with the passed event data', async function () {
- const eventData = {
- emailCreatedAt: new Date('2024-01-01T00:00:00'),
- emailDeletedAt: new Date('2025-02-01T00:00:00'),
- isPrimary: true,
- }
- await EmailChangeHelpers.registerEmailDeletion(userId, email, eventData)
- expect(AnalyticsManager.registerEmailChange).to.have.been.calledOnce
- const callArgs = AnalyticsManager.registerEmailChange.getCall(0).args[0]
- expect(callArgs).to.include({
- userId,
- email,
- action: 'deleted',
- isPrimary: true,
- })
- expect(callArgs.emailCreatedAt).to.eql('2024-01-01T00:00:00.000Z')
- expect(callArgs.emailDeletedAt).to.eql('2025-02-01T00:00:00.000Z')
- })
- })
- describe('when the user is not found', function () {
- beforeEach(function () {
- UserGetter.promises.getUserFullEmails.rejects(
- new Error('User not found')
- )
- })
- it('throws the error', async function () {
- await expect(
- EmailChangeHelpers.registerEmailDeletion(userId, email)
- ).to.eventually.be.rejectedWith('User not found')
- })
- })
- })
- })
|