SubscriptionViewModelBuilderTests.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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. recurly: {
  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. recurly: {
  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.recurly.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.recurly.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 requesterData = { id: this.user._id, ip: '1.2.3.4' }
  212. const usersBestSubscription =
  213. await this.SubscriptionViewModelBuilder.promises.getBestSubscription(
  214. this.user,
  215. requesterData
  216. )
  217. sinon.assert.calledWith(
  218. this.RecurlyWrapper.promises.getSubscription,
  219. this.individualSubscriptionWithoutRecurly.recurlySubscription_id,
  220. { includeAccount: true }
  221. )
  222. sinon.assert.calledWith(
  223. this.SubscriptionUpdater.promises.updateSubscriptionFromRecurly,
  224. this.recurlySubscription,
  225. this.individualSubscriptionWithoutRecurly,
  226. requesterData
  227. )
  228. assert.deepEqual(usersBestSubscription, {
  229. type: 'individual',
  230. subscription: this.individualSubscription,
  231. plan: this.plan,
  232. remainingTrialDays: -1,
  233. })
  234. })
  235. })
  236. it('should return a group subscription when user has one', async function () {
  237. this.SubscriptionLocator.promises.getMemberSubscriptions
  238. .withArgs(this.user)
  239. .resolves([this.groupSubscription])
  240. const usersBestSubscription =
  241. await this.SubscriptionViewModelBuilder.promises.getBestSubscription(
  242. this.user
  243. )
  244. assert.deepEqual(usersBestSubscription, {
  245. type: 'group',
  246. subscription: this.groupSubscription,
  247. plan: this.groupPlan,
  248. remainingTrialDays: -1,
  249. })
  250. })
  251. it('should return a commons subscription when user has an institution affiliation', async function () {
  252. this.InstitutionsGetter.promises.getCurrentInstitutionsWithLicence
  253. .withArgs(this.user._id)
  254. .resolves([this.commonsSubscription])
  255. const usersBestSubscription =
  256. await this.SubscriptionViewModelBuilder.promises.getBestSubscription(
  257. this.user
  258. )
  259. assert.deepEqual(usersBestSubscription, {
  260. type: 'commons',
  261. subscription: this.commonsSubscription,
  262. plan: this.commonsPlan,
  263. })
  264. })
  265. })
  266. describe('with multiple subscriptions', function () {
  267. beforeEach(function () {
  268. this.SubscriptionLocator.promises.getUsersSubscription
  269. .withArgs(this.user)
  270. .resolves(this.individualSubscription)
  271. this.SubscriptionLocator.promises.getMemberSubscriptions
  272. .withArgs(this.user)
  273. .resolves([this.groupSubscription])
  274. this.InstitutionsGetter.promises.getCurrentInstitutionsWithLicence
  275. .withArgs(this.user._id)
  276. .resolves([this.commonsSubscription])
  277. })
  278. it('should return individual when the individual subscription has the best feature set', async function () {
  279. this.commonsPlan.features = {
  280. compileGroup: 'standard',
  281. collaborators: 1,
  282. compileTimeout: 60,
  283. }
  284. const usersBestSubscription =
  285. await this.SubscriptionViewModelBuilder.promises.getBestSubscription(
  286. this.user
  287. )
  288. assert.deepEqual(usersBestSubscription, {
  289. type: 'individual',
  290. subscription: this.individualSubscription,
  291. plan: this.plan,
  292. remainingTrialDays: -1,
  293. })
  294. })
  295. it('should return group when the group subscription has the best feature set', async function () {
  296. this.plan.features = {
  297. compileGroup: 'standard',
  298. collaborators: 1,
  299. compileTimeout: 60,
  300. }
  301. this.commonsPlan.features = {
  302. compileGroup: 'standard',
  303. collaborators: 1,
  304. compileTimeout: 60,
  305. }
  306. const usersBestSubscription =
  307. await this.SubscriptionViewModelBuilder.promises.getBestSubscription(
  308. this.user
  309. )
  310. assert.deepEqual(usersBestSubscription, {
  311. type: 'group',
  312. subscription: this.groupSubscription,
  313. plan: this.groupPlan,
  314. remainingTrialDays: -1,
  315. })
  316. })
  317. it('should return commons when the commons affiliation has the best feature set', async function () {
  318. this.plan.features = {
  319. compileGroup: 'priority',
  320. collaborators: 5,
  321. compileTimeout: 240,
  322. }
  323. this.groupPlan.features = {
  324. compileGroup: 'standard',
  325. collaborators: 1,
  326. compileTimeout: 60,
  327. }
  328. this.commonsPlan.features = {
  329. compileGroup: 'priority',
  330. collaborators: -1,
  331. compileTimeout: 240,
  332. }
  333. const usersBestSubscription =
  334. await this.SubscriptionViewModelBuilder.promises.getBestSubscription(
  335. this.user
  336. )
  337. assert.deepEqual(usersBestSubscription, {
  338. type: 'commons',
  339. subscription: this.commonsSubscription,
  340. plan: this.commonsPlan,
  341. })
  342. })
  343. it('should return individual with equal feature sets', async function () {
  344. this.plan.features = {
  345. compileGroup: 'priority',
  346. collaborators: -1,
  347. compileTimeout: 240,
  348. }
  349. this.groupPlan.features = {
  350. compileGroup: 'priority',
  351. collaborators: -1,
  352. compileTimeout: 240,
  353. }
  354. this.commonsPlan.features = {
  355. compileGroup: 'priority',
  356. collaborators: -1,
  357. compileTimeout: 240,
  358. }
  359. const usersBestSubscription =
  360. await this.SubscriptionViewModelBuilder.promises.getBestSubscription(
  361. this.user
  362. )
  363. assert.deepEqual(usersBestSubscription, {
  364. type: 'individual',
  365. subscription: this.individualSubscription,
  366. plan: this.plan,
  367. remainingTrialDays: -1,
  368. })
  369. })
  370. it('should return group over commons with equal feature sets', async function () {
  371. this.plan.features = {
  372. compileGroup: 'standard',
  373. collaborators: 1,
  374. compileTimeout: 60,
  375. }
  376. this.groupPlan.features = {
  377. compileGroup: 'priority',
  378. collaborators: -1,
  379. compileTimeout: 240,
  380. }
  381. this.commonsPlan.features = {
  382. compileGroup: 'priority',
  383. collaborators: -1,
  384. compileTimeout: 240,
  385. }
  386. const usersBestSubscription =
  387. await this.SubscriptionViewModelBuilder.promises.getBestSubscription(
  388. this.user
  389. )
  390. assert.deepEqual(usersBestSubscription, {
  391. type: 'group',
  392. subscription: this.groupSubscription,
  393. plan: this.groupPlan,
  394. remainingTrialDays: -1,
  395. })
  396. })
  397. })
  398. })