Merge pull request #1097 from sharelatex/hb-fix-institutions-callbacks

Fix multiple callback and silent failures on institutions controller

GitOrigin-RevId: 2883445a4d960e7aca2dfbab45b8e268437e4769
This commit is contained in:
Chrystal Maria Griffiths 2018-11-13 10:32:29 +00:00 committed by sharelatex
parent efaa77dbd7
commit 6ea0b9cf46
2 changed files with 26 additions and 13 deletions

View file

@ -24,14 +24,17 @@ affiliateUsers = (hostname, callback = (error)->) ->
if error? if error?
logger.err error: error, 'problem fetching users by hostname' logger.err error: error, 'problem fetching users by hostname'
return callback(error) return callback(error)
async.map users, ((user) ->
matchingEmails = user.emails.filter (email) -> email.reversedHostname == reversedHostname async.map users, ((user, innerCallback) ->
for email in matchingEmails affiliateUserByReversedHostname user, reversedHostname, innerCallback
addAffiliation user._id, email.email, {confirmedAt: email.confirmedAt}, (error) => ), callback
if error?
logger.err error: error, 'problem adding affiliation while confirming hostname' affiliateUserByReversedHostname = (user, reversedHostname, callback) ->
return callback(error) matchingEmails = user.emails.filter (email) -> email.reversedHostname == reversedHostname
), (error) -> async.map matchingEmails, ((email, innerCallback) ->
if error? addAffiliation user._id, email.email, {confirmedAt: email.confirmedAt}, (error) =>
return callback(error) if error?
callback() logger.err error: error, 'problem adding affiliation while confirming hostname'
return innerCallback(error)
innerCallback()
), callback

View file

@ -47,17 +47,27 @@ describe "InstitutionsController", ->
json: sinon.stub() json: sinon.stub()
@next = sinon.stub() @next = sinon.stub()
describe 'confirmDomain', -> describe 'affiliateUsers', ->
it 'should add affiliations for matching users', (done)-> it 'should add affiliations for matching users', (done)->
@res.sendStatus = (code) => @res.sendStatus = (code) =>
code.should.equal 200
@getUsersByHostname.calledOnce.should.equal true @getUsersByHostname.calledOnce.should.equal true
@addAffiliation.calledThrice.should.equal true @addAffiliation.calledThrice.should.equal true
@addAffiliation.calledWith(@stubbedUser1._id, @stubbedUser1.emails[0].email).should.equal true @addAffiliation.calledWith(@stubbedUser1._id, @stubbedUser1.emails[0].email).should.equal true
@addAffiliation.calledWith(@stubbedUser1._id, @stubbedUser1.emails[2].email).should.equal true @addAffiliation.calledWith(@stubbedUser1._id, @stubbedUser1.emails[2].email).should.equal true
@addAffiliation.calledWith(@stubbedUser2._id, @stubbedUser2.emails[0].email).should.equal true
done() done()
@InstitutionsController.confirmDomain @req, @res, @next @InstitutionsController.confirmDomain @req, @res, @next
describe 'create institution', -> it 'should return errors if last affiliation cannot be added', (done)->
@addAffiliation.onCall(2).callsArgWith(3, new Error("error"))
@next = (error) =>
expect(error).to.exist
@getUsersByHostname.calledOnce.should.equal true
done()
@InstitutionsController.confirmDomain @req, @res, @next
describe 'createInstitution', ->
it 'should create new institution', (done)-> it 'should create new institution', (done)->
@req.body.institution_id = 123 @req.body.institution_id = 123
expectedData = v1Id: 123 expectedData = v1Id: 123