Skip to content

Commit

Permalink
refactor(api): remove old account creation email
Browse files Browse the repository at this point in the history
  • Loading branch information
bpetetot authored Nov 14, 2024
1 parent 89c499b commit 295efe3
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 239 deletions.
36 changes: 0 additions & 36 deletions api/lib/domain/services/mail-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import dayjs from 'dayjs';
import { config } from '../../../src/shared/config.js';
import { LOCALE } from '../../../src/shared/domain/constants.js';
import { tokenService } from '../../../src/shared/domain/services/token-service.js';
import { urlBuilder } from '../../../src/shared/infrastructure/utils/url-builder.js';
import { getEmailDefaultVariables } from '../../../src/shared/mail/domain/emails-default-variables.js';
import { mailer } from '../../../src/shared/mail/infrastructure/services/mailer.js';
import * as translations from '../../../translations/index.js';
Expand All @@ -28,38 +27,6 @@ const PIX_HOME_URL_INTERNATIONAL = {
};
const PIX_APP_URL_INTERNATIONAL = `${config.domain.pixApp + config.domain.tldOrg}`;

/**
* @param email
* @param locale
* @param redirectionUrl
* @returns {Promise<EmailingAttempt>}
*/
function sendAccountCreationEmail({ email, firstName, locale = FRENCH_FRANCE, token, redirectionUrl, i18n }) {
const mailerConfig = _getMailerConfig(locale);
const redirectUrl = redirectionUrl || mailerConfig.pixAppConnectionUrl;

const templateVariables = {
homeName: mailerConfig.homeName,
homeUrl: mailerConfig.homeUrl,
redirectionUrl: urlBuilder.getEmailValidationUrl({ locale, redirectUrl, token }),
helpdeskUrl: mailerConfig.helpdeskUrl,
displayNationalLogo: mailerConfig.displayNationalLogo,
...mailerConfig.translation['pix-account-creation-email'].params,
title: i18n.__({ phrase: 'pix-account-creation-email.params.title', locale }, { firstName }),
};
const pixName = mailerConfig.translation['email-sender-name']['pix-app'];
const accountCreationEmailSubject = mailerConfig.translation['pix-account-creation-email'].subject;

return mailer.sendEmail({
from: EMAIL_ADDRESS_NO_RESPONSE,
fromName: pixName,
to: email,
subject: accountCreationEmailSubject,
template: mailer.accountCreationTemplateId,
variables: templateVariables,
});
}

