PasswordUpdateTests.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. const { expect } = require('chai')
  2. const RateLimiter = require('../../../app/src/infrastructure/RateLimiter')
  3. const UserHelper = require('./helpers/UserHelper')
  4. describe('PasswordUpdate', function () {
  5. let email, password, response, user, userHelper
  6. afterEach(async function () {
  7. await RateLimiter.promises.clearRateLimit(
  8. 'password_reset_rate_limit',
  9. '127.0.0.1'
  10. )
  11. })
  12. beforeEach(async function () {
  13. userHelper = new UserHelper()
  14. email = userHelper.getDefaultEmail()
  15. password = 'old-password'
  16. userHelper = await UserHelper.createUser({ email, password })
  17. userHelper = await UserHelper.loginUser({
  18. email,
  19. password,
  20. })
  21. await userHelper.getCsrfToken()
  22. })
  23. describe('success', function () {
  24. beforeEach(async function () {
  25. response = await userHelper.fetch('/user/password/update', {
  26. method: 'POST',
  27. body: new URLSearchParams({
  28. currentPassword: password,
  29. newPassword1: 'new-password',
  30. newPassword2: 'new-password',
  31. }),
  32. })
  33. userHelper = await UserHelper.getUser({ email })
  34. user = userHelper.user
  35. })
  36. it('should return 200', async function () {
  37. expect(response.status).to.equal(200)
  38. })
  39. it('should update the audit log', function () {
  40. const auditLog = userHelper.getAuditLogWithoutNoise()
  41. expect(auditLog[0]).to.exist
  42. expect(typeof auditLog[0].initiatorId).to.equal('object')
  43. expect(auditLog[0].initiatorId).to.deep.equal(user._id)
  44. expect(auditLog[0].operation).to.equal('update-password')
  45. expect(auditLog[0].ipAddress).to.equal('127.0.0.1')
  46. expect(auditLog[0].timestamp).to.exist
  47. })
  48. })
  49. describe('errors', function () {
  50. describe('missing current password', function () {
  51. beforeEach(async function () {
  52. response = await userHelper.fetch('/user/password/update', {
  53. method: 'POST',
  54. body: new URLSearchParams({
  55. newPassword1: 'new-password',
  56. newPassword2: 'new-password',
  57. }),
  58. })
  59. userHelper = await UserHelper.getUser({ email })
  60. })
  61. it('should return 500', async function () {
  62. expect(response.status).to.equal(500)
  63. })
  64. it('should not update audit log', async function () {
  65. const auditLog = userHelper.getAuditLogWithoutNoise()
  66. expect(auditLog).to.deep.equal([])
  67. })
  68. })
  69. describe('wrong current password', function () {
  70. beforeEach(async function () {
  71. response = await userHelper.fetch('/user/password/update', {
  72. method: 'POST',
  73. body: new URLSearchParams({
  74. currentPassword: 'wrong-password',
  75. newPassword1: 'new-password',
  76. newPassword2: 'new-password',
  77. }),
  78. })
  79. userHelper = await UserHelper.getUser({ email })
  80. })
  81. it('should return 400', async function () {
  82. expect(response.status).to.equal(400)
  83. })
  84. it('should not update audit log', async function () {
  85. const auditLog = userHelper.getAuditLogWithoutNoise()
  86. expect(auditLog).to.deep.equal([])
  87. })
  88. })
  89. describe('newPassword1 does not match newPassword2', function () {
  90. beforeEach(async function () {
  91. response = await userHelper.fetch('/user/password/update', {
  92. method: 'POST',
  93. headers: {
  94. 'Content-Type': 'application/json',
  95. Accept: 'application/json',
  96. },
  97. body: JSON.stringify({
  98. currentPassword: password,
  99. newPassword1: 'new-password',
  100. newPassword2: 'oops-password',
  101. }),
  102. })
  103. userHelper = await UserHelper.getUser({ email })
  104. })
  105. it('should return 400', async function () {
  106. expect(response.status).to.equal(400)
  107. })
  108. it('should return error message', async function () {
  109. const body = await response.json()
  110. expect(body.message).to.equal('Passwords do not match')
  111. })
  112. it('should not update audit log', async function () {
  113. const auditLog = userHelper.getAuditLogWithoutNoise()
  114. expect(auditLog).to.deep.equal([])
  115. })
  116. })
  117. describe('new password is not valid', function () {
  118. beforeEach(async function () {
  119. response = await userHelper.fetch('/user/password/update', {
  120. method: 'POST',
  121. headers: {
  122. 'Content-Type': 'application/json',
  123. Accept: 'application/json',
  124. },
  125. body: JSON.stringify({
  126. currentPassword: password,
  127. newPassword1: 'short',
  128. newPassword2: 'short',
  129. }),
  130. })
  131. userHelper = await UserHelper.getUser({ email })
  132. })
  133. it('should return 400', async function () {
  134. expect(response.status).to.equal(400)
  135. })
  136. it('should return error message', async function () {
  137. const body = await response.json()
  138. expect(body.message).to.equal('password is too short')
  139. })
  140. it('should not update audit log', async function () {
  141. const auditLog = userHelper.getAuditLogWithoutNoise()
  142. expect(auditLog).to.deep.equal([])
  143. })
  144. })
  145. })
  146. })