2019-05-29 05:21:06 -04:00
|
|
|
const should = require('chai').should()
|
|
|
|
const SandboxedModule = require('sandboxed-module')
|
|
|
|
const path = require('path')
|
|
|
|
const sinon = require('sinon')
|
|
|
|
const modulePath = path.join(
|
|
|
|
__dirname,
|
|
|
|
'../../../../app/src/Features/User/UserUpdater'
|
|
|
|
)
|
|
|
|
const tk = require('timekeeper')
|
2020-02-20 11:08:40 -05:00
|
|
|
const { expect } = require('chai')
|
2019-05-29 05:21:06 -04:00
|
|
|
|
|
|
|
describe('UserUpdater', function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
tk.freeze(Date.now())
|
|
|
|
this.mongojs = {
|
|
|
|
db: {},
|
|
|
|
ObjectId(id) {
|
|
|
|
return id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.UserGetter = {
|
|
|
|
getUserEmail: sinon.stub(),
|
|
|
|
getUserByAnyEmail: sinon.stub(),
|
2020-08-12 10:18:45 -04:00
|
|
|
ensureUniqueEmailAddress: sinon.stub(),
|
|
|
|
promises: {
|
|
|
|
getUser: sinon.stub()
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
this.logger = {
|
2020-02-20 11:08:40 -05:00
|
|
|
error: sinon.stub(),
|
2019-05-29 05:21:06 -04:00
|
|
|
log() {},
|
2019-07-01 09:48:09 -04:00
|
|
|
warn: sinon.stub()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
this.addAffiliation = sinon.stub().yields()
|
|
|
|
this.removeAffiliation = sinon.stub().callsArgWith(2, null)
|
|
|
|
this.refreshFeatures = sinon.stub().yields()
|
2020-08-12 10:18:45 -04:00
|
|
|
this.NewsletterManager = {
|
|
|
|
promises: {
|
|
|
|
changeEmail: sinon.stub()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.RecurlyWrapper = {
|
|
|
|
promises: {
|
|
|
|
updateAccountEmailAddress: sinon.stub()
|
|
|
|
}
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
this.UserUpdater = SandboxedModule.require(modulePath, {
|
2019-07-15 06:33:47 -04:00
|
|
|
globals: {
|
|
|
|
console: console
|
|
|
|
},
|
2019-05-29 05:21:06 -04:00
|
|
|
requires: {
|
|
|
|
'logger-sharelatex': this.logger,
|
|
|
|
'../../infrastructure/mongojs': this.mongojs,
|
|
|
|
'metrics-sharelatex': {
|
|
|
|
timeAsyncMethod: sinon.stub()
|
|
|
|
},
|
|
|
|
'./UserGetter': this.UserGetter,
|
|
|
|
'../Institutions/InstitutionsAPI': {
|
|
|
|
addAffiliation: this.addAffiliation,
|
|
|
|
removeAffiliation: this.removeAffiliation
|
|
|
|
},
|
2020-02-20 11:08:40 -05:00
|
|
|
'../../infrastructure/Features': (this.Features = {
|
|
|
|
hasFeature: sinon.stub().returns(false)
|
|
|
|
}),
|
2019-05-29 05:21:06 -04:00
|
|
|
'../Subscription/FeaturesUpdater': {
|
|
|
|
refreshFeatures: this.refreshFeatures
|
|
|
|
},
|
|
|
|
'settings-sharelatex': (this.settings = {}),
|
|
|
|
request: (this.request = {}),
|
2020-02-27 07:46:07 -05:00
|
|
|
'../Newsletter/NewsletterManager': this.NewsletterManager,
|
|
|
|
'../Subscription/RecurlyWrapper': this.RecurlyWrapper
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
this.stubbedUser = {
|
|
|
|
_id: '3131231',
|
|
|
|
name: 'bob',
|
|
|
|
email: 'hello@world.com'
|
|
|
|
}
|
2019-07-22 12:55:35 -04:00
|
|
|
this.newEmail = 'bob@bob.com'
|
|
|
|
this.callback = sinon.stub()
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2019-08-07 10:04:04 -04:00
|
|
|
afterEach(function() {
|
|
|
|
return tk.reset()
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2020-02-20 11:08:40 -05:00
|
|
|
describe('addAffiliationForNewUser', function(done) {
|
|
|
|
beforeEach(function() {
|
|
|
|
this.UserUpdater.updateUser = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(2, null, { n: 1, nModified: 1, ok: 1 })
|
|
|
|
})
|
|
|
|
it('should not remove affiliationUnchecked flag if v1 returns an error', function(done) {
|
|
|
|
this.addAffiliation.yields(true)
|
|
|
|
this.UserUpdater.addAffiliationForNewUser(
|
|
|
|
this.stubbedUser._id,
|
|
|
|
this.newEmail,
|
|
|
|
(error, updated) => {
|
|
|
|
expect(error).to.exist
|
|
|
|
expect(updated).to.be.undefined
|
|
|
|
sinon.assert.notCalled(this.UserUpdater.updateUser)
|
|
|
|
done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
it('should remove affiliationUnchecked flag if v1 does not return an error', function(done) {
|
|
|
|
this.addAffiliation.yields()
|
|
|
|
this.UserUpdater.addAffiliationForNewUser(
|
|
|
|
this.stubbedUser._id,
|
|
|
|
this.newEmail,
|
2020-08-11 05:35:08 -04:00
|
|
|
error => {
|
2020-02-20 11:08:40 -05:00
|
|
|
should.not.exist(error)
|
|
|
|
sinon.assert.calledOnce(this.UserUpdater.updateUser)
|
|
|
|
sinon.assert.calledWithMatch(
|
|
|
|
this.UserUpdater.updateUser,
|
|
|
|
{ _id: this.stubbedUser._id, 'emails.email': this.newEmail },
|
|
|
|
{ $unset: { 'emails.$.affiliationUnchecked': 1 } }
|
|
|
|
)
|
|
|
|
done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-05-29 05:21:06 -04:00
|
|
|
describe('changeEmailAddress', function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
this.UserGetter.getUserEmail.callsArgWith(1, null, this.stubbedUser.email)
|
|
|
|
this.UserUpdater.addEmailAddress = sinon.stub().callsArgWith(2)
|
2019-07-22 12:55:35 -04:00
|
|
|
this.UserUpdater.setDefaultEmailAddress = sinon.stub().yields()
|
2020-02-20 11:08:40 -05:00
|
|
|
this.UserUpdater.removeEmailAddress = sinon.stub().callsArgWith(2)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
it('change email', function(done) {
|
2020-02-20 11:08:40 -05:00
|
|
|
this.UserUpdater.changeEmailAddress(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.stubbedUser._id,
|
|
|
|
this.newEmail,
|
|
|
|
err => {
|
|
|
|
should.not.exist(err)
|
|
|
|
this.UserUpdater.addEmailAddress
|
|
|
|
.calledWith(this.stubbedUser._id, this.newEmail)
|
|
|
|
.should.equal(true)
|
|
|
|
this.UserUpdater.setDefaultEmailAddress
|
2020-08-12 10:18:45 -04:00
|
|
|
.calledWith(this.stubbedUser._id, this.newEmail, true)
|
2019-05-29 05:21:06 -04:00
|
|
|
.should.equal(true)
|
|
|
|
this.UserUpdater.removeEmailAddress
|
|
|
|
.calledWith(this.stubbedUser._id, this.stubbedUser.email)
|
|
|
|
.should.equal(true)
|
2020-02-20 11:08:40 -05:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('validates email', function(done) {
|
2020-02-20 11:08:40 -05:00
|
|
|
this.UserUpdater.changeEmailAddress(this.stubbedUser._id, 'foo', err => {
|
|
|
|
should.exist(err)
|
|
|
|
done()
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2019-06-21 09:46:09 -04:00
|
|
|
it('handle error', function(done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.UserUpdater.removeEmailAddress.callsArgWith(2, new Error('nope'))
|
2020-02-20 11:08:40 -05:00
|
|
|
this.UserUpdater.changeEmailAddress(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.stubbedUser._id,
|
|
|
|
this.newEmail,
|
|
|
|
err => {
|
|
|
|
should.exist(err)
|
2020-02-20 11:08:40 -05:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('addEmailAddress', function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
this.UserGetter.ensureUniqueEmailAddress = sinon.stub().callsArgWith(1)
|
2020-02-20 11:08:40 -05:00
|
|
|
this.UserUpdater.updateUser = sinon.stub().callsArgWith(2, null)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
it('add email', function(done) {
|
2020-02-20 11:08:40 -05:00
|
|
|
this.UserUpdater.addEmailAddress(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.stubbedUser._id,
|
|
|
|
this.newEmail,
|
|
|
|
err => {
|
|
|
|
this.UserGetter.ensureUniqueEmailAddress.called.should.equal(true)
|
|
|
|
should.not.exist(err)
|
|
|
|
const reversedHostname = this.newEmail
|
|
|
|
.split('@')[1]
|
|
|
|
.split('')
|
|
|
|
.reverse()
|
|
|
|
.join('')
|
|
|
|
this.UserUpdater.updateUser
|
|
|
|
.calledWith(this.stubbedUser._id, {
|
|
|
|
$push: {
|
|
|
|
emails: {
|
|
|
|
email: this.newEmail,
|
|
|
|
createdAt: sinon.match.date,
|
|
|
|
reversedHostname
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.should.equal(true)
|
2020-02-20 11:08:40 -05:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('add affiliation', function(done) {
|
|
|
|
const affiliationOptions = {
|
|
|
|
university: { id: 1 },
|
|
|
|
role: 'Prof',
|
|
|
|
department: 'Math'
|
|
|
|
}
|
2020-02-20 11:08:40 -05:00
|
|
|
this.UserUpdater.addEmailAddress(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.stubbedUser._id,
|
|
|
|
this.newEmail,
|
|
|
|
affiliationOptions,
|
|
|
|
err => {
|
|
|
|
should.not.exist(err)
|
|
|
|
this.addAffiliation.calledOnce.should.equal(true)
|
|
|
|
const { args } = this.addAffiliation.lastCall
|
|
|
|
args[0].should.equal(this.stubbedUser._id)
|
|
|
|
args[1].should.equal(this.newEmail)
|
|
|
|
args[2].should.equal(affiliationOptions)
|
2020-02-20 11:08:40 -05:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('handle affiliation error', function(done) {
|
|
|
|
this.addAffiliation.callsArgWith(3, new Error('nope'))
|
2020-02-20 11:08:40 -05:00
|
|
|
this.UserUpdater.addEmailAddress(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.stubbedUser._id,
|
|
|
|
this.newEmail,
|
|
|
|
err => {
|
|
|
|
should.exist(err)
|
|
|
|
this.UserUpdater.updateUser.called.should.equal(false)
|
2020-02-20 11:08:40 -05:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2019-06-21 09:46:09 -04:00
|
|
|
it('validates email', function(done) {
|
2020-02-20 11:08:40 -05:00
|
|
|
this.UserUpdater.addEmailAddress(this.stubbedUser._id, 'bar', err => {
|
|
|
|
should.exist(err)
|
|
|
|
done()
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('removeEmailAddress', function() {
|
|
|
|
beforeEach(function() {
|
2020-02-20 11:08:40 -05:00
|
|
|
this.UserUpdater.updateUser = sinon
|
2019-05-29 05:21:06 -04:00
|
|
|
.stub()
|
2020-02-20 11:08:40 -05:00
|
|
|
.callsArgWith(2, null, { nMatched: 1 })
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
it('remove email', function(done) {
|
2020-02-20 11:08:40 -05:00
|
|
|
this.UserUpdater.removeEmailAddress(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.stubbedUser._id,
|
|
|
|
this.newEmail,
|
|
|
|
err => {
|
|
|
|
should.not.exist(err)
|
|
|
|
this.UserUpdater.updateUser
|
|
|
|
.calledWith(
|
|
|
|
{ _id: this.stubbedUser._id, email: { $ne: this.newEmail } },
|
|
|
|
{ $pull: { emails: { email: this.newEmail } } }
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
2020-02-20 11:08:40 -05:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('remove affiliation', function(done) {
|
2020-02-20 11:08:40 -05:00
|
|
|
this.UserUpdater.removeEmailAddress(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.stubbedUser._id,
|
|
|
|
this.newEmail,
|
|
|
|
err => {
|
|
|
|
should.not.exist(err)
|
|
|
|
this.removeAffiliation.calledOnce.should.equal(true)
|
|
|
|
const { args } = this.removeAffiliation.lastCall
|
|
|
|
args[0].should.equal(this.stubbedUser._id)
|
|
|
|
args[1].should.equal(this.newEmail)
|
2020-02-20 11:08:40 -05:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:47:02 -04:00
|
|
|
it('refresh features', function(done) {
|
|
|
|
this.UserUpdater.removeEmailAddress(
|
|
|
|
this.stubbedUser._id,
|
|
|
|
this.newEmail,
|
|
|
|
err => {
|
|
|
|
should.not.exist(err)
|
|
|
|
sinon.assert.calledWith(this.refreshFeatures, this.stubbedUser._id)
|
|
|
|
done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2019-05-29 05:21:06 -04:00
|
|
|
it('handle error', function(done) {
|
|
|
|
this.UserUpdater.updateUser = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(2, new Error('nope'))
|
|
|
|
|
2020-02-20 11:08:40 -05:00
|
|
|
this.UserUpdater.removeEmailAddress(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.stubbedUser._id,
|
|
|
|
this.newEmail,
|
|
|
|
err => {
|
|
|
|
should.exist(err)
|
2020-02-20 11:08:40 -05:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('handle missed update', function(done) {
|
|
|
|
this.UserUpdater.updateUser = sinon.stub().callsArgWith(2, null, { n: 0 })
|
|
|
|
|
2020-02-20 11:08:40 -05:00
|
|
|
this.UserUpdater.removeEmailAddress(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.stubbedUser._id,
|
|
|
|
this.newEmail,
|
|
|
|
err => {
|
|
|
|
should.exist(err)
|
2020-02-20 11:08:40 -05:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('handle affiliation error', function(done) {
|
|
|
|
this.removeAffiliation.callsArgWith(2, new Error('nope'))
|
2020-02-20 11:08:40 -05:00
|
|
|
this.UserUpdater.removeEmailAddress(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.stubbedUser._id,
|
|
|
|
this.newEmail,
|
|
|
|
err => {
|
|
|
|
should.exist(err)
|
|
|
|
this.UserUpdater.updateUser.called.should.equal(false)
|
2020-02-20 11:08:40 -05:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2019-06-21 09:46:09 -04:00
|
|
|
it('validates email', function(done) {
|
2020-02-20 11:08:40 -05:00
|
|
|
this.UserUpdater.removeEmailAddress(this.stubbedUser._id, 'baz', err => {
|
|
|
|
should.exist(err)
|
|
|
|
done()
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('setDefaultEmailAddress', function() {
|
|
|
|
beforeEach(function() {
|
2019-07-22 12:55:35 -04:00
|
|
|
this.stubbedUser.emails = [
|
|
|
|
{
|
|
|
|
email: this.newEmail,
|
|
|
|
confirmedAt: new Date()
|
|
|
|
}
|
|
|
|
]
|
2020-08-12 10:18:45 -04:00
|
|
|
this.UserGetter.promises.getUser.resolves(this.stubbedUser)
|
|
|
|
this.NewsletterManager.promises.changeEmail.callsArgWith(2, null)
|
|
|
|
this.RecurlyWrapper.promises.updateAccountEmailAddress.resolves()
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
it('set default', function(done) {
|
2020-08-12 10:18:45 -04:00
|
|
|
this.UserUpdater.promises.updateUser = sinon.stub().resolves({ n: 1 })
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2019-07-22 12:55:35 -04:00
|
|
|
this.UserUpdater.setDefaultEmailAddress(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.stubbedUser._id,
|
|
|
|
this.newEmail,
|
2020-08-12 10:18:45 -04:00
|
|
|
false,
|
2019-05-29 05:21:06 -04:00
|
|
|
err => {
|
|
|
|
should.not.exist(err)
|
2020-08-12 10:18:45 -04:00
|
|
|
this.UserUpdater.promises.updateUser
|
2019-05-29 05:21:06 -04:00
|
|
|
.calledWith(
|
|
|
|
{ _id: this.stubbedUser._id, 'emails.email': this.newEmail },
|
|
|
|
{ $set: { email: this.newEmail } }
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
2019-07-22 12:55:35 -04:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('set changed the email in newsletter', function(done) {
|
2020-08-12 10:18:45 -04:00
|
|
|
this.UserUpdater.promises.updateUser = sinon.stub().resolves({ n: 1 })
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2019-07-22 12:55:35 -04:00
|
|
|
this.UserUpdater.setDefaultEmailAddress(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.stubbedUser._id,
|
|
|
|
this.newEmail,
|
2020-08-12 10:18:45 -04:00
|
|
|
false,
|
2019-05-29 05:21:06 -04:00
|
|
|
err => {
|
|
|
|
should.not.exist(err)
|
2020-08-12 10:18:45 -04:00
|
|
|
this.NewsletterManager.promises.changeEmail
|
2019-09-25 10:27:38 -04:00
|
|
|
.calledWith(this.stubbedUser, this.newEmail)
|
2019-05-29 05:21:06 -04:00
|
|
|
.should.equal(true)
|
2020-08-12 10:18:45 -04:00
|
|
|
this.RecurlyWrapper.promises.updateAccountEmailAddress
|
2020-02-27 07:46:07 -05:00
|
|
|
.calledWith(this.stubbedUser._id, this.newEmail)
|
|
|
|
.should.equal(true)
|
2019-07-22 12:55:35 -04:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('handle error', function(done) {
|
2020-08-12 10:18:45 -04:00
|
|
|
this.UserUpdater.promises.updateUser = sinon.stub().rejects(Error('nope'))
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2019-07-22 12:55:35 -04:00
|
|
|
this.UserUpdater.setDefaultEmailAddress(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.stubbedUser._id,
|
|
|
|
this.newEmail,
|
2020-08-12 10:18:45 -04:00
|
|
|
false,
|
2019-05-29 05:21:06 -04:00
|
|
|
err => {
|
|
|
|
should.exist(err)
|
2019-07-22 12:55:35 -04:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('handle missed update', function(done) {
|
2020-08-12 10:18:45 -04:00
|
|
|
this.UserUpdater.promises.updateUser = sinon.stub().resolves({ n: 0 })
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2019-07-22 12:55:35 -04:00
|
|
|
this.UserUpdater.setDefaultEmailAddress(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.stubbedUser._id,
|
|
|
|
this.newEmail,
|
2020-08-12 10:18:45 -04:00
|
|
|
false,
|
2019-05-29 05:21:06 -04:00
|
|
|
err => {
|
|
|
|
should.exist(err)
|
2019-07-22 12:55:35 -04:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2019-06-21 09:46:09 -04:00
|
|
|
it('validates email', function(done) {
|
2019-07-22 12:55:35 -04:00
|
|
|
this.UserUpdater.setDefaultEmailAddress(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.stubbedUser._id,
|
|
|
|
'.edu',
|
2020-08-12 10:18:45 -04:00
|
|
|
false,
|
2019-05-29 05:21:06 -04:00
|
|
|
err => {
|
|
|
|
should.exist(err)
|
2019-07-22 12:55:35 -04:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
2019-07-22 12:55:35 -04:00
|
|
|
|
2019-08-07 10:04:04 -04:00
|
|
|
describe('when email not confirmed', function() {
|
2019-07-22 12:55:35 -04:00
|
|
|
beforeEach(function() {
|
|
|
|
this.stubbedUser.emails = [
|
|
|
|
{
|
|
|
|
email: this.newEmail,
|
|
|
|
confirmedAt: null
|
|
|
|
}
|
|
|
|
]
|
2020-08-12 10:18:45 -04:00
|
|
|
this.UserUpdater.promises.updateUser = sinon.stub()
|
2019-07-22 12:55:35 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should callback with error', function() {
|
|
|
|
this.UserUpdater.setDefaultEmailAddress(
|
|
|
|
this.stubbedUser._id,
|
|
|
|
this.newEmail,
|
2020-08-12 10:18:45 -04:00
|
|
|
false,
|
|
|
|
error => {
|
|
|
|
expect(error).to.exist
|
|
|
|
expect(error.name).to.equal('UnconfirmedEmailError')
|
|
|
|
this.UserUpdater.promises.updateUser.callCount.should.equal(0)
|
|
|
|
this.NewsletterManager.promises.changeEmail.callCount.should.equal(
|
|
|
|
0
|
|
|
|
)
|
|
|
|
}
|
2019-07-22 12:55:35 -04:00
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-08-07 10:04:04 -04:00
|
|
|
describe('when email does not belong to user', function() {
|
2019-07-22 12:55:35 -04:00
|
|
|
beforeEach(function() {
|
|
|
|
this.stubbedUser.emails = []
|
2020-08-12 10:18:45 -04:00
|
|
|
this.UserGetter.promises.getUser.resolves(this.stubbedUser)
|
|
|
|
this.UserUpdater.promises.updateUser = sinon.stub()
|
2019-07-22 12:55:35 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should callback with error', function() {
|
|
|
|
this.UserUpdater.setDefaultEmailAddress(
|
|
|
|
this.stubbedUser._id,
|
|
|
|
this.newEmail,
|
2020-08-12 10:18:45 -04:00
|
|
|
false,
|
|
|
|
error => {
|
|
|
|
expect(error).to.exist
|
|
|
|
expect(error.name).to.equal('Error')
|
|
|
|
this.UserUpdater.promises.updateUser.callCount.should.equal(0)
|
|
|
|
this.NewsletterManager.promises.changeEmail.callCount.should.equal(
|
|
|
|
0
|
|
|
|
)
|
|
|
|
}
|
2019-07-22 12:55:35 -04:00
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2019-06-21 09:46:09 -04:00
|
|
|
describe('confirmEmail', function() {
|
2019-05-29 05:21:06 -04:00
|
|
|
beforeEach(function() {
|
2020-02-20 11:08:40 -05:00
|
|
|
this.UserUpdater.updateUser = sinon.stub().callsArgWith(2, null, { n: 1 })
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should update the email record', function(done) {
|
2020-02-20 11:08:40 -05:00
|
|
|
this.UserUpdater.confirmEmail(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.stubbedUser._id,
|
|
|
|
this.newEmail,
|
|
|
|
err => {
|
|
|
|
should.not.exist(err)
|
|
|
|
this.UserUpdater.updateUser
|
|
|
|
.calledWith(
|
|
|
|
{ _id: this.stubbedUser._id, 'emails.email': this.newEmail },
|
|
|
|
{ $set: { 'emails.$.confirmedAt': new Date() } }
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
2020-02-20 11:08:40 -05:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('add affiliation', function(done) {
|
2020-02-20 11:08:40 -05:00
|
|
|
this.UserUpdater.confirmEmail(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.stubbedUser._id,
|
|
|
|
this.newEmail,
|
|
|
|
err => {
|
|
|
|
should.not.exist(err)
|
|
|
|
this.addAffiliation.calledOnce.should.equal(true)
|
|
|
|
sinon.assert.calledWith(
|
|
|
|
this.addAffiliation,
|
|
|
|
this.stubbedUser._id,
|
|
|
|
this.newEmail,
|
|
|
|
{ confirmedAt: new Date() }
|
|
|
|
)
|
2020-02-20 11:08:40 -05:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('handle error', function(done) {
|
|
|
|
this.UserUpdater.updateUser = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(2, new Error('nope'))
|
|
|
|
|
2020-02-20 11:08:40 -05:00
|
|
|
this.UserUpdater.confirmEmail(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.stubbedUser._id,
|
|
|
|
this.newEmail,
|
|
|
|
err => {
|
|
|
|
should.exist(err)
|
2020-02-20 11:08:40 -05:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('handle missed update', function(done) {
|
|
|
|
this.UserUpdater.updateUser = sinon.stub().callsArgWith(2, null, { n: 0 })
|
|
|
|
|
2020-02-20 11:08:40 -05:00
|
|
|
this.UserUpdater.confirmEmail(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.stubbedUser._id,
|
|
|
|
this.newEmail,
|
|
|
|
err => {
|
|
|
|
should.exist(err)
|
2020-02-20 11:08:40 -05:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('validates email', function(done) {
|
2020-02-20 11:08:40 -05:00
|
|
|
this.UserUpdater.confirmEmail(this.stubbedUser._id, '@', err => {
|
2019-05-29 05:21:06 -04:00
|
|
|
should.exist(err)
|
2020-02-20 11:08:40 -05:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('handle affiliation error', function(done) {
|
|
|
|
this.addAffiliation.callsArgWith(3, new Error('nope'))
|
2020-02-20 11:08:40 -05:00
|
|
|
this.UserUpdater.confirmEmail(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.stubbedUser._id,
|
|
|
|
this.newEmail,
|
|
|
|
err => {
|
|
|
|
should.exist(err)
|
|
|
|
this.UserUpdater.updateUser.called.should.equal(false)
|
2020-02-20 11:08:40 -05:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2019-06-21 09:46:09 -04:00
|
|
|
it('refresh features', function(done) {
|
2020-02-20 11:08:40 -05:00
|
|
|
this.UserUpdater.confirmEmail(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.stubbedUser._id,
|
|
|
|
this.newEmail,
|
|
|
|
err => {
|
|
|
|
should.not.exist(err)
|
2019-06-11 09:14:38 -04:00
|
|
|
sinon.assert.calledWith(this.refreshFeatures, this.stubbedUser._id)
|
2020-02-20 11:08:40 -05:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|