PasswordUpdateTests.mjs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. import { expect } from 'chai'
  2. import PasswordResetRouter from '../../../app/src/Features/PasswordReset/PasswordResetRouter.mjs'
  3. import UserHelper from './helpers/UserHelper.mjs'
  4. describe('PasswordUpdate', function () {
  5. let email, password, response, user, userHelper
  6. afterEach(async function () {
  7. await PasswordResetRouter.rateLimiter.delete('127.0.0.1')
  8. })
  9. beforeEach(async function () {
  10. userHelper = new UserHelper()
  11. email = 'somecooluser@example.com'
  12. password = 'old-password'
  13. userHelper = await UserHelper.createUser({ email, password })
  14. userHelper = await UserHelper.loginUser({
  15. email,
  16. password,
  17. })
  18. await userHelper.getCsrfToken()
  19. })
  20. describe('success', function () {
  21. beforeEach(async function () {
  22. response = await userHelper.fetch('/user/password/update', {
  23. method: 'POST',
  24. body: new URLSearchParams({
  25. currentPassword: password,
  26. newPassword1: 'new-password',
  27. newPassword2: 'new-password',
  28. }),
  29. })
  30. userHelper = await UserHelper.getUser({ email })
  31. user = userHelper.user
  32. })
  33. it('should return 200', async function () {
  34. expect(response.status).to.equal(200)
  35. })
  36. it('should update the audit log', function () {
  37. const auditLog = userHelper.getAuditLogWithoutNoise()
  38. expect(auditLog[0]).to.exist
  39. expect(typeof auditLog[0].initiatorId).to.equal('object')
  40. expect(auditLog[0].initiatorId).to.deep.equal(user._id)
  41. expect(auditLog[0].operation).to.equal('update-password')
  42. expect(auditLog[0].ipAddress).to.equal('127.0.0.1')
  43. expect(auditLog[0].timestamp).to.exist
  44. })
  45. })
  46. describe('errors', function () {
  47. describe('missing current password', function () {
  48. beforeEach(async function () {
  49. response = await userHelper.fetch('/user/password/update', {
  50. method: 'POST',
  51. body: new URLSearchParams({
  52. newPassword1: 'new-password',
  53. newPassword2: 'new-password',
  54. }),
  55. })
  56. userHelper = await UserHelper.getUser({ email })
  57. })
  58. it('should return 500', async function () {
  59. expect(response.status).to.equal(500)
  60. })
  61. it('should not update audit log', async function () {
  62. const auditLog = userHelper.getAuditLogWithoutNoise()
  63. expect(auditLog).to.deep.equal([])
  64. })
  65. })
  66. describe('wrong current password', function () {
  67. beforeEach(async function () {
  68. response = await userHelper.fetch('/user/password/update', {
  69. method: 'POST',
  70. body: new URLSearchParams({
  71. currentPassword: 'wrong-password',
  72. newPassword1: 'new-password',
  73. newPassword2: 'new-password',
  74. }),
  75. })
  76. userHelper = await UserHelper.getUser({ email })
  77. })
  78. it('should return 400', async function () {
  79. expect(response.status).to.equal(400)
  80. })
  81. it('should not update audit log', async function () {
  82. const auditLog = userHelper.getAuditLogWithoutNoise()
  83. expect(auditLog).to.deep.equal([])
  84. })
  85. })
  86. describe('newPassword1 does not match newPassword2', function () {
  87. beforeEach(async function () {
  88. response = await userHelper.fetch('/user/password/update', {
  89. method: 'POST',
  90. headers: {
  91. 'Content-Type': 'application/json',
  92. Accept: 'application/json',
  93. },
  94. body: JSON.stringify({
  95. currentPassword: password,
  96. newPassword1: 'new-password',
  97. newPassword2: 'oops-password',
  98. }),
  99. })
  100. userHelper = await UserHelper.getUser({ email })
  101. })
  102. it('should return 400', async function () {
  103. expect(response.status).to.equal(400)
  104. })
  105. it('should return error message', async function () {
  106. const body = await response.json()
  107. expect(body.message).to.equal('Passwords do not match.')
  108. })
  109. it('should not update audit log', async function () {
  110. const auditLog = userHelper.getAuditLogWithoutNoise()
  111. expect(auditLog).to.deep.equal([])
  112. })
  113. })
  114. describe('new password is not valid', function () {
  115. beforeEach(async function () {
  116. response = await userHelper.fetch('/user/password/update', {
  117. method: 'POST',
  118. headers: {
  119. 'Content-Type': 'application/json',
  120. Accept: 'application/json',
  121. },
  122. body: JSON.stringify({
  123. currentPassword: password,
  124. newPassword1: 'short',
  125. newPassword2: 'short',
  126. }),
  127. })
  128. userHelper = await UserHelper.getUser({ email })
  129. })
  130. it('should return 400', async function () {
  131. expect(response.status).to.equal(400)
  132. })
  133. it('should return error message', async function () {
  134. const body = await response.json()
  135. expect(body.message).to.deep.equal({
  136. type: 'error',
  137. key: 'password-too-short',
  138. text: 'Password too short, minimum 8.',
  139. })
  140. })
  141. it('should not update audit log', async function () {
  142. const auditLog = userHelper.getAuditLogWithoutNoise()
  143. expect(auditLog).to.deep.equal([])
  144. })
  145. })
  146. describe('new password contains part of email', function () {
  147. beforeEach(async function () {
  148. response = await userHelper.fetch('/user/password/update', {
  149. method: 'POST',
  150. headers: {
  151. 'Content-Type': 'application/json',
  152. Accept: 'application/json',
  153. },
  154. body: JSON.stringify({
  155. currentPassword: password,
  156. newPassword1: 'somecooluser123',
  157. newPassword2: 'somecooluser123',
  158. }),
  159. })
  160. userHelper = await UserHelper.getUser({ email })
  161. })
  162. it('should return 400', async function () {
  163. expect(response.status).to.equal(400)
  164. })
  165. it('should return error message', async function () {
  166. const body = await response.json()
  167. expect(body.message).to.deep.equal({
  168. key: 'password-contains-email',
  169. type: 'error',
  170. text: 'Password cannot contain parts of email address.',
  171. })
  172. })
  173. it('should not update audit log', async function () {
  174. const auditLog = userHelper.getAuditLogWithoutNoise()
  175. expect(auditLog).to.deep.equal([])
  176. })
  177. })
  178. describe('new password is too similar to email', function () {
  179. beforeEach(async function () {
  180. response = await userHelper.fetch('/user/password/update', {
  181. method: 'POST',
  182. headers: {
  183. 'Content-Type': 'application/json',
  184. Accept: 'application/json',
  185. },
  186. body: JSON.stringify({
  187. currentPassword: password,
  188. newPassword1: 'coolusersome123',
  189. newPassword2: 'coolusersome123',
  190. }),
  191. })
  192. userHelper = await UserHelper.getUser({ email })
  193. })
  194. it('should return 400', async function () {
  195. expect(response.status).to.equal(400)
  196. })
  197. it('should return error message', async function () {
  198. const body = await response.json()
  199. expect(body.message).to.deep.equal({
  200. key: 'password-too-similar',
  201. type: 'error',
  202. text: 'Password is too similar to parts of email address.',
  203. })
  204. })
  205. it('should not update audit log', async function () {
  206. const auditLog = userHelper.getAuditLogWithoutNoise()
  207. expect(auditLog).to.deep.equal([])
  208. })
  209. })
  210. })
  211. })