Skip to content

Commit

Permalink
test: add test for reminder email
Browse files Browse the repository at this point in the history
  • Loading branch information
th0rgall committed Mar 27, 2024
1 parent 66dabd6 commit 31fe22e
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 10 deletions.
2 changes: 1 addition & 1 deletion api/seeders/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const { auth, db } = require('./app');

/**
* @param {import('firebase-admin/auth').CreateRequest} authProps - passed to auth.createUser
* @param {import('../../src/lib/api/functions').CreateUserRequest & {superfan?: true}} callableProps - replicating what is passed to the createUser callable (and more)
* @param {import('../../src/lib/api/functions').CreateUserRequest & {superfan?: boolean}} callableProps - replicating what is passed to the createUser callable (and more)
*/
exports.createNewUser = async (authProps, callableProps) => {
const user = await auth.createUser({
Expand Down
2 changes: 1 addition & 1 deletion api/src/subscriptions/scheduled/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@ exports.processUserPrivateDocs = async (userPrivateDocs, processFn, processDescr
})
);

logger.log(`${processDescription} succeeded on: ${this.userPrivateDocIds(results)}`);
logger.log(`${processDescription} succeeded for: ${this.userPrivateDocIds(results)}`);
};
79 changes: 71 additions & 8 deletions api/test/renewalScheduler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const sinon = require('sinon');

const { auth } = require('../seeders/app');
const { createNewUser } = require('../seeders/util');
const mail = require('../src/mail');

const PROJECT_NAME = 'demo-test';

Expand All @@ -32,10 +33,6 @@ async function clearFirestore() {
return fetch(deleteURL, { method: 'DELETE' });
}

const threshold = DateTime.now()
.startOf('hour')
.minus({ days: 5 + 7 });

const testEmail = 'fredtest@email.com';
const testFirstName = 'Fred';
const testLang = 'nl';
Expand All @@ -51,7 +48,6 @@ const createUser = (subscriptionParams) =>
stripeSubscription: {
// should be something
cancelAt: null,
canceledAt: threshold.toSeconds(),
renewalInvoiceLink: 'https://fake',
status: 'canceled',
latestInvoiceStatus: 'void',
Expand All @@ -60,10 +56,14 @@ const createUser = (subscriptionParams) =>
}
);

describe('renewalScheduler', () => {
describe('feedback email', () => {
let fakeEmail;
let sendFeedback;

const threshold = DateTime.now()
.startOf('hour')
.minus({ days: 5 + 7 });

beforeEach(() => {
fakeEmail = sinon.fake();
sendFeedback = proxyquire('../src/subscriptions/scheduled/sendFeedbackEmails.js', {
Expand All @@ -84,7 +84,8 @@ describe('renewalScheduler', () => {
it('sends the feedback email when the account was created in the required window ', async () => {
await createUser({
currentPeriodStart: threshold.minus({ minutes: 10 }).toSeconds(),
startDate: threshold.minus({ year: 1 }).toSeconds()
startDate: threshold.minus({ year: 1 }).toSeconds(),
canceledAt: threshold.toSeconds()
});
await sendFeedback();
sinon.assert.calledOnceWithMatch(fakeEmail, testEmail, testFirstName, testLang);
Expand All @@ -94,9 +95,71 @@ describe('renewalScheduler', () => {
const renewTime = threshold.minus({ hour: 1, minutes: 20 });
await createUser({
currentPeriodStart: renewTime.toSeconds(),
startDate: renewTime.minus({ year: 1 }).toSeconds()
startDate: renewTime.minus({ year: 1 }).toSeconds(),
canceledAt: threshold.toSeconds()
});
await sendFeedback();
assert.strictEqual(fakeEmail.callCount, 0);
});
});

describe('renewal reminder email', () => {
let fakeEmail;
let sendReminderEmail;

const threshold = DateTime.now().startOf('hour').minus({ days: 5 });

beforeEach(() => {
fakeEmail = process.env.REAL_EMAIL
? // wrap the real method
sinon.fake(mail.sendSubscriptionRenewalReminderEmail)
: sinon.fake();
sendReminderEmail = proxyquire('../src/subscriptions/scheduled/sendRenewalReminders.js', {
// redirect auth to auth from the seeder utilities
'../../firebase': { auth },
'../../mail': {
sendSubscriptionRenewalReminderEmail: fakeEmail
},
'firebase-functions': { logger: loggerStub }
});
});

it('sends the right reminder email', async () => {
const docProps = {
countryCode: 'BE',
superfan: true,
firstName: testFirstName,
lastName: 'Test',
communicationLanguage: 'nl',
stripeSubscription: {
status: 'past_due',
latestInvoiceStatus: 'open',
cancelAt: null,
renewalInvoiceLink: 'https://welcometomygarden.org/testsubscriptionlink',
currentPeriodStart: threshold.toSeconds(),
startDate: threshold.minus({ year: 1 }).toSeconds(),
canceledAt: null
}
};

const authProps = {
email: 'thor+test1@slowby.travel',
displayName: testFirstName
};
const authUser = await createNewUser(authProps, docProps);

const docs = [
{
id: authUser.uid,
data: () => ({ ...docProps })
}
];
await sendReminderEmail(docs);
sinon.assert.calledOnceWithMatch(fakeEmail, {
email: authProps.email,
firstName: authProps.displayName,
renewalLink: docProps.stripeSubscription.renewalInvoiceLink,
language: docProps.communicationLanguage
});
});
});

0 comments on commit 31fe22e

Please sign in to comment.