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

Add getInstitutionsPlan Function
This commit is contained in:
Timothée Alby 2018-10-03 14:19:56 +01:00 committed by GitHub
commit c74c782cee
2 changed files with 37 additions and 7 deletions

View file

@ -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) ->) ->

View file

@ -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()