FeatureUpdaterTests.coffee 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. expect = require("chai").expect
  2. async = require("async")
  3. UserClient = require "./helpers/User"
  4. request = require "./helpers/request"
  5. settings = require "settings-sharelatex"
  6. {ObjectId} = require("../../../app/js/infrastructure/mongojs")
  7. Subscription = require("../../../app/js/models/Subscription").Subscription
  8. User = require("../../../app/js/models/User").User
  9. FeaturesUpdater = require("../../../app/js/Features/Subscription/FeaturesUpdater")
  10. MockV1Api = require "./helpers/MockV1Api"
  11. logger = require "logger-sharelatex"
  12. logger.logger.level("error")
  13. syncUserAndGetFeatures = (user, callback = (error, features) ->) ->
  14. FeaturesUpdater.refreshFeatures user._id, false, (error) ->
  15. return callback(error) if error?
  16. User.findById user._id, (error, user) ->
  17. return callback(error) if error?
  18. features = user.toObject().features
  19. delete features.$init # mongoose internals
  20. return callback null, features
  21. describe "FeatureUpdater.refreshFeatures", ->
  22. beforeEach (done) ->
  23. @user = new UserClient()
  24. @user.ensureUserExists (error) ->
  25. throw error if error?
  26. done()
  27. describe "when user has no subscriptions", ->
  28. it "should set their features to the basic set", (done) ->
  29. syncUserAndGetFeatures @user, (error, features) =>
  30. throw error if error?
  31. expect(features).to.deep.equal(settings.defaultFeatures)
  32. done()
  33. describe "when the user has an individual subscription", ->
  34. beforeEach ->
  35. Subscription.create {
  36. admin_id: @user._id
  37. planCode: 'collaborator'
  38. customAccount: true
  39. } # returns a promise
  40. it "should set their features to the upgraded set", (done) ->
  41. syncUserAndGetFeatures @user, (error, features) =>
  42. throw error if error?
  43. plan = settings.plans.find (plan) -> plan.planCode == 'collaborator'
  44. expect(features).to.deep.equal(plan.features)
  45. done()
  46. describe "when the user is in a group subscription", ->
  47. beforeEach ->
  48. Subscription.create {
  49. admin_id: ObjectId()
  50. member_ids: [@user._id]
  51. groupAccount: true
  52. planCode: 'collaborator'
  53. customAccount: true
  54. } # returns a promise
  55. it "should set their features to the upgraded set", (done) ->
  56. syncUserAndGetFeatures @user, (error, features) =>
  57. throw error if error?
  58. plan = settings.plans.find (plan) -> plan.planCode == 'collaborator'
  59. expect(features).to.deep.equal(plan.features)
  60. done()
  61. describe "when the user has bonus features", ->
  62. beforeEach ->
  63. User.update {
  64. _id: @user._id
  65. }, {
  66. refered_user_count: 10
  67. } # returns a promise
  68. it "should set their features to the bonus set", (done) ->
  69. syncUserAndGetFeatures @user, (error, features) =>
  70. throw error if error?
  71. expect(features).to.deep.equal(Object.assign(
  72. {}, settings.defaultFeatures, settings.bonus_features[9]
  73. ))
  74. done()
  75. describe "when the user is due bonus features and has extra features that no longer apply", ->
  76. beforeEach ->
  77. User.update {
  78. _id: @user._id
  79. }, {
  80. refered_user_count: 10,
  81. 'features.github': true
  82. } # returns a promise
  83. it "should set their features to the bonus set and downgrade the extras", (done) ->
  84. syncUserAndGetFeatures @user, (error, features) =>
  85. throw error if error?
  86. expect(features).to.deep.equal(Object.assign(
  87. {}, settings.defaultFeatures, settings.bonus_features[9]
  88. ))
  89. done()
  90. describe "when the user has a v1 plan", ->
  91. beforeEach ->
  92. MockV1Api.setUser 42, plan_name: 'free'
  93. User.update {
  94. _id: @user._id
  95. }, {
  96. overleaf:
  97. id: 42
  98. } # returns a promise
  99. it "should set their features to the v1 plan", (done) ->
  100. syncUserAndGetFeatures @user, (error, features) =>
  101. throw error if error?
  102. plan = settings.plans.find (plan) -> plan.planCode == 'v1_free'
  103. expect(features).to.deep.equal(plan.features)
  104. done()
  105. describe "when the user has a v1 plan and bonus features", ->
  106. beforeEach ->
  107. MockV1Api.setUser 42, plan_name: 'free'
  108. User.update {
  109. _id: @user._id
  110. }, {
  111. overleaf:
  112. id: 42
  113. refered_user_count: 10
  114. } # returns a promise
  115. it "should set their features to the best of the v1 plan and bonus features", (done) ->
  116. syncUserAndGetFeatures @user, (error, features) =>
  117. throw error if error?
  118. v1plan = settings.plans.find (plan) -> plan.planCode == 'v1_free'
  119. expectedFeatures = Object.assign(
  120. {}, v1plan.features, settings.bonus_features[9]
  121. )
  122. expect(features).to.deep.equal(expectedFeatures)
  123. done()
  124. describe "when the user has a group and personal subscription", ->
  125. beforeEach (done) ->
  126. Subscription.create {
  127. admin_id: @user._id
  128. planCode: 'professional'
  129. customAccount: true
  130. }, (error) =>
  131. throw error if error?
  132. Subscription.create {
  133. admin_id: ObjectId()
  134. member_ids: [@user._id]
  135. groupAccount: true
  136. planCode: 'collaborator'
  137. customAccount: true
  138. }, done
  139. return
  140. it "should set their features to the best set", (done) ->
  141. syncUserAndGetFeatures @user, (error, features) =>
  142. throw error if error?
  143. plan = settings.plans.find (plan) -> plan.planCode == 'professional'
  144. expect(features).to.deep.equal(plan.features)
  145. done()
  146. describe "when the notifyV1Flag is passed", ->
  147. beforeEach ->
  148. User.update {
  149. _id: @user._id
  150. }, {
  151. overleaf:
  152. id: 42
  153. } # returns a promise
  154. it "should ping the v1 API end point to sync", (done) ->
  155. FeaturesUpdater.refreshFeatures @user._id, true, (error) =>
  156. setTimeout () =>
  157. expect(
  158. MockV1Api.syncUserFeatures.calledWith('42')
  159. ).to.equal true
  160. done()
  161. , 500