PasswordResetTests.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. const { expect } = require('chai')
  2. const UserHelper = require('./helpers/UserHelper')
  3. const { db } = require('../../../app/src/infrastructure/mongodb')
  4. describe('PasswordReset', function () {
  5. let email, response, user, userHelper, token, emailQuery
  6. beforeEach(async function () {
  7. userHelper = new UserHelper()
  8. email = userHelper.getDefaultEmail()
  9. emailQuery = `?email=${encodeURIComponent(email)}`
  10. userHelper = await UserHelper.createUser({ email })
  11. user = userHelper.user
  12. // generate the token
  13. await userHelper.getCsrfToken()
  14. response = await userHelper.fetch('/user/password/reset', {
  15. method: 'POST',
  16. body: new URLSearchParams({ email }),
  17. })
  18. token = (
  19. await db.tokens.findOne({
  20. 'data.user_id': user._id.toString(),
  21. })
  22. ).token
  23. })
  24. describe('with a valid token', function () {
  25. describe('when logged in', function () {
  26. beforeEach(async function () {
  27. userHelper = await UserHelper.loginUser({
  28. email,
  29. password: userHelper.getDefaultPassword(),
  30. })
  31. response = await userHelper.fetch(
  32. `/user/password/set?passwordResetToken=${token}&email=${email}`
  33. )
  34. expect(response.status).to.equal(302)
  35. expect(response.headers.get('location')).to.equal(
  36. UserHelper.url(`/user/password/set${emailQuery}`).toString()
  37. )
  38. // send reset request
  39. response = await userHelper.fetch('/user/password/set', {
  40. method: 'POST',
  41. body: new URLSearchParams({
  42. passwordResetToken: token,
  43. password: 'a-password',
  44. }),
  45. })
  46. userHelper = await UserHelper.getUser({ email })
  47. user = userHelper.user
  48. })
  49. it('update the password', async function () {
  50. expect(user.hashedPassword).to.exist
  51. expect(user.password).to.not.exist
  52. })
  53. it('log the change with initiatorId', async function () {
  54. const auditLog = userHelper.getAuditLogWithoutNoise()
  55. expect(auditLog).to.exist
  56. expect(auditLog[0]).to.exist
  57. expect(typeof auditLog[0].initiatorId).to.equal('object')
  58. expect(auditLog[0].initiatorId).to.deep.equal(user._id)
  59. expect(auditLog[0].operation).to.equal('reset-password')
  60. expect(auditLog[0].ipAddress).to.equal('127.0.0.1')
  61. expect(auditLog[0].timestamp).to.exist
  62. })
  63. })
  64. describe('when logged in as another user', function () {
  65. let otherUser, otherUserEmail
  66. beforeEach(async function () {
  67. otherUserEmail = userHelper.getDefaultEmail()
  68. userHelper = await UserHelper.createUser({ email: otherUserEmail })
  69. otherUser = userHelper.user
  70. userHelper = await UserHelper.loginUser({
  71. email: otherUserEmail,
  72. password: userHelper.getDefaultPassword(),
  73. })
  74. response = await userHelper.fetch(
  75. `/user/password/set?passwordResetToken=${token}&email=${email}`
  76. )
  77. expect(response.status).to.equal(302)
  78. expect(response.headers.get('location')).to.equal(
  79. UserHelper.url(`/user/password/set${emailQuery}`).toString()
  80. )
  81. // send reset request
  82. response = await userHelper.fetch('/user/password/set', {
  83. method: 'POST',
  84. body: new URLSearchParams({
  85. passwordResetToken: token,
  86. password: 'a-password',
  87. }),
  88. })
  89. userHelper = await UserHelper.getUser({ email })
  90. user = userHelper.user
  91. })
  92. it('update the password', async function () {
  93. expect(user.hashedPassword).to.exist
  94. expect(user.password).to.not.exist
  95. })
  96. it('log the change with the logged in user as the initiatorId', async function () {
  97. const auditLog = userHelper.getAuditLogWithoutNoise()
  98. expect(auditLog).to.exist
  99. expect(auditLog[0]).to.exist
  100. expect(typeof auditLog[0].initiatorId).to.equal('object')
  101. expect(auditLog[0].initiatorId).to.deep.equal(otherUser._id)
  102. expect(auditLog[0].operation).to.equal('reset-password')
  103. expect(auditLog[0].ipAddress).to.equal('127.0.0.1')
  104. expect(auditLog[0].timestamp).to.exist
  105. })
  106. })
  107. describe('when not logged in', function () {
  108. beforeEach(async function () {
  109. response = await userHelper.fetch(
  110. `/user/password/set?passwordResetToken=${token}&email=${email}`
  111. )
  112. expect(response.status).to.equal(302)
  113. expect(response.headers.get('location')).to.equal(
  114. UserHelper.url(`/user/password/set${emailQuery}`).toString()
  115. )
  116. // send reset request
  117. response = await userHelper.fetch('/user/password/set', {
  118. method: 'POST',
  119. body: new URLSearchParams({
  120. passwordResetToken: token,
  121. password: 'a-password',
  122. }),
  123. })
  124. userHelper = await UserHelper.getUser({ email })
  125. user = userHelper.user
  126. })
  127. it('updates the password', function () {
  128. expect(user.hashedPassword).to.exist
  129. expect(user.password).to.not.exist
  130. })
  131. it('log the change', async function () {
  132. const auditLog = userHelper.getAuditLogWithoutNoise()
  133. expect(auditLog).to.exist
  134. expect(auditLog[0]).to.exist
  135. expect(auditLog[0].initiatorId).to.equal(null)
  136. expect(auditLog[0].operation).to.equal('reset-password')
  137. expect(auditLog[0].ipAddress).to.equal('127.0.0.1')
  138. expect(auditLog[0].timestamp).to.exist
  139. })
  140. })
  141. describe('password checks', function () {
  142. beforeEach(async function () {
  143. response = await userHelper.fetch(
  144. `/user/password/set?passwordResetToken=${token}&email=${email}`
  145. )
  146. expect(response.status).to.equal(302)
  147. expect(response.headers.get('location')).to.equal(
  148. UserHelper.url(`/user/password/set${emailQuery}`).toString()
  149. )
  150. })
  151. it('without a password should return 400 and not log the change', async function () {
  152. // send reset request
  153. response = await userHelper.fetch('/user/password/set', {
  154. method: 'POST',
  155. body: new URLSearchParams({
  156. passwordResetToken: token,
  157. }),
  158. })
  159. expect(response.status).to.equal(400)
  160. userHelper = await UserHelper.getUser({ email })
  161. const auditLog = userHelper.getAuditLogWithoutNoise()
  162. expect(auditLog).to.deep.equal([])
  163. })
  164. it('without a valid password should return 400 and not log the change', async function () {
  165. // send reset request
  166. response = await userHelper.fetch('/user/password/set', {
  167. method: 'POST',
  168. body: new URLSearchParams({
  169. passwordResetToken: token,
  170. password: 'short',
  171. }),
  172. })
  173. expect(response.status).to.equal(400)
  174. userHelper = await UserHelper.getUser({ email })
  175. const auditLog = userHelper.getAuditLogWithoutNoise()
  176. expect(auditLog).to.deep.equal([])
  177. })
  178. it('should flag email in password', async function () {
  179. const localPart = email.split('@').shift()
  180. // send bad password
  181. response = await userHelper.fetch('/user/password/set', {
  182. method: 'POST',
  183. headers: {
  184. 'Content-Type': 'application/json',
  185. Accept: 'application/json',
  186. },
  187. body: JSON.stringify({
  188. passwordResetToken: token,
  189. password: localPart,
  190. email,
  191. }),
  192. })
  193. expect(response.status).to.equal(400)
  194. const body = await response.json()
  195. expect(body).to.deep.equal({
  196. message: { text: 'Password cannot contain parts of email address' },
  197. })
  198. })
  199. it('should be able to retry after providing an invalid password', async function () {
  200. // send bad password
  201. response = await userHelper.fetch('/user/password/set', {
  202. method: 'POST',
  203. body: new URLSearchParams({
  204. passwordResetToken: token,
  205. password: 'short',
  206. }),
  207. })
  208. expect(response.status).to.equal(400)
  209. // send good password
  210. response = await userHelper.fetch('/user/password/set', {
  211. method: 'POST',
  212. body: new URLSearchParams({
  213. passwordResetToken: token,
  214. password: 'SomeThingVeryStrong!11',
  215. }),
  216. })
  217. expect(response.status).to.equal(200)
  218. userHelper = await UserHelper.getUser({ email })
  219. const auditLog = userHelper.getAuditLogWithoutNoise()
  220. expect(auditLog.length).to.equal(1)
  221. })
  222. it('when the password is the same as current, should return 400 and log the change', async function () {
  223. // send reset request
  224. response = await userHelper.fetch('/user/password/set', {
  225. method: 'POST',
  226. body: new URLSearchParams({
  227. passwordResetToken: token,
  228. password: userHelper.getDefaultPassword(),
  229. }),
  230. })
  231. expect(response.status).to.equal(400)
  232. const body = await response.json()
  233. expect(body.message.key).to.equal('password-must-be-different')
  234. userHelper = await UserHelper.getUser({ email })
  235. const auditLog = userHelper.getAuditLogWithoutNoise()
  236. expect(auditLog.length).to.equal(1)
  237. })
  238. })
  239. })
  240. describe('multiple attempts to set the password, reaching attempt limit', async function () {
  241. beforeEach(async function () {
  242. response = await userHelper.fetch(
  243. `/user/password/set?passwordResetToken=${token}&email=${email}`
  244. )
  245. expect(response.status).to.equal(302)
  246. expect(response.headers.get('location')).to.equal(
  247. UserHelper.url(`/user/password/set${emailQuery}`).toString()
  248. )
  249. })
  250. it('should allow multiple attempts with same-password error, then deny further attempts', async function () {
  251. const sendSamePasswordRequest = async function () {
  252. return userHelper.fetch('/user/password/set', {
  253. method: 'POST',
  254. headers: {
  255. Accept: 'application/json',
  256. },
  257. body: new URLSearchParams({
  258. passwordResetToken: token,
  259. password: userHelper.getDefaultPassword(),
  260. }),
  261. })
  262. }
  263. // Three attempts at setting the password, all rejected for being the same as
  264. // the current password
  265. const response1 = await sendSamePasswordRequest()
  266. expect(response1.status).to.equal(400)
  267. const body1 = await response1.json()
  268. expect(body1.message.key).to.equal('password-must-be-different')
  269. const response2 = await sendSamePasswordRequest()
  270. expect(response2.status).to.equal(400)
  271. const body2 = await response2.json()
  272. expect(body2.message.key).to.equal('password-must-be-different')
  273. const response3 = await sendSamePasswordRequest()
  274. expect(response3.status).to.equal(400)
  275. const body3 = await response3.json()
  276. expect(body3.message.key).to.equal('password-must-be-different')
  277. // Fourth attempt is rejected because the token has been used too many times
  278. const response4 = await sendSamePasswordRequest()
  279. expect(response4.status).to.equal(404)
  280. const body4 = await response4.json()
  281. expect(body4.message.key).to.equal('token-expired')
  282. })
  283. it('should allow multiple attempts with same-password error, then set the password', async function () {
  284. const sendSamePasswordRequest = async function () {
  285. return userHelper.fetch('/user/password/set', {
  286. method: 'POST',
  287. headers: {
  288. Accept: 'application/json',
  289. },
  290. body: new URLSearchParams({
  291. passwordResetToken: token,
  292. password: userHelper.getDefaultPassword(),
  293. }),
  294. })
  295. }
  296. // Two attempts at setting the password, all rejected for being the same as
  297. // the current password
  298. const response1 = await sendSamePasswordRequest()
  299. expect(response1.status).to.equal(400)
  300. const body1 = await response1.json()
  301. expect(body1.message.key).to.equal('password-must-be-different')
  302. const response2 = await sendSamePasswordRequest()
  303. expect(response2.status).to.equal(400)
  304. const body2 = await response2.json()
  305. expect(body2.message.key).to.equal('password-must-be-different')
  306. // Third attempt is succeeds
  307. const response3 = await userHelper.fetch('/user/password/set', {
  308. method: 'POST',
  309. body: new URLSearchParams({
  310. passwordResetToken: token,
  311. password: 'some-new-password',
  312. }),
  313. })
  314. expect(response3.status).to.equal(200)
  315. // Check the user and audit log
  316. userHelper = await UserHelper.getUser({ email })
  317. user = userHelper.user
  318. expect(user.hashedPassword).to.exist
  319. expect(user.password).to.not.exist
  320. const auditLog = userHelper.getAuditLogWithoutNoise()
  321. expect(auditLog).to.exist
  322. expect(auditLog[0]).to.exist
  323. expect(auditLog[0].initiatorId).to.equal(null)
  324. expect(auditLog[0].operation).to.equal('reset-password')
  325. expect(auditLog[0].ipAddress).to.equal('127.0.0.1')
  326. expect(auditLog[0].timestamp).to.exist
  327. })
  328. })
  329. describe('without a valid token', function () {
  330. it('no token should redirect to page to re-request reset token', async function () {
  331. response = await userHelper.fetch(`/user/password/set?&email=${email}`)
  332. expect(response.status).to.equal(302)
  333. expect(response.headers.get('location')).to.equal(
  334. UserHelper.url('/user/password/reset').toString()
  335. )
  336. })
  337. it('should show error for invalid tokens and return 404 if used', async function () {
  338. const invalidToken = 'not-real-token'
  339. response = await userHelper.fetch(
  340. `/user/password/set?&passwordResetToken=${invalidToken}&email=${email}`
  341. )
  342. expect(response.status).to.equal(302)
  343. expect(response.headers.get('location')).to.equal(
  344. UserHelper.url('/user/password/reset?error=token_expired').toString()
  345. )
  346. // send reset request
  347. response = await userHelper.fetch('/user/password/set', {
  348. method: 'POST',
  349. body: new URLSearchParams({
  350. passwordResetToken: invalidToken,
  351. password: 'a-password',
  352. }),
  353. })
  354. expect(response.status).to.equal(404)
  355. })
  356. })
  357. describe('password reset', function () {
  358. it('should return 200 if email field is valid', async function () {
  359. response = await userHelper.fetch(`/user/password/reset`, {
  360. method: 'POST',
  361. body: new URLSearchParams({ email }),
  362. })
  363. expect(response.status).to.equal(200)
  364. })
  365. it('should return 400 if email field is missing', async function () {
  366. response = await userHelper.fetch(`/user/password/reset`, {
  367. method: 'POST',
  368. body: new URLSearchParams({ mail: email }),
  369. })
  370. expect(response.status).to.equal(400)
  371. })
  372. })
  373. describe('password set', function () {
  374. it('should return 200 if password and passwordResetToken fields are valid', async function () {
  375. response = await userHelper.fetch(`/user/password/set`, {
  376. method: 'POST',
  377. body: new URLSearchParams({
  378. password: 'new-password',
  379. passwordResetToken: token,
  380. }),
  381. })
  382. expect(response.status).to.equal(200)
  383. })
  384. it('should return 400 if password field is missing', async function () {
  385. response = await userHelper.fetch(`/user/password/set`, {
  386. method: 'POST',
  387. body: new URLSearchParams({
  388. passwordResetToken: token,
  389. }),
  390. })
  391. expect(response.status).to.equal(400)
  392. })
  393. it('should return 400 if passwordResetToken field is missing', async function () {
  394. response = await userHelper.fetch(`/user/password/set`, {
  395. method: 'POST',
  396. body: new URLSearchParams({
  397. password: 'new-password',
  398. }),
  399. })
  400. expect(response.status).to.equal(400)
  401. })
  402. })
  403. })