PasswordResetTests.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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 = 'somecooluser@example.com'
  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: {
  197. type: 'error',
  198. key: 'password-contains-email',
  199. text: 'Password cannot contain parts of email address',
  200. },
  201. })
  202. })
  203. it('should flag password too similar to email', async function () {
  204. const localPart = email.split('@').shift()
  205. const localPartReversed = localPart.split('').reverse().join('')
  206. // send bad password
  207. response = await userHelper.fetch('/user/password/set', {
  208. method: 'POST',
  209. headers: {
  210. 'Content-Type': 'application/json',
  211. Accept: 'application/json',
  212. },
  213. body: JSON.stringify({
  214. passwordResetToken: token,
  215. password: `${localPartReversed}123`,
  216. email,
  217. }),
  218. })
  219. expect(response.status).to.equal(400)
  220. const body = await response.json()
  221. expect(body).to.deep.equal({
  222. message: {
  223. type: 'error',
  224. key: 'password-too-similar',
  225. text: 'Password is too similar to parts of email address',
  226. },
  227. })
  228. })
  229. it('should be able to retry after providing an invalid password', async function () {
  230. // send bad password
  231. response = await userHelper.fetch('/user/password/set', {
  232. method: 'POST',
  233. body: new URLSearchParams({
  234. passwordResetToken: token,
  235. password: 'short',
  236. }),
  237. })
  238. expect(response.status).to.equal(400)
  239. // send good password
  240. response = await userHelper.fetch('/user/password/set', {
  241. method: 'POST',
  242. body: new URLSearchParams({
  243. passwordResetToken: token,
  244. password: 'SomeThingVeryStrong!11',
  245. }),
  246. })
  247. expect(response.status).to.equal(200)
  248. userHelper = await UserHelper.getUser({ email })
  249. const auditLog = userHelper.getAuditLogWithoutNoise()
  250. expect(auditLog.length).to.equal(1)
  251. })
  252. it('when the password is the same as current, should return 400 and log the change', async function () {
  253. // send reset request
  254. response = await userHelper.fetch('/user/password/set', {
  255. method: 'POST',
  256. body: new URLSearchParams({
  257. passwordResetToken: token,
  258. password: userHelper.getDefaultPassword(),
  259. }),
  260. })
  261. expect(response.status).to.equal(400)
  262. const body = await response.json()
  263. expect(body.message.key).to.equal('password-must-be-different')
  264. userHelper = await UserHelper.getUser({ email })
  265. const auditLog = userHelper.getAuditLogWithoutNoise()
  266. expect(auditLog.length).to.equal(1)
  267. })
  268. })
  269. })
  270. describe('multiple attempts to set the password, reaching attempt limit', async function () {
  271. beforeEach(async function () {
  272. response = await userHelper.fetch(
  273. `/user/password/set?passwordResetToken=${token}&email=${email}`
  274. )
  275. expect(response.status).to.equal(302)
  276. expect(response.headers.get('location')).to.equal(
  277. UserHelper.url(`/user/password/set${emailQuery}`).toString()
  278. )
  279. })
  280. it('should allow multiple attempts with same-password error, then deny further attempts', async function () {
  281. const sendSamePasswordRequest = async function () {
  282. return userHelper.fetch('/user/password/set', {
  283. method: 'POST',
  284. headers: {
  285. Accept: 'application/json',
  286. },
  287. body: new URLSearchParams({
  288. passwordResetToken: token,
  289. password: userHelper.getDefaultPassword(),
  290. }),
  291. })
  292. }
  293. // Three attempts at setting the password, all rejected for being the same as
  294. // the current password
  295. const response1 = await sendSamePasswordRequest()
  296. expect(response1.status).to.equal(400)
  297. const body1 = await response1.json()
  298. expect(body1.message.key).to.equal('password-must-be-different')
  299. const response2 = await sendSamePasswordRequest()
  300. expect(response2.status).to.equal(400)
  301. const body2 = await response2.json()
  302. expect(body2.message.key).to.equal('password-must-be-different')
  303. const response3 = await sendSamePasswordRequest()
  304. expect(response3.status).to.equal(400)
  305. const body3 = await response3.json()
  306. expect(body3.message.key).to.equal('password-must-be-different')
  307. // Fourth attempt is rejected because the token has been used too many times
  308. const response4 = await sendSamePasswordRequest()
  309. expect(response4.status).to.equal(404)
  310. const body4 = await response4.json()
  311. expect(body4.message.key).to.equal('token-expired')
  312. })
  313. it('should allow multiple attempts with same-password error, then set the password', async function () {
  314. const sendSamePasswordRequest = async function () {
  315. return userHelper.fetch('/user/password/set', {
  316. method: 'POST',
  317. headers: {
  318. Accept: 'application/json',
  319. },
  320. body: new URLSearchParams({
  321. passwordResetToken: token,
  322. password: userHelper.getDefaultPassword(),
  323. }),
  324. })
  325. }
  326. // Two attempts at setting the password, all rejected for being the same as
  327. // the current password
  328. const response1 = await sendSamePasswordRequest()
  329. expect(response1.status).to.equal(400)
  330. const body1 = await response1.json()
  331. expect(body1.message.key).to.equal('password-must-be-different')
  332. const response2 = await sendSamePasswordRequest()
  333. expect(response2.status).to.equal(400)
  334. const body2 = await response2.json()
  335. expect(body2.message.key).to.equal('password-must-be-different')
  336. // Third attempt is succeeds
  337. const response3 = await userHelper.fetch('/user/password/set', {
  338. method: 'POST',
  339. body: new URLSearchParams({
  340. passwordResetToken: token,
  341. password: 'some-new-password',
  342. }),
  343. })
  344. expect(response3.status).to.equal(200)
  345. // Check the user and audit log
  346. userHelper = await UserHelper.getUser({ email })
  347. user = userHelper.user
  348. expect(user.hashedPassword).to.exist
  349. expect(user.password).to.not.exist
  350. const auditLog = userHelper.getAuditLogWithoutNoise()
  351. expect(auditLog).to.exist
  352. expect(auditLog[0]).to.exist
  353. expect(auditLog[0].initiatorId).to.equal(null)
  354. expect(auditLog[0].operation).to.equal('reset-password')
  355. expect(auditLog[0].ipAddress).to.equal('127.0.0.1')
  356. expect(auditLog[0].timestamp).to.exist
  357. })
  358. })
  359. describe('without a valid token', function () {
  360. it('no token should redirect to page to re-request reset token', async function () {
  361. response = await userHelper.fetch(`/user/password/set?&email=${email}`)
  362. expect(response.status).to.equal(302)
  363. expect(response.headers.get('location')).to.equal(
  364. UserHelper.url('/user/password/reset').toString()
  365. )
  366. })
  367. it('should show error for invalid tokens and return 404 if used', async function () {
  368. const invalidToken = 'not-real-token'
  369. response = await userHelper.fetch(
  370. `/user/password/set?&passwordResetToken=${invalidToken}&email=${email}`
  371. )
  372. expect(response.status).to.equal(302)
  373. expect(response.headers.get('location')).to.equal(
  374. UserHelper.url('/user/password/reset?error=token_expired').toString()
  375. )
  376. // send reset request
  377. response = await userHelper.fetch('/user/password/set', {
  378. method: 'POST',
  379. body: new URLSearchParams({
  380. passwordResetToken: invalidToken,
  381. password: 'a-password',
  382. }),
  383. })
  384. expect(response.status).to.equal(404)
  385. })
  386. })
  387. describe('password reset', function () {
  388. it('should return 200 if email field is valid', async function () {
  389. response = await userHelper.fetch(`/user/password/reset`, {
  390. method: 'POST',
  391. body: new URLSearchParams({ email }),
  392. })
  393. expect(response.status).to.equal(200)
  394. })
  395. it('should return 400 if email field is missing', async function () {
  396. response = await userHelper.fetch(`/user/password/reset`, {
  397. method: 'POST',
  398. body: new URLSearchParams({ mail: email }),
  399. })
  400. expect(response.status).to.equal(400)
  401. })
  402. })
  403. describe('password set', function () {
  404. it('should return 200 if password and passwordResetToken fields are valid', async function () {
  405. response = await userHelper.fetch(`/user/password/set`, {
  406. method: 'POST',
  407. body: new URLSearchParams({
  408. password: 'new-password',
  409. passwordResetToken: token,
  410. }),
  411. })
  412. expect(response.status).to.equal(200)
  413. })
  414. it('should return 400 if password field is missing', async function () {
  415. response = await userHelper.fetch(`/user/password/set`, {
  416. method: 'POST',
  417. body: new URLSearchParams({
  418. passwordResetToken: token,
  419. }),
  420. })
  421. expect(response.status).to.equal(400)
  422. })
  423. it('should return 400 if passwordResetToken field is missing', async function () {
  424. response = await userHelper.fetch(`/user/password/set`, {
  425. method: 'POST',
  426. body: new URLSearchParams({
  427. password: 'new-password',
  428. }),
  429. })
  430. expect(response.status).to.equal(400)
  431. })
  432. })
  433. })