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.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
|
|
|
|
},
|
2020-09-01 08:37:09 -04:00
|
|
|
'logger-sharelatex': (this.Logger = {
|
2020-02-20 11:08:40 -05:00
|
|
|
error: sinon.stub()
|
2020-09-01 08:37:09 -04:00
|
|
|
}),
|
2019-05-29 05:21:06 -04:00
|
|
|
'metrics-sharelatex': { timeAsyncMethod() {} },
|
2020-02-20 11:08:40 -05:00
|
|
|
'../../infrastructure/Features': (this.Features = {
|
|
|
|
hasFeature: sinon.stub().returns(false)
|
|
|
|
}),
|
2020-09-01 08:37:09 -04:00
|
|
|
'./UserDeleter': (this.UserDeleter = {
|
|
|
|
promises: {
|
|
|
|
deleteNewUser: sinon.stub().resolves()
|
|
|
|
}
|
|
|
|
}),
|
2020-02-20 11:08:40 -05:00
|
|
|
'./UserGetter': (this.UserGetter = {
|
|
|
|
promises: {
|
|
|
|
getUser: sinon.stub().resolves(this.user)
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
'./UserUpdater': (this.UserUpdater = {
|
|
|
|
promises: {
|
|
|
|
addAffiliationForNewUser: sinon
|
|
|
|
.stub()
|
|
|
|
.resolves({ n: 1, nModified: 1, ok: 1 }),
|
|
|
|
updateUser: sinon.stub().resolves()
|
|
|
|
}
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
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() {
|
2020-09-01 08:37:09 -04:00
|
|
|
it('should take the opts and put them in the model', async function() {
|
|
|
|
const user = await this.UserCreator.promises.createNewUser({
|
2019-09-18 11:22:27 -04:00
|
|
|
email: this.email,
|
|
|
|
holdingAccount: true
|
|
|
|
})
|
2020-09-01 08:37:09 -04:00
|
|
|
assert.equal(user.email, this.email)
|
|
|
|
assert.equal(user.holdingAccount, true)
|
|
|
|
assert.equal(user.first_name, 'bob.oswald')
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2020-09-01 08:37:09 -04:00
|
|
|
it('should use the start of the email if the first name is empty string', async function() {
|
|
|
|
const user = await this.UserCreator.promises.createNewUser({
|
2019-09-18 11:22:27 -04:00
|
|
|
email: this.email,
|
|
|
|
holdingAccount: true,
|
|
|
|
first_name: ''
|
|
|
|
})
|
2020-09-01 08:37:09 -04:00
|
|
|
assert.equal(user.email, this.email)
|
|
|
|
assert.equal(user.holdingAccount, true)
|
|
|
|
assert.equal(user.first_name, 'bob.oswald')
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2020-09-01 08:37:09 -04:00
|
|
|
it('should use the first name if passed', async function() {
|
|
|
|
const user = await this.UserCreator.promises.createNewUser({
|
2019-09-18 11:22:27 -04:00
|
|
|
email: this.email,
|
|
|
|
holdingAccount: true,
|
|
|
|
first_name: 'fiiirstname'
|
|
|
|
})
|
2020-09-01 08:37:09 -04:00
|
|
|
assert.equal(user.email, this.email)
|
|
|
|
assert.equal(user.holdingAccount, true)
|
|
|
|
assert.equal(user.first_name, 'fiiirstname')
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2020-09-01 08:37:09 -04:00
|
|
|
it('should use the last name if passed', async function() {
|
|
|
|
const user = await this.UserCreator.promises.createNewUser({
|
2019-09-18 11:22:27 -04:00
|
|
|
email: this.email,
|
|
|
|
holdingAccount: true,
|
|
|
|
last_name: 'lastNammmmeee'
|
|
|
|
})
|
2020-09-01 08:37:09 -04:00
|
|
|
assert.equal(user.email, this.email)
|
|
|
|
assert.equal(user.holdingAccount, true)
|
|
|
|
assert.equal(user.last_name, 'lastNammmmeee')
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2020-09-01 08:37:09 -04:00
|
|
|
it('should set emails attribute', async function() {
|
|
|
|
const user = await this.UserCreator.promises.createNewUser({
|
|
|
|
email: this.email
|
2019-09-18 11:22:27 -04:00
|
|
|
})
|
2020-09-01 08:37:09 -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
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2020-02-20 11:08:40 -05:00
|
|
|
describe('with affiliations feature', function() {
|
|
|
|
let attributes, user
|
|
|
|
beforeEach(function() {
|
|
|
|
attributes = { email: this.email }
|
|
|
|
this.Features.hasFeature = sinon
|
|
|
|
.stub()
|
|
|
|
.withArgs('affiliations')
|
|
|
|
.returns(true)
|
|
|
|
})
|
2020-09-01 08:37:09 -04:00
|
|
|
|
2020-02-20 11:08:40 -05:00
|
|
|
describe('when v1 affiliations API does not return an error', function() {
|
2020-09-01 08:37:09 -04:00
|
|
|
beforeEach(async function() {
|
|
|
|
user = await this.UserCreator.promises.createNewUser(attributes)
|
2020-02-20 11:08:40 -05:00
|
|
|
})
|
2020-09-01 08:37:09 -04:00
|
|
|
|
2020-02-20 11:08:40 -05:00
|
|
|
it('should flag that affiliation is unchecked', function() {
|
|
|
|
user.emails[0].affiliationUnchecked.should.equal(true)
|
|
|
|
})
|
2020-09-01 08:37:09 -04:00
|
|
|
|
2020-02-20 11:08:40 -05:00
|
|
|
it('should try to add affiliation to v1', function() {
|
|
|
|
sinon.assert.calledOnce(
|
|
|
|
this.UserUpdater.promises.addAffiliationForNewUser
|
|
|
|
)
|
|
|
|
sinon.assert.calledWithMatch(
|
|
|
|
this.UserUpdater.promises.addAffiliationForNewUser,
|
|
|
|
user._id,
|
|
|
|
this.email
|
|
|
|
)
|
|
|
|
})
|
2020-09-01 08:37:09 -04:00
|
|
|
|
2020-02-20 11:08:40 -05:00
|
|
|
it('should query for updated user data', function() {
|
|
|
|
sinon.assert.calledOnce(this.UserGetter.promises.getUser)
|
|
|
|
})
|
|
|
|
})
|
2020-09-01 08:37:09 -04:00
|
|
|
|
2020-02-20 11:08:40 -05:00
|
|
|
describe('when v1 affiliations API does return an error', function() {
|
2020-09-01 08:37:09 -04:00
|
|
|
beforeEach(async function() {
|
2020-02-20 11:08:40 -05:00
|
|
|
this.UserUpdater.promises.addAffiliationForNewUser.rejects()
|
2020-09-01 08:37:09 -04:00
|
|
|
user = await this.UserCreator.promises.createNewUser(attributes)
|
2020-02-20 11:08:40 -05:00
|
|
|
})
|
2020-09-01 08:37:09 -04:00
|
|
|
|
2020-02-20 11:08:40 -05:00
|
|
|
it('should flag that affiliation is unchecked', function() {
|
|
|
|
user.emails[0].affiliationUnchecked.should.equal(true)
|
|
|
|
})
|
2020-09-01 08:37:09 -04:00
|
|
|
|
2020-02-20 11:08:40 -05:00
|
|
|
it('should try to add affiliation to v1', function() {
|
|
|
|
sinon.assert.calledOnce(
|
|
|
|
this.UserUpdater.promises.addAffiliationForNewUser
|
|
|
|
)
|
|
|
|
sinon.assert.calledWithMatch(
|
|
|
|
this.UserUpdater.promises.addAffiliationForNewUser,
|
|
|
|
user._id,
|
|
|
|
this.email
|
|
|
|
)
|
|
|
|
})
|
2020-09-01 08:37:09 -04:00
|
|
|
|
|
|
|
it('should not query for updated user data', function() {
|
|
|
|
sinon.assert.notCalled(this.UserGetter.promises.getUser)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should log error', function() {
|
|
|
|
sinon.assert.calledOnce(this.Logger.error)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('when v1 affiliations API returns an error and requireAffiliation=true', function() {
|
|
|
|
beforeEach(async function() {
|
|
|
|
this.UserUpdater.promises.addAffiliationForNewUser.rejects()
|
|
|
|
user = await this.UserCreator.promises.createNewUser(attributes)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should flag that affiliation is unchecked', function() {
|
|
|
|
user.emails[0].affiliationUnchecked.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should try to add affiliation to v1', function() {
|
|
|
|
sinon.assert.calledOnce(
|
|
|
|
this.UserUpdater.promises.addAffiliationForNewUser
|
|
|
|
)
|
|
|
|
sinon.assert.calledWithMatch(
|
|
|
|
this.UserUpdater.promises.addAffiliationForNewUser,
|
|
|
|
user._id,
|
|
|
|
this.email
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should not query for updated user data', function() {
|
|
|
|
sinon.assert.notCalled(this.UserGetter.promises.getUser)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should log error', function() {
|
|
|
|
sinon.assert.calledOnce(this.Logger.error)
|
2020-02-20 11:08:40 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-09-01 08:37:09 -04:00
|
|
|
it('should not add affiliation when without affiliation feature', async function() {
|
2019-09-18 11:22:27 -04:00
|
|
|
const attributes = { email: this.email }
|
2020-09-01 08:37:09 -04:00
|
|
|
await this.UserCreator.promises.createNewUser(attributes)
|
|
|
|
sinon.assert.notCalled(
|
|
|
|
this.UserUpdater.promises.addAffiliationForNewUser
|
|
|
|
)
|
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')
|
|
|
|
})
|
|
|
|
|
2020-02-20 11:08:40 -05:00
|
|
|
it('should add affiliation when with affiliation feature', async function() {
|
|
|
|
this.Features.hasFeature = sinon
|
|
|
|
.stub()
|
|
|
|
.withArgs('affiliations')
|
|
|
|
.returns(true)
|
2019-09-18 11:22:27 -04:00
|
|
|
const attributes = { email: this.email }
|
|
|
|
const user = await this.UserCreator.promises.createNewUser(attributes)
|
2020-02-20 11:08:40 -05:00
|
|
|
sinon.assert.calledOnce(
|
|
|
|
this.UserUpdater.promises.addAffiliationForNewUser
|
|
|
|
)
|
|
|
|
sinon.assert.calledWithMatch(
|
|
|
|
this.UserUpdater.promises.addAffiliationForNewUser,
|
|
|
|
user._id,
|
|
|
|
this.email
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should not add affiliation when without affiliation feature', async function() {
|
|
|
|
this.Features.hasFeature = sinon.stub().returns(false)
|
|
|
|
const attributes = { email: this.email }
|
|
|
|
await this.UserCreator.promises.createNewUser(attributes)
|
|
|
|
sinon.assert.notCalled(
|
|
|
|
this.UserUpdater.promises.addAffiliationForNewUser
|
|
|
|
)
|
2019-09-18 11:22:27 -04:00
|
|
|
})
|
|
|
|
|
2019-10-07 11:23:45 -04:00
|
|
|
it('should include SAML provider ID with email', async function() {
|
|
|
|
const attributes = {
|
|
|
|
email: this.email,
|
|
|
|
samlIdentifiers: [{ email: this.email, providerId: '1' }]
|
|
|
|
}
|
|
|
|
const user = await this.UserCreator.promises.createNewUser(attributes)
|
|
|
|
assert.equal(user.emails[0].samlProviderId, '1')
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|