Skip to content

Commit

Permalink
refactor(api): add template verification in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mariannebost committed May 14, 2024
1 parent 313e337 commit bcbbff7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 29 deletions.
2 changes: 2 additions & 0 deletions api/src/shared/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,8 @@ const configuration = (function () {
config.mailing.brevo.templates.organizationInvitationTemplateId = 'test-organization-invitation-demand-template-id';
config.mailing.brevo.templates.organizationInvitationScoTemplateId =
'test-organization-invitation-sco-demand-template-id';
config.mailing.brevo.templates.certificationCenterInvitationTemplateId =
'test-certification-center-invitation-template-id';
config.mailing.brevo.templates.certificationResultTemplateId = 'test-certification-result-template-id';
config.mailing.brevo.templates.passwordResetTemplateId = 'test-password-reset-template-id';
config.mailing.brevo.templates.emailChangeTemplateId = 'test-email-change-template-id';
Expand Down
55 changes: 26 additions & 29 deletions api/tests/unit/domain/services/mail-service_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,14 @@ describe('Unit | Service | MailService', function () {
it('calls sendEmail with from, to, subject, template', async function () {
// given
const locale = undefined;
const template = 'test-account-creation-template-id';

// given
const expectedOptions = {
from: senderEmailAddress,
to: userEmailAddress,
subject: 'Votre compte Pix a bien été créé',
template: 'test-account-creation-template-id',
template,
};

// when
Expand Down Expand Up @@ -67,7 +68,7 @@ describe('Unit | Service | MailService', function () {

context('according to locale', function () {
context('call sendEmail with localized variable options', function () {
it(`calls sendEmail with from, to, template and locale ${FRENCH_SPOKEN} or undefined`, async function () {
it(`calls sendEmail with from, to, and locale ${FRENCH_SPOKEN} or undefined`, async function () {
// given
const locale = FRENCH_SPOKEN;

Expand Down Expand Up @@ -416,9 +417,10 @@ describe('Unit | Service | MailService', function () {
const organizationInvitationId = 1;
const code = 'ABCDEFGH01';

it('should call sendEmail with from, to, organizationName', async function () {
it('should call sendEmail with from, to, template, organizationName', async function () {
// given
const locale = undefined;
const template = 'test-organization-invitation-demand-template-id';

// when
await mailService.sendOrganizationInvitationEmail({
Expand All @@ -433,15 +435,12 @@ describe('Unit | Service | MailService', function () {
const expectedOptions = {
from: senderEmailAddress,
to: userEmailAddress,
variables: {
organizationName,
},
template,
};
const options = mailer.sendEmail.firstCall.args[0];

expect(options.from).to.equal(expectedOptions.from);
expect(options.to).to.equal(expectedOptions.to);
expect(options.variables.organizationName).to.equal(expectedOptions.variables.organizationName);
expect(options).to.includes(expectedOptions);
expect(options.variables.organizationName).to.equal(organizationName);
});

context('according to tags', function () {
Expand Down Expand Up @@ -585,34 +584,26 @@ describe('Unit | Service | MailService', function () {
});

describe('#sendScoOrganizationInvitationEmail', function () {
const fromName = 'Pix Orga - Ne pas répondre';

const subject = 'Accès à votre espace Pix Orga';
const template = 'test-organization-invitation-sco-demand-template-id';

const organizationName = 'Organization SCO';
const firstName = 'FirstName';
const lastName = 'LastName';
const pixHomeName = 'pix.fr';
const pixHomeUrl = 'https://pix.fr';
const pixOrgaUrl = 'https://orga.pix.fr';
const organizationInvitationId = 1;
const code = 'ABCDEFGH01';

it('should call mail provider with pix-orga url, organization-invitation id, code and null tags', async function () {
it('should call mail provider with pix-orga url, organization-invitation id, code, template and null tags', async function () {
// given
const organizationName = 'Organization SCO';
const firstName = 'FirstName';
const lastName = 'LastName';
const organizationInvitationId = 1;
const code = 'ABCDEFGH01';
const pixOrgaUrl = 'https://orga.pix.fr';
const expectedOptions = {
from: senderEmailAddress,
fromName,
fromName: 'Pix Orga - Ne pas répondre',
to: userEmailAddress,
subject,
template,
subject: 'Accès à votre espace Pix Orga',
template: 'test-organization-invitation-sco-demand-template-id',
variables: {
organizationName,
firstName,
lastName,
pixHomeName,
pixHomeUrl,
pixHomeName: 'pix.fr',
pixHomeUrl: 'https://pix.fr',
pixOrgaHomeUrl: pixOrgaUrl,
locale: FRENCH_FRANCE,
redirectionUrl: `${pixOrgaUrl}/rejoindre?invitationId=${organizationInvitationId}&code=${code}`,
Expand Down Expand Up @@ -655,6 +646,7 @@ describe('Unit | Service | MailService', function () {
expect(sendEmailParameters.from).to.equal(senderEmailAddress);
expect(sendEmailParameters.fromName).to.equal('Pix Certif - Ne pas répondre');
expect(sendEmailParameters.to).to.equal('invited@example.net');
expect(sendEmailParameters.template).to.equal('test-certification-center-invitation-template-id');
expect(sendEmailParameters.variables).to.include({
certificationCenterName: 'Centre Pixou',
pixHomeName: 'pix.fr',
Expand Down Expand Up @@ -786,6 +778,7 @@ describe('Unit | Service | MailService', function () {
const options = mailer.sendEmail.firstCall.args[0];
expect(options.subject).to.equal(translate('verification-code-email.subject', { code }));
expect(options.fromName).to.equal('PIX - Ne pas répondre');
expect(options.template).to.equal('test-email-verification-code-template-id');
expect(options.variables).to.include({
homeName: 'pix.org',
homeUrl: 'https://pix.org/fr/',
Expand Down Expand Up @@ -813,6 +806,7 @@ describe('Unit | Service | MailService', function () {
const options = mailer.sendEmail.firstCall.args[0];
expect(options.subject).to.equal(translate('verification-code-email.subject', { code }));
expect(options.fromName).to.equal('PIX - Ne pas répondre');
expect(options.template).to.equal('test-email-verification-code-template-id');
expect(options.variables).to.include({
homeName: 'pix.fr',
homeUrl: 'https://pix.fr',
Expand Down Expand Up @@ -842,6 +836,7 @@ describe('Unit | Service | MailService', function () {
translate({ phrase: 'verification-code-email.subject', locale: 'en' }, { code }),
);
expect(options.fromName).to.equal('PIX - Noreply');
expect(options.template).to.equal('test-email-verification-code-template-id');
expect(options.variables).to.include({
homeName: 'pix.org',
homeUrl: 'https://pix.org/en-gb/',
Expand Down Expand Up @@ -871,6 +866,7 @@ describe('Unit | Service | MailService', function () {
translate({ phrase: 'verification-code-email.subject', locale: 'nl' }, { code }),
);
expect(options.fromName).to.equal('PIX - Niet beantwoorden');
expect(options.template).to.equal('test-email-verification-code-template-id');
expect(options.variables).to.include({
homeName: 'pix.org',
homeUrl: 'https://pix.org/nl-be/',
Expand Down Expand Up @@ -900,6 +896,7 @@ describe('Unit | Service | MailService', function () {
translate({ phrase: 'verification-code-email.subject', locale: 'es' }, { code }),
);
expect(options.fromName).to.equal('PIX - No contestar');
expect(options.template).to.equal('test-email-verification-code-template-id');
expect(options.variables).to.include({
homeName: 'pix.org',
homeUrl: 'https://pix.org/en-gb/',
Expand Down

0 comments on commit bcbbff7

Please sign in to comment.