FeatureUpdaterTests.coffee 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. manager_ids: [@user._id]
  38. planCode: 'collaborator'
  39. customAccount: true
  40. } # returns a promise
  41. it "should set their features to the upgraded set", (done) ->
  42. syncUserAndGetFeatures @user, (error, features) =>
  43. throw error if error?
  44. plan = settings.plans.find (plan) -> plan.planCode == 'collaborator'
  45. expect(features).to.deep.equal(plan.features)
  46. done()
  47. describe "when the user is in a group subscription", ->
  48. beforeEach ->
  49. Subscription.create {
  50. admin_id: ObjectId()
  51. member_ids: [@user._id]
  52. groupAccount: true
  53. planCode: 'collaborator'
  54. customAccount: true
  55. } # returns a promise
  56. it "should set their features to the upgraded set", (done) ->
  57. syncUserAndGetFeatures @user, (error, features) =>
  58. throw error if error?
  59. plan = settings.plans.find (plan) -> plan.planCode == 'collaborator'
  60. expect(features).to.deep.equal(plan.features)
  61. done()
  62. describe "when the user has bonus features", ->
  63. beforeEach ->
  64. User.update {
  65. _id: @user._id
  66. }, {
  67. refered_user_count: 10
  68. } # returns a promise
  69. it "should set their features to the bonus set", (done) ->
  70. syncUserAndGetFeatures @user, (error, features) =>
  71. throw error if error?
  72. expect(features).to.deep.equal(Object.assign(
  73. {}, settings.defaultFeatures, settings.bonus_features[9]
  74. ))
  75. done()
  76. describe "when the user has affiliations", ->
  77. beforeEach ->
  78. @institutionPlan = settings.plans.find (plan) ->
  79. plan.planCode == settings.institutionPlanCode
  80. @email = @user.emails[0].email
  81. affiliationData =
  82. email: @email
  83. institution: { licence: 'pro_plus' }
  84. MockV1Api.setAffiliations [affiliationData]
  85. it "should not set their features if email is not confirmed", (done) ->
  86. syncUserAndGetFeatures @user, (error, features) =>
  87. expect(features).to.deep.equal(settings.defaultFeatures)
  88. done()
  89. it "should set their features if email is confirmed", (done) ->
  90. @user.confirmEmail @email, (error) =>
  91. syncUserAndGetFeatures @user, (error, features) =>
  92. expect(features).to.deep.equal(@institutionPlan.features)
  93. done()
  94. describe "when the user is due bonus features and has extra features that no longer apply", ->
  95. beforeEach ->
  96. User.update {
  97. _id: @user._id
  98. }, {
  99. refered_user_count: 10,
  100. 'features.github': true
  101. } # returns a promise
  102. it "should set their features to the bonus set and downgrade the extras", (done) ->
  103. syncUserAndGetFeatures @user, (error, features) =>
  104. throw error if error?
  105. expect(features).to.deep.equal(Object.assign(
  106. {}, settings.defaultFeatures, settings.bonus_features[9]
  107. ))
  108. done()
  109. describe "when the user has a v1 plan", ->
  110. beforeEach ->
  111. MockV1Api.setUser 42, plan_name: 'free'
  112. User.update {
  113. _id: @user._id
  114. }, {
  115. overleaf:
  116. id: 42
  117. } # returns a promise
  118. it "should set their features to the v1 plan", (done) ->
  119. syncUserAndGetFeatures @user, (error, features) =>
  120. throw error if error?
  121. plan = settings.plans.find (plan) -> plan.planCode == 'v1_free'
  122. expect(features).to.deep.equal(plan.features)
  123. done()
  124. describe "when the user has a v1 plan and bonus features", ->
  125. beforeEach ->
  126. MockV1Api.setUser 42, plan_name: 'free'
  127. User.update {
  128. _id: @user._id
  129. }, {
  130. overleaf:
  131. id: 42
  132. refered_user_count: 10
  133. } # returns a promise
  134. it "should set their features to the best of the v1 plan and bonus features", (done) ->
  135. syncUserAndGetFeatures @user, (error, features) =>
  136. throw error if error?
  137. v1plan = settings.plans.find (plan) -> plan.planCode == 'v1_free'
  138. expectedFeatures = Object.assign(
  139. {}, v1plan.features, settings.bonus_features[9]
  140. )
  141. expect(features).to.deep.equal(expectedFeatures)
  142. done()
  143. describe "when the user has a group and personal subscription", ->
  144. beforeEach (done) ->
  145. Subscription.create {
  146. admin_id: @user._id
  147. manager_ids: [@user._id]
  148. planCode: 'professional'
  149. customAccount: true
  150. }, (error) =>
  151. throw error if error?
  152. Subscription.create {
  153. admin_id: ObjectId()
  154. member_ids: [@user._id]
  155. groupAccount: true
  156. planCode: 'collaborator'
  157. customAccount: true
  158. }, done
  159. return
  160. it "should set their features to the best set", (done) ->
  161. syncUserAndGetFeatures @user, (error, features) =>
  162. throw error if error?
  163. plan = settings.plans.find (plan) -> plan.planCode == 'professional'
  164. expect(features).to.deep.equal(plan.features)
  165. done()
  166. describe "when the notifyV1Flag is passed", ->
  167. beforeEach ->
  168. User.update {
  169. _id: @user._id
  170. }, {
  171. overleaf:
  172. id: 42
  173. } # returns a promise
  174. it "should ping the v1 API end point to sync", (done) ->
  175. FeaturesUpdater.refreshFeatures @user._id, true, (error) =>
  176. setTimeout () =>
  177. expect(
  178. MockV1Api.syncUserFeatures.calledWith('42')
  179. ).to.equal true
  180. done()
  181. , 500