PasswordResetControllerTests.js 15 KB

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