2019-09-18 11:22:27 -04:00
|
|
|
const SandboxedModule = require('sandboxed-module')
|
2019-05-29 05:21:06 -04:00
|
|
|
const chai = require('chai')
|
2019-09-18 11:22:27 -04:00
|
|
|
const sinon = require('sinon')
|
|
|
|
|
|
|
|
const assert = chai.assert
|
2019-05-29 05:21:06 -04:00
|
|
|
const modulePath = '../../../../app/src/Features/User/UserCreator.js'
|
|
|
|
|
|
|
|
describe('UserCreator', function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
const self = this
|
|
|
|
this.user = { _id: '12390i', ace: {} }
|
2019-09-18 11:22:27 -04:00
|
|
|
this.user.save = sinon.stub().resolves(self.user)
|
|
|
|
this.UserModel = class Project {
|
2019-05-29 05:21:06 -04:00
|
|
|
constructor() {
|
|
|
|
return self.user
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.UserGetter = { getUserByMainEmail: sinon.stub() }
|
|
|
|
this.addAffiliation = sinon.stub().yields()
|
|
|
|
this.UserCreator = SandboxedModule.require(modulePath, {
|
2019-07-15 06:33:47 -04:00
|
|
|
globals: {
|
|
|
|
console: console
|
|
|
|
},
|
2019-05-29 05:21:06 -04:00
|
|
|
requires: {
|
|
|
|
'../../models/User': {
|
|
|
|
User: this.UserModel
|
|
|
|
},
|
|
|
|
'logger-sharelatex': { log: sinon.stub(), err: sinon.stub() },
|
|
|
|
'metrics-sharelatex': { timeAsyncMethod() {} },
|
|
|
|
'../Institutions/InstitutionsAPI': {
|
|
|
|
addAffiliation: this.addAffiliation
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2019-09-18 11:22:27 -04:00
|
|
|
this.email = 'bob.oswald@gmail.com'
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2019-06-21 09:46:09 -04:00
|
|
|
describe('createNewUser', function() {
|
2019-09-18 11:22:27 -04:00
|
|
|
describe('with callbacks', function() {
|
|
|
|
it('should take the opts and put them in the model', function(done) {
|
|
|
|
const opts = {
|
|
|
|
email: this.email,
|
|
|
|
holdingAccount: true
|
|
|
|
}
|
|
|
|
this.UserCreator.createNewUser(opts, (err, user) => {
|
|
|
|
assert.ifError(err)
|
|
|
|
assert.equal(user.email, this.email)
|
|
|
|
assert.equal(user.holdingAccount, true)
|
|
|
|
assert.equal(user.first_name, 'bob.oswald')
|
|
|
|
done()
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2019-09-18 11:22:27 -04:00
|
|
|
it('should use the start of the email if the first name is empty string', function(done) {
|
|
|
|
const opts = {
|
|
|
|
email: this.email,
|
|
|
|
holdingAccount: true,
|
|
|
|
first_name: ''
|
|
|
|
}
|
|
|
|
this.UserCreator.createNewUser(opts, (err, user) => {
|
|
|
|
assert.ifError(err)
|
|
|
|
assert.equal(user.email, this.email)
|
|
|
|
assert.equal(user.holdingAccount, true)
|
|
|
|
assert.equal(user.first_name, 'bob.oswald')
|
|
|
|
done()
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2019-09-18 11:22:27 -04:00
|
|
|
it('should use the first name if passed', function(done) {
|
|
|
|
const opts = {
|
|
|
|
email: this.email,
|
|
|
|
holdingAccount: true,
|
|
|
|
first_name: 'fiiirstname'
|
|
|
|
}
|
|
|
|
this.UserCreator.createNewUser(opts, (err, user) => {
|
|
|
|
assert.ifError(err)
|
|
|
|
assert.equal(user.email, this.email)
|
|
|
|
assert.equal(user.holdingAccount, true)
|
|
|
|
assert.equal(user.first_name, 'fiiirstname')
|
|
|
|
done()
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2019-09-18 11:22:27 -04:00
|
|
|
it('should use the last name if passed', function(done) {
|
|
|
|
const opts = {
|
|
|
|
email: this.email,
|
|
|
|
holdingAccount: true,
|
|
|
|
last_name: 'lastNammmmeee'
|
|
|
|
}
|
|
|
|
this.UserCreator.createNewUser(opts, (err, user) => {
|
|
|
|
assert.ifError(err)
|
|
|
|
assert.equal(user.email, this.email)
|
|
|
|
assert.equal(user.holdingAccount, true)
|
|
|
|
assert.equal(user.last_name, 'lastNammmmeee')
|
|
|
|
done()
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2019-09-18 11:22:27 -04:00
|
|
|
it('should set emails attribute', function(done) {
|
|
|
|
this.UserCreator.createNewUser({ email: this.email }, (err, user) => {
|
|
|
|
assert.ifError(err)
|
2019-05-29 05:21:06 -04:00
|
|
|
user.email.should.equal(this.email)
|
|
|
|
user.emails.length.should.equal(1)
|
|
|
|
user.emails[0].email.should.equal(this.email)
|
|
|
|
user.emails[0].createdAt.should.be.a('date')
|
|
|
|
user.emails[0].reversedHostname.should.equal('moc.liamg')
|
2019-09-18 11:22:27 -04:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2019-09-18 11:22:27 -04:00
|
|
|
it('should add affiliation', function(done) {
|
|
|
|
const attributes = { email: this.email }
|
|
|
|
this.UserCreator.createNewUser(attributes, (err, user) => {
|
|
|
|
assert.ifError(err)
|
|
|
|
sinon.assert.calledOnce(this.addAffiliation)
|
|
|
|
sinon.assert.calledWithMatch(
|
|
|
|
this.addAffiliation,
|
|
|
|
user._id,
|
|
|
|
this.email
|
|
|
|
)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2019-09-18 11:22:27 -04:00
|
|
|
it('should not add affiliation if skipping', function(done) {
|
|
|
|
const attributes = { email: this.email }
|
|
|
|
const options = { skip_affiliation: true }
|
|
|
|
this.UserCreator.createNewUser(attributes, options, (err, user) => {
|
|
|
|
assert.ifError(err)
|
|
|
|
process.nextTick(() => {
|
2019-05-29 05:21:06 -04:00
|
|
|
sinon.assert.notCalled(this.addAffiliation)
|
2019-09-18 11:22:27 -04:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
2019-09-18 11:22:27 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('with promises', function() {
|
|
|
|
it('should take the opts and put them in the model', async function() {
|
|
|
|
const opts = {
|
|
|
|
email: this.email,
|
|
|
|
holdingAccount: true
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2019-09-18 11:22:27 -04:00
|
|
|
const user = await this.UserCreator.promises.createNewUser(opts)
|
|
|
|
assert.equal(user.email, this.email)
|
|
|
|
assert.equal(user.holdingAccount, true)
|
|
|
|
assert.equal(user.first_name, 'bob.oswald')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should add affiliation', async function() {
|
|
|
|
const attributes = { email: this.email }
|
|
|
|
const user = await this.UserCreator.promises.createNewUser(attributes)
|
|
|
|
sinon.assert.calledOnce(this.addAffiliation)
|
|
|
|
sinon.assert.calledWithMatch(this.addAffiliation, user._id, this.email)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should not add affiliation if skipping', async function() {
|
|
|
|
const attributes = { email: this.email }
|
|
|
|
const opts = { skip_affiliation: true }
|
|
|
|
await this.UserCreator.promises.createNewUser(attributes, opts)
|
|
|
|
sinon.assert.notCalled(this.addAffiliation)
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|