Merge pull request #610 from sharelatex/ja-sync-from-v2

Tell v1 when refreshing features in v2
This commit is contained in:
James Allen
2018-06-07 13:30:34 +01:00
committed by GitHub
6 changed files with 159 additions and 110 deletions
@@ -22,6 +22,7 @@ describe "FeaturesUpdater", ->
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' }])
@@ -29,48 +30,63 @@ describe "FeaturesUpdater", ->
@ReferalFeatures.getBonusFeatures = sinon.stub().yields(null, { 'bonus': 'features' })
@FeaturesUpdater._mergeFeatures = sinon.stub().returns({'merged': 'features'})
@callback = sinon.stub()
@FeaturesUpdater.refreshFeatures @user_id, @callback
it "should get the individual features", ->
@FeaturesUpdater._getIndividualFeatures
.calledWith(@user_id)
.should.equal true
describe "normally", ->
beforeEach ->
@FeaturesUpdater.refreshFeatures @user_id, @callback
it "should get the group features", ->
@FeaturesUpdater._getGroupFeatureSets
.calledWith(@user_id)
.should.equal true
it "should get the individual features", ->
@FeaturesUpdater._getIndividualFeatures
.calledWith(@user_id)
.should.equal true
it "should get the v1 features", ->
@FeaturesUpdater._getV1Features
.calledWith(@user_id)
.should.equal true
it "should get the group features", ->
@FeaturesUpdater._getGroupFeatureSets
.calledWith(@user_id)
.should.equal true
it "should get the bonus features", ->
@ReferalFeatures.getBonusFeatures
.calledWith(@user_id)
.should.equal true
it "should get the v1 features", ->
@FeaturesUpdater._getV1Features
.calledWith(@user_id)
.should.equal true
it "should merge from the default features", ->
@FeaturesUpdater._mergeFeatures.calledWith(@Settings.defaultFeatures).should.equal true
it "should get the bonus features", ->
@ReferalFeatures.getBonusFeatures
.calledWith(@user_id)
.should.equal true
it "should merge the individual features", ->
@FeaturesUpdater._mergeFeatures.calledWith(sinon.match.any, { 'individual': 'features' }).should.equal true
it "should merge from the default features", ->
@FeaturesUpdater._mergeFeatures.calledWith(@Settings.defaultFeatures).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 individual features", ->
@FeaturesUpdater._mergeFeatures.calledWith(sinon.match.any, { 'individual': '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 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 bonus features", ->
@FeaturesUpdater._mergeFeatures.calledWith(sinon.match.any, { 'bonus': 'features' }).should.equal true
it "should merge the v1 features", ->
@FeaturesUpdater._mergeFeatures.calledWith(sinon.match.any, { 'v1': '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 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", ->
@@ -16,10 +16,10 @@ describe 'V1SubscriptionManager', ->
err: sinon.stub()
warn: sinon.stub()
"settings-sharelatex":
overleaf:
host: @host = "http://overleaf.example.com"
apis:
v1:
host: @host = "http://overleaf.example.com"
"request": @request = sinon.stub()
@V1SubscriptionManager._v1PlanRequest = sinon.stub()
@userId = 'abcd'
@v1UserId = 42
@user =
@@ -33,33 +33,20 @@ describe 'V1SubscriptionManager', ->
@responseBody =
id: 32,
plan_name: 'pro'
@UserGetter.getUser = sinon.stub()
.yields(null, @user)
@V1SubscriptionManager._v1PlanRequest = sinon.stub()
@V1SubscriptionManager._v1Request = sinon.stub()
.yields(null, @responseBody)
@call = (cb) =>
@V1SubscriptionManager.getPlanCodeFromV1 @userId, cb
describe 'when all goes well', ->
it 'should call getUser', (done) ->
it 'should call _v1Request', (done) ->
@call (err, planCode) =>
expect(
@UserGetter.getUser.callCount
@V1SubscriptionManager._v1Request.callCount
).to.equal 1
expect(
@UserGetter.getUser.calledWith(@userId)
).to.equal true
done()
it 'should call _v1PlanRequest', (done) ->
@call (err, planCode) =>
expect(
@V1SubscriptionManager._v1PlanRequest.callCount
).to.equal 1
expect(
@V1SubscriptionManager._v1PlanRequest.calledWith(
@v1UserId
@V1SubscriptionManager._v1Request.calledWith(
@userId
)
).to.equal true
done()
@@ -80,49 +67,56 @@ describe 'V1SubscriptionManager', ->
expect(planCode).to.equal null
done()
describe '_v1Request', ->
beforeEach ->
@UserGetter.getUser = sinon.stub()
.yields(null, @user)
describe 'when getUser produces an error', ->
beforeEach ->
@UserGetter.getUser = sinon.stub()
.yields(new Error('woops'))
@call = (cb) =>
@V1SubscriptionManager._v1Request @user_id, { url: () -> '/foo' }, cb
it 'should not call _v1PlanRequest', (done) ->
it 'should not call request', (done) ->
@call (err, planCode) =>
expect(
@V1SubscriptionManager._v1PlanRequest.callCount
@request.callCount
).to.equal 0
done()
it 'should produce an error', (done) ->
@call (err, planCode) =>
expect(err).to.exist
expect(planCode).to.not.exist
done()
describe 'when getUser does not find a user', ->
beforeEach ->
@UserGetter.getUser = sinon.stub()
.yields(null, null)
@call = (cb) =>
@V1SubscriptionManager._v1Request @user_id, { url: () -> '/foo' }, cb
it 'should not call _v1PlanRequest', (done) ->
it 'should not call request', (done) ->
@call (err, planCode) =>
expect(
@V1SubscriptionManager._v1PlanRequest.callCount
@request.callCount
).to.equal 0
done()
it 'should produce a null plan-code, without error', (done) ->
@call (err, planCode) =>
it 'should not error', (done) ->
@call (err) =>
expect(err).to.not.exist
expect(planCode).to.not.exist
done()
describe 'when the request to v1 fails', ->
beforeEach ->
@V1SubscriptionManager._v1PlanRequest = sinon.stub()
.yields(new Error('woops'))
@request.yields(new Error('woops'))
@call = (cb) =>
@V1SubscriptionManager._v1Request @user_id, { url: () -> '/foo' }, cb
it 'should produce an error', (done) ->
@call (err, planCode) =>
@call (err) =>
expect(err).to.exist
expect(planCode).to.not.exist
done()