Skip to content

Commit

Permalink
fix 1774: changed account manager error checking, changed loadUser fu…
Browse files Browse the repository at this point in the history
…nction to properly render html, updated unit test
  • Loading branch information
zg009 committed Mar 16, 2024
1 parent 88d3a86 commit c38fe6e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/models/account-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}
}
Expand Down
10 changes: 9 additions & 1 deletion lib/requests/password-reset-email-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
19 changes: 15 additions & 4 deletions test/unit/password-reset-email-request-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()', () => {
Expand Down Expand Up @@ -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()
})
})
Expand Down

0 comments on commit c38fe6e

Please sign in to comment.