2014-02-12 05:23:40 -05:00
|
|
|
sinon = require('sinon')
|
|
|
|
chai = require('chai')
|
|
|
|
assert = require("assert")
|
|
|
|
should = chai.should()
|
|
|
|
modulePath = "../../../../app/js/Features/User/UserCreator.js"
|
|
|
|
SandboxedModule = require('sandboxed-module')
|
|
|
|
|
|
|
|
describe "UserCreator", ->
|
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
self = @
|
2016-11-01 12:55:26 -04:00
|
|
|
@user = {_id:"12390i", ace: {}}
|
2014-02-12 05:23:40 -05:00
|
|
|
@user.save = sinon.stub().callsArgWith(0)
|
|
|
|
@UserModel = class Project
|
|
|
|
constructor: ->
|
|
|
|
return self.user
|
|
|
|
|
2018-05-23 10:12:23 -04:00
|
|
|
@UserGetter =
|
|
|
|
getUserByMainEmail: sinon.stub()
|
2018-07-04 09:12:54 -04:00
|
|
|
@addAffiliation = sinon.stub().yields()
|
2014-02-12 05:23:40 -05:00
|
|
|
@UserCreator = SandboxedModule.require modulePath, requires:
|
|
|
|
"../../models/User": User:@UserModel
|
2018-08-17 09:13:24 -04:00
|
|
|
"logger-sharelatex":{ log: sinon.stub(), err: sinon.stub() }
|
2017-04-03 11:18:30 -04:00
|
|
|
'metrics-sharelatex': {timeAsyncMethod: ()->}
|
2018-07-10 15:49:24 -04:00
|
|
|
"../Institutions/InstitutionsAPI": addAffiliation: @addAffiliation
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
@email = "bob.oswald@gmail.com"
|
|
|
|
|
|
|
|
describe "createNewUser", ->
|
|
|
|
|
|
|
|
it "should take the opts and put them in the model", (done)->
|
|
|
|
opts =
|
|
|
|
email:@email
|
|
|
|
holdingAccount:true
|
|
|
|
@UserCreator.createNewUser opts, (err, user)=>
|
|
|
|
assert.equal user.email, @email
|
|
|
|
assert.equal user.holdingAccount, true
|
2014-04-10 09:43:06 -04:00
|
|
|
assert.equal user.first_name, "bob.oswald"
|
2014-02-12 05:23:40 -05:00
|
|
|
done()
|
|
|
|
|
2016-06-13 08:21:44 -04:00
|
|
|
it "should use the start of the email if the first name is empty string", (done)->
|
|
|
|
opts =
|
|
|
|
email:@email
|
|
|
|
holdingAccount:true
|
|
|
|
first_name:""
|
|
|
|
@UserCreator.createNewUser opts, (err, user)=>
|
|
|
|
assert.equal user.email, @email
|
|
|
|
assert.equal user.holdingAccount, true
|
|
|
|
assert.equal user.first_name, "bob.oswald"
|
|
|
|
done()
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
|
2016-06-13 08:21:44 -04:00
|
|
|
it "should use the first name if passed", (done)->
|
|
|
|
opts =
|
|
|
|
email:@email
|
|
|
|
holdingAccount:true
|
|
|
|
first_name:"fiiirstname"
|
|
|
|
@UserCreator.createNewUser opts, (err, user)=>
|
|
|
|
assert.equal user.email, @email
|
|
|
|
assert.equal user.holdingAccount, true
|
|
|
|
assert.equal user.first_name, "fiiirstname"
|
|
|
|
done()
|
|
|
|
|
|
|
|
it "should use the last name if passed", (done)->
|
|
|
|
opts =
|
|
|
|
email:@email
|
|
|
|
holdingAccount:true
|
|
|
|
last_name:"lastNammmmeee"
|
|
|
|
@UserCreator.createNewUser opts, (err, user)=>
|
|
|
|
assert.equal user.email, @email
|
|
|
|
assert.equal user.holdingAccount, true
|
|
|
|
assert.equal user.last_name, "lastNammmmeee"
|
|
|
|
done()
|
2018-05-28 10:08:37 -04:00
|
|
|
|
|
|
|
it "should set emails attribute", (done)->
|
|
|
|
@UserCreator.createNewUser email: @email, (err, user)=>
|
|
|
|
user.email.should.equal @email
|
|
|
|
user.emails.length.should.equal 1
|
|
|
|
user.emails[0].email.should.equal @email
|
|
|
|
user.emails[0].createdAt.should.be.a 'date'
|
|
|
|
done()
|
2018-07-04 09:12:54 -04:00
|
|
|
|
|
|
|
it "should add affiliation in background", (done)->
|
|
|
|
@UserCreator.createNewUser email: @email, (err, user) =>
|
|
|
|
# addaffiliation should not be called before the callback but only after
|
|
|
|
# a tick of the event loop
|
|
|
|
sinon.assert.notCalled(@addAffiliation)
|
|
|
|
process.nextTick () =>
|
|
|
|
sinon.assert.calledWith(@addAffiliation, user._id, user.email)
|
|
|
|
done()
|
2018-08-17 09:08:59 -04:00
|
|
|
|
|
|
|
it "should not add affiliation if skipping", (done)->
|
|
|
|
attributes = email: @email
|
|
|
|
options = skip_affiliation: true
|
|
|
|
@UserCreator.createNewUser attributes, options, (err, user) =>
|
|
|
|
process.nextTick () =>
|
|
|
|
sinon.assert.notCalled(@addAffiliation)
|
|
|
|
done()
|