FeatureUpdaterTests.coffee 6.6 KB

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