always return an array when getting affiliations

This commit is contained in:
Tim Alby 2018-08-20 16:29:44 +02:00
parent d838f8778a
commit 753fb02c05
2 changed files with 20 additions and 4 deletions

View file

@ -9,7 +9,7 @@ module.exports = InstitutionsAPI =
method: 'GET'
path: "/api/v2/institutions/#{institutionId.toString()}/affiliations"
defaultErrorMessage: "Couldn't get institution affiliations"
}, callback
}, (error, body) -> callback(error, body or [])
getUserAffiliations: (userId, callback = (error, body) ->) ->
@ -17,7 +17,7 @@ module.exports = InstitutionsAPI =
method: 'GET'
path: "/api/v2/users/#{userId.toString()}/affiliations"
defaultErrorMessage: "Couldn't get user affiliations"
}, callback
}, (error, body) -> callback(error, body or [])
addAffiliation: (userId, email, affiliationOptions, callback) ->

View file

@ -11,12 +11,12 @@ describe "InstitutionsAPI", ->
beforeEach ->
@logger = err: sinon.stub(), log: ->
settings = apis: { v1: { url: 'v1.url', user: '', pass: '' } }
@settings = apis: { v1: { url: 'v1.url', user: '', pass: '' } }
@request = sinon.stub()
@InstitutionsAPI = SandboxedModule.require modulePath, requires:
"logger-sharelatex": @logger
"metrics-sharelatex": timeAsyncMethod: sinon.stub()
'settings-sharelatex': settings
'settings-sharelatex': @settings
'request': @request
@stubbedUser =
@ -41,6 +41,14 @@ describe "InstitutionsAPI", ->
body.should.equal responseBody
done()
it 'handle empty response', (done)->
@settings.apis = null
@InstitutionsAPI.getInstitutionAffiliations @institutionId, (err, body) =>
should.not.exist(err)
expect(body).to.be.a 'Array'
body.length.should.equal 0
done()
describe 'getUserAffiliations', ->
it 'get affiliations', (done)->
responseBody = [{ foo: 'bar' }]
@ -65,6 +73,14 @@ describe "InstitutionsAPI", ->
err.message.should.have.string body.errors
done()
it 'handle empty response', (done)->
@settings.apis = null
@InstitutionsAPI.getUserAffiliations @stubbedUser._id, (err, body) =>
should.not.exist(err)
expect(body).to.be.a 'Array'
body.length.should.equal 0
done()
describe 'addAffiliation', ->
beforeEach ->
@request.callsArgWith(1, null, { statusCode: 201 })