SubscriptionViewModelBuilderTests.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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.InstitutionsManager = {
  81. promises: {
  82. fetchV1Data: sinon.stub().resolves(),
  83. },
  84. }
  85. this.RecurlyWrapper = {
  86. promises: {
  87. getSubscription: sinon.stub().resolves(),
  88. },
  89. }
  90. this.SubscriptionUpdater = {
  91. promises: {
  92. updateSubscriptionFromRecurly: sinon.stub().resolves(),
  93. },
  94. }
  95. this.PlansLocator = {
  96. findLocalPlanInSettings: sinon.stub(),
  97. }
  98. this.SubscriptionViewModelBuilder = SandboxedModule.require(modulePath, {
  99. requires: {
  100. '@overleaf/settings': this.Settings,
  101. './SubscriptionLocator': this.SubscriptionLocator,
  102. '../Institutions/InstitutionsGetter': this.InstitutionsGetter,
  103. '../Institutions/InstitutionsManager': this.InstitutionsManager,
  104. './RecurlyWrapper': this.RecurlyWrapper,
  105. './SubscriptionUpdater': this.SubscriptionUpdater,
  106. './PlansLocator': this.PlansLocator,
  107. './V1SubscriptionManager': {},
  108. './SubscriptionFormatters': {},
  109. '../Publishers/PublishersGetter': {},
  110. './SubscriptionHelper': {},
  111. },
  112. })
  113. this.PlansLocator.findLocalPlanInSettings
  114. .withArgs(this.planCode)
  115. .returns(this.plan)
  116. .withArgs(this.groupPlanCode)
  117. .returns(this.groupPlan)
  118. .withArgs(this.commonsPlanCode)
  119. .returns(this.commonsPlan)
  120. })
  121. describe('getBestSubscription', function () {
  122. it('should return a free plan when user has no subscription or affiliation', async function () {
  123. const usersBestSubscription =
  124. await this.SubscriptionViewModelBuilder.promises.getBestSubscription(
  125. this.user
  126. )
  127. assert.deepEqual(usersBestSubscription, { type: 'free' })
  128. })
  129. describe('with a individual subscription only', function () {
  130. it('should return a individual subscription when user has non-Recurly one', async function () {
  131. this.SubscriptionLocator.promises.getUsersSubscription
  132. .withArgs(this.user)
  133. .resolves(this.individualCustomSubscription)
  134. const usersBestSubscription =
  135. await this.SubscriptionViewModelBuilder.promises.getBestSubscription(
  136. this.user
  137. )
  138. assert.deepEqual(usersBestSubscription, {
  139. type: 'individual',
  140. subscription: this.individualCustomSubscription,
  141. plan: this.plan,
  142. remainingTrialDays: -1,
  143. })
  144. })
  145. it('should return a individual subscription when user has an active one', async function () {
  146. this.SubscriptionLocator.promises.getUsersSubscription
  147. .withArgs(this.user)
  148. .resolves(this.individualSubscription)
  149. const usersBestSubscription =
  150. await this.SubscriptionViewModelBuilder.promises.getBestSubscription(
  151. this.user
  152. )
  153. assert.deepEqual(usersBestSubscription, {
  154. type: 'individual',
  155. subscription: this.individualSubscription,
  156. plan: this.plan,
  157. remainingTrialDays: -1,
  158. })
  159. })
  160. it('should return a individual subscription with remaining free trial days', async function () {
  161. const threeDaysLater = new Date()
  162. threeDaysLater.setDate(threeDaysLater.getDate() + 3)
  163. this.individualSubscription.recurlyStatus.trialEndsAt = threeDaysLater
  164. this.SubscriptionLocator.promises.getUsersSubscription
  165. .withArgs(this.user)
  166. .resolves(this.individualSubscription)
  167. const usersBestSubscription =
  168. await this.SubscriptionViewModelBuilder.promises.getBestSubscription(
  169. this.user
  170. )
  171. assert.deepEqual(usersBestSubscription, {
  172. type: 'individual',
  173. subscription: this.individualSubscription,
  174. plan: this.plan,
  175. remainingTrialDays: 3,
  176. })
  177. })
  178. it('should return a individual subscription with free trial on last day', async function () {
  179. const threeHoursLater = new Date()
  180. threeHoursLater.setTime(threeHoursLater.getTime() + 3 * 60 * 60 * 1000)
  181. this.individualSubscription.recurlyStatus.trialEndsAt = threeHoursLater
  182. this.SubscriptionLocator.promises.getUsersSubscription
  183. .withArgs(this.user)
  184. .resolves(this.individualSubscription)
  185. const usersBestSubscription =
  186. await this.SubscriptionViewModelBuilder.promises.getBestSubscription(
  187. this.user
  188. )
  189. assert.deepEqual(usersBestSubscription, {
  190. type: 'individual',
  191. subscription: this.individualSubscription,
  192. plan: this.plan,
  193. remainingTrialDays: 1,
  194. })
  195. })
  196. it('should update subscription if recurly data is missing', async function () {
  197. this.individualSubscriptionWithoutRecurly = {
  198. planCode: this.planCode,
  199. plan: this.plan,
  200. recurlySubscription_id: this.recurlySubscription_id,
  201. }
  202. this.recurlySubscription = {
  203. state: 'active',
  204. }
  205. this.SubscriptionLocator.promises.getUsersSubscription
  206. .withArgs(this.user)
  207. .onCall(0)
  208. .resolves(this.individualSubscriptionWithoutRecurly)
  209. .withArgs(this.user)
  210. .onCall(1)
  211. .resolves(this.individualSubscription)
  212. this.RecurlyWrapper.promises.getSubscription
  213. .withArgs(this.individualSubscription.recurlySubscription_id, {
  214. includeAccount: true,
  215. })
  216. .resolves(this.recurlySubscription)
  217. const usersBestSubscription =
  218. await this.SubscriptionViewModelBuilder.promises.getBestSubscription(
  219. this.user
  220. )
  221. sinon.assert.calledWith(
  222. this.RecurlyWrapper.promises.getSubscription,
  223. this.individualSubscriptionWithoutRecurly.recurlySubscription_id,
  224. { includeAccount: true }
  225. )
  226. sinon.assert.calledWith(
  227. this.SubscriptionUpdater.promises.updateSubscriptionFromRecurly,
  228. this.recurlySubscription,
  229. this.individualSubscriptionWithoutRecurly
  230. )
  231. assert.deepEqual(usersBestSubscription, {
  232. type: 'individual',
  233. subscription: this.individualSubscription,
  234. plan: this.plan,
  235. remainingTrialDays: -1,
  236. })
  237. })
  238. })
  239. it('should return a group subscription when user has one', async function () {
  240. this.SubscriptionLocator.promises.getMemberSubscriptions
  241. .withArgs(this.user)
  242. .resolves([this.groupSubscription])
  243. const usersBestSubscription =
  244. await this.SubscriptionViewModelBuilder.promises.getBestSubscription(
  245. this.user
  246. )
  247. assert.deepEqual(usersBestSubscription, {
  248. type: 'group',
  249. subscription: {},
  250. plan: this.groupPlan,
  251. remainingTrialDays: -1,
  252. })
  253. })
  254. it('should return a group subscription with team name when user has one', async function () {
  255. this.SubscriptionLocator.promises.getMemberSubscriptions
  256. .withArgs(this.user)
  257. .resolves([
  258. Object.assign({}, this.groupSubscription, { teamName: 'test team' }),
  259. ])
  260. const usersBestSubscription =
  261. await this.SubscriptionViewModelBuilder.promises.getBestSubscription(
  262. this.user
  263. )
  264. assert.deepEqual(usersBestSubscription, {
  265. type: 'group',
  266. subscription: { teamName: 'test team' },
  267. plan: this.groupPlan,
  268. remainingTrialDays: -1,
  269. })
  270. })
  271. it('should return a commons subscription when user has an institution affiliation', async function () {
  272. this.InstitutionsGetter.promises.getCurrentInstitutionsWithLicence
  273. .withArgs(this.user._id)
  274. .resolves([this.commonsSubscription])
  275. const usersBestSubscription =
  276. await this.SubscriptionViewModelBuilder.promises.getBestSubscription(
  277. this.user
  278. )
  279. assert.deepEqual(usersBestSubscription, {
  280. type: 'commons',
  281. subscription: this.commonsSubscription,
  282. plan: this.commonsPlan,
  283. })
  284. })
  285. })
  286. describe('with multiple subscriptions', function () {
  287. beforeEach(function () {
  288. this.SubscriptionLocator.promises.getUsersSubscription
  289. .withArgs(this.user)
  290. .resolves(this.individualSubscription)
  291. this.SubscriptionLocator.promises.getMemberSubscriptions
  292. .withArgs(this.user)
  293. .resolves([this.groupSubscription])
  294. this.InstitutionsGetter.promises.getCurrentInstitutionsWithLicence
  295. .withArgs(this.user._id)
  296. .resolves([this.commonsSubscription])
  297. })
  298. it('should return individual when the individual subscription has the best feature set', async function () {
  299. this.commonsPlan.features = {
  300. compileGroup: 'standard',
  301. collaborators: 1,
  302. compileTimeout: 60,
  303. }
  304. const usersBestSubscription =
  305. await this.SubscriptionViewModelBuilder.promises.getBestSubscription(
  306. this.user
  307. )
  308. assert.deepEqual(usersBestSubscription, {
  309. type: 'individual',
  310. subscription: this.individualSubscription,
  311. plan: this.plan,
  312. remainingTrialDays: -1,
  313. })
  314. })
  315. it('should return group when the group subscription has the best feature set', async function () {
  316. this.plan.features = {
  317. compileGroup: 'standard',
  318. collaborators: 1,
  319. compileTimeout: 60,
  320. }
  321. this.commonsPlan.features = {
  322. compileGroup: 'standard',
  323. collaborators: 1,
  324. compileTimeout: 60,
  325. }
  326. const usersBestSubscription =
  327. await this.SubscriptionViewModelBuilder.promises.getBestSubscription(
  328. this.user
  329. )
  330. assert.deepEqual(usersBestSubscription, {
  331. type: 'group',
  332. subscription: {},
  333. plan: this.groupPlan,
  334. remainingTrialDays: -1,
  335. })
  336. })
  337. it('should return commons when the commons affiliation has the best feature set', async function () {
  338. this.plan.features = {
  339. compileGroup: 'priority',
  340. collaborators: 5,
  341. compileTimeout: 240,
  342. }
  343. this.groupPlan.features = {
  344. compileGroup: 'standard',
  345. collaborators: 1,
  346. compileTimeout: 60,
  347. }
  348. this.commonsPlan.features = {
  349. compileGroup: 'priority',
  350. collaborators: -1,
  351. compileTimeout: 240,
  352. }
  353. const usersBestSubscription =
  354. await this.SubscriptionViewModelBuilder.promises.getBestSubscription(
  355. this.user
  356. )
  357. assert.deepEqual(usersBestSubscription, {
  358. type: 'commons',
  359. subscription: this.commonsSubscription,
  360. plan: this.commonsPlan,
  361. })
  362. })
  363. it('should return individual with equal feature sets', async function () {
  364. this.plan.features = {
  365. compileGroup: 'priority',
  366. collaborators: -1,
  367. compileTimeout: 240,
  368. }
  369. this.groupPlan.features = {
  370. compileGroup: 'priority',
  371. collaborators: -1,
  372. compileTimeout: 240,
  373. }
  374. this.commonsPlan.features = {
  375. compileGroup: 'priority',
  376. collaborators: -1,
  377. compileTimeout: 240,
  378. }
  379. const usersBestSubscription =
  380. await this.SubscriptionViewModelBuilder.promises.getBestSubscription(
  381. this.user
  382. )
  383. assert.deepEqual(usersBestSubscription, {
  384. type: 'individual',
  385. subscription: this.individualSubscription,
  386. plan: this.plan,
  387. remainingTrialDays: -1,
  388. })
  389. })
  390. it('should return group over commons with equal feature sets', async function () {
  391. this.plan.features = {
  392. compileGroup: 'standard',
  393. collaborators: 1,
  394. compileTimeout: 60,
  395. }
  396. this.groupPlan.features = {
  397. compileGroup: 'priority',
  398. collaborators: -1,
  399. compileTimeout: 240,
  400. }
  401. this.commonsPlan.features = {
  402. compileGroup: 'priority',
  403. collaborators: -1,
  404. compileTimeout: 240,
  405. }
  406. const usersBestSubscription =
  407. await this.SubscriptionViewModelBuilder.promises.getBestSubscription(
  408. this.user
  409. )
  410. assert.deepEqual(usersBestSubscription, {
  411. type: 'group',
  412. subscription: {},
  413. plan: this.groupPlan,
  414. remainingTrialDays: -1,
  415. })
  416. })
  417. })
  418. })