2014-05-16 12:45:48 -04:00
|
|
|
should = require('chai').should()
|
|
|
|
SandboxedModule = require('sandboxed-module')
|
|
|
|
assert = require('assert')
|
|
|
|
path = require('path')
|
|
|
|
sinon = require('sinon')
|
|
|
|
modulePath = path.join __dirname, "../../../../app/js/Features/User/UserUpdater"
|
|
|
|
expect = require("chai").expect
|
2018-06-19 08:55:34 -04:00
|
|
|
tk = require('timekeeper')
|
2014-05-16 12:45:48 -04:00
|
|
|
|
|
|
|
describe "UserUpdater", ->
|
|
|
|
|
|
|
|
beforeEach ->
|
2018-06-19 08:55:34 -04:00
|
|
|
tk.freeze(Date.now())
|
2014-05-16 12:45:48 -04:00
|
|
|
@mongojs =
|
|
|
|
db:{}
|
|
|
|
ObjectId:(id)-> return id
|
2018-05-23 10:12:23 -04:00
|
|
|
@UserGetter =
|
2018-05-25 07:04:09 -04:00
|
|
|
getUserEmail: sinon.stub()
|
|
|
|
getUserByAnyEmail: sinon.stub()
|
2018-05-28 10:08:37 -04:00
|
|
|
ensureUniqueEmailAddress: sinon.stub()
|
2018-08-08 08:15:51 -04:00
|
|
|
@logger =
|
|
|
|
err: sinon.stub()
|
|
|
|
log: ->
|
|
|
|
warn: ->
|
2018-07-04 09:47:49 -04:00
|
|
|
@addAffiliation = sinon.stub().yields()
|
2018-06-27 12:29:56 -04:00
|
|
|
@removeAffiliation = sinon.stub().callsArgWith(2, null)
|
2018-07-11 08:57:35 -04:00
|
|
|
@refreshFeatures = sinon.stub().yields()
|
2018-08-08 08:15:51 -04:00
|
|
|
@NewsletterManager =
|
|
|
|
changeEmail:sinon.stub()
|
2014-05-16 12:45:48 -04:00
|
|
|
@UserUpdater = SandboxedModule.require modulePath, requires:
|
2018-05-25 07:04:09 -04:00
|
|
|
"logger-sharelatex": @logger
|
2018-08-08 08:15:51 -04:00
|
|
|
"../../infrastructure/mongojs":@mongojs
|
|
|
|
"metrics-sharelatex": timeAsyncMethod: sinon.stub()
|
2018-05-23 10:12:23 -04:00
|
|
|
"./UserGetter": @UserGetter
|
2018-07-10 15:49:24 -04:00
|
|
|
'../Institutions/InstitutionsAPI':
|
2018-06-27 12:29:56 -04:00
|
|
|
addAffiliation: @addAffiliation
|
|
|
|
removeAffiliation: @removeAffiliation
|
2018-07-11 08:57:35 -04:00
|
|
|
'../Subscription/FeaturesUpdater': refreshFeatures: @refreshFeatures
|
2018-07-17 06:26:07 -04:00
|
|
|
"settings-sharelatex": @settings = {}
|
|
|
|
"request": @request = {}
|
2018-08-08 08:15:51 -04:00
|
|
|
"../Newsletter/NewsletterManager": @NewsletterManager
|
2014-05-16 12:45:48 -04:00
|
|
|
|
|
|
|
@stubbedUser =
|
2018-05-25 07:04:09 -04:00
|
|
|
_id: "3131231"
|
2014-05-16 12:45:48 -04:00
|
|
|
name:"bob"
|
|
|
|
email:"hello@world.com"
|
|
|
|
@newEmail = "bob@bob.com"
|
|
|
|
|
2018-06-19 08:55:34 -04:00
|
|
|
afterEach ->
|
|
|
|
tk.reset()
|
|
|
|
|
2018-05-25 07:04:09 -04:00
|
|
|
describe 'changeEmailAddress', ->
|
2014-05-16 12:45:48 -04:00
|
|
|
beforeEach ->
|
2018-05-25 07:04:09 -04:00
|
|
|
@UserGetter.getUserEmail.callsArgWith(1, null, @stubbedUser.email)
|
|
|
|
@UserUpdater.addEmailAddress = sinon.stub().callsArgWith(2)
|
|
|
|
@UserUpdater.setDefaultEmailAddress = sinon.stub().callsArgWith(2)
|
|
|
|
@UserUpdater.removeEmailAddress = sinon.stub().callsArgWith(2)
|
2014-05-16 12:45:48 -04:00
|
|
|
|
2018-05-25 07:04:09 -04:00
|
|
|
it 'change email', (done)->
|
|
|
|
@UserUpdater.changeEmailAddress @stubbedUser._id, @newEmail, (err)=>
|
|
|
|
should.not.exist(err)
|
|
|
|
@UserUpdater.addEmailAddress.calledWith(
|
|
|
|
@stubbedUser._id, @newEmail
|
|
|
|
).should.equal true
|
|
|
|
@UserUpdater.setDefaultEmailAddress.calledWith(
|
|
|
|
@stubbedUser._id, @newEmail
|
|
|
|
).should.equal true
|
|
|
|
@UserUpdater.removeEmailAddress.calledWith(
|
|
|
|
@stubbedUser._id, @stubbedUser.email
|
|
|
|
).should.equal true
|
|
|
|
done()
|
|
|
|
|
2018-07-03 14:58:37 -04:00
|
|
|
it 'validates email', (done)->
|
|
|
|
@UserUpdater.changeEmailAddress @stubbedUser._id, 'foo', (err)=>
|
|
|
|
should.exist(err)
|
|
|
|
done()
|
|
|
|
|
2018-05-25 07:04:09 -04:00
|
|
|
it 'handle error', (done)->
|
|
|
|
@UserUpdater.removeEmailAddress.callsArgWith(2, new Error('nope'))
|
|
|
|
@UserUpdater.changeEmailAddress @stubbedUser._id, @newEmail, (err)=>
|
|
|
|
should.exist(err)
|
|
|
|
done()
|
|
|
|
|
|
|
|
describe 'addEmailAddress', ->
|
|
|
|
beforeEach ->
|
2018-05-28 10:08:37 -04:00
|
|
|
@UserGetter.ensureUniqueEmailAddress = sinon.stub().callsArgWith(1)
|
2018-05-25 07:04:09 -04:00
|
|
|
@UserUpdater.updateUser = sinon.stub().callsArgWith(2, null)
|
|
|
|
|
2018-06-19 07:46:15 -04:00
|
|
|
it 'add email', (done)->
|
2018-05-25 07:04:09 -04:00
|
|
|
@UserUpdater.addEmailAddress @stubbedUser._id, @newEmail, (err)=>
|
2018-05-28 10:08:37 -04:00
|
|
|
@UserGetter.ensureUniqueEmailAddress.called.should.equal true
|
2018-05-25 07:04:09 -04:00
|
|
|
should.not.exist(err)
|
|
|
|
@UserUpdater.updateUser.calledWith(
|
|
|
|
@stubbedUser._id,
|
|
|
|
$push: { emails: { email: @newEmail, createdAt: sinon.match.date } }
|
|
|
|
).should.equal true
|
|
|
|
done()
|
|
|
|
|
2018-06-19 07:46:15 -04:00
|
|
|
it 'add affiliation', (done)->
|
|
|
|
affiliationOptions =
|
|
|
|
university: { id: 1 }
|
|
|
|
role: 'Prof'
|
|
|
|
department: 'Math'
|
|
|
|
@UserUpdater.addEmailAddress @stubbedUser._id, @newEmail, affiliationOptions, (err)=>
|
|
|
|
should.not.exist(err)
|
2018-06-27 12:29:56 -04:00
|
|
|
@addAffiliation.calledOnce.should.equal true
|
|
|
|
args = @addAffiliation.lastCall.args
|
|
|
|
args[0].should.equal @stubbedUser._id
|
|
|
|
args[1].should.equal @newEmail
|
|
|
|
args[2].should.equal affiliationOptions
|
2018-06-19 07:46:15 -04:00
|
|
|
done()
|
|
|
|
|
2018-05-25 07:04:09 -04:00
|
|
|
it 'handle error', (done)->
|
|
|
|
@UserUpdater.updateUser = sinon.stub().callsArgWith(2, new Error('nope'))
|
|
|
|
|
|
|
|
@UserUpdater.addEmailAddress @stubbedUser._id, @newEmail, (err)=>
|
|
|
|
@logger.err.called.should.equal true
|
2014-05-16 12:45:48 -04:00
|
|
|
should.exist(err)
|
|
|
|
done()
|
|
|
|
|
2018-06-19 07:46:15 -04:00
|
|
|
it 'handle affiliation error', (done)->
|
|
|
|
body = errors: 'affiliation error message'
|
2018-06-27 12:29:56 -04:00
|
|
|
@addAffiliation.callsArgWith(3, new Error('nope'))
|
2018-06-19 07:46:15 -04:00
|
|
|
@UserUpdater.addEmailAddress @stubbedUser._id, @newEmail, (err)=>
|
2018-06-27 12:29:56 -04:00
|
|
|
should.exist(err)
|
2018-06-19 07:46:15 -04:00
|
|
|
@UserUpdater.updateUser.called.should.equal false
|
|
|
|
done()
|
|
|
|
|
2018-07-03 14:58:37 -04:00
|
|
|
it 'validates email', (done)->
|
|
|
|
@UserUpdater.addEmailAddress @stubbedUser._id, 'bar', (err)=>
|
|
|
|
should.exist(err)
|
|
|
|
done()
|
|
|
|
|
2018-05-25 07:04:09 -04:00
|
|
|
describe 'removeEmailAddress', ->
|
2018-06-19 07:46:15 -04:00
|
|
|
beforeEach ->
|
2018-05-25 07:04:09 -04:00
|
|
|
@UserUpdater.updateUser = sinon.stub().callsArgWith(2, null, nMatched: 1)
|
2014-05-16 12:45:48 -04:00
|
|
|
|
2018-06-19 07:46:15 -04:00
|
|
|
it 'remove email', (done)->
|
2018-05-25 07:04:09 -04:00
|
|
|
@UserUpdater.removeEmailAddress @stubbedUser._id, @newEmail, (err)=>
|
|
|
|
should.not.exist(err)
|
|
|
|
@UserUpdater.updateUser.calledWith(
|
|
|
|
{ _id: @stubbedUser._id, email: { $ne: @newEmail } },
|
|
|
|
$pull: { emails: { email: @newEmail } }
|
|
|
|
).should.equal true
|
2014-05-16 12:45:48 -04:00
|
|
|
done()
|
2014-06-11 07:46:28 -04:00
|
|
|
|
2018-06-19 07:46:15 -04:00
|
|
|
it 'remove affiliation', (done)->
|
|
|
|
@UserUpdater.removeEmailAddress @stubbedUser._id, @newEmail, (err)=>
|
|
|
|
should.not.exist(err)
|
2018-06-27 12:29:56 -04:00
|
|
|
@removeAffiliation.calledOnce.should.equal true
|
|
|
|
args = @removeAffiliation.lastCall.args
|
|
|
|
args[0].should.equal @stubbedUser._id
|
|
|
|
args[1].should.equal @newEmail
|
2018-06-19 07:46:15 -04:00
|
|
|
done()
|
|
|
|
|
2018-05-25 07:04:09 -04:00
|
|
|
it 'handle error', (done)->
|
|
|
|
@UserUpdater.updateUser = sinon.stub().callsArgWith(2, new Error('nope'))
|
|
|
|
|
|
|
|
@UserUpdater.removeEmailAddress @stubbedUser._id, @newEmail, (err)=>
|
|
|
|
should.exist(err)
|
|
|
|
done()
|
|
|
|
|
|
|
|
it 'handle missed update', (done)->
|
2018-06-19 08:55:34 -04:00
|
|
|
@UserUpdater.updateUser = sinon.stub().callsArgWith(2, null, n: 0)
|
2018-05-25 07:04:09 -04:00
|
|
|
|
|
|
|
@UserUpdater.removeEmailAddress @stubbedUser._id, @newEmail, (err)=>
|
|
|
|
should.exist(err)
|
|
|
|
done()
|
|
|
|
|
2018-06-19 07:46:15 -04:00
|
|
|
it 'handle affiliation error', (done)->
|
2018-06-27 12:29:56 -04:00
|
|
|
@removeAffiliation.callsArgWith(2, new Error('nope'))
|
2018-06-19 07:46:15 -04:00
|
|
|
@UserUpdater.removeEmailAddress @stubbedUser._id, @newEmail, (err)=>
|
2018-06-27 12:29:56 -04:00
|
|
|
should.exist(err)
|
2018-06-19 07:46:15 -04:00
|
|
|
@UserUpdater.updateUser.called.should.equal false
|
|
|
|
done()
|
|
|
|
|
2018-07-03 14:58:37 -04:00
|
|
|
it 'validates email', (done)->
|
|
|
|
@UserUpdater.removeEmailAddress @stubbedUser._id, 'baz', (err)=>
|
|
|
|
should.exist(err)
|
|
|
|
done()
|
|
|
|
|
2018-05-25 07:04:09 -04:00
|
|
|
describe 'setDefaultEmailAddress', ->
|
2018-08-08 08:15:51 -04:00
|
|
|
beforeEach ->
|
|
|
|
@UserGetter.getUserEmail.callsArgWith(1, null, @stubbedUser.email)
|
|
|
|
@NewsletterManager.changeEmail.callsArgWith(2, null)
|
|
|
|
|
2018-05-25 07:04:09 -04:00
|
|
|
it 'set default', (done)->
|
2018-06-19 08:55:34 -04:00
|
|
|
@UserUpdater.updateUser = sinon.stub().callsArgWith(2, null, n: 1)
|
2018-05-25 07:04:09 -04:00
|
|
|
|
|
|
|
@UserUpdater.setDefaultEmailAddress @stubbedUser._id, @newEmail, (err)=>
|
|
|
|
should.not.exist(err)
|
|
|
|
@UserUpdater.updateUser.calledWith(
|
|
|
|
{ _id: @stubbedUser._id, 'emails.email': @newEmail },
|
|
|
|
$set: { email: @newEmail }
|
|
|
|
).should.equal true
|
|
|
|
done()
|
|
|
|
|
2018-08-08 08:15:51 -04:00
|
|
|
it 'set changed the email in newsletter', (done)->
|
|
|
|
@UserUpdater.updateUser = sinon.stub().callsArgWith(2, null, n: 1)
|
|
|
|
|
|
|
|
@UserUpdater.setDefaultEmailAddress @stubbedUser._id, @newEmail, (err)=>
|
|
|
|
should.not.exist(err)
|
|
|
|
@NewsletterManager.changeEmail.calledWith(
|
|
|
|
@stubbedUser.email, @newEmail
|
|
|
|
).should.equal true
|
|
|
|
done()
|
|
|
|
|
2018-05-25 07:04:09 -04:00
|
|
|
it 'handle error', (done)->
|
|
|
|
@UserUpdater.updateUser = sinon.stub().callsArgWith(2, new Error('nope'))
|
|
|
|
|
|
|
|
@UserUpdater.setDefaultEmailAddress @stubbedUser._id, @newEmail, (err)=>
|
|
|
|
should.exist(err)
|
|
|
|
done()
|
|
|
|
|
|
|
|
it 'handle missed update', (done)->
|
2018-06-19 08:55:34 -04:00
|
|
|
@UserUpdater.updateUser = sinon.stub().callsArgWith(2, null, n: 0)
|
2018-05-25 07:04:09 -04:00
|
|
|
|
|
|
|
@UserUpdater.setDefaultEmailAddress @stubbedUser._id, @newEmail, (err)=>
|
|
|
|
should.exist(err)
|
|
|
|
done()
|
|
|
|
|
2018-07-03 14:58:37 -04:00
|
|
|
it 'validates email', (done)->
|
|
|
|
@UserUpdater.setDefaultEmailAddress @stubbedUser._id, '.edu', (err)=>
|
|
|
|
should.exist(err)
|
|
|
|
done()
|
|
|
|
|
2018-06-19 08:55:34 -04:00
|
|
|
describe 'confirmEmail', ->
|
2018-07-04 09:47:49 -04:00
|
|
|
beforeEach ->
|
2018-06-19 08:55:34 -04:00
|
|
|
@UserUpdater.updateUser = sinon.stub().callsArgWith(2, null, n: 1)
|
|
|
|
|
2018-07-04 09:47:49 -04:00
|
|
|
it 'should update the email record', (done)->
|
2018-06-19 08:55:34 -04:00
|
|
|
@UserUpdater.confirmEmail @stubbedUser._id, @newEmail, (err)=>
|
|
|
|
should.not.exist(err)
|
|
|
|
@UserUpdater.updateUser.calledWith(
|
|
|
|
{ _id: @stubbedUser._id, 'emails.email': @newEmail },
|
|
|
|
$set: { 'emails.$.confirmedAt': new Date() }
|
|
|
|
).should.equal true
|
|
|
|
done()
|
|
|
|
|
2018-07-04 09:47:49 -04:00
|
|
|
it 'add affiliation', (done)->
|
|
|
|
@UserUpdater.confirmEmail @stubbedUser._id, @newEmail, (err)=>
|
|
|
|
should.not.exist(err)
|
|
|
|
@addAffiliation.calledOnce.should.equal true
|
|
|
|
sinon.assert.calledWith(@addAffiliation, @stubbedUser._id, @newEmail)
|
|
|
|
done()
|
|
|
|
|
2018-06-19 08:55:34 -04:00
|
|
|
it 'handle error', (done)->
|
|
|
|
@UserUpdater.updateUser = sinon.stub().callsArgWith(2, new Error('nope'))
|
|
|
|
|
|
|
|
@UserUpdater.confirmEmail @stubbedUser._id, @newEmail, (err)=>
|
|
|
|
should.exist(err)
|
|
|
|
done()
|
|
|
|
|
|
|
|
it 'handle missed update', (done)->
|
|
|
|
@UserUpdater.updateUser = sinon.stub().callsArgWith(2, null, n: 0)
|
|
|
|
|
|
|
|
@UserUpdater.confirmEmail @stubbedUser._id, @newEmail, (err)=>
|
|
|
|
should.exist(err)
|
|
|
|
done()
|
2018-07-03 14:58:37 -04:00
|
|
|
|
|
|
|
it 'validates email', (done)->
|
|
|
|
@UserUpdater.confirmEmail @stubbedUser._id, '@', (err)=>
|
|
|
|
should.exist(err)
|
|
|
|
done()
|
2018-07-04 09:47:49 -04:00
|
|
|
|
|
|
|
it 'handle affiliation error', (done)->
|
|
|
|
@addAffiliation.callsArgWith(2, new Error('nope'))
|
|
|
|
@UserUpdater.confirmEmail @stubbedUser._id, @newEmail, (err)=>
|
|
|
|
should.exist(err)
|
|
|
|
@UserUpdater.updateUser.called.should.equal false
|
|
|
|
done()
|
2018-07-11 08:57:35 -04:00
|
|
|
|
|
|
|
it 'refresh features', (done)->
|
|
|
|
@UserUpdater.confirmEmail @stubbedUser._id, @newEmail, (err)=>
|
|
|
|
should.not.exist(err)
|
|
|
|
sinon.assert.calledWith(@refreshFeatures, @stubbedUser._id, true)
|
|
|
|
done()
|