From 753fb02c056884525a1067266ac43f5977b101da Mon Sep 17 00:00:00 2001 From: Tim Alby Date: Mon, 20 Aug 2018 16:29:44 +0200 Subject: [PATCH] always return an array when getting affiliations --- .../Institutions/InstitutionsAPI.coffee | 4 ++-- .../Institutions/InstitutionsAPITests.coffee | 20 +++++++++++++++++-- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/services/web/app/coffee/Features/Institutions/InstitutionsAPI.coffee b/services/web/app/coffee/Features/Institutions/InstitutionsAPI.coffee index d1b2a69816..3d8da2ee80 100644 --- a/services/web/app/coffee/Features/Institutions/InstitutionsAPI.coffee +++ b/services/web/app/coffee/Features/Institutions/InstitutionsAPI.coffee @@ -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) -> diff --git a/services/web/test/unit/coffee/Institutions/InstitutionsAPITests.coffee b/services/web/test/unit/coffee/Institutions/InstitutionsAPITests.coffee index 04f0b61e8e..9588ea32e7 100644 --- a/services/web/test/unit/coffee/Institutions/InstitutionsAPITests.coffee +++ b/services/web/test/unit/coffee/Institutions/InstitutionsAPITests.coffee @@ -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 })