From 51c52282882dc23569a099da24ca24145e234bb4 Mon Sep 17 00:00:00 2001 From: Tim Alby Date: Mon, 24 Sep 2018 13:16:31 +0100 Subject: [PATCH] add getInstitutionsPlan function --- .../Institutions/InstitutionsFeatures.coffee | 12 +++++-- .../InstitutionsFeaturesTests.coffee | 32 ++++++++++++++++--- 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/services/web/app/coffee/Features/Institutions/InstitutionsFeatures.coffee b/services/web/app/coffee/Features/Institutions/InstitutionsFeatures.coffee index 4ce233406b..d780461492 100644 --- a/services/web/app/coffee/Features/Institutions/InstitutionsFeatures.coffee +++ b/services/web/app/coffee/Features/Institutions/InstitutionsFeatures.coffee @@ -5,11 +5,17 @@ logger = require 'logger-sharelatex' module.exports = InstitutionsFeatures = getInstitutionsFeatures: (userId, callback = (error, features) ->) -> + InstitutionsFeatures.getInstitutionsPlan userId, (error, plan) -> + return callback error if error? + plan = PlansLocator.findLocalPlanInSettings plan + callback(null, plan?.features or {}) + + + getInstitutionsPlan: (userId, callback = (error, plan) ->) -> InstitutionsFeatures.hasLicence userId, (error, hasLicence) -> return callback error if error? - return callback(null, {}) unless hasLicence - plan = PlansLocator.findLocalPlanInSettings Settings.institutionPlanCode - callback(null, plan?.features or {}) + return callback(null, null) unless hasLicence + callback(null, Settings.institutionPlanCode) hasLicence: (userId, callback = (error, hasLicence) ->) -> diff --git a/services/web/test/unit/coffee/Institutions/InstitutionsFeaturesTests.coffee b/services/web/test/unit/coffee/Institutions/InstitutionsFeaturesTests.coffee index 8811f72c35..5fe21627c2 100644 --- a/services/web/test/unit/coffee/Institutions/InstitutionsFeaturesTests.coffee +++ b/services/web/test/unit/coffee/Institutions/InstitutionsFeaturesTests.coffee @@ -61,26 +61,50 @@ describe 'InstitutionsFeatures', -> describe "getInstitutionsFeatures", -> beforeEach -> - @InstitutionsFeatures.hasLicence = sinon.stub() + @InstitutionsFeatures.getInstitutionsPlan = sinon.stub() @testFeatures = features: { institution: 'all' } @PlansLocator.findLocalPlanInSettings.withArgs(@institutionPlanCode).returns(@testFeatures) it 'should handle error', (done)-> - @InstitutionsFeatures.hasLicence.yields(new Error('Nope')) + @InstitutionsFeatures.getInstitutionsPlan.yields(new Error('Nope')) @InstitutionsFeatures.getInstitutionsFeatures @userId, (error, features) -> expect(error).to.exist done() it 'should return no feaures if user has no plan code', (done) -> - @InstitutionsFeatures.hasLicence.yields(null, false) + @InstitutionsFeatures.getInstitutionsPlan.yields(null, null) @InstitutionsFeatures.getInstitutionsFeatures @userId, (error, features) -> expect(error).to.not.exist expect(features).to.deep.equal {} done() it 'should return feaures if user has affiliations plan code', (done) -> - @InstitutionsFeatures.hasLicence.yields(null, true) + @InstitutionsFeatures.getInstitutionsPlan.yields(null, @institutionPlanCode) @InstitutionsFeatures.getInstitutionsFeatures @userId, (error, features) => expect(error).to.not.exist expect(features).to.deep.equal @testFeatures.features done() + + describe "getInstitutionsPlan", -> + beforeEach -> + @InstitutionsFeatures.hasLicence = sinon.stub() + + it 'should handle error', (done)-> + @InstitutionsFeatures.hasLicence.yields(new Error('Nope')) + @InstitutionsFeatures.getInstitutionsPlan @userId, (error) -> + expect(error).to.exist + done() + + it 'should return no plan if user has no licence', (done) -> + @InstitutionsFeatures.hasLicence.yields(null, false) + @InstitutionsFeatures.getInstitutionsPlan @userId, (error, plan) -> + expect(error).to.not.exist + expect(plan).to.equal null + done() + + it 'should return plan if user has licence', (done) -> + @InstitutionsFeatures.hasLicence.yields(null, true) + @InstitutionsFeatures.getInstitutionsPlan @userId, (error, plan) => + expect(error).to.not.exist + expect(plan).to.equal @institutionPlanCode + done()