function sendCertificationResultEmail({
email,
sessionId,
Expand Down Expand Up @@ -430,7 +397,6 @@ function _formatUrlWithLocale(url, locale) {
}

const mailService = {
sendAccountCreationEmail,
sendAccountRecoveryEmail,
sendCertificationResultEmail,
sendOrganizationInvitationEmail,
Expand All @@ -445,7 +411,6 @@ const mailService = {

/**
* @typedef {Object} MailService
* @property {function} sendAccountCreationEmail
* @property {function} sendAccountRecoveryEmail
* @property {function} sendCertificationCenterInvitationEmail
* @property {function} sendCertificationResultEmail
Expand All @@ -459,7 +424,6 @@ const mailService = {
*/
export {
mailService,
sendAccountCreationEmail,
sendAccountRecoveryEmail,
sendCertificationCenterInvitationEmail,
sendCertificationResultEmail,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,17 +210,13 @@ describe('Unit | Identity Access Management | Application | Controller | User',
const cryptoService = {
hashPassword: sinon.stub(),
};
const mailService = {
sendAccountCreationEmail: sinon.stub(),
};
const localeService = {
getCanonicalLocale: sinon.stub(),
};

dependencies = {
userSerializer,
cryptoService,
mailService,
localeService,
requestResponseUtils,
};
Expand Down
200 changes: 1 addition & 199 deletions api/tests/unit/domain/services/mail-service_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,14 @@ import { config as settings } from '../../../../src/shared/config.js';
import { LOCALE } from '../../../../src/shared/domain/constants.js';
import { tokenService } from '../../../../src/shared/domain/services/token-service.js';
import { getI18n } from '../../../../src/shared/infrastructure/i18n/i18n.js';
import { urlBuilder } from '../../../../src/shared/infrastructure/utils/url-builder.js';
import { mailer } from '../../../../src/shared/mail/infrastructure/services/mailer.js';
import en from '../../../../translations/en.json' with { type: 'json' };
import fr from '../../../../translations/fr.json' with { type: 'json' };
import { es } from '../../../../translations/index.js';
import nl from '../../../../translations/nl.json' with { type: 'json' };
import { expect, sinon } from '../../../test-helper.js';

const mainTranslationsMapping = {
fr,
en,
nl,
es,
};
const mainTranslationsMapping = { fr, en, nl, es };

const { ENGLISH_SPOKEN, FRENCH_FRANCE, FRENCH_SPOKEN, DUTCH_SPOKEN, SPANISH_SPOKEN } = LOCALE;

Expand All @@ -25,203 +19,11 @@ const i18n = getI18n();
describe('Unit | Service | MailService', function () {
const senderEmailAddress = '[email protected]';
const userEmailAddress = '[email protected]';
const userFirstName = 'Bob';

beforeEach(function () {
sinon.stub(mailer, 'sendEmail').resolves();
});

describe('#sendAccountCreationEmail', function () {
it('calls sendEmail with from, to, subject, template', async function () {
// given
const locale = undefined;
const template = 'test-account-creation-template-id';
const token = '00000000-0000-0000-0000-000000000000';
const redirectionUrl = 'https://where-i-should.go';

sinon.stub(urlBuilder, 'getEmailValidationUrl').returns('http://redirect.uri');

const expectedOptions = {
from: senderEmailAddress,
to: userEmailAddress,
subject: 'Votre compte Pix a bien été créé',
template,
};

// when
await mailService.sendAccountCreationEmail({ email: userEmailAddress, locale, token, redirectionUrl, i18n });

// then
const options = mailer.sendEmail.firstCall.args[0];
expect(options).to.include(expectedOptions);
expect(urlBuilder.getEmailValidationUrl).to.have.been.calledWith({
locale: 'fr-fr',
redirectUrl: redirectionUrl,
token,
});
});

context('according to redirectionUrl', function () {
context('if redirectionUrl is provided', function () {
it('calls sendEmail with provided value', async function () {
// given
const token = 'XXX';
const redirectionUrl = 'https://pix.fr';
const locale = FRENCH_FRANCE;
const expectedParams = new URLSearchParams({ token, redirect_url: redirectionUrl });

// when
await mailService.sendAccountCreationEmail({ email: userEmailAddress, locale, token, redirectionUrl, i18n });

// then
const actualRedirectionUrl = mailer.sendEmail.firstCall.args[0].variables.redirectionUrl;
expect(actualRedirectionUrl).to.equal(
`https://app.pix.fr/api/users/validate-email?${expectedParams.toString()}`,
);
});
});
});

context('according to locale', function () {
context('call sendEmail with localized variable options', function () {
it(`calls sendEmail with from, to, and locale ${FRENCH_SPOKEN} or undefined`, async function () {
// given
const locale = FRENCH_SPOKEN;
const expectedParams = new URLSearchParams({ redirect_url: 'https://app.pix.org/connexion/?lang=fr' });

// when
await mailService.sendAccountCreationEmail({
email: userEmailAddress,
firstName: userFirstName,
locale,
i18n,
});

// then
const options = mailer.sendEmail.firstCall.args[0];
expect(options.fromName).to.equal('PIX - Ne pas répondre');
expect(options.variables).to.include({
homeName: 'pix.org',
homeUrl: 'https://pix.org/fr/',
helpdeskUrl: 'https://pix.org/fr/support',
displayNationalLogo: false,
redirectionUrl: `https://app.pix.org/api/users/validate-email?${expectedParams.toString()}`,
...mainTranslationsMapping.fr['pix-account-creation-email'].params,
title: 'Bonjour Bob,',
});
});

it(`calls sendEmail with from, to, template and locale ${FRENCH_FRANCE}`, async function () {
// given
const locale = FRENCH_FRANCE;
const expectedParams = new URLSearchParams({ redirect_url: 'https://app.pix.fr/connexion' });

// when
await mailService.sendAccountCreationEmail({
email: userEmailAddress,
firstName: userFirstName,
locale,
i18n,
});

// then
const options = mailer.sendEmail.firstCall.args[0];
expect(options.fromName).to.equal('PIX - Ne pas répondre');
expect(options.variables).to.include({
homeName: 'pix.fr',
homeUrl: 'https://pix.fr',
helpdeskUrl: 'https://pix.fr/support',
displayNationalLogo: true,
redirectionUrl: `https://app.pix.fr/api/users/validate-email?${expectedParams.toString()}`,
...mainTranslationsMapping.fr['pix-account-creation-email'].params,
title: 'Bonjour Bob,',
});
});

it(`calls sendEmail with from, to, template and locale ${ENGLISH_SPOKEN}`, async function () {
// given
const locale = ENGLISH_SPOKEN;
const expectedParams = new URLSearchParams({ redirect_url: 'https://app.pix.org/connexion/?lang=en' });

// when
await mailService.sendAccountCreationEmail({
email: userEmailAddress,
firstName: userFirstName,
locale,
i18n,
});

// then
const options = mailer.sendEmail.firstCall.args[0];
expect(options.fromName).to.equal('PIX - Noreply');
expect(options.variables).to.include({
homeName: 'pix.org',
homeUrl: 'https://pix.org/en/',
helpdeskUrl: 'https://pix.org/en/support',
displayNationalLogo: false,
redirectionUrl: `https://app.pix.org/api/users/validate-email?${expectedParams.toString()}`,
...mainTranslationsMapping.en['pix-account-creation-email'].params,
title: 'Hello Bob,',
});
});

it(`calls sendEmail with from, to, template and locale ${DUTCH_SPOKEN}`, async function () {
// given
const locale = DUTCH_SPOKEN;
const expectedParams = new URLSearchParams({ redirect_url: 'https://app.pix.org/connexion/?lang=nl' });

// when
await mailService.sendAccountCreationEmail({
email: userEmailAddress,
firstName: userFirstName,
locale,
i18n,
});

// then
const options = mailer.sendEmail.firstCall.args[0];
expect(options.fromName).to.equal('PIX - Niet beantwoorden');
expect(options.variables).to.include({
homeName: 'pix.org',
homeUrl: 'https://pix.org/nl-be/',
helpdeskUrl: 'https://pix.org/nl-be/support',
displayNationalLogo: false,
redirectionUrl: `https://app.pix.org/api/users/validate-email?${expectedParams.toString()}`,
...mainTranslationsMapping.nl['pix-account-creation-email'].params,
title: 'Hallo Bob,',
});
});

it(`calls sendEmail with from, to, template and locale ${SPANISH_SPOKEN}`, async function () {
// given
const locale = SPANISH_SPOKEN;
const expectedParams = new URLSearchParams({ redirect_url: 'https://app.pix.org/connexion/?lang=es' });

// when
await mailService.sendAccountCreationEmail({
email: userEmailAddress,
firstName: userFirstName,
locale,
i18n,
});

// then
const options = mailer.sendEmail.firstCall.args[0];
expect(options.fromName).to.equal('PIX - No contestar');
expect(options.variables).to.include({
homeName: 'pix.org',
homeUrl: 'https://pix.org/en/',
helpdeskUrl: 'https://pix.org/en/support',
displayNationalLogo: false,
redirectionUrl: `https://app.pix.org/api/users/validate-email?${expectedParams.toString()}`,
...mainTranslationsMapping.es['pix-account-creation-email'].params,
title: 'Hola Bob,',
});
});
});
});
});

describe('#sendCertificationResultEmail', function () {
it(`should call sendEmail with from, to, template, tags, ${FRENCH_FRANCE} and ${ENGLISH_SPOKEN} translations`, async function () {
// given
Expand Down

0 comments on commit 295efe3

Please sign in to comment.