2020-08-13 09:42:28 -04:00
|
|
|
const { expect } = require('chai')
|
|
|
|
const UserHelper = require('./helpers/UserHelper')
|
2020-10-07 09:16:54 -04:00
|
|
|
const { db } = require('../../../app/src/infrastructure/mongodb')
|
2020-08-13 09:42:28 -04:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('PasswordReset', function () {
|
2021-03-31 05:35:29 -04:00
|
|
|
let email, response, user, userHelper, token, emailQuery
|
2021-04-14 09:17:21 -04:00
|
|
|
beforeEach(async function () {
|
2020-08-13 09:42:28 -04:00
|
|
|
userHelper = new UserHelper()
|
2023-03-22 05:36:59 -04:00
|
|
|
email = 'somecooluser@example.com'
|
2021-03-31 05:35:29 -04:00
|
|
|
emailQuery = `?email=${encodeURIComponent(email)}`
|
2020-08-13 09:42:28 -04:00
|
|
|
userHelper = await UserHelper.createUser({ email })
|
|
|
|
user = userHelper.user
|
|
|
|
|
|
|
|
// generate the token
|
|
|
|
await userHelper.getCsrfToken()
|
2022-10-17 09:53:55 -04:00
|
|
|
response = await userHelper.fetch('/user/password/reset', {
|
|
|
|
method: 'POST',
|
|
|
|
body: new URLSearchParams({ email }),
|
2020-08-13 09:42:28 -04:00
|
|
|
})
|
|
|
|
|
2020-12-15 05:23:54 -05:00
|
|
|
token = (
|
|
|
|
await db.tokens.findOne({
|
2021-04-27 03:52:58 -04:00
|
|
|
'data.user_id': user._id.toString(),
|
2020-12-15 05:23:54 -05:00
|
|
|
})
|
|
|
|
).token
|
2020-08-13 09:42:28 -04:00
|
|
|
})
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('with a valid token', function () {
|
|
|
|
describe('when logged in', function () {
|
|
|
|
beforeEach(async function () {
|
2020-08-13 09:42:28 -04:00
|
|
|
userHelper = await UserHelper.loginUser({
|
|
|
|
email,
|
2021-04-27 03:52:58 -04:00
|
|
|
password: userHelper.getDefaultPassword(),
|
2020-08-13 09:42:28 -04:00
|
|
|
})
|
2022-10-17 09:53:55 -04:00
|
|
|
response = await userHelper.fetch(
|
|
|
|
`/user/password/set?passwordResetToken=${token}&email=${email}`
|
2020-08-13 09:42:28 -04:00
|
|
|
)
|
2022-10-17 09:53:55 -04:00
|
|
|
expect(response.status).to.equal(302)
|
|
|
|
expect(response.headers.get('location')).to.equal(
|
|
|
|
UserHelper.url(`/user/password/set${emailQuery}`).toString()
|
2021-03-31 05:35:29 -04:00
|
|
|
)
|
2020-08-13 09:42:28 -04:00
|
|
|
// send reset request
|
2022-10-17 09:53:55 -04:00
|
|
|
response = await userHelper.fetch('/user/password/set', {
|
|
|
|
method: 'POST',
|
|
|
|
body: new URLSearchParams({
|
2020-08-13 09:42:28 -04:00
|
|
|
passwordResetToken: token,
|
2021-04-27 03:52:58 -04:00
|
|
|
password: 'a-password',
|
2022-10-17 09:53:55 -04:00
|
|
|
}),
|
2020-08-13 09:42:28 -04:00
|
|
|
})
|
|
|
|
userHelper = await UserHelper.getUser({ email })
|
|
|
|
user = userHelper.user
|
|
|
|
})
|
2021-04-14 09:17:21 -04:00
|
|
|
it('update the password', async function () {
|
2020-08-13 09:42:28 -04:00
|
|
|
expect(user.hashedPassword).to.exist
|
|
|
|
expect(user.password).to.not.exist
|
|
|
|
})
|
2021-04-14 09:17:21 -04:00
|
|
|
it('log the change with initiatorId', async function () {
|
2021-03-26 05:52:21 -04:00
|
|
|
const auditLog = userHelper.getAuditLogWithoutNoise()
|
|
|
|
expect(auditLog).to.exist
|
|
|
|
expect(auditLog[0]).to.exist
|
|
|
|
expect(typeof auditLog[0].initiatorId).to.equal('object')
|
|
|
|
expect(auditLog[0].initiatorId).to.deep.equal(user._id)
|
|
|
|
expect(auditLog[0].operation).to.equal('reset-password')
|
|
|
|
expect(auditLog[0].ipAddress).to.equal('127.0.0.1')
|
|
|
|
expect(auditLog[0].timestamp).to.exist
|
2020-08-13 09:42:28 -04:00
|
|
|
})
|
|
|
|
})
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('when logged in as another user', function () {
|
2020-08-13 09:42:28 -04:00
|
|
|
let otherUser, otherUserEmail
|
2021-04-14 09:17:21 -04:00
|
|
|
beforeEach(async function () {
|
2020-08-13 09:42:28 -04:00
|
|
|
otherUserEmail = userHelper.getDefaultEmail()
|
|
|
|
userHelper = await UserHelper.createUser({ email: otherUserEmail })
|
|
|
|
otherUser = userHelper.user
|
|
|
|
userHelper = await UserHelper.loginUser({
|
|
|
|
email: otherUserEmail,
|
2021-04-27 03:52:58 -04:00
|
|
|
password: userHelper.getDefaultPassword(),
|
2020-08-13 09:42:28 -04:00
|
|
|
})
|
2022-10-17 09:53:55 -04:00
|
|
|
response = await userHelper.fetch(
|
|
|
|
`/user/password/set?passwordResetToken=${token}&email=${email}`
|
2020-08-13 09:42:28 -04:00
|
|
|
)
|
2022-10-17 09:53:55 -04:00
|
|
|
expect(response.status).to.equal(302)
|
|
|
|
expect(response.headers.get('location')).to.equal(
|
|
|
|
UserHelper.url(`/user/password/set${emailQuery}`).toString()
|
2021-03-31 05:35:29 -04:00
|
|
|
)
|
2020-08-13 09:42:28 -04:00
|
|
|
// send reset request
|
2022-10-17 09:53:55 -04:00
|
|
|
response = await userHelper.fetch('/user/password/set', {
|
|
|
|
method: 'POST',
|
|
|
|
body: new URLSearchParams({
|
2020-08-13 09:42:28 -04:00
|
|
|
passwordResetToken: token,
|
2021-04-27 03:52:58 -04:00
|
|
|
password: 'a-password',
|
2022-10-17 09:53:55 -04:00
|
|
|
}),
|
2020-08-13 09:42:28 -04:00
|
|
|
})
|
|
|
|
userHelper = await UserHelper.getUser({ email })
|
|
|
|
user = userHelper.user
|
|
|
|
})
|
2021-04-14 09:17:21 -04:00
|
|
|
it('update the password', async function () {
|
2020-08-13 09:42:28 -04:00
|
|
|
expect(user.hashedPassword).to.exist
|
|
|
|
expect(user.password).to.not.exist
|
|
|
|
})
|
2021-04-14 09:17:21 -04:00
|
|
|
it('log the change with the logged in user as the initiatorId', async function () {
|
2021-03-26 05:52:21 -04:00
|
|
|
const auditLog = userHelper.getAuditLogWithoutNoise()
|
|
|
|
expect(auditLog).to.exist
|
|
|
|
expect(auditLog[0]).to.exist
|
|
|
|
expect(typeof auditLog[0].initiatorId).to.equal('object')
|
|
|
|
expect(auditLog[0].initiatorId).to.deep.equal(otherUser._id)
|
|
|
|
expect(auditLog[0].operation).to.equal('reset-password')
|
|
|
|
expect(auditLog[0].ipAddress).to.equal('127.0.0.1')
|
|
|
|
expect(auditLog[0].timestamp).to.exist
|
2020-08-13 09:42:28 -04:00
|
|
|
})
|
|
|
|
})
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('when not logged in', function () {
|
|
|
|
beforeEach(async function () {
|
2022-10-17 09:53:55 -04:00
|
|
|
response = await userHelper.fetch(
|
|
|
|
`/user/password/set?passwordResetToken=${token}&email=${email}`
|
2020-08-13 09:42:28 -04:00
|
|
|
)
|
2022-10-17 09:53:55 -04:00
|
|
|
expect(response.status).to.equal(302)
|
|
|
|
expect(response.headers.get('location')).to.equal(
|
|
|
|
UserHelper.url(`/user/password/set${emailQuery}`).toString()
|
2021-03-31 05:35:29 -04:00
|
|
|
)
|
2020-08-13 09:42:28 -04:00
|
|
|
// send reset request
|
2022-10-17 09:53:55 -04:00
|
|
|
response = await userHelper.fetch('/user/password/set', {
|
|
|
|
method: 'POST',
|
|
|
|
body: new URLSearchParams({
|
2020-08-13 09:42:28 -04:00
|
|
|
passwordResetToken: token,
|
2021-04-27 03:52:58 -04:00
|
|
|
password: 'a-password',
|
2022-10-17 09:53:55 -04:00
|
|
|
}),
|
2020-08-13 09:42:28 -04:00
|
|
|
})
|
|
|
|
userHelper = await UserHelper.getUser({ email })
|
|
|
|
user = userHelper.user
|
|
|
|
})
|
2021-04-14 09:17:21 -04:00
|
|
|
it('updates the password', function () {
|
2020-08-13 09:42:28 -04:00
|
|
|
expect(user.hashedPassword).to.exist
|
|
|
|
expect(user.password).to.not.exist
|
|
|
|
})
|
2021-04-14 09:17:21 -04:00
|
|
|
it('log the change', async function () {
|
2021-03-26 05:52:21 -04:00
|
|
|
const auditLog = userHelper.getAuditLogWithoutNoise()
|
|
|
|
expect(auditLog).to.exist
|
|
|
|
expect(auditLog[0]).to.exist
|
|
|
|
expect(auditLog[0].initiatorId).to.equal(null)
|
|
|
|
expect(auditLog[0].operation).to.equal('reset-password')
|
|
|
|
expect(auditLog[0].ipAddress).to.equal('127.0.0.1')
|
|
|
|
expect(auditLog[0].timestamp).to.exist
|
2020-08-13 09:42:28 -04:00
|
|
|
})
|
|
|
|
})
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('password checks', function () {
|
|
|
|
beforeEach(async function () {
|
2022-10-17 09:53:55 -04:00
|
|
|
response = await userHelper.fetch(
|
|
|
|
`/user/password/set?passwordResetToken=${token}&email=${email}`
|
2020-08-13 09:42:28 -04:00
|
|
|
)
|
2022-10-17 09:53:55 -04:00
|
|
|
expect(response.status).to.equal(302)
|
|
|
|
expect(response.headers.get('location')).to.equal(
|
|
|
|
UserHelper.url(`/user/password/set${emailQuery}`).toString()
|
2021-03-31 05:35:29 -04:00
|
|
|
)
|
2020-08-13 09:42:28 -04:00
|
|
|
})
|
2021-04-14 09:17:21 -04:00
|
|
|
it('without a password should return 400 and not log the change', async function () {
|
2020-08-13 09:42:28 -04:00
|
|
|
// send reset request
|
2022-10-17 09:53:55 -04:00
|
|
|
response = await userHelper.fetch('/user/password/set', {
|
|
|
|
method: 'POST',
|
|
|
|
body: new URLSearchParams({
|
2021-04-27 03:52:58 -04:00
|
|
|
passwordResetToken: token,
|
2022-10-17 09:53:55 -04:00
|
|
|
}),
|
2020-08-13 09:42:28 -04:00
|
|
|
})
|
2022-10-17 09:53:55 -04:00
|
|
|
expect(response.status).to.equal(400)
|
2020-08-13 09:42:28 -04:00
|
|
|
userHelper = await UserHelper.getUser({ email })
|
2021-03-26 05:52:21 -04:00
|
|
|
|
|
|
|
const auditLog = userHelper.getAuditLogWithoutNoise()
|
|
|
|
expect(auditLog).to.deep.equal([])
|
2020-08-13 09:42:28 -04:00
|
|
|
})
|
|
|
|
|
2021-11-09 06:01:27 -05:00
|
|
|
it('without a valid password should return 400 and not log the change', async function () {
|
2020-08-13 09:42:28 -04:00
|
|
|
// send reset request
|
2022-10-17 09:53:55 -04:00
|
|
|
response = await userHelper.fetch('/user/password/set', {
|
|
|
|
method: 'POST',
|
|
|
|
body: new URLSearchParams({
|
2020-08-13 09:42:28 -04:00
|
|
|
passwordResetToken: token,
|
2021-04-27 03:52:58 -04:00
|
|
|
password: 'short',
|
2022-10-17 09:53:55 -04:00
|
|
|
}),
|
2020-08-13 09:42:28 -04:00
|
|
|
})
|
2022-10-17 09:53:55 -04:00
|
|
|
expect(response.status).to.equal(400)
|
2020-08-13 09:42:28 -04:00
|
|
|
userHelper = await UserHelper.getUser({ email })
|
2021-03-26 05:52:21 -04:00
|
|
|
|
2021-11-09 06:01:27 -05:00
|
|
|
const auditLog = userHelper.getAuditLogWithoutNoise()
|
|
|
|
expect(auditLog).to.deep.equal([])
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should flag email in password', async function () {
|
|
|
|
const localPart = email.split('@').shift()
|
|
|
|
// send bad password
|
2022-10-17 09:53:55 -04:00
|
|
|
response = await userHelper.fetch('/user/password/set', {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
Accept: 'application/json',
|
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
2021-11-09 06:01:27 -05:00
|
|
|
passwordResetToken: token,
|
|
|
|
password: localPart,
|
|
|
|
email,
|
2022-10-17 09:53:55 -04:00
|
|
|
}),
|
2021-11-09 06:01:27 -05:00
|
|
|
})
|
2022-10-17 09:53:55 -04:00
|
|
|
expect(response.status).to.equal(400)
|
|
|
|
const body = await response.json()
|
|
|
|
expect(body).to.deep.equal({
|
2023-04-11 09:16:51 -04:00
|
|
|
message: {
|
|
|
|
type: 'error',
|
|
|
|
key: 'password-contains-email',
|
|
|
|
text: 'Password cannot contain parts of email address',
|
|
|
|
},
|
2021-11-09 06:01:27 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2023-03-22 05:36:59 -04:00
|
|
|
it('should flag password too similar to email', async function () {
|
|
|
|
const localPart = email.split('@').shift()
|
|
|
|
const localPartReversed = localPart.split('').reverse().join('')
|
|
|
|
// send bad password
|
|
|
|
response = await userHelper.fetch('/user/password/set', {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
Accept: 'application/json',
|
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
|
|
|
passwordResetToken: token,
|
|
|
|
password: `${localPartReversed}123`,
|
|
|
|
email,
|
|
|
|
}),
|
|
|
|
})
|
|
|
|
expect(response.status).to.equal(400)
|
|
|
|
const body = await response.json()
|
|
|
|
expect(body).to.deep.equal({
|
2023-04-11 09:16:51 -04:00
|
|
|
message: {
|
|
|
|
type: 'error',
|
|
|
|
key: 'password-too-similar',
|
|
|
|
text: 'Password is too similar to parts of email address',
|
|
|
|
},
|
2023-03-22 05:36:59 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-11-09 06:01:27 -05:00
|
|
|
it('should be able to retry after providing an invalid password', async function () {
|
|
|
|
// send bad password
|
2022-10-17 09:53:55 -04:00
|
|
|
response = await userHelper.fetch('/user/password/set', {
|
|
|
|
method: 'POST',
|
|
|
|
body: new URLSearchParams({
|
2021-11-09 06:01:27 -05:00
|
|
|
passwordResetToken: token,
|
|
|
|
password: 'short',
|
2022-10-17 09:53:55 -04:00
|
|
|
}),
|
2021-11-09 06:01:27 -05:00
|
|
|
})
|
2022-10-17 09:53:55 -04:00
|
|
|
expect(response.status).to.equal(400)
|
2021-11-09 06:01:27 -05:00
|
|
|
|
|
|
|
// send good password
|
2022-10-17 09:53:55 -04:00
|
|
|
response = await userHelper.fetch('/user/password/set', {
|
|
|
|
method: 'POST',
|
|
|
|
body: new URLSearchParams({
|
2021-11-09 06:01:27 -05:00
|
|
|
passwordResetToken: token,
|
|
|
|
password: 'SomeThingVeryStrong!11',
|
2022-10-17 09:53:55 -04:00
|
|
|
}),
|
2021-11-09 06:01:27 -05:00
|
|
|
})
|
2022-10-17 09:53:55 -04:00
|
|
|
expect(response.status).to.equal(200)
|
2021-11-09 06:01:27 -05:00
|
|
|
userHelper = await UserHelper.getUser({ email })
|
|
|
|
|
2021-03-26 05:52:21 -04:00
|
|
|
const auditLog = userHelper.getAuditLogWithoutNoise()
|
2021-10-20 04:44:52 -04:00
|
|
|
expect(auditLog.length).to.equal(1)
|
2020-08-13 09:42:28 -04:00
|
|
|
})
|
2022-09-27 07:24:17 -04:00
|
|
|
|
|
|
|
it('when the password is the same as current, should return 400 and log the change', async function () {
|
|
|
|
// send reset request
|
2022-10-17 09:53:55 -04:00
|
|
|
response = await userHelper.fetch('/user/password/set', {
|
|
|
|
method: 'POST',
|
|
|
|
body: new URLSearchParams({
|
2022-09-27 07:24:17 -04:00
|
|
|
passwordResetToken: token,
|
|
|
|
password: userHelper.getDefaultPassword(),
|
2022-10-17 09:53:55 -04:00
|
|
|
}),
|
2022-09-27 07:24:17 -04:00
|
|
|
})
|
2022-10-17 09:53:55 -04:00
|
|
|
expect(response.status).to.equal(400)
|
|
|
|
const body = await response.json()
|
|
|
|
expect(body.message.key).to.equal('password-must-be-different')
|
2022-09-27 07:24:17 -04:00
|
|
|
userHelper = await UserHelper.getUser({ email })
|
|
|
|
|
|
|
|
const auditLog = userHelper.getAuditLogWithoutNoise()
|
|
|
|
expect(auditLog.length).to.equal(1)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('multiple attempts to set the password, reaching attempt limit', async function () {
|
|
|
|
beforeEach(async function () {
|
2022-10-17 09:53:55 -04:00
|
|
|
response = await userHelper.fetch(
|
|
|
|
`/user/password/set?passwordResetToken=${token}&email=${email}`
|
2022-09-27 07:24:17 -04:00
|
|
|
)
|
2022-10-17 09:53:55 -04:00
|
|
|
expect(response.status).to.equal(302)
|
|
|
|
expect(response.headers.get('location')).to.equal(
|
|
|
|
UserHelper.url(`/user/password/set${emailQuery}`).toString()
|
2022-09-27 07:24:17 -04:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should allow multiple attempts with same-password error, then deny further attempts', async function () {
|
|
|
|
const sendSamePasswordRequest = async function () {
|
2022-10-17 09:53:55 -04:00
|
|
|
return userHelper.fetch('/user/password/set', {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
Accept: 'application/json',
|
|
|
|
},
|
|
|
|
body: new URLSearchParams({
|
2022-09-27 07:24:17 -04:00
|
|
|
passwordResetToken: token,
|
|
|
|
password: userHelper.getDefaultPassword(),
|
2022-10-17 09:53:55 -04:00
|
|
|
}),
|
2022-09-27 07:24:17 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
// Three attempts at setting the password, all rejected for being the same as
|
|
|
|
// the current password
|
|
|
|
const response1 = await sendSamePasswordRequest()
|
2022-10-17 09:53:55 -04:00
|
|
|
expect(response1.status).to.equal(400)
|
|
|
|
const body1 = await response1.json()
|
|
|
|
expect(body1.message.key).to.equal('password-must-be-different')
|
2022-09-27 07:24:17 -04:00
|
|
|
const response2 = await sendSamePasswordRequest()
|
2022-10-17 09:53:55 -04:00
|
|
|
expect(response2.status).to.equal(400)
|
|
|
|
const body2 = await response2.json()
|
|
|
|
expect(body2.message.key).to.equal('password-must-be-different')
|
2022-09-27 07:24:17 -04:00
|
|
|
const response3 = await sendSamePasswordRequest()
|
2022-10-17 09:53:55 -04:00
|
|
|
expect(response3.status).to.equal(400)
|
|
|
|
const body3 = await response3.json()
|
|
|
|
expect(body3.message.key).to.equal('password-must-be-different')
|
2022-09-27 07:24:17 -04:00
|
|
|
// Fourth attempt is rejected because the token has been used too many times
|
|
|
|
const response4 = await sendSamePasswordRequest()
|
2022-10-17 09:53:55 -04:00
|
|
|
expect(response4.status).to.equal(404)
|
|
|
|
const body4 = await response4.json()
|
|
|
|
expect(body4.message.key).to.equal('token-expired')
|
2022-09-27 07:24:17 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should allow multiple attempts with same-password error, then set the password', async function () {
|
|
|
|
const sendSamePasswordRequest = async function () {
|
2022-10-17 09:53:55 -04:00
|
|
|
return userHelper.fetch('/user/password/set', {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
Accept: 'application/json',
|
|
|
|
},
|
|
|
|
body: new URLSearchParams({
|
2022-09-27 07:24:17 -04:00
|
|
|
passwordResetToken: token,
|
|
|
|
password: userHelper.getDefaultPassword(),
|
2022-10-17 09:53:55 -04:00
|
|
|
}),
|
2022-09-27 07:24:17 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
// Two attempts at setting the password, all rejected for being the same as
|
|
|
|
// the current password
|
|
|
|
const response1 = await sendSamePasswordRequest()
|
2022-10-17 09:53:55 -04:00
|
|
|
expect(response1.status).to.equal(400)
|
|
|
|
const body1 = await response1.json()
|
|
|
|
expect(body1.message.key).to.equal('password-must-be-different')
|
2022-09-27 07:24:17 -04:00
|
|
|
const response2 = await sendSamePasswordRequest()
|
2022-10-17 09:53:55 -04:00
|
|
|
expect(response2.status).to.equal(400)
|
|
|
|
const body2 = await response2.json()
|
|
|
|
expect(body2.message.key).to.equal('password-must-be-different')
|
2022-09-27 07:24:17 -04:00
|
|
|
// Third attempt is succeeds
|
2022-10-17 09:53:55 -04:00
|
|
|
const response3 = await userHelper.fetch('/user/password/set', {
|
|
|
|
method: 'POST',
|
|
|
|
body: new URLSearchParams({
|
2022-09-27 07:24:17 -04:00
|
|
|
passwordResetToken: token,
|
|
|
|
password: 'some-new-password',
|
2022-10-17 09:53:55 -04:00
|
|
|
}),
|
2022-09-27 07:24:17 -04:00
|
|
|
})
|
2022-10-17 09:53:55 -04:00
|
|
|
expect(response3.status).to.equal(200)
|
2022-09-27 07:24:17 -04:00
|
|
|
// Check the user and audit log
|
|
|
|
userHelper = await UserHelper.getUser({ email })
|
|
|
|
user = userHelper.user
|
|
|
|
expect(user.hashedPassword).to.exist
|
|
|
|
expect(user.password).to.not.exist
|
|
|
|
const auditLog = userHelper.getAuditLogWithoutNoise()
|
|
|
|
expect(auditLog).to.exist
|
|
|
|
expect(auditLog[0]).to.exist
|
|
|
|
expect(auditLog[0].initiatorId).to.equal(null)
|
|
|
|
expect(auditLog[0].operation).to.equal('reset-password')
|
|
|
|
expect(auditLog[0].ipAddress).to.equal('127.0.0.1')
|
|
|
|
expect(auditLog[0].timestamp).to.exist
|
2020-08-13 09:42:28 -04:00
|
|
|
})
|
|
|
|
})
|
2022-09-27 07:24:17 -04:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('without a valid token', function () {
|
|
|
|
it('no token should redirect to page to re-request reset token', async function () {
|
2022-10-17 09:53:55 -04:00
|
|
|
response = await userHelper.fetch(`/user/password/set?&email=${email}`)
|
|
|
|
expect(response.status).to.equal(302)
|
|
|
|
expect(response.headers.get('location')).to.equal(
|
|
|
|
UserHelper.url('/user/password/reset').toString()
|
2020-08-13 09:42:28 -04:00
|
|
|
)
|
|
|
|
})
|
2022-09-27 07:24:17 -04:00
|
|
|
it('should show error for invalid tokens and return 404 if used', async function () {
|
2020-08-13 09:42:28 -04:00
|
|
|
const invalidToken = 'not-real-token'
|
2022-10-17 09:53:55 -04:00
|
|
|
response = await userHelper.fetch(
|
|
|
|
`/user/password/set?&passwordResetToken=${invalidToken}&email=${email}`
|
2020-08-13 09:42:28 -04:00
|
|
|
)
|
2022-10-17 09:53:55 -04:00
|
|
|
expect(response.status).to.equal(302)
|
|
|
|
expect(response.headers.get('location')).to.equal(
|
|
|
|
UserHelper.url('/user/password/reset?error=token_expired').toString()
|
2021-03-31 05:35:29 -04:00
|
|
|
)
|
2020-08-13 09:42:28 -04:00
|
|
|
// send reset request
|
2022-10-17 09:53:55 -04:00
|
|
|
response = await userHelper.fetch('/user/password/set', {
|
|
|
|
method: 'POST',
|
|
|
|
body: new URLSearchParams({
|
2020-08-13 09:42:28 -04:00
|
|
|
passwordResetToken: invalidToken,
|
2021-04-27 03:52:58 -04:00
|
|
|
password: 'a-password',
|
2022-10-17 09:53:55 -04:00
|
|
|
}),
|
2020-08-13 09:42:28 -04:00
|
|
|
})
|
2022-10-17 09:53:55 -04:00
|
|
|
expect(response.status).to.equal(404)
|
2020-08-13 09:42:28 -04:00
|
|
|
})
|
|
|
|
})
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('password reset', function () {
|
|
|
|
it('should return 200 if email field is valid', async function () {
|
2022-10-17 09:53:55 -04:00
|
|
|
response = await userHelper.fetch(`/user/password/reset`, {
|
|
|
|
method: 'POST',
|
|
|
|
body: new URLSearchParams({ email }),
|
2021-02-03 07:37:16 -05:00
|
|
|
})
|
2022-10-17 09:53:55 -04:00
|
|
|
expect(response.status).to.equal(200)
|
2021-02-03 07:37:16 -05:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should return 400 if email field is missing', async function () {
|
2022-10-17 09:53:55 -04:00
|
|
|
response = await userHelper.fetch(`/user/password/reset`, {
|
|
|
|
method: 'POST',
|
|
|
|
body: new URLSearchParams({ mail: email }),
|
2021-02-03 07:37:16 -05:00
|
|
|
})
|
2022-10-17 09:53:55 -04:00
|
|
|
expect(response.status).to.equal(400)
|
2021-02-03 07:37:16 -05:00
|
|
|
})
|
|
|
|
})
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('password set', function () {
|
|
|
|
it('should return 200 if password and passwordResetToken fields are valid', async function () {
|
2022-10-17 09:53:55 -04:00
|
|
|
response = await userHelper.fetch(`/user/password/set`, {
|
|
|
|
method: 'POST',
|
|
|
|
body: new URLSearchParams({
|
2021-02-03 07:37:16 -05:00
|
|
|
password: 'new-password',
|
2021-04-27 03:52:58 -04:00
|
|
|
passwordResetToken: token,
|
2022-10-17 09:53:55 -04:00
|
|
|
}),
|
2021-02-03 07:37:16 -05:00
|
|
|
})
|
2022-10-17 09:53:55 -04:00
|
|
|
expect(response.status).to.equal(200)
|
2021-02-03 07:37:16 -05:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should return 400 if password field is missing', async function () {
|
2022-10-17 09:53:55 -04:00
|
|
|
response = await userHelper.fetch(`/user/password/set`, {
|
|
|
|
method: 'POST',
|
|
|
|
body: new URLSearchParams({
|
2021-04-27 03:52:58 -04:00
|
|
|
passwordResetToken: token,
|
2022-10-17 09:53:55 -04:00
|
|
|
}),
|
2021-02-03 07:37:16 -05:00
|
|
|
})
|
2022-10-17 09:53:55 -04:00
|
|
|
expect(response.status).to.equal(400)
|
2021-02-03 07:37:16 -05:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should return 400 if passwordResetToken field is missing', async function () {
|
2022-10-17 09:53:55 -04:00
|
|
|
response = await userHelper.fetch(`/user/password/set`, {
|
|
|
|
method: 'POST',
|
|
|
|
body: new URLSearchParams({
|
2021-04-27 03:52:58 -04:00
|
|
|
password: 'new-password',
|
2022-10-17 09:53:55 -04:00
|
|
|
}),
|
2021-02-03 07:37:16 -05:00
|
|
|
})
|
2022-10-17 09:53:55 -04:00
|
|
|
expect(response.status).to.equal(400)
|
2021-02-03 07:37:16 -05:00
|
|
|
})
|
|
|
|
})
|
2020-08-13 09:42:28 -04:00
|
|
|
})
|