FeaturesUpdaterTests.coffee 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. SandboxedModule = require('sandboxed-module')
  2. should = require('chai').should()
  3. expect = require('chai').expect
  4. sinon = require 'sinon'
  5. modulePath = "../../../../app/js/Features/Subscription/FeaturesUpdater"
  6. assert = require("chai").assert
  7. ObjectId = require('mongoose').Types.ObjectId
  8. describe "FeaturesUpdater", ->
  9. beforeEach ->
  10. @user_id = ObjectId().toString()
  11. @FeaturesUpdater = SandboxedModule.require modulePath, requires:
  12. './UserFeaturesUpdater': @UserFeaturesUpdater = {}
  13. './SubscriptionLocator': @SubscriptionLocator = {}
  14. './PlansLocator': @PlansLocator = {}
  15. "logger-sharelatex": log:->
  16. 'settings-sharelatex': @Settings = {}
  17. "../Referal/ReferalFeatures" : @ReferalFeatures = {}
  18. "./V1SubscriptionManager": @V1SubscriptionManager = {}
  19. describe "refreshFeatures", ->
  20. beforeEach ->
  21. @V1SubscriptionManager.notifyV1OfFeaturesChange = sinon.stub().yields()
  22. @UserFeaturesUpdater.updateFeatures = sinon.stub().yields()
  23. @FeaturesUpdater._getIndividualFeatures = sinon.stub().yields(null, { 'individual': 'features' })
  24. @FeaturesUpdater._getGroupFeatureSets = sinon.stub().yields(null, [{ 'group': 'features' }, { 'group': 'features2' }])
  25. @FeaturesUpdater._getV1Features = sinon.stub().yields(null, { 'v1': 'features' })
  26. @ReferalFeatures.getBonusFeatures = sinon.stub().yields(null, { 'bonus': 'features' })
  27. @FeaturesUpdater._mergeFeatures = sinon.stub().returns({'merged': 'features'})
  28. @callback = sinon.stub()
  29. describe "normally", ->
  30. beforeEach ->
  31. @FeaturesUpdater.refreshFeatures @user_id, @callback
  32. it "should get the individual features", ->
  33. @FeaturesUpdater._getIndividualFeatures
  34. .calledWith(@user_id)
  35. .should.equal true
  36. it "should get the group features", ->
  37. @FeaturesUpdater._getGroupFeatureSets
  38. .calledWith(@user_id)
  39. .should.equal true
  40. it "should get the v1 features", ->
  41. @FeaturesUpdater._getV1Features
  42. .calledWith(@user_id)
  43. .should.equal true
  44. it "should get the bonus features", ->
  45. @ReferalFeatures.getBonusFeatures
  46. .calledWith(@user_id)
  47. .should.equal true
  48. it "should merge from the default features", ->
  49. @FeaturesUpdater._mergeFeatures.calledWith(@Settings.defaultFeatures).should.equal true
  50. it "should merge the individual features", ->
  51. @FeaturesUpdater._mergeFeatures.calledWith(sinon.match.any, { 'individual': 'features' }).should.equal true
  52. it "should merge the group features", ->
  53. @FeaturesUpdater._mergeFeatures.calledWith(sinon.match.any, { 'group': 'features' }).should.equal true
  54. @FeaturesUpdater._mergeFeatures.calledWith(sinon.match.any, { 'group': 'features2' }).should.equal true
  55. it "should merge the v1 features", ->
  56. @FeaturesUpdater._mergeFeatures.calledWith(sinon.match.any, { 'v1': 'features' }).should.equal true
  57. it "should merge the bonus features", ->
  58. @FeaturesUpdater._mergeFeatures.calledWith(sinon.match.any, { 'bonus': 'features' }).should.equal true
  59. it "should update the user with the merged features", ->
  60. @UserFeaturesUpdater.updateFeatures
  61. .calledWith(@user_id, {'merged': 'features'})
  62. .should.equal true
  63. it "should notify v1", ->
  64. @V1SubscriptionManager.notifyV1OfFeaturesChange
  65. .called.should.equal true
  66. describe "with notifyV1 == false", ->
  67. beforeEach ->
  68. @FeaturesUpdater.refreshFeatures @user_id, false, @callback
  69. it "should not notify v1", ->
  70. @V1SubscriptionManager.notifyV1OfFeaturesChange
  71. .called.should.equal false
  72. describe "_mergeFeatures", ->
  73. it "should prefer priority over standard for compileGroup", ->
  74. expect(@FeaturesUpdater._mergeFeatures({
  75. compileGroup: 'priority'
  76. }, {
  77. compileGroup: 'standard'
  78. })).to.deep.equal({
  79. compileGroup: 'priority'
  80. })
  81. expect(@FeaturesUpdater._mergeFeatures({
  82. compileGroup: 'standard'
  83. }, {
  84. compileGroup: 'priority'
  85. })).to.deep.equal({
  86. compileGroup: 'priority'
  87. })
  88. expect(@FeaturesUpdater._mergeFeatures({
  89. compileGroup: 'priority'
  90. }, {
  91. compileGroup: 'priority'
  92. })).to.deep.equal({
  93. compileGroup: 'priority'
  94. })
  95. expect(@FeaturesUpdater._mergeFeatures({
  96. compileGroup: 'standard'
  97. }, {
  98. compileGroup: 'standard'
  99. })).to.deep.equal({
  100. compileGroup: 'standard'
  101. })
  102. it "should prefer -1 over any other for collaborators", ->
  103. expect(@FeaturesUpdater._mergeFeatures({
  104. collaborators: -1
  105. }, {
  106. collaborators: 10
  107. })).to.deep.equal({
  108. collaborators: -1
  109. })
  110. expect(@FeaturesUpdater._mergeFeatures({
  111. collaborators: 10
  112. }, {
  113. collaborators: -1
  114. })).to.deep.equal({
  115. collaborators: -1
  116. })
  117. expect(@FeaturesUpdater._mergeFeatures({
  118. collaborators: 4
  119. }, {
  120. collaborators: 10
  121. })).to.deep.equal({
  122. collaborators: 10
  123. })
  124. it "should prefer the higher of compileTimeout", ->
  125. expect(@FeaturesUpdater._mergeFeatures({
  126. compileTimeout: 20
  127. }, {
  128. compileTimeout: 10
  129. })).to.deep.equal({
  130. compileTimeout: 20
  131. })
  132. expect(@FeaturesUpdater._mergeFeatures({
  133. compileTimeout: 10
  134. }, {
  135. compileTimeout: 20
  136. })).to.deep.equal({
  137. compileTimeout: 20
  138. })
  139. it "should prefer the true over false for other keys", ->
  140. expect(@FeaturesUpdater._mergeFeatures({
  141. github: true
  142. }, {
  143. github: false
  144. })).to.deep.equal({
  145. github: true
  146. })
  147. expect(@FeaturesUpdater._mergeFeatures({
  148. github: false
  149. }, {
  150. github: true
  151. })).to.deep.equal({
  152. github: true
  153. })
  154. expect(@FeaturesUpdater._mergeFeatures({
  155. github: true
  156. }, {
  157. github: true
  158. })).to.deep.equal({
  159. github: true
  160. })
  161. expect(@FeaturesUpdater._mergeFeatures({
  162. github: false
  163. }, {
  164. github: false
  165. })).to.deep.equal({
  166. github: false
  167. })