Преглед изворни кода

Merge pull request #961 from sharelatex/ta-account-sync-affiliations

Add getInstitutionsPlan Function
Timothée Alby пре 8 година
родитељ
комит
c74c782cee

+ 9 - 3
services/web/app/coffee/Features/Institutions/InstitutionsFeatures.coffee

@@ -5,13 +5,19 @@ logger = require 'logger-sharelatex'
 
 module.exports = InstitutionsFeatures =
 	getInstitutionsFeatures: (userId, callback = (error, features) ->) ->
-		InstitutionsFeatures.hasLicence userId, (error, hasLicence) ->
+		InstitutionsFeatures.getInstitutionsPlan userId, (error, plan) ->
 			return callback error if error?
-			return callback(null, {}) unless hasLicence
-			plan = PlansLocator.findLocalPlanInSettings Settings.institutionPlanCode
+			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, null) unless hasLicence
+			callback(null, Settings.institutionPlanCode)
+
+
 	hasLicence: (userId, callback = (error, hasLicence) ->) ->
 		InstitutionsGetter.getConfirmedInstitutions userId, (error, institutions) ->
 			return callback error if error?

+ 28 - 4
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()