PasswordUpdateTests.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. const { expect } = require('chai')
  2. const PasswordResetRouter = require('../../../app/src/Features/PasswordReset/PasswordResetRouter')
  3. const UserHelper = require('./helpers/UserHelper')
  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 = userHelper.getDefaultEmail()
  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.equal('password is too short')
  136. })
  137. it('should not update audit log', async function () {
  138. const auditLog = userHelper.getAuditLogWithoutNoise()
  139. expect(auditLog).to.deep.equal([])
  140. })
  141. })
  142. })
  143. })