FeaturesUpdaterTests.coffee 6.1 KB

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