| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370 |
- /* eslint-disable
- handle-callback-err,
- max-len,
- no-return-assign,
- no-unused-vars,
- */
- // TODO: This file was created by bulk-decaffeinate.
- // Fix any style issues and re-enable lint.
- /*
- * decaffeinate suggestions:
- * DS102: Remove unnecessary code created because of implicit returns
- * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
- */
- const SandboxedModule = require('sandboxed-module')
- const assert = require('assert')
- const path = require('path')
- const sinon = require('sinon')
- const sinonChai = require('sinon-chai')
- const chai = require('chai')
- const modulePath = path.join(
- __dirname,
- '../../../../app/src/Features/PasswordReset/PasswordResetHandler'
- )
- const should = require('chai').should()
- chai.use(sinonChai)
- const { expect } = chai
- describe('PasswordResetHandler', function() {
- beforeEach(function() {
- this.settings = { siteUrl: 'www.sharelatex.com' }
- this.OneTimeTokenHandler = {
- getNewToken: sinon.stub(),
- getValueFromTokenAndExpire: sinon.stub()
- }
- this.UserGetter = {
- getUserByMainEmail: sinon.stub(),
- getUser: sinon.stub(),
- getUserByAnyEmail: sinon.stub()
- }
- this.EmailHandler = { sendEmail: sinon.stub() }
- this.AuthenticationManager = {
- setUserPassword: sinon.stub(),
- setUserPasswordInV1: sinon.stub(),
- setUserPasswordInV2: sinon.stub()
- }
- this.PasswordResetHandler = SandboxedModule.require(modulePath, {
- globals: { console: console },
- requires: {
- '../User/UserGetter': this.UserGetter,
- '../Security/OneTimeTokenHandler': this.OneTimeTokenHandler,
- '../Email/EmailHandler': this.EmailHandler,
- '../Authentication/AuthenticationManager': this.AuthenticationManager,
- 'settings-sharelatex': this.settings,
- 'logger-sharelatex': {
- log() {},
- err() {}
- }
- }
- })
- this.token = '12312321i'
- this.user_id = 'user_id_here'
- this.user = { email: (this.email = 'bob@bob.com'), _id: 'user-id' }
- this.password = 'my great secret password'
- this.callback = sinon.stub()
- // this should not have any effect now
- this.settings.overleaf = true
- })
- afterEach(function() {
- this.settings.overleaf = false
- })
- describe('generateAndEmailResetToken', function() {
- it('should check the user exists', function() {
- this.UserGetter.getUserByAnyEmail.yields()
- this.PasswordResetHandler.generateAndEmailResetToken(
- this.user.email,
- this.callback
- )
- this.UserGetter.getUserByAnyEmail.should.have.been.calledWith(
- this.user.email
- )
- })
- it('should send the email with the token', function(done) {
- this.UserGetter.getUserByAnyEmail.yields(null, this.user)
- this.OneTimeTokenHandler.getNewToken.yields(null, this.token)
- this.EmailHandler.sendEmail.yields()
- this.PasswordResetHandler.generateAndEmailResetToken(
- this.user.email,
- (err, status) => {
- this.EmailHandler.sendEmail.called.should.equal(true)
- status.should.equal('primary')
- const args = this.EmailHandler.sendEmail.args[0]
- args[0].should.equal('passwordResetRequested')
- args[1].setNewPasswordUrl.should.equal(
- `${this.settings.siteUrl}/user/password/set?passwordResetToken=${
- this.token
- }&email=${encodeURIComponent(this.user.email)}`
- )
- done()
- }
- )
- })
- describe('when the email exists', function() {
- beforeEach(function() {
- this.UserGetter.getUserByAnyEmail.yields(null, this.user)
- this.OneTimeTokenHandler.getNewToken.yields(null, this.token)
- this.EmailHandler.sendEmail.yields()
- this.PasswordResetHandler.generateAndEmailResetToken(
- this.email,
- this.callback
- )
- })
- it('should set the password token data to the user id and email', function() {
- this.OneTimeTokenHandler.getNewToken.should.have.been.calledWith(
- 'password',
- {
- email: this.email,
- user_id: this.user._id
- }
- )
- })
- it('should send an email with the token', function() {
- this.EmailHandler.sendEmail.called.should.equal(true)
- const args = this.EmailHandler.sendEmail.args[0]
- args[0].should.equal('passwordResetRequested')
- args[1].setNewPasswordUrl.should.equal(
- `${this.settings.siteUrl}/user/password/set?passwordResetToken=${
- this.token
- }&email=${encodeURIComponent(this.user.email)}`
- )
- })
- it('should return status == true', function() {
- this.callback.calledWith(null, 'primary').should.equal(true)
- })
- })
- describe("when the email doesn't exist", function() {
- beforeEach(function() {
- this.UserGetter.getUserByAnyEmail.yields(null, null)
- this.PasswordResetHandler.generateAndEmailResetToken(
- this.email,
- this.callback
- )
- })
- it('should not set the password token data', function() {
- this.OneTimeTokenHandler.getNewToken.called.should.equal(false)
- })
- it('should send an email with the token', function() {
- this.EmailHandler.sendEmail.called.should.equal(false)
- })
- it('should return status == null', function() {
- this.callback.calledWith(null, null).should.equal(true)
- })
- })
- describe('when the email is a secondary email', function() {
- beforeEach(function() {
- this.UserGetter.getUserByAnyEmail.callsArgWith(1, null, this.user)
- this.PasswordResetHandler.generateAndEmailResetToken(
- 'secondary@email.com',
- this.callback
- )
- })
- it('should not set the password token data', function() {
- this.OneTimeTokenHandler.getNewToken.called.should.equal(false)
- })
- it('should not send an email with the token', function() {
- this.EmailHandler.sendEmail.called.should.equal(false)
- })
- it('should return status == secondary', function() {
- this.callback.calledWith(null, 'secondary').should.equal(true)
- })
- })
- })
- describe('setNewUserPassword', function() {
- describe('when no data is found', function() {
- beforeEach(function() {
- this.OneTimeTokenHandler.getValueFromTokenAndExpire.yields(null, null)
- this.PasswordResetHandler.setNewUserPassword(
- this.token,
- this.password,
- this.callback
- )
- })
- it('should return exists == false', function() {
- this.callback.calledWith(null, false).should.equal(true)
- })
- })
- describe('when the token has a user_id and email', function() {
- beforeEach(function() {
- this.OneTimeTokenHandler.getValueFromTokenAndExpire
- .withArgs('password', this.token)
- .yields(null, {
- user_id: this.user._id,
- email: this.email
- })
- this.AuthenticationManager.setUserPassword
- .withArgs(this.user._id, this.password)
- .yields(null, true, this.user._id)
- })
- describe('when no user is found with this email', function() {
- beforeEach(function() {
- this.UserGetter.getUserByMainEmail
- .withArgs(this.email)
- .yields(null, null)
- })
- it('should return found == false', function(done) {
- this.PasswordResetHandler.setNewUserPassword(
- this.token,
- this.password,
- (err, found) => {
- if (err != null) {
- return done(err)
- }
- expect(found).to.be.false
- done()
- }
- )
- })
- })
- describe("when the email and user don't match", function() {
- beforeEach(function() {
- this.UserGetter.getUserByMainEmail
- .withArgs(this.email)
- .yields(null, { _id: 'not-the-same', email: this.email })
- })
- it('should return found == false', function(done) {
- this.PasswordResetHandler.setNewUserPassword(
- this.token,
- this.password,
- (err, found) => {
- if (err != null) {
- return done(err)
- }
- expect(found).to.be.false
- done()
- }
- )
- })
- })
- describe('when the email and user match', function() {
- beforeEach(function() {
- this.PasswordResetHandler.getUserForPasswordResetToken = sinon
- .stub()
- .withArgs(this.token)
- .yields(null, this.user)
- })
- it('should return found == true and the user id', function(done) {
- this.PasswordResetHandler.setNewUserPassword(
- this.token,
- this.password,
- (err, found, userId) => {
- if (err != null) {
- return done(err)
- }
- expect(found).to.be.true
- expect(userId).to.equal(this.user._id)
- done()
- }
- )
- })
- })
- })
- describe('when the token has a v1_user_id and email', function() {
- beforeEach(function() {
- this.user.overleaf = { id: 184 }
- this.OneTimeTokenHandler.getValueFromTokenAndExpire
- .withArgs('password', this.token)
- .yields(null, {
- v1_user_id: this.user.overleaf.id,
- email: this.email
- })
- this.AuthenticationManager.setUserPassword
- .withArgs(this.user._id, this.password)
- .yields(null, true)
- })
- describe('when no user is found with this email', function() {
- beforeEach(function() {
- this.UserGetter.getUserByMainEmail
- .withArgs(this.email)
- .yields(null, null)
- })
- it('should return found == false', function(done) {
- this.PasswordResetHandler.setNewUserPassword(
- this.token,
- this.password,
- (err, found) => {
- if (err != null) {
- return done(err)
- }
- expect(found).to.be.false
- done()
- }
- )
- })
- })
- describe("when the email and user don't match", function() {
- beforeEach(function() {
- this.UserGetter.getUserByMainEmail.withArgs(this.email).yields(null, {
- _id: this.user._id,
- email: this.email,
- overleaf: { id: 'not-the-same' }
- })
- })
- it('should return found == false', function(done) {
- this.PasswordResetHandler.setNewUserPassword(
- this.token,
- this.password,
- (err, found) => {
- if (err != null) {
- return done(err)
- }
- expect(found).to.be.false
- done()
- }
- )
- })
- })
- describe('when the email and user match', function() {
- beforeEach(function() {
- this.UserGetter.getUserByMainEmail
- .withArgs(this.email)
- .yields(null, this.user)
- })
- it('should return found == true and the user id', function(done) {
- this.PasswordResetHandler.setNewUserPassword(
- this.token,
- this.password,
- (err, found, userId) => {
- if (err != null) {
- return done(err)
- }
- expect(found).to.be.true
- expect(userId).to.equal(this.user._id)
- done()
- }
- )
- })
- })
- })
- })
- })
|