PasswordResetHandlerTests.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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. getValueFromTokenAndExpire: sinon.stub(),
  29. }
  30. this.UserGetter = {
  31. getUserByMainEmail: sinon.stub(),
  32. getUser: sinon.stub(),
  33. getUserByAnyEmail: sinon.stub(),
  34. }
  35. this.EmailHandler = { sendEmail: sinon.stub() }
  36. this.AuthenticationManager = {
  37. setUserPasswordInV2: sinon.stub(),
  38. promises: {
  39. setUserPassword: sinon.stub().resolves(),
  40. },
  41. }
  42. this.PasswordResetHandler = SandboxedModule.require(modulePath, {
  43. requires: {
  44. '../User/UserAuditLogHandler': (this.UserAuditLogHandler = {
  45. promises: {
  46. addEntry: sinon.stub().resolves(),
  47. },
  48. }),
  49. '../User/UserGetter': this.UserGetter,
  50. '../Security/OneTimeTokenHandler': this.OneTimeTokenHandler,
  51. '../Email/EmailHandler': this.EmailHandler,
  52. '../Authentication/AuthenticationManager': this.AuthenticationManager,
  53. '@overleaf/settings': this.settings,
  54. },
  55. })
  56. this.token = '12312321i'
  57. this.user_id = 'user_id_here'
  58. this.user = { email: (this.email = 'bob@bob.com'), _id: this.user_id }
  59. this.password = 'my great secret password'
  60. this.callback = sinon.stub()
  61. // this should not have any effect now
  62. this.settings.overleaf = true
  63. })
  64. afterEach(function () {
  65. this.settings.overleaf = false
  66. })
  67. describe('generateAndEmailResetToken', function () {
  68. it('should check the user exists', function () {
  69. this.UserGetter.getUserByAnyEmail.yields()
  70. this.PasswordResetHandler.generateAndEmailResetToken(
  71. this.user.email,
  72. this.callback
  73. )
  74. this.UserGetter.getUserByAnyEmail.should.have.been.calledWith(
  75. this.user.email
  76. )
  77. })
  78. it('should send the email with the token', function (done) {
  79. this.UserGetter.getUserByAnyEmail.yields(null, this.user)
  80. this.OneTimeTokenHandler.getNewToken.yields(null, this.token)
  81. this.EmailHandler.sendEmail.yields()
  82. this.PasswordResetHandler.generateAndEmailResetToken(
  83. this.user.email,
  84. (err, status) => {
  85. this.EmailHandler.sendEmail.called.should.equal(true)
  86. status.should.equal('primary')
  87. const args = this.EmailHandler.sendEmail.args[0]
  88. args[0].should.equal('passwordResetRequested')
  89. args[1].setNewPasswordUrl.should.equal(
  90. `${this.settings.siteUrl}/user/password/set?passwordResetToken=${
  91. this.token
  92. }&email=${encodeURIComponent(this.user.email)}`
  93. )
  94. done()
  95. }
  96. )
  97. })
  98. describe('when the email exists', function () {
  99. beforeEach(function () {
  100. this.UserGetter.getUserByAnyEmail.yields(null, this.user)
  101. this.OneTimeTokenHandler.getNewToken.yields(null, this.token)
  102. this.EmailHandler.sendEmail.yields()
  103. this.PasswordResetHandler.generateAndEmailResetToken(
  104. this.email,
  105. this.callback
  106. )
  107. })
  108. it('should set the password token data to the user id and email', function () {
  109. this.OneTimeTokenHandler.getNewToken.should.have.been.calledWith(
  110. 'password',
  111. {
  112. email: this.email,
  113. user_id: this.user._id,
  114. }
  115. )
  116. })
  117. it('should send an email with the token', function () {
  118. this.EmailHandler.sendEmail.called.should.equal(true)
  119. const args = this.EmailHandler.sendEmail.args[0]
  120. args[0].should.equal('passwordResetRequested')
  121. args[1].setNewPasswordUrl.should.equal(
  122. `${this.settings.siteUrl}/user/password/set?passwordResetToken=${
  123. this.token
  124. }&email=${encodeURIComponent(this.user.email)}`
  125. )
  126. })
  127. it('should return status == true', function () {
  128. this.callback.calledWith(null, 'primary').should.equal(true)
  129. })
  130. })
  131. describe("when the email doesn't exist", function () {
  132. beforeEach(function () {
  133. this.UserGetter.getUserByAnyEmail.yields(null, null)
  134. this.PasswordResetHandler.generateAndEmailResetToken(
  135. this.email,
  136. this.callback
  137. )
  138. })
  139. it('should not set the password token data', function () {
  140. this.OneTimeTokenHandler.getNewToken.called.should.equal(false)
  141. })
  142. it('should send an email with the token', function () {
  143. this.EmailHandler.sendEmail.called.should.equal(false)
  144. })
  145. it('should return status == null', function () {
  146. this.callback.calledWith(null, null).should.equal(true)
  147. })
  148. })
  149. describe('when the email is a secondary email', function () {
  150. beforeEach(function () {
  151. this.UserGetter.getUserByAnyEmail.callsArgWith(1, null, this.user)
  152. this.PasswordResetHandler.generateAndEmailResetToken(
  153. 'secondary@email.com',
  154. this.callback
  155. )
  156. })
  157. it('should not set the password token data', function () {
  158. this.OneTimeTokenHandler.getNewToken.called.should.equal(false)
  159. })
  160. it('should not send an email with the token', function () {
  161. this.EmailHandler.sendEmail.called.should.equal(false)
  162. })
  163. it('should return status == secondary', function () {
  164. this.callback.calledWith(null, 'secondary').should.equal(true)
  165. })
  166. })
  167. })
  168. describe('setNewUserPassword', function () {
  169. beforeEach(function () {
  170. this.auditLog = { ip: '0:0:0:0' }
  171. })
  172. describe('when no data is found', function () {
  173. beforeEach(function () {
  174. this.OneTimeTokenHandler.getValueFromTokenAndExpire.yields(null, null)
  175. })
  176. it('should return found == false and reset == false', function () {
  177. this.PasswordResetHandler.setNewUserPassword(
  178. this.token,
  179. this.password,
  180. this.auditLog,
  181. (error, result) => {
  182. expect(error).to.not.exist
  183. expect(result).to.deep.equal({
  184. found: false,
  185. reset: false,
  186. userId: null,
  187. })
  188. }
  189. )
  190. })
  191. })
  192. describe('when the token has a user_id and email', function () {
  193. beforeEach(function () {
  194. this.OneTimeTokenHandler.getValueFromTokenAndExpire
  195. .withArgs('password', this.token)
  196. .yields(null, {
  197. user_id: this.user._id,
  198. email: this.email,
  199. })
  200. this.AuthenticationManager.promises.setUserPassword
  201. .withArgs(this.user, this.password)
  202. .resolves(true)
  203. })
  204. describe('when no user is found with this email', function () {
  205. beforeEach(function () {
  206. this.UserGetter.getUserByMainEmail
  207. .withArgs(this.email)
  208. .yields(null, null)
  209. })
  210. it('should return found == false and reset == false', function (done) {
  211. this.PasswordResetHandler.setNewUserPassword(
  212. this.token,
  213. this.password,
  214. this.auditLog,
  215. (err, result) => {
  216. const { found, reset, userId } = result
  217. expect(err).to.not.exist
  218. expect(found).to.be.false
  219. expect(reset).to.be.false
  220. done()
  221. }
  222. )
  223. })
  224. })
  225. describe("when the email and user don't match", function () {
  226. beforeEach(function () {
  227. this.UserGetter.getUserByMainEmail
  228. .withArgs(this.email)
  229. .yields(null, { _id: 'not-the-same', email: this.email })
  230. })
  231. it('should return found == false and reset == false', function (done) {
  232. this.PasswordResetHandler.setNewUserPassword(
  233. this.token,
  234. this.password,
  235. this.auditLog,
  236. (err, result) => {
  237. const { found, reset, userId } = result
  238. expect(err).to.not.exist
  239. expect(found).to.be.false
  240. expect(reset).to.be.false
  241. done()
  242. }
  243. )
  244. })
  245. })
  246. describe('when the email and user match', function () {
  247. describe('success', function () {
  248. beforeEach(function () {
  249. this.UserGetter.getUserByMainEmail.yields(null, this.user)
  250. })
  251. it('should update the user audit log', function (done) {
  252. this.PasswordResetHandler.setNewUserPassword(
  253. this.token,
  254. this.password,
  255. this.auditLog,
  256. (error, result) => {
  257. const { reset, userId } = result
  258. expect(error).to.not.exist
  259. const logCall =
  260. this.UserAuditLogHandler.promises.addEntry.lastCall
  261. expect(logCall.args[0]).to.equal(this.user_id)
  262. expect(logCall.args[1]).to.equal('reset-password')
  263. expect(logCall.args[2]).to.equal(undefined)
  264. expect(logCall.args[3]).to.equal(this.auditLog.ip)
  265. expect(logCall.args[4]).to.equal(undefined)
  266. done()
  267. }
  268. )
  269. })
  270. it('should return reset == true and the user id', function (done) {
  271. this.PasswordResetHandler.setNewUserPassword(
  272. this.token,
  273. this.password,
  274. this.auditLog,
  275. (err, result) => {
  276. const { reset, userId } = result
  277. expect(err).to.not.exist
  278. expect(reset).to.be.true
  279. expect(userId).to.equal(this.user._id)
  280. done()
  281. }
  282. )
  283. })
  284. describe('when logged in', function () {
  285. beforeEach(function () {
  286. this.auditLog.initiatorId = this.user_id
  287. })
  288. it('should update the user audit log with initiatorId', function (done) {
  289. this.PasswordResetHandler.setNewUserPassword(
  290. this.token,
  291. this.password,
  292. this.auditLog,
  293. (error, result) => {
  294. const { reset, userId } = result
  295. expect(error).to.not.exist
  296. const logCall =
  297. this.UserAuditLogHandler.promises.addEntry.lastCall
  298. expect(logCall.args[0]).to.equal(this.user_id)
  299. expect(logCall.args[1]).to.equal('reset-password')
  300. expect(logCall.args[2]).to.equal(this.user_id)
  301. expect(logCall.args[3]).to.equal(this.auditLog.ip)
  302. expect(logCall.args[4]).to.equal(undefined)
  303. done()
  304. }
  305. )
  306. })
  307. })
  308. })
  309. describe('errors', function () {
  310. describe('via UserAuditLogHandler', function () {
  311. beforeEach(function () {
  312. this.PasswordResetHandler.promises.getUserForPasswordResetToken =
  313. sinon.stub().withArgs(this.token).resolves(this.user)
  314. this.UserAuditLogHandler.promises.addEntry.rejects(
  315. new Error('oops')
  316. )
  317. })
  318. it('should return the error', function (done) {
  319. this.PasswordResetHandler.setNewUserPassword(
  320. this.token,
  321. this.password,
  322. this.auditLog,
  323. (error, _result) => {
  324. expect(error).to.exist
  325. expect(
  326. this.UserAuditLogHandler.promises.addEntry.callCount
  327. ).to.equal(1)
  328. expect(this.AuthenticationManager.promises.setUserPassword).to
  329. .not.have.been.called
  330. done()
  331. }
  332. )
  333. })
  334. })
  335. })
  336. })
  337. })
  338. describe('when the token has a v1_user_id and email', function () {
  339. beforeEach(function () {
  340. this.user.overleaf = { id: 184 }
  341. this.OneTimeTokenHandler.getValueFromTokenAndExpire
  342. .withArgs('password', this.token)
  343. .yields(null, {
  344. v1_user_id: this.user.overleaf.id,
  345. email: this.email,
  346. })
  347. this.AuthenticationManager.promises.setUserPassword
  348. .withArgs(this.user, this.password)
  349. .resolves(true)
  350. })
  351. describe('when no user is reset with this email', function () {
  352. beforeEach(function () {
  353. this.UserGetter.getUserByMainEmail
  354. .withArgs(this.email)
  355. .yields(null, null)
  356. })
  357. it('should return reset == false', function (done) {
  358. this.PasswordResetHandler.setNewUserPassword(
  359. this.token,
  360. this.password,
  361. this.auditLog,
  362. (err, result) => {
  363. const { reset, userId } = result
  364. expect(err).to.not.exist
  365. expect(reset).to.be.false
  366. done()
  367. }
  368. )
  369. })
  370. })
  371. describe("when the email and user don't match", function () {
  372. beforeEach(function () {
  373. this.UserGetter.getUserByMainEmail.withArgs(this.email).yields(null, {
  374. _id: this.user._id,
  375. email: this.email,
  376. overleaf: { id: 'not-the-same' },
  377. })
  378. })
  379. it('should return reset == false', function (done) {
  380. this.PasswordResetHandler.setNewUserPassword(
  381. this.token,
  382. this.password,
  383. this.auditLog,
  384. (err, result) => {
  385. const { reset, userId } = result
  386. expect(err).to.not.exist
  387. expect(reset).to.be.false
  388. done()
  389. }
  390. )
  391. })
  392. })
  393. describe('when the email and user match', function () {
  394. beforeEach(function () {
  395. this.UserGetter.getUserByMainEmail
  396. .withArgs(this.email)
  397. .yields(null, this.user)
  398. })
  399. it('should return reset == true and the user id', function (done) {
  400. this.PasswordResetHandler.setNewUserPassword(
  401. this.token,
  402. this.password,
  403. this.auditLog,
  404. (err, result) => {
  405. const { reset, userId } = result
  406. expect(err).to.not.exist
  407. expect(reset).to.be.true
  408. expect(userId).to.equal(this.user._id)
  409. done()
  410. }
  411. )
  412. })
  413. })
  414. })
  415. })
  416. })