0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-03 23:00:14 -05:00

🐛 Fixed error message for when 2fa email sending fails (#21541)

Fixes
https://linear.app/ghost/issue/ENG-1683/display-useful-error-message-if-2fa-email-send-fails
This commit is contained in:
Princi Vershwal 2024-11-06 17:48:41 +05:30 committed by GitHub
parent 466d7fef3f
commit 1655799aea
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 119 additions and 5 deletions

View file

@ -1,6 +1,7 @@
const {
BadRequestError
} = require('@tryghost/errors');
const errors = require('@tryghost/errors');
const emailTemplate = require('../lib/emails/signin');
const UAParser = require('ua-parser-js');
const got = require('got');
@ -244,11 +245,18 @@ module.exports = function createSessionService({
deviceDetails: await getDeviceDetails(session.user_agent, session.ip)
});
await mailer.send({
to: recipient,
subject: `${token} is your Ghost sign in verification code`,
html: email
});
try {
await mailer.send({
to: recipient,
subject: `${token} is your Ghost sign in verification code`,
html: email
});
} catch (error) {
throw new errors.EmailError({
...error,
message: 'Failed to send email. Please check your site configuration and try again.'
});
}
}
/**

View file

@ -366,4 +366,110 @@ describe('SessionService', function () {
const authCodeSecond = await sessionServiceSecond.generateAuthCodeForUser(req, res);
should.notEqual(authCodeFirst, authCodeSecond);
});
it('sends an email with the auth code', async function () {
const getSession = async (req) => {
if (req.session) {
return req.session;
}
req.session = {
user_id: 'user-123',
ip: '0.0.0.0',
user_agent: 'Fake'
};
return req.session;
};
const findUserById = sinon.stub().resolves({
id: 'user-123',
get: sinon.stub().returns('test@example.com')
});
const mailer = {
send: sinon.stub().resolves()
};
const getSettingsCache = sinon.stub().returns('site-title');
const getBlogLogo = sinon.stub().returns('logo.png');
const urlUtils = {
urlFor: sinon.stub().returns('https://example.com')
};
const t = sinon.stub().callsFake(text => text);
const sessionService = SessionService({
getSession,
findUserById,
getSettingsCache,
getBlogLogo,
urlUtils,
mailer,
t,
labs: {
isSet: () => false
}
});
const req = Object.create(express.request);
const res = Object.create(express.response);
await sessionService.sendAuthCodeToUser(req, res);
should.ok(mailer.send.calledOnce);
const emailArgs = mailer.send.firstCall.args[0];
should.equal(emailArgs.to, 'test@example.com');
emailArgs.subject.should.match(/Ghost sign in verification code/);
});
it('throws an error when mail fails to send', async function () {
const getSession = async (req) => {
if (req.session) {
return req.session;
}
req.session = {
user_id: 'user-123',
ip: '0.0.0.0',
user_agent: 'Fake'
};
return req.session;
};
const findUserById = sinon.stub().resolves({
id: 'user-123',
get: sinon.stub().returns('test@example.com')
});
const mailer = {
send: sinon.stub().rejects(new Error('Mail error'))
};
const getSettingsCache = sinon.stub().returns('site-title');
const getBlogLogo = sinon.stub().returns('logo.png');
const urlUtils = {
urlFor: sinon.stub().returns('https://example.com')
};
const t = sinon.stub().callsFake(text => text);
const sessionService = SessionService({
getSession,
findUserById,
getSettingsCache,
getBlogLogo,
urlUtils,
mailer,
t,
labs: {
isSet: () => false
}
});
const req = Object.create(express.request);
const res = Object.create(express.response);
await should(sessionService.sendAuthCodeToUser(req, res))
.rejectedWith({
message: 'Failed to send email. Please check your site configuration and try again.'
});
});
});