PasswordResetControllerTests.js 15 KB

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