PasswordResetControllerTests.mjs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. import esmock from 'esmock'
  2. import sinon from 'sinon'
  3. import { expect } from 'chai'
  4. import MockResponse from '../helpers/MockResponse.js'
  5. const MODULE_PATH = new URL(
  6. '../../../../app/src/Features/PasswordReset/PasswordResetController.mjs',
  7. import.meta.url
  8. ).pathname
  9. describe('PasswordResetController', function () {
  10. beforeEach(async function () {
  11. this.email = 'bob@bob.com'
  12. this.user_id = 'mock-user-id'
  13. this.token = 'my security token that was emailed to me'
  14. this.password = 'my new password'
  15. this.req = {
  16. body: {
  17. email: this.email,
  18. passwordResetToken: this.token,
  19. password: this.password,
  20. },
  21. i18n: {
  22. translate() {
  23. return '.'
  24. },
  25. },
  26. session: {},
  27. query: {},
  28. }
  29. this.res = new MockResponse()
  30. this.settings = {}
  31. this.PasswordResetHandler = {
  32. generateAndEmailResetToken: sinon.stub(),
  33. promises: {
  34. generateAndEmailResetToken: sinon.stub(),
  35. setNewUserPassword: sinon.stub().resolves({
  36. found: true,
  37. reset: true,
  38. userID: this.user_id,
  39. mustReconfirm: true,
  40. }),
  41. getUserForPasswordResetToken: sinon
  42. .stub()
  43. .withArgs(this.token)
  44. .resolves({
  45. user: { _id: this.user_id },
  46. remainingPeeks: 1,
  47. }),
  48. },
  49. }
  50. this.UserSessionsManager = {
  51. promises: {
  52. removeSessionsFromRedis: sinon.stub().resolves(),
  53. },
  54. }
  55. this.UserUpdater = {
  56. promises: {
  57. removeReconfirmFlag: sinon.stub().resolves(),
  58. },
  59. }
  60. this.SplitTestHandler = {
  61. promises: {
  62. getAssignment: sinon.stub().resolves('default'),
  63. },
  64. }
  65. this.PasswordResetController = await esmock.strict(MODULE_PATH, {
  66. '@overleaf/settings': this.settings,
  67. '../../../../app/src/Features/PasswordReset/PasswordResetHandler':
  68. this.PasswordResetHandler,
  69. '../../../../app/src/Features/Authentication/AuthenticationManager': {
  70. validatePassword: sinon.stub().returns(null),
  71. },
  72. '../../../../app/src/Features/Authentication/AuthenticationController':
  73. (this.AuthenticationController = {
  74. getLoggedInUserId: sinon.stub(),
  75. finishLogin: sinon.stub(),
  76. setAuditInfo: sinon.stub(),
  77. }),
  78. '../../../../app/src/Features/User/UserGetter': (this.UserGetter = {
  79. promises: {
  80. getUser: sinon.stub(),
  81. },
  82. }),
  83. '../../../../app/src/Features/User/UserSessionsManager':
  84. this.UserSessionsManager,
  85. '../../../../app/src/Features/User/UserUpdater': this.UserUpdater,
  86. '../../../../app/src/Features/SplitTests/SplitTestHandler':
  87. this.SplitTestHandler,
  88. })
  89. })
  90. describe('requestReset', function () {
  91. it('should tell the handler to process that email', function (done) {
  92. this.PasswordResetHandler.promises.generateAndEmailResetToken.resolves(
  93. 'primary'
  94. )
  95. this.res.callback = () => {
  96. this.res.statusCode.should.equal(200)
  97. this.res.json.calledWith(sinon.match.has('message')).should.equal(true)
  98. expect(
  99. this.PasswordResetHandler.promises.generateAndEmailResetToken.lastCall
  100. .args[0]
  101. ).equal(this.email)
  102. done()
  103. }
  104. this.PasswordResetController.requestReset(this.req, this.res)
  105. })
  106. it('should send a 500 if there is an error', function (done) {
  107. this.PasswordResetHandler.promises.generateAndEmailResetToken.rejects(
  108. new Error('error')
  109. )
  110. this.PasswordResetController.requestReset(this.req, this.res, error => {
  111. expect(error).to.exist
  112. done()
  113. })
  114. })
  115. it("should send a 404 if the email doesn't exist", function (done) {
  116. this.PasswordResetHandler.promises.generateAndEmailResetToken.resolves(
  117. null
  118. )
  119. this.res.callback = () => {
  120. this.res.statusCode.should.equal(404)
  121. this.res.json.calledWith(sinon.match.has('message')).should.equal(true)
  122. done()
  123. }
  124. this.PasswordResetController.requestReset(this.req, this.res)
  125. })
  126. it('should send a 404 if the email is registered as a secondard email', function (done) {
  127. this.PasswordResetHandler.promises.generateAndEmailResetToken.resolves(
  128. 'secondary'
  129. )
  130. this.res.callback = () => {
  131. this.res.statusCode.should.equal(404)
  132. this.res.json.calledWith(sinon.match.has('message')).should.equal(true)
  133. done()
  134. }
  135. this.PasswordResetController.requestReset(this.req, this.res)
  136. })
  137. it('should normalize the email address', function (done) {
  138. this.email = ' UPperCaseEMAILWithSpacesAround@example.Com '
  139. this.req.body.email = this.email
  140. this.PasswordResetHandler.promises.generateAndEmailResetToken.resolves(
  141. 'primary'
  142. )
  143. this.res.callback = () => {
  144. this.res.statusCode.should.equal(200)
  145. this.res.json.calledWith(sinon.match.has('message')).should.equal(true)
  146. done()
  147. }
  148. this.PasswordResetController.requestReset(this.req, this.res)
  149. })
  150. })
  151. describe('setNewUserPassword', function () {
  152. beforeEach(function () {
  153. this.req.session.resetToken = this.token
  154. })
  155. it('should tell the user handler to reset the password', function (done) {
  156. this.res.sendStatus = code => {
  157. code.should.equal(200)
  158. this.PasswordResetHandler.promises.setNewUserPassword
  159. .calledWith(this.token, this.password)
  160. .should.equal(true)
  161. done()
  162. }
  163. this.PasswordResetController.setNewUserPassword(this.req, this.res)
  164. })
  165. it('should preserve spaces in the password', function (done) {
  166. this.password = this.req.body.password = ' oh! clever! spaces around! '
  167. this.res.sendStatus = code => {
  168. code.should.equal(200)
  169. this.PasswordResetHandler.promises.setNewUserPassword.should.have.been.calledWith(
  170. this.token,
  171. this.password
  172. )
  173. done()
  174. }
  175. this.PasswordResetController.setNewUserPassword(this.req, this.res)
  176. })
  177. it('should send 404 if the token was not found', function (done) {
  178. this.PasswordResetHandler.promises.setNewUserPassword.resolves({
  179. found: false,
  180. reset: false,
  181. userId: this.user_id,
  182. })
  183. this.res.status = code => {
  184. code.should.equal(404)
  185. return this.res
  186. }
  187. this.res.json = data => {
  188. data.message.key.should.equal('token-expired')
  189. done()
  190. }
  191. this.PasswordResetController.setNewUserPassword(this.req, this.res)
  192. })
  193. it('should return 500 if not reset', function (done) {
  194. this.PasswordResetHandler.promises.setNewUserPassword.resolves({
  195. found: true,
  196. reset: false,
  197. userId: this.user_id,
  198. })
  199. this.res.status = code => {
  200. code.should.equal(500)
  201. return this.res
  202. }
  203. this.res.json = data => {
  204. expect(data.message).to.exist
  205. done()
  206. }
  207. this.PasswordResetController.setNewUserPassword(this.req, this.res)
  208. })
  209. it('should return 400 (Bad Request) if there is no password', function (done) {
  210. this.req.body.password = ''
  211. this.res.status = code => {
  212. code.should.equal(400)
  213. return this.res
  214. }
  215. this.res.json = data => {
  216. data.message.key.should.equal('invalid-password')
  217. this.PasswordResetHandler.promises.setNewUserPassword.called.should.equal(
  218. false
  219. )
  220. done()
  221. }
  222. this.PasswordResetController.setNewUserPassword(this.req, this.res)
  223. })
  224. it('should return 400 (Bad Request) if there is no passwordResetToken', function (done) {
  225. this.req.body.passwordResetToken = ''
  226. this.res.status = code => {
  227. code.should.equal(400)
  228. return this.res
  229. }
  230. this.res.json = data => {
  231. data.message.key.should.equal('invalid-password')
  232. this.PasswordResetHandler.promises.setNewUserPassword.called.should.equal(
  233. false
  234. )
  235. done()
  236. }
  237. this.PasswordResetController.setNewUserPassword(this.req, this.res)
  238. })
  239. it('should return 400 (Bad Request) if the password is invalid', function (done) {
  240. this.req.body.password = 'correct horse battery staple'
  241. const err = new Error('bad')
  242. err.name = 'InvalidPasswordError'
  243. this.PasswordResetHandler.promises.setNewUserPassword.rejects(err)
  244. this.res.status = code => {
  245. code.should.equal(400)
  246. return this.res
  247. }
  248. this.res.json = data => {
  249. data.message.key.should.equal('invalid-password')
  250. this.PasswordResetHandler.promises.setNewUserPassword.called.should.equal(
  251. true
  252. )
  253. done()
  254. }
  255. this.PasswordResetController.setNewUserPassword(this.req, this.res)
  256. })
  257. it('should clear sessions', function (done) {
  258. this.res.sendStatus = code => {
  259. this.UserSessionsManager.promises.removeSessionsFromRedis.callCount.should.equal(
  260. 1
  261. )
  262. done()
  263. }
  264. this.PasswordResetController.setNewUserPassword(this.req, this.res)
  265. })
  266. it('should call removeReconfirmFlag if user.must_reconfirm', function (done) {
  267. this.res.sendStatus = code => {
  268. this.UserUpdater.promises.removeReconfirmFlag.callCount.should.equal(1)
  269. done()
  270. }
  271. this.PasswordResetController.setNewUserPassword(this.req, this.res)
  272. })
  273. describe('catch errors', function () {
  274. it('should return 404 for NotFoundError', function (done) {
  275. const anError = new Error('oops')
  276. anError.name = 'NotFoundError'
  277. this.PasswordResetHandler.promises.setNewUserPassword.rejects(anError)
  278. this.res.status = code => {
  279. code.should.equal(404)
  280. return this.res
  281. }
  282. this.res.json = data => {
  283. data.message.key.should.equal('token-expired')
  284. done()
  285. }
  286. this.PasswordResetController.setNewUserPassword(this.req, this.res)
  287. })
  288. it('should return 400 for InvalidPasswordError', function (done) {
  289. const anError = new Error('oops')
  290. anError.name = 'InvalidPasswordError'
  291. this.PasswordResetHandler.promises.setNewUserPassword.rejects(anError)
  292. this.res.status = code => {
  293. code.should.equal(400)
  294. return this.res
  295. }
  296. this.res.json = data => {
  297. data.message.key.should.equal('invalid-password')
  298. done()
  299. }
  300. this.PasswordResetController.setNewUserPassword(this.req, this.res)
  301. })
  302. it('should return 500 for other errors', function (done) {
  303. const anError = new Error('oops')
  304. this.PasswordResetHandler.promises.setNewUserPassword.rejects(anError)
  305. this.res.status = code => {
  306. code.should.equal(500)
  307. return this.res
  308. }
  309. this.res.json = data => {
  310. expect(data.message).to.exist
  311. done()
  312. }
  313. this.res.sendStatus = code => {
  314. code.should.equal(500)
  315. done()
  316. }
  317. this.PasswordResetController.setNewUserPassword(this.req, this.res)
  318. })
  319. })
  320. describe('when doLoginAfterPasswordReset is set', function () {
  321. beforeEach(function () {
  322. this.user = {
  323. _id: this.userId,
  324. email: 'joe@example.com',
  325. }
  326. this.UserGetter.promises.getUser.resolves(this.user)
  327. this.req.session.doLoginAfterPasswordReset = 'true'
  328. })
  329. it('should login user', function (done) {
  330. this.AuthenticationController.finishLogin.callsFake((...args) => {
  331. expect(args[0]).to.equal(this.user)
  332. done()
  333. })
  334. this.PasswordResetController.setNewUserPassword(this.req, this.res)
  335. })
  336. })
  337. })
  338. describe('renderSetPasswordForm', function () {
  339. describe('with token in query-string', function () {
  340. beforeEach(function () {
  341. this.req.query.passwordResetToken = this.token
  342. })
  343. it('should set session.resetToken and redirect', function (done) {
  344. this.req.session.should.not.have.property('resetToken')
  345. this.res.redirect = path => {
  346. path.should.equal('/user/password/set')
  347. this.req.session.resetToken.should.equal(this.token)
  348. done()
  349. }
  350. this.PasswordResetController.renderSetPasswordForm(this.req, this.res)
  351. })
  352. })
  353. describe('with expired token in query', function () {
  354. beforeEach(function () {
  355. this.req.query.passwordResetToken = this.token
  356. this.PasswordResetHandler.promises.getUserForPasswordResetToken = sinon
  357. .stub()
  358. .withArgs(this.token)
  359. .resolves({ user: { _id: this.user_id }, remainingPeeks: 0 })
  360. })
  361. it('should redirect to the reset request page with an error message', function (done) {
  362. this.res.redirect = path => {
  363. path.should.equal('/user/password/reset?error=token_expired')
  364. this.req.session.should.not.have.property('resetToken')
  365. done()
  366. }
  367. this.res.render = (templatePath, options) => {
  368. done('should not render')
  369. }
  370. this.PasswordResetController.renderSetPasswordForm(this.req, this.res)
  371. })
  372. })
  373. describe('with token and email in query-string', function () {
  374. beforeEach(function () {
  375. this.req.query.passwordResetToken = this.token
  376. this.req.query.email = 'foo@bar.com'
  377. })
  378. it('should set session.resetToken and redirect with email', function (done) {
  379. this.req.session.should.not.have.property('resetToken')
  380. this.res.redirect = path => {
  381. path.should.equal('/user/password/set?email=foo%40bar.com')
  382. this.req.session.resetToken.should.equal(this.token)
  383. done()
  384. }
  385. this.PasswordResetController.renderSetPasswordForm(this.req, this.res)
  386. })
  387. })
  388. describe('with token and invalid email in query-string', function () {
  389. beforeEach(function () {
  390. this.req.query.passwordResetToken = this.token
  391. this.req.query.email = 'not-an-email'
  392. })
  393. it('should set session.resetToken and redirect without email', function (done) {
  394. this.req.session.should.not.have.property('resetToken')
  395. this.res.redirect = path => {
  396. path.should.equal('/user/password/set')
  397. this.req.session.resetToken.should.equal(this.token)
  398. done()
  399. }
  400. this.PasswordResetController.renderSetPasswordForm(this.req, this.res)
  401. })
  402. })
  403. describe('with token and non-string email in query-string', function () {
  404. beforeEach(function () {
  405. this.req.query.passwordResetToken = this.token
  406. this.req.query.email = { foo: 'bar' }
  407. })
  408. it('should set session.resetToken and redirect without email', function (done) {
  409. this.req.session.should.not.have.property('resetToken')
  410. this.res.redirect = path => {
  411. path.should.equal('/user/password/set')
  412. this.req.session.resetToken.should.equal(this.token)
  413. done()
  414. }
  415. this.PasswordResetController.renderSetPasswordForm(this.req, this.res)
  416. })
  417. })
  418. describe('without a token in query-string', function () {
  419. describe('with token in session', function () {
  420. beforeEach(function () {
  421. this.req.session.resetToken = this.token
  422. })
  423. it('should render the page, passing the reset token', function (done) {
  424. this.res.render = (templatePath, options) => {
  425. options.passwordResetToken.should.equal(this.token)
  426. done()
  427. }
  428. this.PasswordResetController.renderSetPasswordForm(this.req, this.res)
  429. })
  430. it('should clear the req.session.resetToken', function (done) {
  431. this.res.render = (templatePath, options) => {
  432. this.req.session.should.not.have.property('resetToken')
  433. done()
  434. }
  435. this.PasswordResetController.renderSetPasswordForm(this.req, this.res)
  436. })
  437. })
  438. describe('without a token in session', function () {
  439. it('should redirect to the reset request page', function (done) {
  440. this.res.redirect = path => {
  441. path.should.equal('/user/password/reset')
  442. this.req.session.should.not.have.property('resetToken')
  443. done()
  444. }
  445. this.PasswordResetController.renderSetPasswordForm(this.req, this.res)
  446. })
  447. })
  448. })
  449. })
  450. })