PasswordResetControllerTests.js 16 KB

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