FeaturesUpdaterTests.coffee 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. @UserFeaturesUpdater.updateFeatures = sinon.stub().yields()
  22. @FeaturesUpdater._getIndividualFeatures = sinon.stub().yields(null, { 'individual': 'features' })
  23. @FeaturesUpdater._getGroupFeatureSets = sinon.stub().yields(null, [{ 'group': 'features' }, { 'group': 'features2' }])
  24. @FeaturesUpdater._getV1Features = sinon.stub().yields(null, { 'v1': 'features' })
  25. @ReferalFeatures.getBonusFeatures = sinon.stub().yields(null, { 'bonus': 'features' })
  26. @FeaturesUpdater._mergeFeatures = sinon.stub().returns({'merged': 'features'})
  27. @callback = sinon.stub()
  28. @FeaturesUpdater.refreshFeatures @user_id, @callback
  29. it "should get the individual features", ->
  30. @FeaturesUpdater._getIndividualFeatures
  31. .calledWith(@user_id)
  32. .should.equal true
  33. it "should get the group features", ->
  34. @FeaturesUpdater._getGroupFeatureSets
  35. .calledWith(@user_id)
  36. .should.equal true
  37. it "should get the v1 features", ->
  38. @FeaturesUpdater._getV1Features
  39. .calledWith(@user_id)
  40. .should.equal true
  41. it "should get the bonus features", ->
  42. @ReferalFeatures.getBonusFeatures
  43. .calledWith(@user_id)
  44. .should.equal true
  45. it "should merge from the default features", ->
  46. @FeaturesUpdater._mergeFeatures.calledWith(@Settings.defaultFeatures).should.equal true
  47. it "should merge the individual features", ->
  48. @FeaturesUpdater._mergeFeatures.calledWith(sinon.match.any, { 'individual': 'features' }).should.equal true
  49. it "should merge the group features", ->
  50. @FeaturesUpdater._mergeFeatures.calledWith(sinon.match.any, { 'group': 'features' }).should.equal true
  51. @FeaturesUpdater._mergeFeatures.calledWith(sinon.match.any, { 'group': 'features2' }).should.equal true
  52. it "should merge the v1 features", ->
  53. @FeaturesUpdater._mergeFeatures.calledWith(sinon.match.any, { 'v1': 'features' }).should.equal true
  54. it "should merge the bonus features", ->
  55. @FeaturesUpdater._mergeFeatures.calledWith(sinon.match.any, { 'bonus': 'features' }).should.equal true
  56. it "should update the user with the merged features", ->
  57. @UserFeaturesUpdater.updateFeatures
  58. .calledWith(@user_id, {'merged': 'features'})
  59. .should.equal true
  60. describe "_mergeFeatures", ->
  61. it "should prefer priority over standard for compileGroup", ->
  62. expect(@FeaturesUpdater._mergeFeatures({
  63. compileGroup: 'priority'
  64. }, {
  65. compileGroup: 'standard'
  66. })).to.deep.equal({
  67. compileGroup: 'priority'
  68. })
  69. expect(@FeaturesUpdater._mergeFeatures({
  70. compileGroup: 'standard'
  71. }, {
  72. compileGroup: 'priority'
  73. })).to.deep.equal({
  74. compileGroup: 'priority'
  75. })
  76. expect(@FeaturesUpdater._mergeFeatures({
  77. compileGroup: 'priority'
  78. }, {
  79. compileGroup: 'priority'
  80. })).to.deep.equal({
  81. compileGroup: 'priority'
  82. })
  83. expect(@FeaturesUpdater._mergeFeatures({
  84. compileGroup: 'standard'
  85. }, {
  86. compileGroup: 'standard'
  87. })).to.deep.equal({
  88. compileGroup: 'standard'
  89. })
  90. it "should prefer -1 over any other for collaborators", ->
  91. expect(@FeaturesUpdater._mergeFeatures({
  92. collaborators: -1
  93. }, {
  94. collaborators: 10
  95. })).to.deep.equal({
  96. collaborators: -1
  97. })
  98. expect(@FeaturesUpdater._mergeFeatures({
  99. collaborators: 10
  100. }, {
  101. collaborators: -1
  102. })).to.deep.equal({
  103. collaborators: -1
  104. })
  105. expect(@FeaturesUpdater._mergeFeatures({
  106. collaborators: 4
  107. }, {
  108. collaborators: 10
  109. })).to.deep.equal({
  110. collaborators: 10
  111. })
  112. it "should prefer the higher of compileTimeout", ->
  113. expect(@FeaturesUpdater._mergeFeatures({
  114. compileTimeout: 20
  115. }, {
  116. compileTimeout: 10
  117. })).to.deep.equal({
  118. compileTimeout: 20
  119. })
  120. expect(@FeaturesUpdater._mergeFeatures({
  121. compileTimeout: 10
  122. }, {
  123. compileTimeout: 20
  124. })).to.deep.equal({
  125. compileTimeout: 20
  126. })
  127. it "should prefer the true over false for other keys", ->
  128. expect(@FeaturesUpdater._mergeFeatures({
  129. github: true
  130. }, {
  131. github: false
  132. })).to.deep.equal({
  133. github: true
  134. })
  135. expect(@FeaturesUpdater._mergeFeatures({
  136. github: false
  137. }, {
  138. github: true
  139. })).to.deep.equal({
  140. github: true
  141. })
  142. expect(@FeaturesUpdater._mergeFeatures({
  143. github: true
  144. }, {
  145. github: true
  146. })).to.deep.equal({
  147. github: true
  148. })
  149. expect(@FeaturesUpdater._mergeFeatures({
  150. github: false
  151. }, {
  152. github: false
  153. })).to.deep.equal({
  154. github: false
  155. })