diff --git a/services/web/app/src/Features/User/SAMLIdentityManager.js b/services/web/app/src/Features/User/SAMLIdentityManager.js index 60712e7bbb..d8c729cec8 100644 --- a/services/web/app/src/Features/User/SAMLIdentityManager.js +++ b/services/web/app/src/Features/User/SAMLIdentityManager.js @@ -128,14 +128,17 @@ async function _addInstitutionEmail(userId, email, providerId, auditLog) { } } -async function _sendLinkedEmail(userId, providerName) { +async function _sendLinkedEmail(userId, providerName, institutionEmail) { const user = await UserGetter.promises.getUser(userId, { email: 1 }) const emailOptions = { to: user.email, actionDescribed: `an Institutional SSO account at ${providerName} was linked to your account ${ user.email }`, - action: 'institutional SSO account linked' + action: 'institutional SSO account linked', + message: [ + `Linked:
${institutionEmail}
` + ] } EmailHandler.sendEmail('securityAlert', emailOptions, error => { if (error) { @@ -144,11 +147,14 @@ async function _sendLinkedEmail(userId, providerName) { }) } -function _sendUnlinkedEmail(primaryEmail, providerName) { +function _sendUnlinkedEmail(primaryEmail, providerName, institutionEmail) { const emailOptions = { to: primaryEmail, - actionDescribed: `an Institutional SSO account at ${providerName} is no longer linked to your account ${primaryEmail}`, - action: 'institutional SSO account no longer linked' + actionDescribed: `an Institutional SSO account at ${providerName} was unlinked from your account ${primaryEmail}`, + action: 'institutional SSO account no longer linked', + message: [ + `No longer linked:
${institutionEmail}
` + ] } EmailHandler.sendEmail('securityAlert', emailOptions, error => { if (error) { @@ -208,7 +214,7 @@ async function linkAccounts( auditLog ) await _addInstitutionEmail(userId, institutionEmail, providerId, auditLog) - await _sendLinkedEmail(userId, providerName) + await _sendLinkedEmail(userId, providerName, institutionEmail) // update v1 affiliations record if (hasEntitlement) { await InstitutionsAPI.promises.addEntitlement(userId, institutionEmail) @@ -256,7 +262,7 @@ async function unlinkAccounts( // update v1 affiliations record await InstitutionsAPI.promises.removeEntitlement(userId, institutionEmail) // send email - _sendUnlinkedEmail(primaryEmail, providerName) + _sendUnlinkedEmail(primaryEmail, providerName, institutionEmail) } async function updateEntitlement( diff --git a/services/web/test/unit/src/User/SAMLIdentityManagerTests.js b/services/web/test/unit/src/User/SAMLIdentityManagerTests.js index 64c80192d6..ce3757f04e 100644 --- a/services/web/test/unit/src/User/SAMLIdentityManagerTests.js +++ b/services/web/test/unit/src/User/SAMLIdentityManagerTests.js @@ -5,6 +5,8 @@ const SandboxedModule = require('sandboxed-module') const modulePath = '../../../../app/src/Features/User/SAMLIdentityManager.js' describe('SAMLIdentityManager', function() { + const linkedEmail = 'another@example.com' + beforeEach(function() { this.Errors = { EmailExistsError: sinon.stub(), @@ -274,7 +276,13 @@ describe('SAMLIdentityManager', function() { }, () => { expect(this.User.update).to.have.been.called - expect(this.EmailHandler.sendEmail).to.have.been.called + expect(this.EmailHandler.sendEmail).to.have.been.calledOnce + const emailArgs = this.EmailHandler.sendEmail.lastCall.args + expect(emailArgs[0]).to.equal('securityAlert') + expect(emailArgs[1].to).to.equal(this.user.email) + expect(emailArgs[1].actionDescribed).to.contain('was linked') + expect(emailArgs[1].message[0]).to.contain('Linked') + expect(emailArgs[1].message[0]).to.contain(this.user.email) } ) }) @@ -282,7 +290,6 @@ describe('SAMLIdentityManager', function() { }) describe('unlinkAccounts', function() { - const linkedEmail = 'another@example.com' it('should update the audit log', async function() { await this.SAMLIdentityManager.unlinkAccounts( this.user._id, @@ -344,6 +351,9 @@ describe('SAMLIdentityManager', function() { const emailArgs = this.EmailHandler.sendEmail.lastCall.args expect(emailArgs[0]).to.equal('securityAlert') expect(emailArgs[1].to).to.equal(this.user.email) + expect(emailArgs[1].actionDescribed).to.contain('was unlinked') + expect(emailArgs[1].message[0]).to.contain('No longer linked') + expect(emailArgs[1].message[0]).to.contain(linkedEmail) }) describe('errors', function() {