Skip to content

Commit a652136

Browse files
authored
Merge pull request #19914 from mozilla/chore/fxa-mailer-mock
chore(auth-server): Add shared fxaMailer mock
2 parents d1da571 + 687d11f commit a652136

2 files changed

Lines changed: 38 additions & 20 deletions

File tree

packages/fxa-auth-server/test/local/routes/password.js

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,9 @@ const log = require('../../../lib/log');
1717
const random = require('../../../lib/crypto/random');
1818
const glean = mocks.mockGlean();
1919
const Container = require('typedi').Container;
20-
const { FxaMailer } = require('../../../lib/senders/fxa-mailer');
2120

2221
const TEST_EMAIL = '[email protected]';
2322

24-
function setupFxaMailerMock() {
25-
const mockFxaMailer = {
26-
sendRecoveryEmail: sinon.stub().resolves(),
27-
sendPasswordForgotOtpEmail: sinon.stub().resolves(),
28-
splitEmails: sinon.stub().returns({ to: TEST_EMAIL, cc: [] }),
29-
helpers: {
30-
constructLocalTimeString: sinon.stub().returns({
31-
timeNow: '12:00:00 PM (UTC)',
32-
dateNow: 'Monday, Jan 1, 2024',
33-
}),
34-
},
35-
};
36-
37-
Container.set(FxaMailer, mockFxaMailer);
38-
39-
return mockFxaMailer;
40-
}
41-
4223
function makeRoutes(options = {}) {
4324
const config = options.config || {
4425
verifierVersion: 0,
@@ -92,7 +73,7 @@ describe('/password', () => {
9273
// mailer mock must be done before route creation/require
9374
// otherwise it won't pickup the mock we define because
9475
// of module caching
95-
mockFxaMailer = setupFxaMailerMock();
76+
mockFxaMailer = mocks.mockFxaMailer();
9677
mockAccountEventsManager = mocks.mockAccountEventsManager();
9778
glean.resetPassword.emailSent.reset();
9879
});

packages/fxa-auth-server/test/mocks.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const { AccountEventsManager } = require('../lib/account-events');
2020
const { gleanMetrics } = require('../lib/metrics/glean');
2121
const { PriceManager } = require('@fxa/payments/customer');
2222
const { ProductConfigurationManager } = require('@fxa/shared/cms');
23+
const { FxaMailer } = require('../lib/senders/fxa-mailer');
2324

2425
const proxyquire = require('proxyquire');
2526
const amplitudeModule = proxyquire('../lib/metrics/amplitude', {
@@ -355,6 +356,7 @@ module.exports = {
355356
unMockAccountEventsManager,
356357
mockPriceManager,
357358
mockProductConfigurationManager,
359+
mockFxaMailer,
358360
};
359361

360362
function mockCustoms(errors) {
@@ -1125,3 +1127,38 @@ function mockProductConfigurationManager() {
11251127
Container.set(ProductConfigurationManager, productConfigurationManager);
11261128
return productConfigurationManager;
11271129
}
1130+
1131+
/**
1132+
* Used to mock the FxaMailer, injecting the mock into the Container. Be sure
1133+
* to call this before the code under test requests an FxaMailer instance from
1134+
* the Container.
1135+
*
1136+
* `canSend` is defaulted to a stub that resolves to `false`, so email
1137+
* sending is disabled by default in tests. Call mock setup with an override to enable
1138+
* sending for specific tests.
1139+
* ```
1140+
* const mockFxaMailer = mocks.mockFxaMailer({ canSend: sinon.stub().resolves(true) });
1141+
* // or, if you don't need to spy on the method:
1142+
* const mockFxaMailer = mocks.mockFxaMailer({ canSend: true });
1143+
* ```
1144+
* @param {*} overrides
1145+
* @returns {object} The mock FxaMailer instance for spy and assertion use.
1146+
* @usage
1147+
*
1148+
* ``` ts
1149+
* const mockFxaMailer = mocks.mockFxaMailer();
1150+
* // arrange, act, assert
1151+
* sinon.assert.calledOnce(mockFxaMailer.sendRecoveryEmail);
1152+
* ```
1153+
*/
1154+
function mockFxaMailer(overrides) {
1155+
const mockFxaMailer = {
1156+
// add new email methods here!
1157+
canSend: sinon.stub().resolves(false),
1158+
sendRecoveryEmail: sinon.stub().resolves(),
1159+
sendPasswordForgotOtpEmail: sinon.stub().resolves(),
1160+
...overrides,
1161+
};
1162+
Container.set(FxaMailer, mockFxaMailer);
1163+
return mockFxaMailer;
1164+
}

0 commit comments

Comments
 (0)