2018-07-10 05:42:17 -04:00
|
|
|
SandboxedModule = require('sandboxed-module')
|
|
|
|
assert = require('assert')
|
|
|
|
require('chai').should()
|
|
|
|
expect = require('chai').expect
|
|
|
|
sinon = require('sinon')
|
|
|
|
modulePath = require('path').join __dirname, '../../../../app/js/Features/Institutions/InstitutionsFeatures.js'
|
|
|
|
|
|
|
|
describe 'InstitutionsFeatures', ->
|
|
|
|
|
|
|
|
beforeEach ->
|
2018-08-10 07:40:25 -04:00
|
|
|
@InstitutionsGetter = getConfirmedInstitutions: sinon.stub()
|
2018-07-10 05:42:17 -04:00
|
|
|
@PlansLocator = findLocalPlanInSettings: sinon.stub()
|
|
|
|
@institutionPlanCode = 'institution_plan_code'
|
|
|
|
@InstitutionsFeatures = SandboxedModule.require modulePath, requires:
|
2018-08-10 07:40:25 -04:00
|
|
|
'./InstitutionsGetter': @InstitutionsGetter
|
2018-07-10 05:42:17 -04:00
|
|
|
'../Subscription/PlansLocator': @PlansLocator
|
|
|
|
'settings-sharelatex': institutionPlanCode: @institutionPlanCode
|
|
|
|
'logger-sharelatex':
|
|
|
|
log:->
|
|
|
|
err:->
|
|
|
|
|
|
|
|
@userId = '12345abcde'
|
|
|
|
|
|
|
|
describe "hasLicence", ->
|
|
|
|
it 'should handle error', (done)->
|
2018-08-10 07:40:25 -04:00
|
|
|
@InstitutionsGetter.getConfirmedInstitutions.yields(new Error('Nope'))
|
2018-07-10 05:42:17 -04:00
|
|
|
@InstitutionsFeatures.hasLicence @userId, (error, hasLicence) ->
|
|
|
|
expect(error).to.exist
|
|
|
|
done()
|
|
|
|
|
|
|
|
it 'should return false if user has no confirmed affiliations', (done) ->
|
2018-08-10 07:40:25 -04:00
|
|
|
institutions = []
|
|
|
|
@InstitutionsGetter.getConfirmedInstitutions.yields(null, institutions)
|
2018-07-10 05:42:17 -04:00
|
|
|
@InstitutionsFeatures.hasLicence @userId, (error, hasLicence) ->
|
|
|
|
expect(error).to.not.exist
|
|
|
|
expect(hasLicence).to.be.false
|
|
|
|
done()
|
|
|
|
|
|
|
|
it 'should return false if user has no paid affiliations', (done) ->
|
2018-08-10 07:40:25 -04:00
|
|
|
institutions = [
|
|
|
|
{ licence: 'free' }
|
2018-07-10 05:42:17 -04:00
|
|
|
]
|
2018-08-10 07:40:25 -04:00
|
|
|
@InstitutionsGetter.getConfirmedInstitutions.yields(null, institutions)
|
2018-07-10 05:42:17 -04:00
|
|
|
@InstitutionsFeatures.hasLicence @userId, (error, hasLicence) ->
|
|
|
|
expect(error).to.not.exist
|
|
|
|
expect(hasLicence).to.be.false
|
|
|
|
done()
|
|
|
|
|
|
|
|
it 'should return true if user has confirmed paid affiliation', (done)->
|
2018-08-10 07:40:25 -04:00
|
|
|
institutions = [
|
|
|
|
{ licence: 'pro_plus' }
|
|
|
|
{ licence: 'free' }
|
|
|
|
{ licence: 'pro' }
|
|
|
|
{ licence: null }
|
2018-07-10 05:42:17 -04:00
|
|
|
]
|
2018-08-10 07:40:25 -04:00
|
|
|
@InstitutionsGetter.getConfirmedInstitutions.yields(null, institutions)
|
2018-07-10 05:42:17 -04:00
|
|
|
@InstitutionsFeatures.hasLicence @userId, (error, hasLicence) ->
|
|
|
|
expect(error).to.not.exist
|
|
|
|
expect(hasLicence).to.be.true
|
|
|
|
done()
|
|
|
|
|
|
|
|
describe "getInstitutionsFeatures", ->
|
|
|
|
beforeEach ->
|
|
|
|
@InstitutionsFeatures.hasLicence = sinon.stub()
|
|
|
|
@testFeatures = features: { institution: 'all' }
|
|
|
|
@PlansLocator.findLocalPlanInSettings.withArgs(@institutionPlanCode).returns(@testFeatures)
|
|
|
|
|
|
|
|
it 'should handle error', (done)->
|
|
|
|
@InstitutionsFeatures.hasLicence.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.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.getInstitutionsFeatures @userId, (error, features) =>
|
|
|
|
expect(error).to.not.exist
|
|
|
|
expect(features).to.deep.equal @testFeatures.features
|
|
|
|
done()
|