PasswordUpdateTests.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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.request.post('/user/password/update', {
  26. form: {
  27. currentPassword: password,
  28. newPassword1: 'new-password',
  29. newPassword2: 'new-password',
  30. },
  31. simple: false,
  32. })
  33. userHelper = await UserHelper.getUser({ email })
  34. user = userHelper.user
  35. })
  36. it('should return 200', async function () {
  37. expect(response.statusCode).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.request.post('/user/password/update', {
  53. form: {
  54. newPassword1: 'new-password',
  55. newPassword2: 'new-password',
  56. },
  57. simple: false,
  58. })
  59. userHelper = await UserHelper.getUser({ email })
  60. })
  61. it('should return 500', async function () {
  62. expect(response.statusCode).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.request.post('/user/password/update', {
  72. form: {
  73. currentPassword: 'wrong-password',
  74. newPassword1: 'new-password',
  75. newPassword2: 'new-password',
  76. },
  77. simple: false,
  78. })
  79. userHelper = await UserHelper.getUser({ email })
  80. })
  81. it('should return 400', async function () {
  82. expect(response.statusCode).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.request.post('/user/password/update', {
  92. form: {
  93. currentPassword: password,
  94. newPassword1: 'new-password',
  95. newPassword2: 'oops-password',
  96. },
  97. json: true,
  98. simple: false,
  99. })
  100. userHelper = await UserHelper.getUser({ email })
  101. })
  102. it('should return 400', async function () {
  103. expect(response.statusCode).to.equal(400)
  104. })
  105. it('should return error message', async function () {
  106. expect(response.body.message).to.equal('Passwords do not match')
  107. })
  108. it('should not update audit log', async function () {
  109. const auditLog = userHelper.getAuditLogWithoutNoise()
  110. expect(auditLog).to.deep.equal([])
  111. })
  112. })
  113. describe('new password is not valid', function () {
  114. beforeEach(async function () {
  115. response = await userHelper.request.post('/user/password/update', {
  116. form: {
  117. currentPassword: password,
  118. newPassword1: 'short',
  119. newPassword2: 'short',
  120. },
  121. json: true,
  122. simple: false,
  123. })
  124. userHelper = await UserHelper.getUser({ email })
  125. })
  126. it('should return 400', async function () {
  127. expect(response.statusCode).to.equal(400)
  128. })
  129. it('should return error message', async function () {
  130. expect(response.body.message).to.equal('password is too short')
  131. })
  132. it('should not update audit log', async function () {
  133. const auditLog = userHelper.getAuditLogWithoutNoise()
  134. expect(auditLog).to.deep.equal([])
  135. })
  136. })
  137. })
  138. })