FeatureUpdaterTests.coffee 6.2 KB

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