PasswordResetTests.js 17 KB

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