PasswordResetControllerTests.js 13 KB

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