PasswordResetHandlerTests.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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. sinon.assert.calledWith(
  269. this.UserAuditLogHandler.promises.addEntry,
  270. this.user_id,
  271. 'reset-password',
  272. undefined,
  273. this.auditLog.ip,
  274. { token: this.token.substring(0, 10) }
  275. )
  276. expect(error).to.not.exist
  277. done()
  278. }
  279. )
  280. })
  281. it('should return reset == true and the user id', function (done) {
  282. this.PasswordResetHandler.setNewUserPassword(
  283. this.token,
  284. this.password,
  285. this.auditLog,
  286. (err, result) => {
  287. const { reset, userId } = result
  288. expect(err).to.not.exist
  289. expect(reset).to.be.true
  290. expect(userId).to.equal(this.user._id)
  291. done()
  292. }
  293. )
  294. })
  295. it('should expire the token', function (done) {
  296. this.PasswordResetHandler.setNewUserPassword(
  297. this.token,
  298. this.password,
  299. this.auditLog,
  300. (_err, _result) => {
  301. expect(this.OneTimeTokenHandler.expireToken.called).to.equal(
  302. true
  303. )
  304. done()
  305. }
  306. )
  307. })
  308. describe('when logged in', function () {
  309. beforeEach(function () {
  310. this.auditLog.initiatorId = this.user_id
  311. })
  312. it('should update the user audit log with initiatorId', function (done) {
  313. this.PasswordResetHandler.setNewUserPassword(
  314. this.token,
  315. this.password,
  316. this.auditLog,
  317. (error, result) => {
  318. const { reset, userId } = result
  319. expect(error).to.not.exist
  320. sinon.assert.calledWith(
  321. this.UserAuditLogHandler.promises.addEntry,
  322. this.user_id,
  323. 'reset-password',
  324. this.user_id,
  325. this.auditLog.ip,
  326. { token: this.token.substring(0, 10) }
  327. )
  328. done()
  329. }
  330. )
  331. })
  332. })
  333. })
  334. describe('errors', function () {
  335. describe('via setUserPassword', function () {
  336. beforeEach(function () {
  337. this.PasswordResetHandler.promises.getUserForPasswordResetToken =
  338. sinon.stub().withArgs(this.token).resolves(this.user)
  339. this.AuthenticationManager.promises.setUserPassword
  340. .withArgs(this.user, this.password)
  341. .rejects()
  342. })
  343. it('should return the error', function (done) {
  344. this.PasswordResetHandler.setNewUserPassword(
  345. this.token,
  346. this.password,
  347. this.auditLog,
  348. (error, _result) => {
  349. expect(error).to.exist
  350. expect(
  351. this.UserAuditLogHandler.promises.addEntry.callCount
  352. ).to.equal(1)
  353. done()
  354. }
  355. )
  356. })
  357. })
  358. describe('via UserAuditLogHandler', function () {
  359. beforeEach(function () {
  360. this.PasswordResetHandler.promises.getUserForPasswordResetToken =
  361. sinon.stub().withArgs(this.token).resolves(this.user)
  362. this.UserAuditLogHandler.promises.addEntry.rejects(
  363. new Error('oops')
  364. )
  365. })
  366. it('should return the error', function (done) {
  367. this.PasswordResetHandler.setNewUserPassword(
  368. this.token,
  369. this.password,
  370. this.auditLog,
  371. (error, _result) => {
  372. expect(error).to.exist
  373. expect(
  374. this.UserAuditLogHandler.promises.addEntry.callCount
  375. ).to.equal(1)
  376. expect(this.AuthenticationManager.promises.setUserPassword).to
  377. .not.have.been.called
  378. done()
  379. }
  380. )
  381. })
  382. })
  383. })
  384. })
  385. })
  386. describe('when the token has a v1_user_id and email', function () {
  387. beforeEach(function () {
  388. this.user.overleaf = { id: 184 }
  389. this.OneTimeTokenHandler.peekValueFromToken
  390. .withArgs('password', this.token)
  391. .yields(null, {
  392. v1_user_id: this.user.overleaf.id,
  393. email: this.email,
  394. })
  395. this.AuthenticationManager.promises.setUserPassword
  396. .withArgs(this.user, this.password)
  397. .resolves(true)
  398. this.OneTimeTokenHandler.expireToken = sinon
  399. .stub()
  400. .callsArgWith(2, null)
  401. })
  402. describe('when no user is reset with this email', function () {
  403. beforeEach(function () {
  404. this.UserGetter.getUserByMainEmail
  405. .withArgs(this.email)
  406. .yields(null, null)
  407. })
  408. it('should return reset == false', function (done) {
  409. this.PasswordResetHandler.setNewUserPassword(
  410. this.token,
  411. this.password,
  412. this.auditLog,
  413. (err, result) => {
  414. const { reset, userId } = result
  415. expect(err).to.not.exist
  416. expect(reset).to.be.false
  417. expect(this.OneTimeTokenHandler.expireToken.called).to.equal(
  418. false
  419. )
  420. done()
  421. }
  422. )
  423. })
  424. })
  425. describe("when the email and user don't match", function () {
  426. beforeEach(function () {
  427. this.UserGetter.getUserByMainEmail.withArgs(this.email).yields(null, {
  428. _id: this.user._id,
  429. email: this.email,
  430. overleaf: { id: 'not-the-same' },
  431. })
  432. })
  433. it('should return reset == false', function (done) {
  434. this.PasswordResetHandler.setNewUserPassword(
  435. this.token,
  436. this.password,
  437. this.auditLog,
  438. (err, result) => {
  439. const { reset, userId } = result
  440. expect(err).to.not.exist
  441. expect(reset).to.be.false
  442. expect(this.OneTimeTokenHandler.expireToken.called).to.equal(
  443. false
  444. )
  445. done()
  446. }
  447. )
  448. })
  449. })
  450. describe('when the email and user match', function () {
  451. beforeEach(function () {
  452. this.UserGetter.getUserByMainEmail
  453. .withArgs(this.email)
  454. .yields(null, this.user)
  455. })
  456. it('should return reset == true and the user id', function (done) {
  457. this.PasswordResetHandler.setNewUserPassword(
  458. this.token,
  459. this.password,
  460. this.auditLog,
  461. (err, result) => {
  462. const { reset, userId } = result
  463. expect(err).to.not.exist
  464. expect(reset).to.be.true
  465. expect(userId).to.equal(this.user._id)
  466. expect(this.OneTimeTokenHandler.expireToken.called).to.equal(true)
  467. done()
  468. }
  469. )
  470. })
  471. })
  472. })
  473. })
  474. })