PasswordResetHandlerTests.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. /* eslint-disable
  2. n/handle-callback-err,
  3. max-len,
  4. no-return-assign,
  5. no-unused-vars,
  6. */
  7. // TODO: This file was created by bulk-decaffeinate.
  8. // Fix any style issues and re-enable lint.
  9. /*
  10. * decaffeinate suggestions:
  11. * DS102: Remove unnecessary code created because of implicit returns
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. const SandboxedModule = require('sandboxed-module')
  15. const assert = require('assert')
  16. const path = require('path')
  17. const sinon = require('sinon')
  18. const { expect } = require('chai')
  19. const modulePath = path.join(
  20. __dirname,
  21. '../../../../app/src/Features/PasswordReset/PasswordResetHandler'
  22. )
  23. describe('PasswordResetHandler', function () {
  24. beforeEach(function () {
  25. this.settings = { siteUrl: 'https://www.overleaf.com' }
  26. this.OneTimeTokenHandler = {
  27. getNewToken: sinon.stub(),
  28. peekValueFromToken: sinon.stub(),
  29. expireToken: sinon.stub(),
  30. }
  31. this.UserGetter = {
  32. getUserByMainEmail: sinon.stub(),
  33. getUser: sinon.stub(),
  34. getUserByAnyEmail: sinon.stub(),
  35. }
  36. this.EmailHandler = { sendEmail: sinon.stub() }
  37. this.AuthenticationManager = {
  38. setUserPasswordInV2: sinon.stub(),
  39. promises: {
  40. setUserPassword: sinon.stub().resolves(),
  41. },
  42. }
  43. this.PasswordResetHandler = SandboxedModule.require(modulePath, {
  44. requires: {
  45. '../User/UserAuditLogHandler': (this.UserAuditLogHandler = {
  46. promises: {
  47. addEntry: sinon.stub().resolves(),
  48. },
  49. }),
  50. '../User/UserGetter': this.UserGetter,
  51. '../Security/OneTimeTokenHandler': this.OneTimeTokenHandler,
  52. '../Email/EmailHandler': this.EmailHandler,
  53. '../Authentication/AuthenticationManager': this.AuthenticationManager,
  54. '@overleaf/settings': this.settings,
  55. },
  56. })
  57. this.token = '12312321i'
  58. this.user_id = 'user_id_here'
  59. this.user = { email: (this.email = 'bob@bob.com'), _id: this.user_id }
  60. this.password = 'my great secret password'
  61. this.callback = sinon.stub()
  62. // this should not have any effect now
  63. this.settings.overleaf = true
  64. })
  65. afterEach(function () {
  66. this.settings.overleaf = false
  67. })
  68. describe('generateAndEmailResetToken', function () {
  69. it('should check the user exists', function () {
  70. this.UserGetter.getUserByAnyEmail.yields()
  71. this.PasswordResetHandler.generateAndEmailResetToken(
  72. this.user.email,
  73. this.callback
  74. )
  75. this.UserGetter.getUserByAnyEmail.should.have.been.calledWith(
  76. this.user.email
  77. )
  78. })
  79. it('should send the email with the token', function (done) {
  80. this.UserGetter.getUserByAnyEmail.yields(null, this.user)
  81. this.OneTimeTokenHandler.getNewToken.yields(null, this.token)
  82. this.EmailHandler.sendEmail.yields()
  83. this.PasswordResetHandler.generateAndEmailResetToken(
  84. this.user.email,
  85. (err, status) => {
  86. this.EmailHandler.sendEmail.called.should.equal(true)
  87. status.should.equal('primary')
  88. const args = this.EmailHandler.sendEmail.args[0]
  89. args[0].should.equal('passwordResetRequested')
  90. args[1].setNewPasswordUrl.should.equal(
  91. `${this.settings.siteUrl}/user/password/set?passwordResetToken=${
  92. this.token
  93. }&email=${encodeURIComponent(this.user.email)}`
  94. )
  95. done()
  96. }
  97. )
  98. })
  99. describe('when the email exists', function () {
  100. beforeEach(function () {
  101. this.UserGetter.getUserByAnyEmail.yields(null, this.user)
  102. this.OneTimeTokenHandler.getNewToken.yields(null, this.token)
  103. this.EmailHandler.sendEmail.yields()
  104. this.PasswordResetHandler.generateAndEmailResetToken(
  105. this.email,
  106. this.callback
  107. )
  108. })
  109. it('should set the password token data to the user id and email', function () {
  110. this.OneTimeTokenHandler.getNewToken.should.have.been.calledWith(
  111. 'password',
  112. {
  113. email: this.email,
  114. user_id: this.user._id,
  115. }
  116. )
  117. })
  118. it('should send an email with the token', function () {
  119. this.EmailHandler.sendEmail.called.should.equal(true)
  120. const args = this.EmailHandler.sendEmail.args[0]
  121. args[0].should.equal('passwordResetRequested')
  122. args[1].setNewPasswordUrl.should.equal(
  123. `${this.settings.siteUrl}/user/password/set?passwordResetToken=${
  124. this.token
  125. }&email=${encodeURIComponent(this.user.email)}`
  126. )
  127. })
  128. it('should return status == true', function () {
  129. this.callback.calledWith(null, 'primary').should.equal(true)
  130. })
  131. })
  132. describe("when the email doesn't exist", function () {
  133. beforeEach(function () {
  134. this.UserGetter.getUserByAnyEmail.yields(null, null)
  135. this.PasswordResetHandler.generateAndEmailResetToken(
  136. this.email,
  137. this.callback
  138. )
  139. })
  140. it('should not set the password token data', function () {
  141. this.OneTimeTokenHandler.getNewToken.called.should.equal(false)
  142. })
  143. it('should send an email with the token', function () {
  144. this.EmailHandler.sendEmail.called.should.equal(false)
  145. })
  146. it('should return status == null', function () {
  147. this.callback.calledWith(null, null).should.equal(true)
  148. })
  149. })
  150. describe('when the email is a secondary email', function () {
  151. beforeEach(function () {
  152. this.UserGetter.getUserByAnyEmail.callsArgWith(1, null, this.user)
  153. this.PasswordResetHandler.generateAndEmailResetToken(
  154. 'secondary@email.com',
  155. this.callback
  156. )
  157. })
  158. it('should not set the password token data', function () {
  159. this.OneTimeTokenHandler.getNewToken.called.should.equal(false)
  160. })
  161. it('should not send an email with the token', function () {
  162. this.EmailHandler.sendEmail.called.should.equal(false)
  163. })
  164. it('should return status == secondary', function () {
  165. this.callback.calledWith(null, 'secondary').should.equal(true)
  166. })
  167. })
  168. })
  169. describe('setNewUserPassword', function () {
  170. beforeEach(function () {
  171. this.auditLog = { ip: '0:0:0:0' }
  172. })
  173. describe('when no data is found', function () {
  174. beforeEach(function () {
  175. this.OneTimeTokenHandler.peekValueFromToken.yields(null, null)
  176. })
  177. it('should return found == false and reset == false', function () {
  178. this.PasswordResetHandler.setNewUserPassword(
  179. this.token,
  180. this.password,
  181. this.auditLog,
  182. (error, result) => {
  183. expect(error).to.not.exist
  184. expect(result).to.deep.equal({
  185. found: false,
  186. reset: false,
  187. userId: null,
  188. })
  189. }
  190. )
  191. })
  192. })
  193. describe('when the token has a user_id and email', function () {
  194. beforeEach(function () {
  195. this.OneTimeTokenHandler.peekValueFromToken
  196. .withArgs('password', this.token)
  197. .yields(null, {
  198. user_id: this.user._id,
  199. email: this.email,
  200. })
  201. this.AuthenticationManager.promises.setUserPassword
  202. .withArgs(this.user, this.password)
  203. .resolves(true)
  204. this.OneTimeTokenHandler.expireToken = sinon
  205. .stub()
  206. .callsArgWith(2, null)
  207. })
  208. describe('when no user is found with this email', function () {
  209. beforeEach(function () {
  210. this.UserGetter.getUserByMainEmail
  211. .withArgs(this.email)
  212. .yields(null, null)
  213. })
  214. it('should return found == false and reset == false', function (done) {
  215. this.PasswordResetHandler.setNewUserPassword(
  216. this.token,
  217. this.password,
  218. this.auditLog,
  219. (err, result) => {
  220. const { found, reset, userId } = result
  221. expect(err).to.not.exist
  222. expect(found).to.be.false
  223. expect(reset).to.be.false
  224. expect(this.OneTimeTokenHandler.expireToken.callCount).to.equal(0)
  225. done()
  226. }
  227. )
  228. })
  229. })
  230. describe("when the email and user don't match", function () {
  231. beforeEach(function () {
  232. this.UserGetter.getUserByMainEmail
  233. .withArgs(this.email)
  234. .yields(null, { _id: 'not-the-same', email: this.email })
  235. this.OneTimeTokenHandler.expireToken.callsArgWith(2, null)
  236. })
  237. it('should return found == false and reset == false', function (done) {
  238. this.PasswordResetHandler.setNewUserPassword(
  239. this.token,
  240. this.password,
  241. this.auditLog,
  242. (err, result) => {
  243. const { found, reset, userId } = result
  244. expect(err).to.not.exist
  245. expect(found).to.be.false
  246. expect(reset).to.be.false
  247. expect(this.OneTimeTokenHandler.expireToken.callCount).to.equal(0)
  248. done()
  249. }
  250. )
  251. })
  252. })
  253. describe('when the email and user match', function () {
  254. describe('success', function () {
  255. beforeEach(function () {
  256. this.UserGetter.getUserByMainEmail.yields(null, this.user)
  257. this.OneTimeTokenHandler.expireToken = sinon
  258. .stub()
  259. .callsArgWith(2, null)
  260. })
  261. it('should update the user audit log', function (done) {
  262. this.PasswordResetHandler.setNewUserPassword(
  263. this.token,
  264. this.password,
  265. this.auditLog,
  266. (error, result) => {
  267. const { reset, userId } = result
  268. expect(error).to.not.exist
  269. const logCall =
  270. this.UserAuditLogHandler.promises.addEntry.lastCall
  271. expect(logCall.args[0]).to.equal(this.user_id)
  272. expect(logCall.args[1]).to.equal('reset-password')
  273. expect(logCall.args[2]).to.equal(undefined)
  274. expect(logCall.args[3]).to.equal(this.auditLog.ip)
  275. expect(logCall.args[4]).to.equal(undefined)
  276. done()
  277. }
  278. )
  279. })
  280. it('should return reset == true and the user id', function (done) {
  281. this.PasswordResetHandler.setNewUserPassword(
  282. this.token,
  283. this.password,
  284. this.auditLog,
  285. (err, result) => {
  286. const { reset, userId } = result
  287. expect(err).to.not.exist
  288. expect(reset).to.be.true
  289. expect(userId).to.equal(this.user._id)
  290. done()
  291. }
  292. )
  293. })
  294. it('should expire the token', function (done) {
  295. this.PasswordResetHandler.setNewUserPassword(
  296. this.token,
  297. this.password,
  298. this.auditLog,
  299. (_err, _result) => {
  300. expect(this.OneTimeTokenHandler.expireToken.called).to.equal(
  301. true
  302. )
  303. done()
  304. }
  305. )
  306. })
  307. describe('when logged in', function () {
  308. beforeEach(function () {
  309. this.auditLog.initiatorId = this.user_id
  310. })
  311. it('should update the user audit log with initiatorId', function (done) {
  312. this.PasswordResetHandler.setNewUserPassword(
  313. this.token,
  314. this.password,
  315. this.auditLog,
  316. (error, result) => {
  317. const { reset, userId } = result
  318. expect(error).to.not.exist
  319. const logCall =
  320. this.UserAuditLogHandler.promises.addEntry.lastCall
  321. expect(logCall.args[0]).to.equal(this.user_id)
  322. expect(logCall.args[1]).to.equal('reset-password')
  323. expect(logCall.args[2]).to.equal(this.user_id)
  324. expect(logCall.args[3]).to.equal(this.auditLog.ip)
  325. expect(logCall.args[4]).to.equal(undefined)
  326. done()
  327. }
  328. )
  329. })
  330. })
  331. })
  332. describe('errors', function () {
  333. describe('via setUserPassword', function () {
  334. beforeEach(function () {
  335. this.PasswordResetHandler.promises.getUserForPasswordResetToken =
  336. sinon.stub().withArgs(this.token).resolves(this.user)
  337. this.AuthenticationManager.promises.setUserPassword
  338. .withArgs(this.user, this.password)
  339. .rejects()
  340. })
  341. it('should return the error', function (done) {
  342. this.PasswordResetHandler.setNewUserPassword(
  343. this.token,
  344. this.password,
  345. this.auditLog,
  346. (error, _result) => {
  347. expect(error).to.exist
  348. expect(
  349. this.UserAuditLogHandler.promises.addEntry.callCount
  350. ).to.equal(1)
  351. done()
  352. }
  353. )
  354. })
  355. })
  356. describe('via UserAuditLogHandler', function () {
  357. beforeEach(function () {
  358. this.PasswordResetHandler.promises.getUserForPasswordResetToken =
  359. sinon.stub().withArgs(this.token).resolves(this.user)
  360. this.UserAuditLogHandler.promises.addEntry.rejects(
  361. new Error('oops')
  362. )
  363. })
  364. it('should return the error', function (done) {
  365. this.PasswordResetHandler.setNewUserPassword(
  366. this.token,
  367. this.password,
  368. this.auditLog,
  369. (error, _result) => {
  370. expect(error).to.exist
  371. expect(
  372. this.UserAuditLogHandler.promises.addEntry.callCount
  373. ).to.equal(1)
  374. expect(this.AuthenticationManager.promises.setUserPassword).to
  375. .not.have.been.called
  376. done()
  377. }
  378. )
  379. })
  380. })
  381. })
  382. })
  383. })
  384. describe('when the token has a v1_user_id and email', function () {
  385. beforeEach(function () {
  386. this.user.overleaf = { id: 184 }
  387. this.OneTimeTokenHandler.peekValueFromToken
  388. .withArgs('password', this.token)
  389. .yields(null, {
  390. v1_user_id: this.user.overleaf.id,
  391. email: this.email,
  392. })
  393. this.AuthenticationManager.promises.setUserPassword
  394. .withArgs(this.user, this.password)
  395. .resolves(true)
  396. this.OneTimeTokenHandler.expireToken = sinon
  397. .stub()
  398. .callsArgWith(2, null)
  399. })
  400. describe('when no user is reset with this email', function () {
  401. beforeEach(function () {
  402. this.UserGetter.getUserByMainEmail
  403. .withArgs(this.email)
  404. .yields(null, null)
  405. })
  406. it('should return reset == false', function (done) {
  407. this.PasswordResetHandler.setNewUserPassword(
  408. this.token,
  409. this.password,
  410. this.auditLog,
  411. (err, result) => {
  412. const { reset, userId } = result
  413. expect(err).to.not.exist
  414. expect(reset).to.be.false
  415. expect(this.OneTimeTokenHandler.expireToken.called).to.equal(
  416. false
  417. )
  418. done()
  419. }
  420. )
  421. })
  422. })
  423. describe("when the email and user don't match", function () {
  424. beforeEach(function () {
  425. this.UserGetter.getUserByMainEmail.withArgs(this.email).yields(null, {
  426. _id: this.user._id,
  427. email: this.email,
  428. overleaf: { id: 'not-the-same' },
  429. })
  430. })
  431. it('should return reset == false', function (done) {
  432. this.PasswordResetHandler.setNewUserPassword(
  433. this.token,
  434. this.password,
  435. this.auditLog,
  436. (err, result) => {
  437. const { reset, userId } = result
  438. expect(err).to.not.exist
  439. expect(reset).to.be.false
  440. expect(this.OneTimeTokenHandler.expireToken.called).to.equal(
  441. false
  442. )
  443. done()
  444. }
  445. )
  446. })
  447. })
  448. describe('when the email and user match', function () {
  449. beforeEach(function () {
  450. this.UserGetter.getUserByMainEmail
  451. .withArgs(this.email)
  452. .yields(null, this.user)
  453. })
  454. it('should return reset == true and the user id', function (done) {
  455. this.PasswordResetHandler.setNewUserPassword(
  456. this.token,
  457. this.password,
  458. this.auditLog,
  459. (err, result) => {
  460. const { reset, userId } = result
  461. expect(err).to.not.exist
  462. expect(reset).to.be.true
  463. expect(userId).to.equal(this.user._id)
  464. expect(this.OneTimeTokenHandler.expireToken.called).to.equal(true)
  465. done()
  466. }
  467. )
  468. })
  469. })
  470. })
  471. })
  472. })