PasswordResetControllerTests.js 16 KB

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