SubscriptionGroupHandlerTests.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  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. educational: {
  131. collaborator: {
  132. USD: {
  133. 5: {
  134. price_in_cents: 10000,
  135. additional_license_legacy_price_in_cents: 5000,
  136. },
  137. },
  138. },
  139. },
  140. }
  141. this.Handler = SandboxedModule.require(modulePath, {
  142. requires: {
  143. './SubscriptionUpdater': this.SubscriptionUpdater,
  144. './SubscriptionLocator': this.SubscriptionLocator,
  145. './SubscriptionController': this.SubscriptionController,
  146. './SubscriptionHandler': this.SubscriptionHandler,
  147. '../../models/Subscription': {
  148. Subscription: this.Subscription,
  149. },
  150. './RecurlyClient': this.RecurlyClient,
  151. './PlansLocator': this.PlansLocator,
  152. './RecurlyEntities': this.RecurlyEntities,
  153. '../Authentication/SessionManager': this.SessionManager,
  154. './GroupPlansData': this.GroupPlansData,
  155. },
  156. })
  157. })
  158. describe('removeUserFromGroup', function () {
  159. it('should call the subscription updater to remove the user', async function () {
  160. await this.Handler.promises.removeUserFromGroup(
  161. this.adminUser_id,
  162. this.user._id
  163. )
  164. this.SubscriptionUpdater.promises.removeUserFromGroup
  165. .calledWith(this.adminUser_id, this.user._id)
  166. .should.equal(true)
  167. })
  168. })
  169. describe('replaceUserReferencesInGroups', function () {
  170. beforeEach(async function () {
  171. this.oldId = 'ba5eba11'
  172. this.newId = '5ca1ab1e'
  173. await this.Handler.promises.replaceUserReferencesInGroups(
  174. this.oldId,
  175. this.newId
  176. )
  177. })
  178. it('replaces the admin_id', function () {
  179. this.Subscription.updateOne
  180. .calledWith({ admin_id: this.oldId }, { admin_id: this.newId })
  181. .should.equal(true)
  182. })
  183. it('replaces the manager_ids', function () {
  184. this.Subscription.updateMany
  185. .calledWith(
  186. { manager_ids: 'ba5eba11' },
  187. { $addToSet: { manager_ids: '5ca1ab1e' } }
  188. )
  189. .should.equal(true)
  190. this.Subscription.updateMany
  191. .calledWith(
  192. { manager_ids: 'ba5eba11' },
  193. { $pull: { manager_ids: 'ba5eba11' } }
  194. )
  195. .should.equal(true)
  196. })
  197. it('replaces the member ids', function () {
  198. this.Subscription.updateMany
  199. .calledWith(
  200. { member_ids: this.oldId },
  201. { $addToSet: { member_ids: this.newId } }
  202. )
  203. .should.equal(true)
  204. this.Subscription.updateMany
  205. .calledWith(
  206. { member_ids: this.oldId },
  207. { $pull: { member_ids: this.oldId } }
  208. )
  209. .should.equal(true)
  210. })
  211. })
  212. describe('isUserPartOfGroup', function () {
  213. beforeEach(function () {
  214. this.subscription_id = '123ed13123'
  215. })
  216. it('should return true when user is part of subscription', async function () {
  217. this.SubscriptionLocator.promises.getSubscriptionByMemberIdAndId.resolves(
  218. {
  219. _id: this.subscription_id,
  220. }
  221. )
  222. const partOfGroup = await this.Handler.promises.isUserPartOfGroup(
  223. this.user_id,
  224. this.subscription_id
  225. )
  226. partOfGroup.should.equal(true)
  227. })
  228. it('should return false when no subscription is found', async function () {
  229. this.SubscriptionLocator.promises.getSubscriptionByMemberIdAndId.resolves(
  230. null
  231. )
  232. const partOfGroup = await this.Handler.promises.isUserPartOfGroup(
  233. this.user_id,
  234. this.subscription_id
  235. )
  236. partOfGroup.should.equal(false)
  237. })
  238. })
  239. describe('getTotalConfirmedUsersInGroup', function () {
  240. describe('for existing subscriptions', function () {
  241. beforeEach(function () {
  242. this.subscription.member_ids = ['12321', '3121321']
  243. })
  244. it('should call the subscription locator and return 2 users', async function () {
  245. const count = await this.Handler.promises.getTotalConfirmedUsersInGroup(
  246. this.subscription_id
  247. )
  248. this.SubscriptionLocator.promises.getSubscription
  249. .calledWith(this.subscription_id)
  250. .should.equal(true)
  251. count.should.equal(2)
  252. })
  253. })
  254. describe('for nonexistent subscriptions', function () {
  255. it('should return undefined', async function () {
  256. const count =
  257. await this.Handler.promises.getTotalConfirmedUsersInGroup('fake-id')
  258. expect(count).not.to.exist
  259. })
  260. })
  261. })
  262. describe('getUsersGroupSubscriptionDetails', function () {
  263. beforeEach(function () {
  264. this.req = new MockRequest()
  265. this.PlansLocator.findLocalPlanInSettings = sinon.stub().returns({
  266. ...this.localPlanInSettings,
  267. canUseFlexibleLicensing: true,
  268. })
  269. })
  270. it('should throw if the subscription is not a group plan', async function () {
  271. this.SubscriptionLocator.promises.getUsersSubscription = sinon
  272. .stub()
  273. .resolves({ groupPlan: false })
  274. await expect(
  275. this.Handler.promises.getUsersGroupSubscriptionDetails(
  276. this.adminUser_id
  277. )
  278. ).to.be.rejectedWith('User subscription is not a group plan')
  279. })
  280. it('should return users group subscription details', async function () {
  281. const data = await this.Handler.promises.getUsersGroupSubscriptionDetails(
  282. this.adminUser_id
  283. )
  284. expect(data).to.deep.equal({
  285. userId: this.adminUser_id,
  286. subscription: {
  287. groupPlan: true,
  288. recurlyStatus: {
  289. state: 'active',
  290. },
  291. },
  292. plan: {
  293. membersLimit: 5,
  294. membersLimitAddOn: this.RecurlyEntities.MEMBERS_LIMIT_ADD_ON_CODE,
  295. canUseFlexibleLicensing: true,
  296. },
  297. recurlySubscription: this.recurlySubscription,
  298. })
  299. })
  300. })
  301. describe('add seats subscription change', function () {
  302. beforeEach(function () {
  303. this.req = new MockRequest()
  304. Object.assign(this.req.body, { adding: this.adding })
  305. this.PlansLocator.findLocalPlanInSettings = sinon.stub().returns({
  306. ...this.localPlanInSettings,
  307. canUseFlexibleLicensing: true,
  308. })
  309. })
  310. describe('has "additional-license" add-on', function () {
  311. beforeEach(function () {
  312. this.recurlySubscription.addOns = [
  313. {
  314. code: this.RecurlyEntities.MEMBERS_LIMIT_ADD_ON_CODE,
  315. quantity: 6,
  316. },
  317. ]
  318. this.prevQuantity = this.recurlySubscription.addOns[0].quantity
  319. this.previewSubscriptionChange.nextAddOns = [
  320. {
  321. code: this.RecurlyEntities.MEMBERS_LIMIT_ADD_ON_CODE,
  322. quantity: this.prevQuantity + this.adding,
  323. },
  324. ]
  325. })
  326. afterEach(function () {
  327. sinon.assert.notCalled(
  328. this.recurlySubscription.getRequestForAddOnPurchase
  329. )
  330. this.recurlySubscription.getRequestForAddOnUpdate
  331. .calledWith(
  332. this.RecurlyEntities.MEMBERS_LIMIT_ADD_ON_CODE,
  333. this.recurlySubscription.addOns[0].quantity + this.adding
  334. )
  335. .should.equal(true)
  336. })
  337. describe('previewAddSeatsSubscriptionChange', function () {
  338. it('should return the subscription change preview', async function () {
  339. const preview =
  340. await this.Handler.promises.previewAddSeatsSubscriptionChange(
  341. this.adminUser_id,
  342. this.adding
  343. )
  344. this.RecurlyClient.promises.getPaymentMethod
  345. .calledWith(this.adminUser_id)
  346. .should.equal(true)
  347. this.RecurlyClient.promises.previewSubscriptionChange
  348. .calledWith(this.changeRequest)
  349. .should.equal(true)
  350. this.SubscriptionController.makeChangePreview
  351. .calledWith(
  352. {
  353. type: 'add-on-update',
  354. addOn: {
  355. code: this.RecurlyEntities.MEMBERS_LIMIT_ADD_ON_CODE,
  356. quantity:
  357. this.previewSubscriptionChange.nextAddOns[0].quantity,
  358. prevQuantity: this.prevQuantity,
  359. },
  360. },
  361. this.previewSubscriptionChange,
  362. this.paymentMethod
  363. )
  364. .should.equal(true)
  365. preview.should.equal(this.changePreview)
  366. })
  367. })
  368. describe('createAddSeatsSubscriptionChange', function () {
  369. it('should change the subscription', async function () {
  370. const result =
  371. await this.Handler.promises.createAddSeatsSubscriptionChange(
  372. this.adminUser_id,
  373. this.adding
  374. )
  375. this.RecurlyClient.promises.applySubscriptionChangeRequest
  376. .calledWith(this.changeRequest)
  377. .should.equal(true)
  378. this.SubscriptionHandler.promises.syncSubscription
  379. .calledWith(
  380. { uuid: this.recurlySubscription.id },
  381. this.adminUser_id
  382. )
  383. .should.equal(true)
  384. expect(result).to.deep.equal({
  385. adding: this.req.body.adding,
  386. })
  387. })
  388. })
  389. })
  390. describe('has no "additional-license" add-on', function () {
  391. beforeEach(function () {
  392. this.recurlySubscription.addOns = []
  393. this.prevQuantity = this.recurlySubscription.addOns[0]?.quantity ?? 0
  394. this.previewSubscriptionChange.nextAddOns = [
  395. {
  396. code: this.RecurlyEntities.MEMBERS_LIMIT_ADD_ON_CODE,
  397. quantity: this.prevQuantity + this.adding,
  398. },
  399. ]
  400. this.PlansLocator.findLocalPlanInSettings = sinon.stub().returns({
  401. ...this.localPlanInSettings,
  402. planCode: 'group_collaborator_5_enterprise',
  403. canUseFlexibleLicensing: true,
  404. })
  405. })
  406. afterEach(function () {
  407. sinon.assert.notCalled(
  408. this.recurlySubscription.getRequestForAddOnUpdate
  409. )
  410. })
  411. describe('previewAddSeatsSubscriptionChange', function () {
  412. let preview
  413. afterEach(function () {
  414. this.RecurlyClient.promises.getPaymentMethod
  415. .calledWith(this.adminUser_id)
  416. .should.equal(true)
  417. this.RecurlyClient.promises.previewSubscriptionChange
  418. .calledWith(this.changeRequest)
  419. .should.equal(true)
  420. this.SubscriptionController.makeChangePreview
  421. .calledWith(
  422. {
  423. type: 'add-on-update',
  424. addOn: {
  425. code: this.RecurlyEntities.MEMBERS_LIMIT_ADD_ON_CODE,
  426. quantity:
  427. this.previewSubscriptionChange.nextAddOns[0].quantity,
  428. prevQuantity: this.prevQuantity,
  429. },
  430. },
  431. this.previewSubscriptionChange,
  432. this.paymentMethod
  433. )
  434. .should.equal(true)
  435. preview.should.equal(this.changePreview)
  436. })
  437. it('should return the subscription change preview with legacy add-on price', async function () {
  438. this.recurlySubscription.planPrice =
  439. this.GroupPlansData.enterprise.collaborator.USD[5].price_in_cents /
  440. 100 -
  441. 1
  442. preview =
  443. await this.Handler.promises.previewAddSeatsSubscriptionChange(
  444. this.adminUser_id,
  445. this.adding
  446. )
  447. this.recurlySubscription.getRequestForAddOnPurchase
  448. .calledWithExactly(
  449. this.RecurlyEntities.MEMBERS_LIMIT_ADD_ON_CODE,
  450. this.adding,
  451. this.GroupPlansData.enterprise.collaborator.USD[5]
  452. .additional_license_legacy_price_in_cents / 100
  453. )
  454. .should.equal(true)
  455. })
  456. it('should return the subscription change preview with non-legacy add-on price', async function () {
  457. this.recurlySubscription.planPrice =
  458. this.GroupPlansData.enterprise.collaborator.USD[5].price_in_cents /
  459. 100
  460. preview =
  461. await this.Handler.promises.previewAddSeatsSubscriptionChange(
  462. this.adminUser_id,
  463. this.adding
  464. )
  465. this.recurlySubscription.getRequestForAddOnPurchase
  466. .calledWithExactly(
  467. this.RecurlyEntities.MEMBERS_LIMIT_ADD_ON_CODE,
  468. this.adding,
  469. undefined
  470. )
  471. .should.equal(true)
  472. })
  473. it('should return the subscription change preview with legacy add-on price for small educational group', async function () {
  474. this.PlansLocator.findLocalPlanInSettings = sinon.stub().returns({
  475. ...this.localPlanInSettings,
  476. planCode: 'group_collaborator_5_educational',
  477. canUseFlexibleLicensing: true,
  478. })
  479. this.recurlySubscription.planPrice =
  480. this.GroupPlansData.enterprise.collaborator.USD[5].price_in_cents /
  481. 100 +
  482. 1
  483. preview =
  484. await this.Handler.promises.previewAddSeatsSubscriptionChange(
  485. this.adminUser_id,
  486. this.adding
  487. )
  488. this.recurlySubscription.getRequestForAddOnPurchase
  489. .calledWithExactly(
  490. this.RecurlyEntities.MEMBERS_LIMIT_ADD_ON_CODE,
  491. this.adding,
  492. this.GroupPlansData.enterprise.collaborator.USD[5]
  493. .additional_license_legacy_price_in_cents / 100
  494. )
  495. .should.equal(true)
  496. })
  497. it('should return the subscription change preview with non-legacy add-on price for small educational group', async function () {
  498. this.PlansLocator.findLocalPlanInSettings = sinon.stub().returns({
  499. ...this.localPlanInSettings,
  500. planCode: 'group_collaborator_5_educational',
  501. canUseFlexibleLicensing: true,
  502. })
  503. this.recurlySubscription.planPrice =
  504. this.GroupPlansData.enterprise.collaborator.USD[5].price_in_cents /
  505. 100
  506. preview =
  507. await this.Handler.promises.previewAddSeatsSubscriptionChange(
  508. this.adminUser_id,
  509. this.adding
  510. )
  511. this.recurlySubscription.getRequestForAddOnPurchase
  512. .calledWithExactly(
  513. this.RecurlyEntities.MEMBERS_LIMIT_ADD_ON_CODE,
  514. this.adding,
  515. undefined
  516. )
  517. .should.equal(true)
  518. })
  519. })
  520. })
  521. })
  522. describe('ensureFlexibleLicensingEnabled', function () {
  523. it('should throw if the subscription can not use flexible licensing', async function () {
  524. await expect(
  525. this.Handler.promises.ensureFlexibleLicensingEnabled({
  526. canUseFlexibleLicensing: false,
  527. })
  528. ).to.be.rejectedWith('The group plan does not support flexible licensing')
  529. })
  530. it('should not throw if the subscription can use flexible licensing', async function () {
  531. await expect(
  532. this.Handler.promises.ensureFlexibleLicensingEnabled({
  533. canUseFlexibleLicensing: true,
  534. })
  535. ).to.not.be.rejected
  536. })
  537. })
  538. describe('ensureSubscriptionIsActive', function () {
  539. it('should throw if the subscription is not active', async function () {
  540. await expect(
  541. this.Handler.promises.ensureSubscriptionIsActive({})
  542. ).to.be.rejectedWith('The subscription is not active')
  543. })
  544. it('should not throw if the subscription is active', async function () {
  545. await expect(
  546. this.Handler.promises.ensureSubscriptionIsActive({
  547. recurlyStatus: { state: 'active' },
  548. })
  549. ).to.not.be.rejected
  550. })
  551. })
  552. describe('ensureSubscriptionCollectionMethodIsNotManual', function () {
  553. it('should throw if the subscription is manually collected', async function () {
  554. await expect(
  555. this.Handler.promises.ensureSubscriptionCollectionMethodIsNotManual({
  556. get isCollectionMethodManual() {
  557. return true
  558. },
  559. })
  560. ).to.be.rejectedWith('This subscription is being collected manually')
  561. })
  562. it('should not throw if the subscription is automatically collected', async function () {
  563. await expect(
  564. this.Handler.promises.ensureSubscriptionCollectionMethodIsNotManual({
  565. get isCollectionMethodManual() {
  566. return false
  567. },
  568. })
  569. ).to.not.be.rejected
  570. })
  571. })
  572. describe('ensureSubscriptionHasNoPendingChanges', function () {
  573. it('should throw if the subscription has pending change', async function () {
  574. await expect(
  575. this.Handler.promises.ensureSubscriptionHasNoPendingChanges({
  576. pendingChange: {},
  577. })
  578. ).to.be.rejectedWith('This subscription has a pending change')
  579. })
  580. it('should not throw if the subscription has no pending change', async function () {
  581. await expect(
  582. this.Handler.promises.ensureSubscriptionHasNoPendingChanges({})
  583. ).to.not.be.rejected
  584. })
  585. })
  586. describe('upgradeGroupPlan', function () {
  587. it('should upgrade the subscription for flexible licensing group plans', async function () {
  588. this.SubscriptionLocator.promises.getUsersSubscription = sinon
  589. .stub()
  590. .resolves({
  591. groupPlan: true,
  592. recurlyStatus: {
  593. state: 'active',
  594. },
  595. planCode: 'group_collaborator',
  596. })
  597. await this.Handler.promises.upgradeGroupPlan(this.user_id)
  598. this.recurlySubscription.getRequestForGroupPlanUpgrade
  599. .calledWith('group_professional')
  600. .should.equal(true)
  601. this.RecurlyClient.promises.applySubscriptionChangeRequest
  602. .calledWith(this.changeRequest)
  603. .should.equal(true)
  604. this.SubscriptionHandler.promises.syncSubscription
  605. .calledWith({ uuid: this.changeRequest.subscription.id }, this.user_id)
  606. .should.equal(true)
  607. })
  608. it('should upgrade the subscription for legacy group plans', async function () {
  609. this.SubscriptionLocator.promises.getUsersSubscription = sinon
  610. .stub()
  611. .resolves({
  612. groupPlan: true,
  613. recurlyStatus: {
  614. state: 'active',
  615. },
  616. planCode: 'group_collaborator_10_educational',
  617. })
  618. await this.Handler.promises.upgradeGroupPlan(this.user_id)
  619. this.recurlySubscription.getRequestForGroupPlanUpgrade
  620. .calledWith('group_professional_10_educational')
  621. .should.equal(true)
  622. this.RecurlyClient.promises.applySubscriptionChangeRequest
  623. .calledWith(this.changeRequest)
  624. .should.equal(true)
  625. this.SubscriptionHandler.promises.syncSubscription
  626. .calledWith({ uuid: this.changeRequest.subscription.id }, this.user_id)
  627. .should.equal(true)
  628. })
  629. it('should fail the upgrade if is professional already', async function () {
  630. this.SubscriptionLocator.promises.getUsersSubscription = sinon
  631. .stub()
  632. .resolves({
  633. groupPlan: true,
  634. recurlyStatus: {
  635. state: 'active',
  636. },
  637. planCode: 'group_professional',
  638. })
  639. await expect(
  640. this.Handler.promises.upgradeGroupPlan(this.user_id)
  641. ).to.be.rejectedWith('Not eligible for group plan upgrade')
  642. })
  643. it('should fail the upgrade if not group plan', async function () {
  644. this.SubscriptionLocator.promises.getUsersSubscription = sinon
  645. .stub()
  646. .resolves({
  647. groupPlan: false,
  648. recurlyStatus: {
  649. state: 'active',
  650. },
  651. planCode: 'test_plan_code',
  652. })
  653. await expect(
  654. this.Handler.promises.upgradeGroupPlan(this.user_id)
  655. ).to.be.rejectedWith('Not eligible for group plan upgrade')
  656. })
  657. })
  658. describe('getGroupPlanUpgradePreview', function () {
  659. it('should generate preview for subscription upgrade', async function () {
  660. this.SubscriptionLocator.promises.getUsersSubscription = sinon
  661. .stub()
  662. .resolves({
  663. groupPlan: true,
  664. recurlyStatus: {
  665. state: 'active',
  666. },
  667. planCode: 'group_collaborator',
  668. })
  669. const result = await this.Handler.promises.getGroupPlanUpgradePreview(
  670. this.user_id
  671. )
  672. this.RecurlyClient.promises.previewSubscriptionChange
  673. .calledWith(this.changeRequest)
  674. .should.equal(true)
  675. result.should.equal(this.changePreview)
  676. })
  677. })
  678. })