|
|
@@ -1,26 +1,13 @@
|
|
|
-/* eslint-disable
|
|
|
- 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 should = require('chai').should()
|
|
|
const SandboxedModule = require('sandboxed-module')
|
|
|
-const assert = require('assert')
|
|
|
const path = require('path')
|
|
|
const sinon = require('sinon')
|
|
|
const modulePath = path.join(
|
|
|
__dirname,
|
|
|
'../../../../app/src/Features/User/UserUpdater'
|
|
|
)
|
|
|
-const { expect } = require('chai')
|
|
|
const tk = require('timekeeper')
|
|
|
+const { expect } = require('chai')
|
|
|
|
|
|
describe('UserUpdater', function() {
|
|
|
beforeEach(function() {
|
|
|
@@ -37,7 +24,7 @@ describe('UserUpdater', function() {
|
|
|
ensureUniqueEmailAddress: sinon.stub()
|
|
|
}
|
|
|
this.logger = {
|
|
|
- err: sinon.stub(),
|
|
|
+ error: sinon.stub(),
|
|
|
log() {},
|
|
|
warn: sinon.stub()
|
|
|
}
|
|
|
@@ -60,6 +47,9 @@ describe('UserUpdater', function() {
|
|
|
addAffiliation: this.addAffiliation,
|
|
|
removeAffiliation: this.removeAffiliation
|
|
|
},
|
|
|
+ '../../infrastructure/Features': (this.Features = {
|
|
|
+ hasFeature: sinon.stub().returns(false)
|
|
|
+ }),
|
|
|
'../Subscription/FeaturesUpdater': {
|
|
|
refreshFeatures: this.refreshFeatures
|
|
|
},
|
|
|
@@ -82,18 +72,55 @@ describe('UserUpdater', function() {
|
|
|
return tk.reset()
|
|
|
})
|
|
|
|
|
|
+ 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,
|
|
|
+ (error, updated) => {
|
|
|
+ should.not.exist(error)
|
|
|
+ expect(updated).to.deep.equal({ n: 1, nModified: 1, ok: 1 })
|
|
|
+ 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()
|
|
|
+ }
|
|
|
+ )
|
|
|
+ })
|
|
|
+ })
|
|
|
+
|
|
|
describe('changeEmailAddress', function() {
|
|
|
beforeEach(function() {
|
|
|
this.UserGetter.getUserEmail.callsArgWith(1, null, this.stubbedUser.email)
|
|
|
this.UserUpdater.addEmailAddress = sinon.stub().callsArgWith(2)
|
|
|
this.UserUpdater.setDefaultEmailAddress = sinon.stub().yields()
|
|
|
- return (this.UserUpdater.removeEmailAddress = sinon
|
|
|
- .stub()
|
|
|
- .callsArgWith(2))
|
|
|
+ this.UserUpdater.removeEmailAddress = sinon.stub().callsArgWith(2)
|
|
|
})
|
|
|
|
|
|
it('change email', function(done) {
|
|
|
- return this.UserUpdater.changeEmailAddress(
|
|
|
+ this.UserUpdater.changeEmailAddress(
|
|
|
this.stubbedUser._id,
|
|
|
this.newEmail,
|
|
|
err => {
|
|
|
@@ -107,30 +134,26 @@ describe('UserUpdater', function() {
|
|
|
this.UserUpdater.removeEmailAddress
|
|
|
.calledWith(this.stubbedUser._id, this.stubbedUser.email)
|
|
|
.should.equal(true)
|
|
|
- return done()
|
|
|
+ done()
|
|
|
}
|
|
|
)
|
|
|
})
|
|
|
|
|
|
it('validates email', function(done) {
|
|
|
- return this.UserUpdater.changeEmailAddress(
|
|
|
- this.stubbedUser._id,
|
|
|
- 'foo',
|
|
|
- err => {
|
|
|
- should.exist(err)
|
|
|
- return done()
|
|
|
- }
|
|
|
- )
|
|
|
+ this.UserUpdater.changeEmailAddress(this.stubbedUser._id, 'foo', err => {
|
|
|
+ should.exist(err)
|
|
|
+ done()
|
|
|
+ })
|
|
|
})
|
|
|
|
|
|
it('handle error', function(done) {
|
|
|
this.UserUpdater.removeEmailAddress.callsArgWith(2, new Error('nope'))
|
|
|
- return this.UserUpdater.changeEmailAddress(
|
|
|
+ this.UserUpdater.changeEmailAddress(
|
|
|
this.stubbedUser._id,
|
|
|
this.newEmail,
|
|
|
err => {
|
|
|
should.exist(err)
|
|
|
- return done()
|
|
|
+ done()
|
|
|
}
|
|
|
)
|
|
|
})
|
|
|
@@ -139,11 +162,11 @@ describe('UserUpdater', function() {
|
|
|
describe('addEmailAddress', function() {
|
|
|
beforeEach(function() {
|
|
|
this.UserGetter.ensureUniqueEmailAddress = sinon.stub().callsArgWith(1)
|
|
|
- return (this.UserUpdater.updateUser = sinon.stub().callsArgWith(2, null))
|
|
|
+ this.UserUpdater.updateUser = sinon.stub().callsArgWith(2, null)
|
|
|
})
|
|
|
|
|
|
it('add email', function(done) {
|
|
|
- return this.UserUpdater.addEmailAddress(
|
|
|
+ this.UserUpdater.addEmailAddress(
|
|
|
this.stubbedUser._id,
|
|
|
this.newEmail,
|
|
|
err => {
|
|
|
@@ -165,7 +188,7 @@ describe('UserUpdater', function() {
|
|
|
}
|
|
|
})
|
|
|
.should.equal(true)
|
|
|
- return done()
|
|
|
+ done()
|
|
|
}
|
|
|
)
|
|
|
})
|
|
|
@@ -176,7 +199,7 @@ describe('UserUpdater', function() {
|
|
|
role: 'Prof',
|
|
|
department: 'Math'
|
|
|
}
|
|
|
- return this.UserUpdater.addEmailAddress(
|
|
|
+ this.UserUpdater.addEmailAddress(
|
|
|
this.stubbedUser._id,
|
|
|
this.newEmail,
|
|
|
affiliationOptions,
|
|
|
@@ -187,7 +210,7 @@ describe('UserUpdater', function() {
|
|
|
args[0].should.equal(this.stubbedUser._id)
|
|
|
args[1].should.equal(this.newEmail)
|
|
|
args[2].should.equal(affiliationOptions)
|
|
|
- return done()
|
|
|
+ done()
|
|
|
}
|
|
|
)
|
|
|
})
|
|
|
@@ -197,52 +220,47 @@ describe('UserUpdater', function() {
|
|
|
.stub()
|
|
|
.callsArgWith(2, new Error('nope'))
|
|
|
|
|
|
- return this.UserUpdater.addEmailAddress(
|
|
|
+ this.UserUpdater.addEmailAddress(
|
|
|
this.stubbedUser._id,
|
|
|
this.newEmail,
|
|
|
err => {
|
|
|
this.logger.warn.called.should.equal(true)
|
|
|
should.exist(err)
|
|
|
- return done()
|
|
|
+ done()
|
|
|
}
|
|
|
)
|
|
|
})
|
|
|
|
|
|
it('handle affiliation error', function(done) {
|
|
|
- const body = { errors: 'affiliation error message' }
|
|
|
this.addAffiliation.callsArgWith(3, new Error('nope'))
|
|
|
- return this.UserUpdater.addEmailAddress(
|
|
|
+ this.UserUpdater.addEmailAddress(
|
|
|
this.stubbedUser._id,
|
|
|
this.newEmail,
|
|
|
err => {
|
|
|
should.exist(err)
|
|
|
this.UserUpdater.updateUser.called.should.equal(false)
|
|
|
- return done()
|
|
|
+ done()
|
|
|
}
|
|
|
)
|
|
|
})
|
|
|
|
|
|
it('validates email', function(done) {
|
|
|
- return this.UserUpdater.addEmailAddress(
|
|
|
- this.stubbedUser._id,
|
|
|
- 'bar',
|
|
|
- err => {
|
|
|
- should.exist(err)
|
|
|
- return done()
|
|
|
- }
|
|
|
- )
|
|
|
+ this.UserUpdater.addEmailAddress(this.stubbedUser._id, 'bar', err => {
|
|
|
+ should.exist(err)
|
|
|
+ done()
|
|
|
+ })
|
|
|
})
|
|
|
})
|
|
|
|
|
|
describe('removeEmailAddress', function() {
|
|
|
beforeEach(function() {
|
|
|
- return (this.UserUpdater.updateUser = sinon
|
|
|
+ this.UserUpdater.updateUser = sinon
|
|
|
.stub()
|
|
|
- .callsArgWith(2, null, { nMatched: 1 }))
|
|
|
+ .callsArgWith(2, null, { nMatched: 1 })
|
|
|
})
|
|
|
|
|
|
it('remove email', function(done) {
|
|
|
- return this.UserUpdater.removeEmailAddress(
|
|
|
+ this.UserUpdater.removeEmailAddress(
|
|
|
this.stubbedUser._id,
|
|
|
this.newEmail,
|
|
|
err => {
|
|
|
@@ -253,13 +271,13 @@ describe('UserUpdater', function() {
|
|
|
{ $pull: { emails: { email: this.newEmail } } }
|
|
|
)
|
|
|
.should.equal(true)
|
|
|
- return done()
|
|
|
+ done()
|
|
|
}
|
|
|
)
|
|
|
})
|
|
|
|
|
|
it('remove affiliation', function(done) {
|
|
|
- return this.UserUpdater.removeEmailAddress(
|
|
|
+ this.UserUpdater.removeEmailAddress(
|
|
|
this.stubbedUser._id,
|
|
|
this.newEmail,
|
|
|
err => {
|
|
|
@@ -268,7 +286,7 @@ describe('UserUpdater', function() {
|
|
|
const { args } = this.removeAffiliation.lastCall
|
|
|
args[0].should.equal(this.stubbedUser._id)
|
|
|
args[1].should.equal(this.newEmail)
|
|
|
- return done()
|
|
|
+ done()
|
|
|
}
|
|
|
)
|
|
|
})
|
|
|
@@ -278,12 +296,12 @@ describe('UserUpdater', function() {
|
|
|
.stub()
|
|
|
.callsArgWith(2, new Error('nope'))
|
|
|
|
|
|
- return this.UserUpdater.removeEmailAddress(
|
|
|
+ this.UserUpdater.removeEmailAddress(
|
|
|
this.stubbedUser._id,
|
|
|
this.newEmail,
|
|
|
err => {
|
|
|
should.exist(err)
|
|
|
- return done()
|
|
|
+ done()
|
|
|
}
|
|
|
)
|
|
|
})
|
|
|
@@ -291,38 +309,34 @@ describe('UserUpdater', function() {
|
|
|
it('handle missed update', function(done) {
|
|
|
this.UserUpdater.updateUser = sinon.stub().callsArgWith(2, null, { n: 0 })
|
|
|
|
|
|
- return this.UserUpdater.removeEmailAddress(
|
|
|
+ this.UserUpdater.removeEmailAddress(
|
|
|
this.stubbedUser._id,
|
|
|
this.newEmail,
|
|
|
err => {
|
|
|
should.exist(err)
|
|
|
- return done()
|
|
|
+ done()
|
|
|
}
|
|
|
)
|
|
|
})
|
|
|
|
|
|
it('handle affiliation error', function(done) {
|
|
|
this.removeAffiliation.callsArgWith(2, new Error('nope'))
|
|
|
- return this.UserUpdater.removeEmailAddress(
|
|
|
+ this.UserUpdater.removeEmailAddress(
|
|
|
this.stubbedUser._id,
|
|
|
this.newEmail,
|
|
|
err => {
|
|
|
should.exist(err)
|
|
|
this.UserUpdater.updateUser.called.should.equal(false)
|
|
|
- return done()
|
|
|
+ done()
|
|
|
}
|
|
|
)
|
|
|
})
|
|
|
|
|
|
it('validates email', function(done) {
|
|
|
- return this.UserUpdater.removeEmailAddress(
|
|
|
- this.stubbedUser._id,
|
|
|
- 'baz',
|
|
|
- err => {
|
|
|
- should.exist(err)
|
|
|
- return done()
|
|
|
- }
|
|
|
- )
|
|
|
+ this.UserUpdater.removeEmailAddress(this.stubbedUser._id, 'baz', err => {
|
|
|
+ should.exist(err)
|
|
|
+ done()
|
|
|
+ })
|
|
|
})
|
|
|
})
|
|
|
|
|
|
@@ -462,13 +476,11 @@ describe('UserUpdater', function() {
|
|
|
|
|
|
describe('confirmEmail', function() {
|
|
|
beforeEach(function() {
|
|
|
- return (this.UserUpdater.updateUser = sinon
|
|
|
- .stub()
|
|
|
- .callsArgWith(2, null, { n: 1 }))
|
|
|
+ this.UserUpdater.updateUser = sinon.stub().callsArgWith(2, null, { n: 1 })
|
|
|
})
|
|
|
|
|
|
it('should update the email record', function(done) {
|
|
|
- return this.UserUpdater.confirmEmail(
|
|
|
+ this.UserUpdater.confirmEmail(
|
|
|
this.stubbedUser._id,
|
|
|
this.newEmail,
|
|
|
err => {
|
|
|
@@ -479,13 +491,13 @@ describe('UserUpdater', function() {
|
|
|
{ $set: { 'emails.$.confirmedAt': new Date() } }
|
|
|
)
|
|
|
.should.equal(true)
|
|
|
- return done()
|
|
|
+ done()
|
|
|
}
|
|
|
)
|
|
|
})
|
|
|
|
|
|
it('add affiliation', function(done) {
|
|
|
- return this.UserUpdater.confirmEmail(
|
|
|
+ this.UserUpdater.confirmEmail(
|
|
|
this.stubbedUser._id,
|
|
|
this.newEmail,
|
|
|
err => {
|
|
|
@@ -497,7 +509,7 @@ describe('UserUpdater', function() {
|
|
|
this.newEmail,
|
|
|
{ confirmedAt: new Date() }
|
|
|
)
|
|
|
- return done()
|
|
|
+ done()
|
|
|
}
|
|
|
)
|
|
|
})
|
|
|
@@ -507,12 +519,12 @@ describe('UserUpdater', function() {
|
|
|
.stub()
|
|
|
.callsArgWith(2, new Error('nope'))
|
|
|
|
|
|
- return this.UserUpdater.confirmEmail(
|
|
|
+ this.UserUpdater.confirmEmail(
|
|
|
this.stubbedUser._id,
|
|
|
this.newEmail,
|
|
|
err => {
|
|
|
should.exist(err)
|
|
|
- return done()
|
|
|
+ done()
|
|
|
}
|
|
|
)
|
|
|
})
|
|
|
@@ -520,44 +532,44 @@ describe('UserUpdater', function() {
|
|
|
it('handle missed update', function(done) {
|
|
|
this.UserUpdater.updateUser = sinon.stub().callsArgWith(2, null, { n: 0 })
|
|
|
|
|
|
- return this.UserUpdater.confirmEmail(
|
|
|
+ this.UserUpdater.confirmEmail(
|
|
|
this.stubbedUser._id,
|
|
|
this.newEmail,
|
|
|
err => {
|
|
|
should.exist(err)
|
|
|
- return done()
|
|
|
+ done()
|
|
|
}
|
|
|
)
|
|
|
})
|
|
|
|
|
|
it('validates email', function(done) {
|
|
|
- return this.UserUpdater.confirmEmail(this.stubbedUser._id, '@', err => {
|
|
|
+ this.UserUpdater.confirmEmail(this.stubbedUser._id, '@', err => {
|
|
|
should.exist(err)
|
|
|
- return done()
|
|
|
+ done()
|
|
|
})
|
|
|
})
|
|
|
|
|
|
it('handle affiliation error', function(done) {
|
|
|
this.addAffiliation.callsArgWith(3, new Error('nope'))
|
|
|
- return this.UserUpdater.confirmEmail(
|
|
|
+ this.UserUpdater.confirmEmail(
|
|
|
this.stubbedUser._id,
|
|
|
this.newEmail,
|
|
|
err => {
|
|
|
should.exist(err)
|
|
|
this.UserUpdater.updateUser.called.should.equal(false)
|
|
|
- return done()
|
|
|
+ done()
|
|
|
}
|
|
|
)
|
|
|
})
|
|
|
|
|
|
it('refresh features', function(done) {
|
|
|
- return this.UserUpdater.confirmEmail(
|
|
|
+ this.UserUpdater.confirmEmail(
|
|
|
this.stubbedUser._id,
|
|
|
this.newEmail,
|
|
|
err => {
|
|
|
should.not.exist(err)
|
|
|
sinon.assert.calledWith(this.refreshFeatures, this.stubbedUser._id)
|
|
|
- return done()
|
|
|
+ done()
|
|
|
}
|
|
|
)
|
|
|
})
|