PasswordUpdateTests.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. user = (await UserHelper.getUser({ email })).user
  34. })
  35. it('should return 200', async function() {
  36. expect(response.statusCode).to.equal(200)
  37. })
  38. it('should update the audit log', function() {
  39. expect(user.auditLog[0]).to.exist
  40. expect(typeof user.auditLog[0].initiatorId).to.equal('object')
  41. expect(user.auditLog[0].initiatorId).to.deep.equal(user._id)
  42. expect(user.auditLog[0].operation).to.equal('update-password')
  43. expect(user.auditLog[0].ipAddress).to.equal('127.0.0.1')
  44. expect(user.auditLog[0].timestamp).to.exist
  45. })
  46. })
  47. describe('errors', function() {
  48. describe('missing current password', function() {
  49. beforeEach(async function() {
  50. response = await userHelper.request.post('/user/password/update', {
  51. form: {
  52. newPassword1: 'new-password',
  53. newPassword2: 'new-password'
  54. },
  55. simple: false
  56. })
  57. user = (await UserHelper.getUser({ email })).user
  58. })
  59. it('should return 500', async function() {
  60. expect(response.statusCode).to.equal(500)
  61. })
  62. it('should not update audit log', async function() {
  63. expect(user.auditLog[0]).to.not.exist
  64. })
  65. })
  66. describe('wrong current password', function() {
  67. beforeEach(async function() {
  68. response = await userHelper.request.post('/user/password/update', {
  69. form: {
  70. currentPassword: 'wrong-password',
  71. newPassword1: 'new-password',
  72. newPassword2: 'new-password'
  73. },
  74. simple: false
  75. })
  76. user = (await UserHelper.getUser({ email })).user
  77. })
  78. it('should return 400', async function() {
  79. expect(response.statusCode).to.equal(400)
  80. })
  81. it('should not update audit log', async function() {
  82. expect(user.auditLog[0]).to.not.exist
  83. })
  84. })
  85. describe('newPassword1 does not match newPassword2', function() {
  86. beforeEach(async function() {
  87. response = await userHelper.request.post('/user/password/update', {
  88. form: {
  89. currentPassword: password,
  90. newPassword1: 'new-password',
  91. newPassword2: 'oops-password'
  92. },
  93. json: true,
  94. simple: false
  95. })
  96. user = (await UserHelper.getUser({ email })).user
  97. })
  98. it('should return 400', async function() {
  99. expect(response.statusCode).to.equal(400)
  100. })
  101. it('should return error message', async function() {
  102. expect(response.body.message).to.equal('Passwords do not match')
  103. })
  104. it('should not update audit log', async function() {
  105. expect(user.auditLog[0]).to.not.exist
  106. })
  107. })
  108. describe('new password is not valid', function() {
  109. beforeEach(async function() {
  110. response = await userHelper.request.post('/user/password/update', {
  111. form: {
  112. currentPassword: password,
  113. newPassword1: 'short',
  114. newPassword2: 'short'
  115. },
  116. json: true,
  117. simple: false
  118. })
  119. user = (await UserHelper.getUser({ email })).user
  120. })
  121. it('should return 400', async function() {
  122. expect(response.statusCode).to.equal(400)
  123. })
  124. it('should return error message', async function() {
  125. expect(response.body.message).to.equal('password is too short')
  126. })
  127. it('should not update audit log', async function() {
  128. expect(user.auditLog[0]).to.not.exist
  129. })
  130. })
  131. })
  132. })