SubscriptionViewModelBuilderTests.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. const SandboxedModule = require('sandboxed-module')
  2. const sinon = require('sinon')
  3. const { assert } = require('chai')
  4. const modulePath =
  5. '../../../../app/src/Features/Subscription/SubscriptionViewModelBuilder'
  6. describe('SubscriptionViewModelBuilder', function () {
  7. beforeEach(function () {
  8. this.user = { _id: '5208dd34438842e2db333333' }
  9. this.recurlySubscription_id = '123abc456def'
  10. this.planCode = 'collaborator_monthly'
  11. this.planFeatures = {
  12. compileGroup: 'priority',
  13. collaborators: -1,
  14. compileTimeout: 240,
  15. }
  16. this.plan = {
  17. planCode: this.planCode,
  18. features: this.planFeatures,
  19. }
  20. this.individualSubscription = {
  21. planCode: this.planCode,
  22. plan: this.plan,
  23. recurlySubscription_id: this.recurlySubscription_id,
  24. recurlyStatus: {
  25. state: 'active',
  26. },
  27. }
  28. this.individualCustomSubscription = {
  29. planCode: this.planCode,
  30. plan: this.plan,
  31. recurlySubscription_id: this.recurlySubscription_id,
  32. }
  33. this.groupPlanCode = 'group_collaborator_monthly'
  34. this.groupPlanFeatures = {
  35. compileGroup: 'priority',
  36. collaborators: 10,
  37. compileTimeout: 240,
  38. }
  39. this.groupPlan = {
  40. planCode: this.groupPlanCode,
  41. features: this.groupPlanFeatures,
  42. }
  43. this.groupSubscription = {
  44. planCode: this.groupPlanCode,
  45. plan: this.plan,
  46. recurlyStatus: {
  47. state: 'active',
  48. },
  49. }
  50. this.commonsPlanCode = 'commons_license'
  51. this.commonsPlanFeatures = {
  52. compileGroup: 'priority',
  53. collaborators: '-1',
  54. compileTimeout: 240,
  55. }
  56. this.commonsPlan = {
  57. planCode: this.commonsPlanCode,
  58. features: this.commonsPlanFeatures,
  59. }
  60. this.commonsSubscription = {
  61. planCode: this.commonsPlanCode,
  62. plan: this.commonsPlan,
  63. name: 'Digital Science',
  64. }
  65. this.Settings = {
  66. institutionPlanCode: this.commonsPlanCode,
  67. }
  68. this.SubscriptionLocator = {
  69. promises: {
  70. getUsersSubscription: sinon.stub().resolves(),
  71. getMemberSubscriptions: sinon.stub().resolves(),
  72. },
  73. findLocalPlanInSettings: sinon.stub(),
  74. }
  75. this.InstitutionsGetter = {
  76. promises: {
  77. getCurrentInstitutionsWithLicence: sinon.stub().resolves(),
  78. },
  79. }
  80. this.RecurlyWrapper = {
  81. promises: {
  82. getSubscription: sinon.stub().resolves(),
  83. },
  84. }
  85. this.SubscriptionUpdater = {
  86. promises: {
  87. updateSubscriptionFromRecurly: sinon.stub().resolves(),
  88. },
  89. }
  90. this.PlansLocator = {
  91. findLocalPlanInSettings: sinon.stub(),
  92. }
  93. this.SubscriptionViewModelBuilder = SandboxedModule.require(modulePath, {
  94. requires: {
  95. '@overleaf/settings': this.Settings,
  96. './SubscriptionLocator': this.SubscriptionLocator,
  97. '../Institutions/InstitutionsGetter': this.InstitutionsGetter,
  98. './RecurlyWrapper': this.RecurlyWrapper,
  99. './SubscriptionUpdater': this.SubscriptionUpdater,
  100. './PlansLocator': this.PlansLocator,
  101. './V1SubscriptionManager': {},
  102. './SubscriptionFormatters': {},
  103. '../Publishers/PublishersGetter': {},
  104. './SubscriptionHelper': {},
  105. },
  106. })
  107. this.PlansLocator.findLocalPlanInSettings
  108. .withArgs(this.planCode)
  109. .returns(this.plan)
  110. .withArgs(this.groupPlanCode)
  111. .returns(this.groupPlan)
  112. .withArgs(this.commonsPlanCode)
  113. .returns(this.commonsPlan)
  114. })
  115. describe('getBestSubscription', function () {
  116. it('should return a free plan when user has no subscription or affiliation', async function () {
  117. const usersBestSubscription =
  118. await this.SubscriptionViewModelBuilder.promises.getBestSubscription(
  119. this.user
  120. )
  121. assert.deepEqual(usersBestSubscription, { type: 'free' })
  122. })
  123. describe('with a individual subscription only', function () {
  124. it('should return a individual subscription when user has non-Recurly one', async function () {
  125. this.SubscriptionLocator.promises.getUsersSubscription
  126. .withArgs(this.user)
  127. .resolves(this.individualCustomSubscription)
  128. const usersBestSubscription =
  129. await this.SubscriptionViewModelBuilder.promises.getBestSubscription(
  130. this.user
  131. )
  132. assert.deepEqual(usersBestSubscription, {
  133. type: 'individual',
  134. subscription: this.individualCustomSubscription,
  135. plan: this.plan,
  136. remainingTrialDays: -1,
  137. })
  138. })
  139. it('should return a individual subscription when user has an active one', async function () {
  140. this.SubscriptionLocator.promises.getUsersSubscription
  141. .withArgs(this.user)
  142. .resolves(this.individualSubscription)
  143. const usersBestSubscription =
  144. await this.SubscriptionViewModelBuilder.promises.getBestSubscription(
  145. this.user
  146. )
  147. assert.deepEqual(usersBestSubscription, {
  148. type: 'individual',
  149. subscription: this.individualSubscription,
  150. plan: this.plan,
  151. remainingTrialDays: -1,
  152. })
  153. })
  154. it('should return a individual subscription with remaining free trial days', async function () {
  155. const threeDaysLater = new Date()
  156. threeDaysLater.setDate(threeDaysLater.getDate() + 3)
  157. this.individualSubscription.recurlyStatus.trialEndsAt = threeDaysLater
  158. this.SubscriptionLocator.promises.getUsersSubscription
  159. .withArgs(this.user)
  160. .resolves(this.individualSubscription)
  161. const usersBestSubscription =
  162. await this.SubscriptionViewModelBuilder.promises.getBestSubscription(
  163. this.user
  164. )
  165. assert.deepEqual(usersBestSubscription, {
  166. type: 'individual',
  167. subscription: this.individualSubscription,
  168. plan: this.plan,
  169. remainingTrialDays: 3,
  170. })
  171. })
  172. it('should return a individual subscription with free trial on last day', async function () {
  173. const threeHoursLater = new Date()
  174. threeHoursLater.setTime(threeHoursLater.getTime() + 3 * 60 * 60 * 1000)
  175. this.individualSubscription.recurlyStatus.trialEndsAt = threeHoursLater
  176. this.SubscriptionLocator.promises.getUsersSubscription
  177. .withArgs(this.user)
  178. .resolves(this.individualSubscription)
  179. const usersBestSubscription =
  180. await this.SubscriptionViewModelBuilder.promises.getBestSubscription(
  181. this.user
  182. )
  183. assert.deepEqual(usersBestSubscription, {
  184. type: 'individual',
  185. subscription: this.individualSubscription,
  186. plan: this.plan,
  187. remainingTrialDays: 1,
  188. })
  189. })
  190. it('should update subscription if recurly data is missing', async function () {
  191. this.individualSubscriptionWithoutRecurly = {
  192. planCode: this.planCode,
  193. plan: this.plan,
  194. recurlySubscription_id: this.recurlySubscription_id,
  195. }
  196. this.recurlySubscription = {
  197. state: 'active',
  198. }
  199. this.SubscriptionLocator.promises.getUsersSubscription
  200. .withArgs(this.user)
  201. .onCall(0)
  202. .resolves(this.individualSubscriptionWithoutRecurly)
  203. .withArgs(this.user)
  204. .onCall(1)
  205. .resolves(this.individualSubscription)
  206. this.RecurlyWrapper.promises.getSubscription
  207. .withArgs(this.individualSubscription.recurlySubscription_id, {
  208. includeAccount: true,
  209. })
  210. .resolves(this.recurlySubscription)
  211. const usersBestSubscription =
  212. await this.SubscriptionViewModelBuilder.promises.getBestSubscription(
  213. this.user
  214. )
  215. sinon.assert.calledWith(
  216. this.RecurlyWrapper.promises.getSubscription,
  217. this.individualSubscriptionWithoutRecurly.recurlySubscription_id,
  218. { includeAccount: true }
  219. )
  220. sinon.assert.calledWith(
  221. this.SubscriptionUpdater.promises.updateSubscriptionFromRecurly,
  222. this.recurlySubscription,
  223. this.individualSubscriptionWithoutRecurly
  224. )
  225. assert.deepEqual(usersBestSubscription, {
  226. type: 'individual',
  227. subscription: this.individualSubscription,
  228. plan: this.plan,
  229. remainingTrialDays: -1,
  230. })
  231. })
  232. })
  233. it('should return a group subscription when user has one', async function () {
  234. this.SubscriptionLocator.promises.getMemberSubscriptions
  235. .withArgs(this.user)
  236. .resolves([this.groupSubscription])
  237. const usersBestSubscription =
  238. await this.SubscriptionViewModelBuilder.promises.getBestSubscription(
  239. this.user
  240. )
  241. assert.deepEqual(usersBestSubscription, {
  242. type: 'group',
  243. subscription: this.groupSubscription,
  244. plan: this.groupPlan,
  245. remainingTrialDays: -1,
  246. })
  247. })
  248. it('should return a commons subscription when user has an institution affiliation', async function () {
  249. this.InstitutionsGetter.promises.getCurrentInstitutionsWithLicence
  250. .withArgs(this.user._id)
  251. .resolves([this.commonsSubscription])
  252. const usersBestSubscription =
  253. await this.SubscriptionViewModelBuilder.promises.getBestSubscription(
  254. this.user
  255. )
  256. assert.deepEqual(usersBestSubscription, {
  257. type: 'commons',
  258. subscription: this.commonsSubscription,
  259. plan: this.commonsPlan,
  260. })
  261. })
  262. })
  263. describe('with multiple subscriptions', function () {
  264. beforeEach(function () {
  265. this.SubscriptionLocator.promises.getUsersSubscription
  266. .withArgs(this.user)
  267. .resolves(this.individualSubscription)
  268. this.SubscriptionLocator.promises.getMemberSubscriptions
  269. .withArgs(this.user)
  270. .resolves([this.groupSubscription])
  271. this.InstitutionsGetter.promises.getCurrentInstitutionsWithLicence
  272. .withArgs(this.user._id)
  273. .resolves([this.commonsSubscription])
  274. })
  275. it('should return individual when the individual subscription has the best feature set', async function () {
  276. this.commonsPlan.features = {
  277. compileGroup: 'standard',
  278. collaborators: 1,
  279. compileTimeout: 60,
  280. }
  281. const usersBestSubscription =
  282. await this.SubscriptionViewModelBuilder.promises.getBestSubscription(
  283. this.user
  284. )
  285. assert.deepEqual(usersBestSubscription, {
  286. type: 'individual',
  287. subscription: this.individualSubscription,
  288. plan: this.plan,
  289. remainingTrialDays: -1,
  290. })
  291. })
  292. it('should return group when the group subscription has the best feature set', async function () {
  293. this.plan.features = {
  294. compileGroup: 'standard',
  295. collaborators: 1,
  296. compileTimeout: 60,
  297. }
  298. this.commonsPlan.features = {
  299. compileGroup: 'standard',
  300. collaborators: 1,
  301. compileTimeout: 60,
  302. }
  303. const usersBestSubscription =
  304. await this.SubscriptionViewModelBuilder.promises.getBestSubscription(
  305. this.user
  306. )
  307. assert.deepEqual(usersBestSubscription, {
  308. type: 'group',
  309. subscription: this.groupSubscription,
  310. plan: this.groupPlan,
  311. remainingTrialDays: -1,
  312. })
  313. })
  314. it('should return commons when the commons affiliation has the best feature set', async function () {
  315. this.plan.features = {
  316. compileGroup: 'priority',
  317. collaborators: 5,
  318. compileTimeout: 240,
  319. }
  320. this.groupPlan.features = {
  321. compileGroup: 'standard',
  322. collaborators: 1,
  323. compileTimeout: 60,
  324. }
  325. this.commonsPlan.features = {
  326. compileGroup: 'priority',
  327. collaborators: -1,
  328. compileTimeout: 240,
  329. }
  330. const usersBestSubscription =
  331. await this.SubscriptionViewModelBuilder.promises.getBestSubscription(
  332. this.user
  333. )
  334. assert.deepEqual(usersBestSubscription, {
  335. type: 'commons',
  336. subscription: this.commonsSubscription,
  337. plan: this.commonsPlan,
  338. })
  339. })
  340. it('should return individual with equal feature sets', async function () {
  341. this.plan.features = {
  342. compileGroup: 'priority',
  343. collaborators: -1,
  344. compileTimeout: 240,
  345. }
  346. this.groupPlan.features = {
  347. compileGroup: 'priority',
  348. collaborators: -1,
  349. compileTimeout: 240,
  350. }
  351. this.commonsPlan.features = {
  352. compileGroup: 'priority',
  353. collaborators: -1,
  354. compileTimeout: 240,
  355. }
  356. const usersBestSubscription =
  357. await this.SubscriptionViewModelBuilder.promises.getBestSubscription(
  358. this.user
  359. )
  360. assert.deepEqual(usersBestSubscription, {
  361. type: 'individual',
  362. subscription: this.individualSubscription,
  363. plan: this.plan,
  364. remainingTrialDays: -1,
  365. })
  366. })
  367. it('should return group over commons with equal feature sets', async function () {
  368. this.plan.features = {
  369. compileGroup: 'standard',
  370. collaborators: 1,
  371. compileTimeout: 60,
  372. }
  373. this.groupPlan.features = {
  374. compileGroup: 'priority',
  375. collaborators: -1,
  376. compileTimeout: 240,
  377. }
  378. this.commonsPlan.features = {
  379. compileGroup: 'priority',
  380. collaborators: -1,
  381. compileTimeout: 240,
  382. }
  383. const usersBestSubscription =
  384. await this.SubscriptionViewModelBuilder.promises.getBestSubscription(
  385. this.user
  386. )
  387. assert.deepEqual(usersBestSubscription, {
  388. type: 'group',
  389. subscription: this.groupSubscription,
  390. plan: this.groupPlan,
  391. remainingTrialDays: -1,
  392. })
  393. })
  394. })
  395. })