FeatureUpdaterTests.coffee 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 a v1 plan", ->
  76. beforeEach ->
  77. MockV1Api.setUser 42, plan_name: 'free'
  78. User.update {
  79. _id: @user._id
  80. }, {
  81. overleaf:
  82. id: 42
  83. } # returns a promise
  84. it "should set their features to the v1 plan", (done) ->
  85. syncUserAndGetFeatures @user, (error, features) =>
  86. throw error if error?
  87. plan = settings.plans.find (plan) -> plan.planCode == 'v1_free'
  88. expect(features).to.deep.equal(plan.features)
  89. done()
  90. describe "when the user has a v1 plan and bonus features", ->
  91. beforeEach ->
  92. MockV1Api.setUser 42, plan_name: 'free'
  93. User.update {
  94. _id: @user._id
  95. }, {
  96. overleaf:
  97. id: 42
  98. refered_user_count: 10
  99. } # returns a promise
  100. it "should set their features to the best of the v1 plan and bonus features", (done) ->
  101. syncUserAndGetFeatures @user, (error, features) =>
  102. throw error if error?
  103. v1plan = settings.plans.find (plan) -> plan.planCode == 'v1_free'
  104. expectedFeatures = Object.assign(
  105. {}, v1plan.features, settings.bonus_features[9]
  106. )
  107. expect(features).to.deep.equal(expectedFeatures)
  108. done()
  109. describe "when the user has a group and personal subscription", ->
  110. beforeEach (done) ->
  111. Subscription.create {
  112. admin_id: @user._id
  113. planCode: 'professional'
  114. customAccount: true
  115. }, (error) =>
  116. throw error if error?
  117. Subscription.create {
  118. admin_id: ObjectId()
  119. member_ids: [@user._id]
  120. groupAccount: true
  121. planCode: 'collaborator'
  122. customAccount: true
  123. }, done
  124. return
  125. it "should set their features to the best set", (done) ->
  126. syncUserAndGetFeatures @user, (error, features) =>
  127. throw error if error?
  128. plan = settings.plans.find (plan) -> plan.planCode == 'professional'
  129. expect(features).to.deep.equal(plan.features)
  130. done()
  131. describe "when the notifyV1Flag is passed", ->
  132. beforeEach ->
  133. User.update {
  134. _id: @user._id
  135. }, {
  136. overleaf:
  137. id: 42
  138. } # returns a promise
  139. it "should ping the v1 API end point to sync", (done) ->
  140. FeaturesUpdater.refreshFeatures @user._id, true, (error) =>
  141. setTimeout () =>
  142. expect(
  143. MockV1Api.syncUserFeatures.calledWith('42')
  144. ).to.equal true
  145. done()
  146. , 500