FeaturesUpdaterTests.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. const SandboxedModule = require('sandboxed-module')
  2. const { expect } = require('chai')
  3. const sinon = require('sinon')
  4. const { ObjectId } = require('mongodb')
  5. const MODULE_PATH = '../../../../app/src/Features/Subscription/FeaturesUpdater'
  6. describe('FeaturesUpdater', function () {
  7. beforeEach(function () {
  8. this.user = {
  9. _id: new ObjectId(),
  10. features: {},
  11. }
  12. this.v1UserId = 12345
  13. this.subscriptions = {
  14. individual: { planCode: 'individual-plan' },
  15. group1: { planCode: 'group-plan-1' },
  16. group2: { planCode: 'group-plan-2' },
  17. noDropbox: { planCode: 'no-dropbox' },
  18. }
  19. this.UserFeaturesUpdater = {
  20. promises: {
  21. updateFeatures: sinon
  22. .stub()
  23. .resolves({ features: { some: 'features' }, featuresChanged: true }),
  24. },
  25. }
  26. this.SubscriptionLocator = {
  27. promises: {
  28. getUserIndividualSubscription: sinon.stub(),
  29. getGroupSubscriptionsMemberOf: sinon.stub(),
  30. },
  31. }
  32. this.SubscriptionLocator.promises.getUserIndividualSubscription
  33. .withArgs(this.user._id)
  34. .resolves(this.subscriptions.individual)
  35. this.SubscriptionLocator.promises.getGroupSubscriptionsMemberOf
  36. .withArgs(this.user._id)
  37. .resolves([this.subscriptions.group1, this.subscriptions.group2])
  38. this.Settings = {
  39. defaultFeatures: { default: 'features' },
  40. plans: [
  41. { planCode: 'individual-plan', features: { individual: 'features' } },
  42. { planCode: 'group-plan-1', features: { group1: 'features' } },
  43. { planCode: 'group-plan-2', features: { group2: 'features' } },
  44. { planCode: 'v1-plan', features: { v1: 'features' } },
  45. { planCode: 'no-dropbox', features: { dropbox: false } },
  46. ],
  47. features: {
  48. all: {
  49. default: 'features',
  50. individual: 'features',
  51. group1: 'features',
  52. group2: 'features',
  53. institutions: 'features',
  54. v1: 'features',
  55. grandfathered: 'features',
  56. bonus: 'features',
  57. },
  58. },
  59. }
  60. this.ReferalFeatures = {
  61. promises: {
  62. getBonusFeatures: sinon.stub().resolves({ bonus: 'features' }),
  63. },
  64. }
  65. this.V1SubscriptionManager = {
  66. getGrandfatheredFeaturesForV1User: sinon.stub(),
  67. promises: {
  68. getPlanCodeFromV1: sinon.stub(),
  69. },
  70. }
  71. this.V1SubscriptionManager.promises.getPlanCodeFromV1
  72. .withArgs(this.user._id)
  73. .resolves({ planCode: 'v1-plan', v1Id: this.v1UserId })
  74. this.V1SubscriptionManager.getGrandfatheredFeaturesForV1User
  75. .withArgs(this.v1UserId)
  76. .returns({ grandfathered: 'features' })
  77. this.InstitutionsFeatures = {
  78. promises: {
  79. getInstitutionsFeatures: sinon
  80. .stub()
  81. .resolves({ institutions: 'features' }),
  82. },
  83. }
  84. this.UserGetter = {
  85. promises: {
  86. getUser: sinon.stub().resolves(null),
  87. },
  88. }
  89. this.UserGetter.promises.getUser.withArgs(this.user._id).resolves(this.user)
  90. this.UserGetter.promises.getUser
  91. .withArgs({ 'overleaf.id': this.v1UserId })
  92. .resolves(this.user)
  93. this.AnalyticsManager = {
  94. setUserPropertyForUser: sinon.stub(),
  95. }
  96. this.Modules = {
  97. promises: { hooks: { fire: sinon.stub().resolves() } },
  98. }
  99. this.FeaturesHelper = {
  100. mergeFeatures: sinon.stub().callsFake((a, b) => ({ ...a, ...b })),
  101. }
  102. this.FeaturesUpdater = SandboxedModule.require(MODULE_PATH, {
  103. requires: {
  104. './UserFeaturesUpdater': this.UserFeaturesUpdater,
  105. './SubscriptionLocator': this.SubscriptionLocator,
  106. './FeaturesHelper': this.FeaturesHelper,
  107. '@overleaf/settings': this.Settings,
  108. '../Referal/ReferalFeatures': this.ReferalFeatures,
  109. './V1SubscriptionManager': this.V1SubscriptionManager,
  110. '../Institutions/InstitutionsFeatures': this.InstitutionsFeatures,
  111. '../User/UserGetter': this.UserGetter,
  112. '../Analytics/AnalyticsManager': this.AnalyticsManager,
  113. '../../infrastructure/Modules': this.Modules,
  114. },
  115. })
  116. })
  117. describe('refreshFeatures', function () {
  118. it('should return features and featuresChanged', async function () {
  119. const { features, featuresChanged } =
  120. await this.FeaturesUpdater.promises.refreshFeatures(
  121. this.user._id,
  122. 'test'
  123. )
  124. expect(features).to.exist
  125. expect(featuresChanged).to.exist
  126. })
  127. describe('normally', function () {
  128. beforeEach(async function () {
  129. await this.FeaturesUpdater.promises.refreshFeatures(
  130. this.user._id,
  131. 'test'
  132. )
  133. })
  134. it('should update the user with the merged features', function () {
  135. expect(
  136. this.UserFeaturesUpdater.promises.updateFeatures
  137. ).to.have.been.calledWith(this.user._id, this.Settings.features.all)
  138. })
  139. it('should send the corresponding feature set user property', function () {
  140. expect(
  141. this.AnalyticsManager.setUserPropertyForUser
  142. ).to.have.been.calledWith(this.user._id, 'feature-set', 'all')
  143. })
  144. })
  145. describe('with a non-standard feature set', async function () {
  146. beforeEach(async function () {
  147. this.SubscriptionLocator.promises.getGroupSubscriptionsMemberOf
  148. .withArgs(this.user._id)
  149. .resolves(null)
  150. await this.FeaturesUpdater.promises.refreshFeatures(
  151. this.user._id,
  152. 'test'
  153. )
  154. })
  155. it('should send mixed feature set user property', function () {
  156. sinon.assert.calledWith(
  157. this.AnalyticsManager.setUserPropertyForUser,
  158. this.user._id,
  159. 'feature-set',
  160. 'mixed'
  161. )
  162. })
  163. })
  164. describe('when losing dropbox feature', async function () {
  165. beforeEach(async function () {
  166. this.user.features = { dropbox: true }
  167. this.SubscriptionLocator.promises.getUserIndividualSubscription
  168. .withArgs(this.user._id)
  169. .resolves(this.subscriptions.noDropbox)
  170. await this.FeaturesUpdater.promises.refreshFeatures(
  171. this.user._id,
  172. 'test'
  173. )
  174. })
  175. it('should fire module hook to unlink dropbox', function () {
  176. expect(this.Modules.promises.hooks.fire).to.have.been.calledWith(
  177. 'removeDropbox',
  178. this.user._id,
  179. 'test'
  180. )
  181. })
  182. })
  183. })
  184. describe('doSyncFromV1', function () {
  185. describe('when all goes well', function () {
  186. beforeEach(async function () {
  187. await this.FeaturesUpdater.promises.doSyncFromV1(this.v1UserId)
  188. })
  189. it('should update the user with the merged features', function () {
  190. expect(
  191. this.UserFeaturesUpdater.promises.updateFeatures
  192. ).to.have.been.calledWith(this.user._id, this.Settings.features.all)
  193. })
  194. })
  195. describe('when getUser produces an error', function () {
  196. beforeEach(function () {
  197. this.UserGetter.promises.getUser.rejects(new Error('woops'))
  198. })
  199. it('should propagate the error', async function () {
  200. const someId = 9090
  201. await expect(this.FeaturesUpdater.promises.doSyncFromV1(someId)).to.be
  202. .rejected
  203. expect(this.UserFeaturesUpdater.promises.updateFeatures).not.to.have
  204. .been.called
  205. })
  206. })
  207. describe('when getUser does not find a user', function () {
  208. beforeEach(async function () {
  209. const someOtherId = 987
  210. await this.FeaturesUpdater.promises.doSyncFromV1(someOtherId)
  211. })
  212. it('should not update the user', function () {
  213. expect(this.UserFeaturesUpdater.promises.updateFeatures).not.to.have
  214. .been.called
  215. })
  216. })
  217. })
  218. })