diff --git a/api/lib/domain/services/mail-service.js b/api/lib/domain/services/mail-service.js index f73b3133fe9..566df1cb6db 100644 --- a/api/lib/domain/services/mail-service.js +++ b/api/lib/domain/services/mail-service.js @@ -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'; @@ -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} - */ -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, @@ -430,7 +397,6 @@ function _formatUrlWithLocale(url, locale) { } const mailService = { - sendAccountCreationEmail, sendAccountRecoveryEmail, sendCertificationResultEmail, sendOrganizationInvitationEmail, @@ -445,7 +411,6 @@ const mailService = { /** * @typedef {Object} MailService - * @property {function} sendAccountCreationEmail * @property {function} sendAccountRecoveryEmail * @property {function} sendCertificationCenterInvitationEmail * @property {function} sendCertificationResultEmail @@ -459,7 +424,6 @@ const mailService = { */ export { mailService, - sendAccountCreationEmail, sendAccountRecoveryEmail, sendCertificationCenterInvitationEmail, sendCertificationResultEmail, diff --git a/api/tests/identity-access-management/unit/application/user/user.controller.test.js b/api/tests/identity-access-management/unit/application/user/user.controller.test.js index 6a23f868133..4da69bbc15c 100644 --- a/api/tests/identity-access-management/unit/application/user/user.controller.test.js +++ b/api/tests/identity-access-management/unit/application/user/user.controller.test.js @@ -210,9 +210,6 @@ describe('Unit | Identity Access Management | Application | Controller | User', const cryptoService = { hashPassword: sinon.stub(), }; - const mailService = { - sendAccountCreationEmail: sinon.stub(), - }; const localeService = { getCanonicalLocale: sinon.stub(), }; @@ -220,7 +217,6 @@ describe('Unit | Identity Access Management | Application | Controller | User', dependencies = { userSerializer, cryptoService, - mailService, localeService, requestResponseUtils, }; diff --git a/api/tests/unit/domain/services/mail-service_test.js b/api/tests/unit/domain/services/mail-service_test.js index 19bbd5806c1..a6e57547d82 100644 --- a/api/tests/unit/domain/services/mail-service_test.js +++ b/api/tests/unit/domain/services/mail-service_test.js @@ -3,7 +3,6 @@ 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' }; @@ -11,12 +10,7 @@ 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; @@ -25,203 +19,11 @@ const i18n = getI18n(); describe('Unit | Service | MailService', function () { const senderEmailAddress = 'ne-pas-repondre@pix.fr'; const userEmailAddress = 'user@example.net'; - 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