Merge pull request #825 from sharelatex/ta-skip-affiliation-on-create

Skip Affiliation on Create
This commit is contained in:
Timothée Alby 2018-08-17 17:50:17 +02:00 committed by GitHub
commit 9d3e649710
3 changed files with 21 additions and 7 deletions

View file

@ -80,6 +80,8 @@ makeAffiliationRequest = (requestOptions, callback = (error) ->) ->
errorMessage = "#{response.statusCode}: #{body.errors}" errorMessage = "#{response.statusCode}: #{body.errors}"
else else
errorMessage = "#{requestOptions.defaultErrorMessage}: #{response.statusCode}" errorMessage = "#{requestOptions.defaultErrorMessage}: #{response.statusCode}"
logger.err path: requestOptions.path, body: requestOptions.body, errorMessage
return callback(new Error(errorMessage)) return callback(new Error(errorMessage))
callback(null, body) callback(null, body)

View file

@ -6,15 +6,18 @@ metrics = require('metrics-sharelatex')
module.exports = UserCreator = module.exports = UserCreator =
createNewUser: (opts, callback)-> createNewUser: (attributes, options, callback = (error, user) ->)->
logger.log opts:opts, "creating new user" if arguments.length == 2
callback = options
options = {}
logger.log user: attributes, "creating new user"
user = new User() user = new User()
username = opts.email.match(/^[^@]*/) username = attributes.email.match(/^[^@]*/)
if !opts.first_name? or opts.first_name == "" if !attributes.first_name? or attributes.first_name == ""
opts.first_name = username[0] attributes.first_name = username[0]
for key, value of opts for key, value of attributes
user[key] = value user[key] = value
user.ace.syntaxValidation = true user.ace.syntaxValidation = true
@ -27,6 +30,7 @@ module.exports = UserCreator =
user.save (err)-> user.save (err)->
callback(err, user) callback(err, user)
return if options?.skip_affiliation
# call addaffiliation after the main callback so it runs in the # call addaffiliation after the main callback so it runs in the
# background. There is no guaranty this will run so we must no rely on it # background. There is no guaranty this will run so we must no rely on it
addAffiliation user._id, user.email, (error) -> addAffiliation user._id, user.email, (error) ->

View file

@ -20,7 +20,7 @@ describe "UserCreator", ->
@addAffiliation = sinon.stub().yields() @addAffiliation = sinon.stub().yields()
@UserCreator = SandboxedModule.require modulePath, requires: @UserCreator = SandboxedModule.require modulePath, requires:
"../../models/User": User:@UserModel "../../models/User": User:@UserModel
"logger-sharelatex":{log:->} "logger-sharelatex":{ log: sinon.stub(), err: sinon.stub() }
'metrics-sharelatex': {timeAsyncMethod: ()->} 'metrics-sharelatex': {timeAsyncMethod: ()->}
"../Institutions/InstitutionsAPI": addAffiliation: @addAffiliation "../Institutions/InstitutionsAPI": addAffiliation: @addAffiliation
@ -88,3 +88,11 @@ describe "UserCreator", ->
process.nextTick () => process.nextTick () =>
sinon.assert.calledWith(@addAffiliation, user._id, user.email) sinon.assert.calledWith(@addAffiliation, user._id, user.email)
done() done()
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()