SubscriptionGroupHandlerTests.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  1. const SandboxedModule = require('sandboxed-module')
  2. const sinon = require('sinon')
  3. const { expect } = require('chai')
  4. const MockRequest = require('../helpers/MockRequest')
  5. const modulePath =
  6. '../../../../app/src/Features/Subscription/SubscriptionGroupHandler'
  7. describe('SubscriptionGroupHandler', function () {
  8. beforeEach(function () {
  9. this.adminUser_id = '12321'
  10. this.newEmail = 'bob@smith.com'
  11. this.user_id = '3121321'
  12. this.email = 'jim@example.com'
  13. this.user = { _id: this.user_id, email: this.newEmail }
  14. this.subscription_id = '31DSd1123D'
  15. this.adding = 1
  16. this.paymentMethod = { cardType: 'Visa', lastFour: '1111' }
  17. this.RecurlyEntities = {
  18. MEMBERS_LIMIT_ADD_ON_CODE: 'additional-license',
  19. }
  20. this.localPlanInSettings = {
  21. membersLimit: 5,
  22. membersLimitAddOn: this.RecurlyEntities.MEMBERS_LIMIT_ADD_ON_CODE,
  23. }
  24. this.subscription = {
  25. admin_id: this.adminUser_id,
  26. manager_ids: [this.adminUser_id],
  27. _id: this.subscription_id,
  28. }
  29. this.changeRequest = {
  30. timeframe: 'now',
  31. subscription: {
  32. id: 'test_id',
  33. },
  34. }
  35. this.recurlySubscription = {
  36. id: 123,
  37. addOns: [
  38. {
  39. code: this.RecurlyEntities.MEMBERS_LIMIT_ADD_ON_CODE,
  40. quantity: 1,
  41. },
  42. ],
  43. getRequestForAddOnUpdate: sinon.stub().returns(this.changeRequest),
  44. getRequestForGroupPlanUpgrade: sinon.stub().returns(this.changeRequest),
  45. getRequestForAddOnPurchase: sinon.stub().returns(this.changeRequest),
  46. getRequestForFlexibleLicensingGroupPlanUpgrade: sinon
  47. .stub()
  48. .returns(this.changeRequest),
  49. currency: 'USD',
  50. hasAddOn(code) {
  51. return this.addOns.some(addOn => addOn.code === code)
  52. },
  53. }
  54. this.SubscriptionLocator = {
  55. promises: {
  56. getUsersSubscription: sinon.stub().resolves({
  57. groupPlan: true,
  58. recurlyStatus: {
  59. state: 'active',
  60. },
  61. }),
  62. getSubscriptionByMemberIdAndId: sinon.stub(),
  63. getSubscription: sinon.stub().resolves(this.subscription),
  64. },
  65. }
  66. this.changePreview = {
  67. currency: 'USD',
  68. }
  69. this.SubscriptionController = {
  70. makeChangePreview: sinon.stub().resolves(this.changePreview),
  71. getPlanNameForDisplay: sinon.stub().resolves(),
  72. }
  73. this.SubscriptionUpdater = {
  74. promises: {
  75. removeUserFromGroup: sinon.stub().resolves(),
  76. getSubscription: sinon.stub().resolves(),
  77. },
  78. }
  79. this.Subscription = {
  80. updateOne: sinon.stub().returns({ exec: sinon.stub().resolves }),
  81. updateMany: sinon.stub().returns({ exec: sinon.stub().resolves }),
  82. findOne: sinon.stub().returns({ exec: sinon.stub().resolves }),
  83. }
  84. this.SessionManager = {
  85. getLoggedInUserId: sinon.stub().returns(this.user._id),
  86. }
  87. this.previewSubscriptionChange = {
  88. nextAddOns: [
  89. {
  90. code: this.RecurlyEntities.MEMBERS_LIMIT_ADD_ON_CODE,
  91. quantity: this.recurlySubscription.addOns[0].quantity + this.adding,
  92. },
  93. ],
  94. subscription: {
  95. planName: 'test plan',
  96. },
  97. }
  98. this.applySubscriptionChange = {}
  99. this.RecurlyClient = {
  100. promises: {
  101. getSubscription: sinon.stub().resolves(this.recurlySubscription),
  102. getPaymentMethod: sinon.stub().resolves(this.paymentMethod),
  103. previewSubscriptionChange: sinon
  104. .stub()
  105. .resolves(this.previewSubscriptionChange),
  106. applySubscriptionChangeRequest: sinon
  107. .stub()
  108. .resolves(this.applySubscriptionChange),
  109. },
  110. }
  111. this.PlansLocator = {
  112. findLocalPlanInSettings: sinon.stub().returns(this.localPlanInSettings),
  113. }
  114. this.SubscriptionHandler = {
  115. promises: {
  116. syncSubscription: sinon.stub().resolves(),
  117. },
  118. }
  119. this.GroupPlansData = {
  120. enterprise: {
  121. collaborator: {
  122. USD: {
  123. 5: {
  124. price_in_cents: 10000,
  125. additional_license_legacy_price_in_cents: 5000,
  126. },
  127. },
  128. },
  129. },
  130. }
  131. this.Handler = SandboxedModule.require(modulePath, {
  132. requires: {
  133. './SubscriptionUpdater': this.SubscriptionUpdater,
  134. './SubscriptionLocator': this.SubscriptionLocator,
  135. './SubscriptionController': this.SubscriptionController,
  136. './SubscriptionHandler': this.SubscriptionHandler,
  137. '../../models/Subscription': {
  138. Subscription: this.Subscription,
  139. },
  140. './RecurlyClient': this.RecurlyClient,
  141. './PlansLocator': this.PlansLocator,
  142. './RecurlyEntities': this.RecurlyEntities,
  143. '../Authentication/SessionManager': this.SessionManager,
  144. './GroupPlansData': this.GroupPlansData,
  145. },
  146. })
  147. })
  148. describe('removeUserFromGroup', function () {
  149. it('should call the subscription updater to remove the user', async function () {
  150. await this.Handler.promises.removeUserFromGroup(
  151. this.adminUser_id,
  152. this.user._id
  153. )
  154. this.SubscriptionUpdater.promises.removeUserFromGroup
  155. .calledWith(this.adminUser_id, this.user._id)
  156. .should.equal(true)
  157. })
  158. })
  159. describe('replaceUserReferencesInGroups', function () {
  160. beforeEach(async function () {
  161. this.oldId = 'ba5eba11'
  162. this.newId = '5ca1ab1e'
  163. await this.Handler.promises.replaceUserReferencesInGroups(
  164. this.oldId,
  165. this.newId
  166. )
  167. })
  168. it('replaces the admin_id', function () {
  169. this.Subscription.updateOne
  170. .calledWith({ admin_id: this.oldId }, { admin_id: this.newId })
  171. .should.equal(true)
  172. })
  173. it('replaces the manager_ids', function () {
  174. this.Subscription.updateMany
  175. .calledWith(
  176. { manager_ids: 'ba5eba11' },
  177. { $addToSet: { manager_ids: '5ca1ab1e' } }
  178. )
  179. .should.equal(true)
  180. this.Subscription.updateMany
  181. .calledWith(
  182. { manager_ids: 'ba5eba11' },
  183. { $pull: { manager_ids: 'ba5eba11' } }
  184. )
  185. .should.equal(true)
  186. })
  187. it('replaces the member ids', function () {
  188. this.Subscription.updateMany
  189. .calledWith(
  190. { member_ids: this.oldId },
  191. { $addToSet: { member_ids: this.newId } }
  192. )
  193. .should.equal(true)
  194. this.Subscription.updateMany
  195. .calledWith(
  196. { member_ids: this.oldId },
  197. { $pull: { member_ids: this.oldId } }
  198. )
  199. .should.equal(true)
  200. })
  201. })
  202. describe('isUserPartOfGroup', function () {
  203. beforeEach(function () {
  204. this.subscription_id = '123ed13123'
  205. })
  206. it('should return true when user is part of subscription', async function () {
  207. this.SubscriptionLocator.promises.getSubscriptionByMemberIdAndId.resolves(
  208. {
  209. _id: this.subscription_id,
  210. }
  211. )
  212. const partOfGroup = await this.Handler.promises.isUserPartOfGroup(
  213. this.user_id,
  214. this.subscription_id
  215. )
  216. partOfGroup.should.equal(true)
  217. })
  218. it('should return false when no subscription is found', async function () {
  219. this.SubscriptionLocator.promises.getSubscriptionByMemberIdAndId.resolves(
  220. null
  221. )
  222. const partOfGroup = await this.Handler.promises.isUserPartOfGroup(
  223. this.user_id,
  224. this.subscription_id
  225. )
  226. partOfGroup.should.equal(false)
  227. })
  228. })
  229. describe('getTotalConfirmedUsersInGroup', function () {
  230. describe('for existing subscriptions', function () {
  231. beforeEach(function () {
  232. this.subscription.member_ids = ['12321', '3121321']
  233. })
  234. it('should call the subscription locator and return 2 users', async function () {
  235. const count = await this.Handler.promises.getTotalConfirmedUsersInGroup(
  236. this.subscription_id
  237. )
  238. this.SubscriptionLocator.promises.getSubscription
  239. .calledWith(this.subscription_id)
  240. .should.equal(true)
  241. count.should.equal(2)
  242. })
  243. })
  244. describe('for nonexistent subscriptions', function () {
  245. it('should return undefined', async function () {
  246. const count =
  247. await this.Handler.promises.getTotalConfirmedUsersInGroup('fake-id')
  248. expect(count).not.to.exist
  249. })
  250. })
  251. })
  252. describe('getUsersGroupSubscriptionDetails', function () {
  253. beforeEach(function () {
  254. this.req = new MockRequest()
  255. this.PlansLocator.findLocalPlanInSettings = sinon.stub().returns({
  256. ...this.localPlanInSettings,
  257. canUseFlexibleLicensing: true,
  258. })
  259. })
  260. it('should throw if the subscription is not a group plan', async function () {
  261. this.SubscriptionLocator.promises.getUsersSubscription = sinon
  262. .stub()
  263. .resolves({ groupPlan: false })
  264. await expect(
  265. this.Handler.promises.getUsersGroupSubscriptionDetails(
  266. this.adminUser_id
  267. )
  268. ).to.be.rejectedWith('User subscription is not a group plan')
  269. })
  270. it('should return users group subscription details', async function () {
  271. const data = await this.Handler.promises.getUsersGroupSubscriptionDetails(
  272. this.adminUser_id
  273. )
  274. expect(data).to.deep.equal({
  275. userId: this.adminUser_id,
  276. subscription: {
  277. groupPlan: true,
  278. recurlyStatus: {
  279. state: 'active',
  280. },
  281. },
  282. plan: {
  283. membersLimit: 5,
  284. membersLimitAddOn: this.RecurlyEntities.MEMBERS_LIMIT_ADD_ON_CODE,
  285. canUseFlexibleLicensing: true,
  286. },
  287. recurlySubscription: this.recurlySubscription,
  288. })
  289. })
  290. })
  291. describe('add seats subscription change', function () {
  292. beforeEach(function () {
  293. this.req = new MockRequest()
  294. Object.assign(this.req.body, { adding: this.adding })
  295. this.PlansLocator.findLocalPlanInSettings = sinon.stub().returns({
  296. ...this.localPlanInSettings,
  297. canUseFlexibleLicensing: true,
  298. })
  299. })
  300. describe('has "additional-license" add-on', function () {
  301. beforeEach(function () {
  302. this.recurlySubscription.addOns = [
  303. {
  304. code: this.RecurlyEntities.MEMBERS_LIMIT_ADD_ON_CODE,
  305. quantity: 6,
  306. },
  307. ]
  308. this.prevQuantity = this.recurlySubscription.addOns[0].quantity
  309. this.previewSubscriptionChange.nextAddOns = [
  310. {
  311. code: this.RecurlyEntities.MEMBERS_LIMIT_ADD_ON_CODE,
  312. quantity: this.prevQuantity + this.adding,
  313. },
  314. ]
  315. })
  316. afterEach(function () {
  317. sinon.assert.notCalled(
  318. this.recurlySubscription.getRequestForAddOnPurchase
  319. )
  320. this.recurlySubscription.getRequestForAddOnUpdate
  321. .calledWith(
  322. this.RecurlyEntities.MEMBERS_LIMIT_ADD_ON_CODE,
  323. this.recurlySubscription.addOns[0].quantity + this.adding
  324. )
  325. .should.equal(true)
  326. })
  327. describe('previewAddSeatsSubscriptionChange', function () {
  328. it('should return the subscription change preview', async function () {
  329. const preview =
  330. await this.Handler.promises.previewAddSeatsSubscriptionChange(
  331. this.adminUser_id,
  332. this.adding
  333. )
  334. this.RecurlyClient.promises.getPaymentMethod
  335. .calledWith(this.adminUser_id)
  336. .should.equal(true)
  337. this.RecurlyClient.promises.previewSubscriptionChange
  338. .calledWith(this.changeRequest)
  339. .should.equal(true)
  340. this.SubscriptionController.makeChangePreview
  341. .calledWith(
  342. {
  343. type: 'add-on-update',
  344. addOn: {
  345. code: this.RecurlyEntities.MEMBERS_LIMIT_ADD_ON_CODE,
  346. quantity:
  347. this.previewSubscriptionChange.nextAddOns[0].quantity,
  348. prevQuantity: this.prevQuantity,
  349. },
  350. },
  351. this.previewSubscriptionChange,
  352. this.paymentMethod
  353. )
  354. .should.equal(true)
  355. preview.should.equal(this.changePreview)
  356. })
  357. })
  358. describe('createAddSeatsSubscriptionChange', function () {
  359. it('should change the subscription', async function () {
  360. const result =
  361. await this.Handler.promises.createAddSeatsSubscriptionChange(
  362. this.adminUser_id,
  363. this.adding
  364. )
  365. this.RecurlyClient.promises.applySubscriptionChangeRequest
  366. .calledWith(this.changeRequest)
  367. .should.equal(true)
  368. this.SubscriptionHandler.promises.syncSubscription
  369. .calledWith(
  370. { uuid: this.recurlySubscription.id },
  371. this.adminUser_id
  372. )
  373. .should.equal(true)
  374. expect(result).to.deep.equal({
  375. adding: this.req.body.adding,
  376. })
  377. })
  378. })
  379. })
  380. describe('has no "additional-license" add-on', function () {
  381. beforeEach(function () {
  382. this.recurlySubscription.addOns = []
  383. this.prevQuantity = this.recurlySubscription.addOns[0]?.quantity ?? 0
  384. this.previewSubscriptionChange.nextAddOns = [
  385. {
  386. code: this.RecurlyEntities.MEMBERS_LIMIT_ADD_ON_CODE,
  387. quantity: this.prevQuantity + this.adding,
  388. },
  389. ]
  390. this.PlansLocator.findLocalPlanInSettings = sinon.stub().returns({
  391. ...this.localPlanInSettings,
  392. planCode: 'group_collaborator_5_enterprise',
  393. canUseFlexibleLicensing: true,
  394. })
  395. })
  396. afterEach(function () {
  397. sinon.assert.notCalled(
  398. this.recurlySubscription.getRequestForAddOnUpdate
  399. )
  400. })
  401. describe('previewAddSeatsSubscriptionChange', function () {
  402. let preview
  403. afterEach(function () {
  404. this.RecurlyClient.promises.getPaymentMethod
  405. .calledWith(this.adminUser_id)
  406. .should.equal(true)
  407. this.RecurlyClient.promises.previewSubscriptionChange
  408. .calledWith(this.changeRequest)
  409. .should.equal(true)
  410. this.SubscriptionController.makeChangePreview
  411. .calledWith(
  412. {
  413. type: 'add-on-update',
  414. addOn: {
  415. code: this.RecurlyEntities.MEMBERS_LIMIT_ADD_ON_CODE,
  416. quantity:
  417. this.previewSubscriptionChange.nextAddOns[0].quantity,
  418. prevQuantity: this.prevQuantity,
  419. },
  420. },
  421. this.previewSubscriptionChange,
  422. this.paymentMethod
  423. )
  424. .should.equal(true)
  425. preview.should.equal(this.changePreview)
  426. })
  427. it('should return the subscription change preview with legacy add-on price', async function () {
  428. this.recurlySubscription.planPrice =
  429. this.GroupPlansData.enterprise.collaborator.USD[5].price_in_cents /
  430. 100 -
  431. 1
  432. preview =
  433. await this.Handler.promises.previewAddSeatsSubscriptionChange(
  434. this.adminUser_id,
  435. this.adding
  436. )
  437. this.recurlySubscription.getRequestForAddOnPurchase
  438. .calledWithExactly(
  439. this.RecurlyEntities.MEMBERS_LIMIT_ADD_ON_CODE,
  440. this.adding,
  441. this.GroupPlansData.enterprise.collaborator.USD[5]
  442. .additional_license_legacy_price_in_cents / 100
  443. )
  444. .should.equal(true)
  445. })
  446. it('should return the subscription change preview with non-legacy add-on price', async function () {
  447. this.recurlySubscription.planPrice =
  448. this.GroupPlansData.enterprise.collaborator.USD[5].price_in_cents /
  449. 100
  450. preview =
  451. await this.Handler.promises.previewAddSeatsSubscriptionChange(
  452. this.adminUser_id,
  453. this.adding
  454. )
  455. this.recurlySubscription.getRequestForAddOnPurchase
  456. .calledWithExactly(
  457. this.RecurlyEntities.MEMBERS_LIMIT_ADD_ON_CODE,
  458. this.adding,
  459. undefined
  460. )
  461. .should.equal(true)
  462. })
  463. })
  464. })
  465. })
  466. describe('ensureFlexibleLicensingEnabled', function () {
  467. it('should throw if the subscription can not use flexible licensing', async function () {
  468. await expect(
  469. this.Handler.promises.ensureFlexibleLicensingEnabled({
  470. canUseFlexibleLicensing: false,
  471. })
  472. ).to.be.rejectedWith('The group plan does not support flexible licensing')
  473. })
  474. it('should not throw if the subscription can use flexible licensing', async function () {
  475. await expect(
  476. this.Handler.promises.ensureFlexibleLicensingEnabled({
  477. canUseFlexibleLicensing: true,
  478. })
  479. ).to.not.be.rejected
  480. })
  481. })
  482. describe('ensureSubscriptionIsActive', function () {
  483. it('should throw if the subscription is not active', async function () {
  484. await expect(
  485. this.Handler.promises.ensureSubscriptionIsActive({})
  486. ).to.be.rejectedWith('The subscription is not active')
  487. })
  488. it('should not throw if the subscription is active', async function () {
  489. await expect(
  490. this.Handler.promises.ensureSubscriptionIsActive({
  491. recurlyStatus: { state: 'active' },
  492. })
  493. ).to.not.be.rejected
  494. })
  495. })
  496. describe('ensureSubscriptionCollectionMethodIsNotManual', function () {
  497. it('should throw if the subscription is manually collected', async function () {
  498. await expect(
  499. this.Handler.promises.ensureSubscriptionCollectionMethodIsNotManual({
  500. get isCollectionMethodManual() {
  501. return true
  502. },
  503. })
  504. ).to.be.rejectedWith('This subscription is being collected manually')
  505. })
  506. it('should not throw if the subscription is automatically collected', async function () {
  507. await expect(
  508. this.Handler.promises.ensureSubscriptionCollectionMethodIsNotManual({
  509. get isCollectionMethodManual() {
  510. return false
  511. },
  512. })
  513. ).to.not.be.rejected
  514. })
  515. })
  516. describe('upgradeGroupPlan', function () {
  517. it('should upgrade the subscription for flexible licensing group plans', async function () {
  518. this.SubscriptionLocator.promises.getUsersSubscription = sinon
  519. .stub()
  520. .resolves({
  521. groupPlan: true,
  522. recurlyStatus: {
  523. state: 'active',
  524. },
  525. planCode: 'group_collaborator',
  526. })
  527. await this.Handler.promises.upgradeGroupPlan(this.user_id)
  528. this.recurlySubscription.getRequestForGroupPlanUpgrade
  529. .calledWith('group_professional')
  530. .should.equal(true)
  531. this.RecurlyClient.promises.applySubscriptionChangeRequest
  532. .calledWith(this.changeRequest)
  533. .should.equal(true)
  534. this.SubscriptionHandler.promises.syncSubscription
  535. .calledWith({ uuid: this.changeRequest.subscription.id }, this.user_id)
  536. .should.equal(true)
  537. })
  538. it('should upgrade the subscription for legacy group plans', async function () {
  539. this.SubscriptionLocator.promises.getUsersSubscription = sinon
  540. .stub()
  541. .resolves({
  542. groupPlan: true,
  543. recurlyStatus: {
  544. state: 'active',
  545. },
  546. planCode: 'group_collaborator_10_educational',
  547. })
  548. await this.Handler.promises.upgradeGroupPlan(this.user_id)
  549. this.recurlySubscription.getRequestForGroupPlanUpgrade
  550. .calledWith('group_professional_10_educational')
  551. .should.equal(true)
  552. this.RecurlyClient.promises.applySubscriptionChangeRequest
  553. .calledWith(this.changeRequest)
  554. .should.equal(true)
  555. this.SubscriptionHandler.promises.syncSubscription
  556. .calledWith({ uuid: this.changeRequest.subscription.id }, this.user_id)
  557. .should.equal(true)
  558. })
  559. it('should fail the upgrade if is professional already', async function () {
  560. this.SubscriptionLocator.promises.getUsersSubscription = sinon
  561. .stub()
  562. .resolves({
  563. groupPlan: true,
  564. recurlyStatus: {
  565. state: 'active',
  566. },
  567. planCode: 'group_professional',
  568. })
  569. await expect(
  570. this.Handler.promises.upgradeGroupPlan(this.user_id)
  571. ).to.be.rejectedWith('Not eligible for group plan upgrade')
  572. })
  573. it('should fail the upgrade if not group plan', async function () {
  574. this.SubscriptionLocator.promises.getUsersSubscription = sinon
  575. .stub()
  576. .resolves({
  577. groupPlan: false,
  578. recurlyStatus: {
  579. state: 'active',
  580. },
  581. planCode: 'test_plan_code',
  582. })
  583. await expect(
  584. this.Handler.promises.upgradeGroupPlan(this.user_id)
  585. ).to.be.rejectedWith('Not eligible for group plan upgrade')
  586. })
  587. })
  588. describe('getGroupPlanUpgradePreview', function () {
  589. it('should generate preview for subscription upgrade', async function () {
  590. this.SubscriptionLocator.promises.getUsersSubscription = sinon
  591. .stub()
  592. .resolves({
  593. groupPlan: true,
  594. recurlyStatus: {
  595. state: 'active',
  596. },
  597. planCode: 'group_collaborator',
  598. })
  599. const result = await this.Handler.promises.getGroupPlanUpgradePreview(
  600. this.user_id
  601. )
  602. this.RecurlyClient.promises.previewSubscriptionChange
  603. .calledWith(this.changeRequest)
  604. .should.equal(true)
  605. result.should.equal(this.changePreview)
  606. })
  607. })
  608. })