overleaf/services/web/test/unit/coffee/Institutions/InstitutionsFeaturesTests.coffee

87 lines
3.2 KiB
CoffeeScript
Raw Normal View History

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 11:40:25 +00:00
@InstitutionsGetter = getConfirmedInstitutions: sinon.stub()
@PlansLocator = findLocalPlanInSettings: sinon.stub()
@institutionPlanCode = 'institution_plan_code'
@InstitutionsFeatures = SandboxedModule.require modulePath, requires:
2018-08-10 11:40:25 +00:00
'./InstitutionsGetter': @InstitutionsGetter
'../Subscription/PlansLocator': @PlansLocator
'settings-sharelatex': institutionPlanCode: @institutionPlanCode
'logger-sharelatex':
log:->
err:->
@userId = '12345abcde'
describe "hasLicence", ->
it 'should handle error', (done)->
2018-08-10 11:40:25 +00:00
@InstitutionsGetter.getConfirmedInstitutions.yields(new Error('Nope'))
@InstitutionsFeatures.hasLicence @userId, (error, hasLicence) ->
expect(error).to.exist
done()
it 'should return false if user has no confirmed affiliations', (done) ->
2018-08-10 11:40:25 +00:00
institutions = []
@InstitutionsGetter.getConfirmedInstitutions.yields(null, institutions)
@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 11:40:25 +00:00
institutions = [
{ licence: 'free' }
]
2018-08-10 11:40:25 +00:00
@InstitutionsGetter.getConfirmedInstitutions.yields(null, institutions)
@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 11:40:25 +00:00
institutions = [
{ licence: 'pro_plus' }
{ licence: 'free' }
{ licence: 'pro' }
{ licence: null }
]
2018-08-10 11:40:25 +00:00
@InstitutionsGetter.getConfirmedInstitutions.yields(null, institutions)
@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()