| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- SandboxedModule = require('sandboxed-module')
- should = require('chai').should()
- expect = require('chai').expect
- sinon = require 'sinon'
- modulePath = "../../../../app/js/Features/Subscription/FeaturesUpdater"
- assert = require("chai").assert
- ObjectId = require('mongoose').Types.ObjectId
- describe "FeaturesUpdater", ->
- beforeEach ->
- @user_id = ObjectId().toString()
- @FeaturesUpdater = SandboxedModule.require modulePath, requires:
- './UserFeaturesUpdater': @UserFeaturesUpdater = {}
- './SubscriptionLocator': @SubscriptionLocator = {}
- './PlansLocator': @PlansLocator = {}
- "logger-sharelatex": log:->
- 'settings-sharelatex': @Settings = {}
- "../Referal/ReferalFeatures" : @ReferalFeatures = {}
- "./V1SubscriptionManager": @V1SubscriptionManager = {}
- '../Institutions/InstitutionsFeatures': @InstitutionsFeatures = {}
- describe "refreshFeatures", ->
- beforeEach ->
- @V1SubscriptionManager.notifyV1OfFeaturesChange = sinon.stub().yields()
- @UserFeaturesUpdater.updateFeatures = sinon.stub().yields()
- @FeaturesUpdater._getIndividualFeatures = sinon.stub().yields(null, { 'individual': 'features' })
- @FeaturesUpdater._getGroupFeatureSets = sinon.stub().yields(null, [{ 'group': 'features' }, { 'group': 'features2' }])
- @InstitutionsFeatures.getInstitutionsFeatures = sinon.stub().yields(null, { 'institutions': 'features' })
- @FeaturesUpdater._getV1Features = sinon.stub().yields(null, { 'v1': 'features' })
- @ReferalFeatures.getBonusFeatures = sinon.stub().yields(null, { 'bonus': 'features' })
- @FeaturesUpdater._mergeFeatures = sinon.stub().returns({'merged': 'features'})
- @callback = sinon.stub()
- describe "normally", ->
- beforeEach ->
- @FeaturesUpdater.refreshFeatures @user_id, @callback
- it "should get the individual features", ->
- @FeaturesUpdater._getIndividualFeatures
- .calledWith(@user_id)
- .should.equal true
- it "should get the group features", ->
- @FeaturesUpdater._getGroupFeatureSets
- .calledWith(@user_id)
- .should.equal true
- it "should get the institution features", ->
- @InstitutionsFeatures.getInstitutionsFeatures
- .calledWith(@user_id)
- .should.equal true
- it "should get the v1 features", ->
- @FeaturesUpdater._getV1Features
- .calledWith(@user_id)
- .should.equal true
- it "should get the bonus features", ->
- @ReferalFeatures.getBonusFeatures
- .calledWith(@user_id)
- .should.equal true
- it "should merge from the default features", ->
- @FeaturesUpdater._mergeFeatures.calledWith(@Settings.defaultFeatures).should.equal true
- it "should merge the individual features", ->
- @FeaturesUpdater._mergeFeatures.calledWith(sinon.match.any, { 'individual': 'features' }).should.equal true
- it "should merge the group features", ->
- @FeaturesUpdater._mergeFeatures.calledWith(sinon.match.any, { 'group': 'features' }).should.equal true
- @FeaturesUpdater._mergeFeatures.calledWith(sinon.match.any, { 'group': 'features2' }).should.equal true
- it "should merge the institutions features", ->
- @FeaturesUpdater._mergeFeatures.calledWith(sinon.match.any, { 'institutions': 'features' }).should.equal true
- it "should merge the v1 features", ->
- @FeaturesUpdater._mergeFeatures.calledWith(sinon.match.any, { 'v1': 'features' }).should.equal true
- it "should merge the bonus features", ->
- @FeaturesUpdater._mergeFeatures.calledWith(sinon.match.any, { 'bonus': 'features' }).should.equal true
- it "should update the user with the merged features", ->
- @UserFeaturesUpdater.updateFeatures
- .calledWith(@user_id, {'merged': 'features'})
- .should.equal true
- it "should notify v1", ->
- @V1SubscriptionManager.notifyV1OfFeaturesChange
- .called.should.equal true
- describe "with notifyV1 == false", ->
- beforeEach ->
- @FeaturesUpdater.refreshFeatures @user_id, false, @callback
- it "should not notify v1", ->
- @V1SubscriptionManager.notifyV1OfFeaturesChange
- .called.should.equal false
- describe "_mergeFeatures", ->
- it "should prefer priority over standard for compileGroup", ->
- expect(@FeaturesUpdater._mergeFeatures({
- compileGroup: 'priority'
- }, {
- compileGroup: 'standard'
- })).to.deep.equal({
- compileGroup: 'priority'
- })
- expect(@FeaturesUpdater._mergeFeatures({
- compileGroup: 'standard'
- }, {
- compileGroup: 'priority'
- })).to.deep.equal({
- compileGroup: 'priority'
- })
- expect(@FeaturesUpdater._mergeFeatures({
- compileGroup: 'priority'
- }, {
- compileGroup: 'priority'
- })).to.deep.equal({
- compileGroup: 'priority'
- })
- expect(@FeaturesUpdater._mergeFeatures({
- compileGroup: 'standard'
- }, {
- compileGroup: 'standard'
- })).to.deep.equal({
- compileGroup: 'standard'
- })
- it "should prefer -1 over any other for collaborators", ->
- expect(@FeaturesUpdater._mergeFeatures({
- collaborators: -1
- }, {
- collaborators: 10
- })).to.deep.equal({
- collaborators: -1
- })
- expect(@FeaturesUpdater._mergeFeatures({
- collaborators: 10
- }, {
- collaborators: -1
- })).to.deep.equal({
- collaborators: -1
- })
- expect(@FeaturesUpdater._mergeFeatures({
- collaborators: 4
- }, {
- collaborators: 10
- })).to.deep.equal({
- collaborators: 10
- })
- it "should prefer the higher of compileTimeout", ->
- expect(@FeaturesUpdater._mergeFeatures({
- compileTimeout: 20
- }, {
- compileTimeout: 10
- })).to.deep.equal({
- compileTimeout: 20
- })
- expect(@FeaturesUpdater._mergeFeatures({
- compileTimeout: 10
- }, {
- compileTimeout: 20
- })).to.deep.equal({
- compileTimeout: 20
- })
- it "should prefer the true over false for other keys", ->
- expect(@FeaturesUpdater._mergeFeatures({
- github: true
- }, {
- github: false
- })).to.deep.equal({
- github: true
- })
- expect(@FeaturesUpdater._mergeFeatures({
- github: false
- }, {
- github: true
- })).to.deep.equal({
- github: true
- })
- expect(@FeaturesUpdater._mergeFeatures({
- github: true
- }, {
- github: true
- })).to.deep.equal({
- github: true
- })
- expect(@FeaturesUpdater._mergeFeatures({
- github: false
- }, {
- github: false
- })).to.deep.equal({
- github: false
- })
|