From c38fe6e9caee200f5e50b1de605069d706f9a454 Mon Sep 17 00:00:00 2001 From: Zach Date: Sat, 16 Mar 2024 11:11:18 -0500 Subject: [PATCH] fix 1774: changed account manager error checking, changed loadUser function to properly render html, updated unit test --- lib/models/account-manager.js | 2 +- lib/requests/password-reset-email-request.js | 10 +++++++++- .../unit/password-reset-email-request-test.js | 19 +++++++++++++++---- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/lib/models/account-manager.js b/lib/models/account-manager.js index c41cb1ade..f1d4f8152 100644 --- a/lib/models/account-manager.js +++ b/lib/models/account-manager.js @@ -537,7 +537,7 @@ class AccountManager { throw new Error('Email service is not set up') } - if (!userAccount.email) { + if (userAccount && !userAccount.email) { throw new Error('Account recovery email has not been provided') } } diff --git a/lib/requests/password-reset-email-request.js b/lib/requests/password-reset-email-request.js index a4e40c33a..91cf1fcdd 100644 --- a/lib/requests/password-reset-email-request.js +++ b/lib/requests/password-reset-email-request.js @@ -123,7 +123,15 @@ class PasswordResetEmailRequest extends AuthRequest { return this.accountManager.accountExists(username) .then(exists => { if (!exists) { - throw new Error('Account not found for that username') + try { + const userAccount = this.accountManager.userAccountFrom({ username }) + this.accountManager.verifyEmailDependencies(userAccount) + } catch (err) { + console.log(err.message) + if (err.message === 'Account recovery email has not been provided') { + return this.renderSuccess() + } + } } const userData = { username } diff --git a/test/unit/password-reset-email-request-test.js b/test/unit/password-reset-email-request-test.js index 2861a0fe2..81e85bcbf 100644 --- a/test/unit/password-reset-email-request-test.js +++ b/test/unit/password-reset-email-request-test.js @@ -14,6 +14,7 @@ const HttpMocks = require('node-mocks-http') const PasswordResetEmailRequest = require('../../lib/requests/password-reset-email-request') const AccountManager = require('../../lib/models/account-manager') const SolidHost = require('../../lib/models/solid-host') +const EmailService = require('../../lib/services/email-service') describe('PasswordResetEmailRequest', () => { describe('constructor()', () => { @@ -175,16 +176,26 @@ describe('PasswordResetEmailRequest', () => { it('should throw an error if the user does not exist', done => { const host = SolidHost.from({ serverUri: 'https://example.com' }) const store = { suffixAcl: '.acl' } - const accountManager = AccountManager.from({ host, multiuser: true, store }) + const emailService = sinon.stub().returns(EmailService) + const accountManager = AccountManager.from({ host, multiuser: true, store, emailService }) accountManager.accountExists = sinon.stub().resolves(false) const username = 'alice' - const options = { accountManager, username } const request = new PasswordResetEmailRequest(options) + sinon.spy(request, 'renderSuccess') + sinon.spy(accountManager, 'userAccountFrom') + sinon.spy(accountManager, 'verifyEmailDependencies') + request.loadUser() - .catch(error => { - expect(error.message).to.equal('Account not found for that username') + .then(() => { + expect(accountManager.userAccountFrom).to.have.been.called() + expect(accountManager.verifyEmailDependencies).to.have.been.called() + expect(accountManager.verifyEmailDependencies).to.throw() + done() + }) + .catch(() => { + expect(request.renderSuccess).to.have.been.called() done() }) })