SubscriptionGroupHandlerTests.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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. createdAt: '2025-01-01T00:00:00Z',
  50. currency: 'USD',
  51. hasAddOn(code) {
  52. return this.addOns.some(addOn => addOn.code === code)
  53. },
  54. }
  55. this.SubscriptionLocator = {
  56. promises: {
  57. getUsersSubscription: sinon.stub().resolves({ groupPlan: true }),
  58. getSubscriptionByMemberIdAndId: sinon.stub(),
  59. getSubscription: sinon.stub().resolves(this.subscription),
  60. },
  61. }
  62. this.changePreview = {
  63. currency: 'USD',
  64. }
  65. this.SubscriptionController = {
  66. makeChangePreview: sinon.stub().resolves(this.changePreview),
  67. }
  68. this.SubscriptionUpdater = {
  69. promises: {
  70. removeUserFromGroup: sinon.stub().resolves(),
  71. getSubscription: sinon.stub().resolves(),
  72. },
  73. }
  74. this.Subscription = {
  75. updateOne: sinon.stub().returns({ exec: sinon.stub().resolves }),
  76. updateMany: sinon.stub().returns({ exec: sinon.stub().resolves }),
  77. findOne: sinon.stub().returns({ exec: sinon.stub().resolves }),
  78. }
  79. this.SessionManager = {
  80. getLoggedInUserId: sinon.stub().returns(this.user._id),
  81. }
  82. this.previewSubscriptionChange = {
  83. nextAddOns: [
  84. {
  85. code: this.RecurlyEntities.MEMBERS_LIMIT_ADD_ON_CODE,
  86. quantity: this.recurlySubscription.addOns[0].quantity + this.adding,
  87. },
  88. ],
  89. subscription: {
  90. planName: 'test plan',
  91. },
  92. }
  93. this.applySubscriptionChange = {}
  94. this.RecurlyClient = {
  95. promises: {
  96. getSubscription: sinon.stub().resolves(this.recurlySubscription),
  97. getPaymentMethod: sinon.stub().resolves(this.paymentMethod),
  98. previewSubscriptionChange: sinon
  99. .stub()
  100. .resolves(this.previewSubscriptionChange),
  101. applySubscriptionChangeRequest: sinon
  102. .stub()
  103. .resolves(this.applySubscriptionChange),
  104. },
  105. }
  106. this.PlansLocator = {
  107. findLocalPlanInSettings: sinon.stub().returns(this.localPlanInSettings),
  108. }
  109. this.SubscriptionHandler = {
  110. promises: {
  111. syncSubscription: sinon.stub().resolves(),
  112. },
  113. }
  114. this.GroupPlansData = {
  115. enterprise: {
  116. collaborator: {
  117. USD: {
  118. 5: {
  119. price_in_cents: 10000,
  120. additional_license_legacy_price_in_cents: 5000,
  121. },
  122. },
  123. },
  124. },
  125. }
  126. this.Handler = SandboxedModule.require(modulePath, {
  127. requires: {
  128. './SubscriptionUpdater': this.SubscriptionUpdater,
  129. './SubscriptionLocator': this.SubscriptionLocator,
  130. './SubscriptionController': this.SubscriptionController,
  131. './SubscriptionHandler': this.SubscriptionHandler,
  132. '../../models/Subscription': {
  133. Subscription: this.Subscription,
  134. },
  135. './RecurlyClient': this.RecurlyClient,
  136. './PlansLocator': this.PlansLocator,
  137. './RecurlyEntities': this.RecurlyEntities,
  138. '../Authentication/SessionManager': this.SessionManager,
  139. './GroupPlansData': this.GroupPlansData,
  140. },
  141. })
  142. })
  143. describe('removeUserFromGroup', function () {
  144. it('should call the subscription updater to remove the user', async function () {
  145. await this.Handler.promises.removeUserFromGroup(
  146. this.adminUser_id,
  147. this.user._id
  148. )
  149. this.SubscriptionUpdater.promises.removeUserFromGroup
  150. .calledWith(this.adminUser_id, this.user._id)
  151. .should.equal(true)
  152. })
  153. })
  154. describe('replaceUserReferencesInGroups', function () {
  155. beforeEach(async function () {
  156. this.oldId = 'ba5eba11'
  157. this.newId = '5ca1ab1e'
  158. await this.Handler.promises.replaceUserReferencesInGroups(
  159. this.oldId,
  160. this.newId
  161. )
  162. })
  163. it('replaces the admin_id', function () {
  164. this.Subscription.updateOne
  165. .calledWith({ admin_id: this.oldId }, { admin_id: this.newId })
  166. .should.equal(true)
  167. })
  168. it('replaces the manager_ids', function () {
  169. this.Subscription.updateMany
  170. .calledWith(
  171. { manager_ids: 'ba5eba11' },
  172. { $addToSet: { manager_ids: '5ca1ab1e' } }
  173. )
  174. .should.equal(true)
  175. this.Subscription.updateMany
  176. .calledWith(
  177. { manager_ids: 'ba5eba11' },
  178. { $pull: { manager_ids: 'ba5eba11' } }
  179. )
  180. .should.equal(true)
  181. })
  182. it('replaces the member ids', function () {
  183. this.Subscription.updateMany
  184. .calledWith(
  185. { member_ids: this.oldId },
  186. { $addToSet: { member_ids: this.newId } }
  187. )
  188. .should.equal(true)
  189. this.Subscription.updateMany
  190. .calledWith(
  191. { member_ids: this.oldId },
  192. { $pull: { member_ids: this.oldId } }
  193. )
  194. .should.equal(true)
  195. })
  196. })
  197. describe('isUserPartOfGroup', function () {
  198. beforeEach(function () {
  199. this.subscription_id = '123ed13123'
  200. })
  201. it('should return true when user is part of subscription', async function () {
  202. this.SubscriptionLocator.promises.getSubscriptionByMemberIdAndId.resolves(
  203. {
  204. _id: this.subscription_id,
  205. }
  206. )
  207. const partOfGroup = await this.Handler.promises.isUserPartOfGroup(
  208. this.user_id,
  209. this.subscription_id
  210. )
  211. partOfGroup.should.equal(true)
  212. })
  213. it('should return false when no subscription is found', async function () {
  214. this.SubscriptionLocator.promises.getSubscriptionByMemberIdAndId.resolves(
  215. null
  216. )
  217. const partOfGroup = await this.Handler.promises.isUserPartOfGroup(
  218. this.user_id,
  219. this.subscription_id
  220. )
  221. partOfGroup.should.equal(false)
  222. })
  223. })
  224. describe('getTotalConfirmedUsersInGroup', function () {
  225. describe('for existing subscriptions', function () {
  226. beforeEach(function () {
  227. this.subscription.member_ids = ['12321', '3121321']
  228. })
  229. it('should call the subscription locator and return 2 users', async function () {
  230. const count = await this.Handler.promises.getTotalConfirmedUsersInGroup(
  231. this.subscription_id
  232. )
  233. this.SubscriptionLocator.promises.getSubscription
  234. .calledWith(this.subscription_id)
  235. .should.equal(true)
  236. count.should.equal(2)
  237. })
  238. })
  239. describe('for nonexistent subscriptions', function () {
  240. it('should return undefined', async function () {
  241. const count =
  242. await this.Handler.promises.getTotalConfirmedUsersInGroup('fake-id')
  243. expect(count).not.to.exist
  244. })
  245. })
  246. })
  247. describe('getUsersGroupSubscriptionDetails', function () {
  248. beforeEach(function () {
  249. this.req = new MockRequest()
  250. this.PlansLocator.findLocalPlanInSettings = sinon.stub().returns({
  251. ...this.localPlanInSettings,
  252. canUseFlexibleLicensing: true,
  253. })
  254. })
  255. it('should throw if the subscription is not a group plan', async function () {
  256. this.SubscriptionLocator.promises.getUsersSubscription = sinon
  257. .stub()
  258. .resolves({ groupPlan: false })
  259. await expect(
  260. this.Handler.promises.getUsersGroupSubscriptionDetails(this.req)
  261. ).to.be.rejectedWith('User subscription is not a group plan')
  262. })
  263. it('should return users group subscription details', async function () {
  264. const data = await this.Handler.promises.getUsersGroupSubscriptionDetails(
  265. this.req
  266. )
  267. expect(data).to.deep.equal({
  268. subscription: { groupPlan: true },
  269. plan: {
  270. membersLimit: 5,
  271. membersLimitAddOn: this.RecurlyEntities.MEMBERS_LIMIT_ADD_ON_CODE,
  272. canUseFlexibleLicensing: true,
  273. },
  274. recurlySubscription: this.recurlySubscription,
  275. })
  276. })
  277. })
  278. describe('add seats subscription change', function () {
  279. beforeEach(function () {
  280. this.req = new MockRequest()
  281. Object.assign(this.req.body, { adding: this.adding })
  282. this.PlansLocator.findLocalPlanInSettings = sinon.stub().returns({
  283. ...this.localPlanInSettings,
  284. canUseFlexibleLicensing: true,
  285. })
  286. })
  287. describe('has "additional-license" add-on', function () {
  288. beforeEach(function () {
  289. this.recurlySubscription.addOns = [
  290. {
  291. code: this.RecurlyEntities.MEMBERS_LIMIT_ADD_ON_CODE,
  292. quantity: 6,
  293. },
  294. ]
  295. this.prevQuantity = this.recurlySubscription.addOns[0].quantity
  296. this.previewSubscriptionChange.nextAddOns = [
  297. {
  298. code: this.RecurlyEntities.MEMBERS_LIMIT_ADD_ON_CODE,
  299. quantity: this.prevQuantity + this.adding,
  300. },
  301. ]
  302. })
  303. afterEach(function () {
  304. sinon.assert.notCalled(
  305. this.recurlySubscription.getRequestForAddOnPurchase
  306. )
  307. this.recurlySubscription.getRequestForAddOnUpdate
  308. .calledWith(
  309. this.RecurlyEntities.MEMBERS_LIMIT_ADD_ON_CODE,
  310. this.recurlySubscription.addOns[0].quantity + this.adding
  311. )
  312. .should.equal(true)
  313. })
  314. describe('previewAddSeatsSubscriptionChange', function () {
  315. it('should return the subscription change preview', async function () {
  316. const preview =
  317. await this.Handler.promises.previewAddSeatsSubscriptionChange(
  318. this.req
  319. )
  320. this.RecurlyClient.promises.getPaymentMethod
  321. .calledWith(this.user_id)
  322. .should.equal(true)
  323. this.RecurlyClient.promises.previewSubscriptionChange
  324. .calledWith(this.changeRequest)
  325. .should.equal(true)
  326. this.SubscriptionController.makeChangePreview
  327. .calledWith(
  328. {
  329. type: 'add-on-update',
  330. addOn: {
  331. code: this.RecurlyEntities.MEMBERS_LIMIT_ADD_ON_CODE,
  332. quantity:
  333. this.previewSubscriptionChange.nextAddOns[0].quantity,
  334. prevQuantity: this.prevQuantity,
  335. },
  336. },
  337. this.previewSubscriptionChange,
  338. this.paymentMethod
  339. )
  340. .should.equal(true)
  341. preview.should.equal(this.changePreview)
  342. })
  343. })
  344. describe('createAddSeatsSubscriptionChange', function () {
  345. it('should change the subscription', async function () {
  346. const result =
  347. await this.Handler.promises.createAddSeatsSubscriptionChange(
  348. this.req
  349. )
  350. this.RecurlyClient.promises.applySubscriptionChangeRequest
  351. .calledWith(this.changeRequest)
  352. .should.equal(true)
  353. this.SubscriptionHandler.promises.syncSubscription
  354. .calledWith({ uuid: this.recurlySubscription.id }, this.user_id)
  355. .should.equal(true)
  356. expect(result).to.deep.equal({
  357. adding: this.req.body.adding,
  358. })
  359. })
  360. })
  361. })
  362. describe('has no "additional-license" add-on', function () {
  363. beforeEach(function () {
  364. this.recurlySubscription.addOns = []
  365. this.prevQuantity = this.recurlySubscription.addOns[0]?.quantity ?? 0
  366. this.previewSubscriptionChange.nextAddOns = [
  367. {
  368. code: this.RecurlyEntities.MEMBERS_LIMIT_ADD_ON_CODE,
  369. quantity: this.prevQuantity + this.adding,
  370. },
  371. ]
  372. this.PlansLocator.findLocalPlanInSettings = sinon.stub().returns({
  373. ...this.localPlanInSettings,
  374. planCode: 'group_collaborator_5_enterprise',
  375. canUseFlexibleLicensing: true,
  376. })
  377. })
  378. afterEach(function () {
  379. sinon.assert.notCalled(
  380. this.recurlySubscription.getRequestForAddOnUpdate
  381. )
  382. })
  383. describe('previewAddSeatsSubscriptionChange', function () {
  384. let preview
  385. afterEach(function () {
  386. this.RecurlyClient.promises.getPaymentMethod
  387. .calledWith(this.user_id)
  388. .should.equal(true)
  389. this.RecurlyClient.promises.previewSubscriptionChange
  390. .calledWith(this.changeRequest)
  391. .should.equal(true)
  392. this.SubscriptionController.makeChangePreview
  393. .calledWith(
  394. {
  395. type: 'add-on-update',
  396. addOn: {
  397. code: this.RecurlyEntities.MEMBERS_LIMIT_ADD_ON_CODE,
  398. quantity:
  399. this.previewSubscriptionChange.nextAddOns[0].quantity,
  400. prevQuantity: this.prevQuantity,
  401. },
  402. },
  403. this.previewSubscriptionChange,
  404. this.paymentMethod
  405. )
  406. .should.equal(true)
  407. preview.should.equal(this.changePreview)
  408. })
  409. it('should return the subscription change preview with legacy add-on price', async function () {
  410. this.recurlySubscription.createdAt = '2025-01-01T00:00:00Z'
  411. preview =
  412. await this.Handler.promises.previewAddSeatsSubscriptionChange(
  413. this.req
  414. )
  415. this.recurlySubscription.getRequestForAddOnPurchase
  416. .calledWithExactly(
  417. this.RecurlyEntities.MEMBERS_LIMIT_ADD_ON_CODE,
  418. this.adding,
  419. this.GroupPlansData.enterprise.collaborator.USD[5]
  420. .additional_license_legacy_price_in_cents / 100
  421. )
  422. .should.equal(true)
  423. })
  424. it('should return the subscription change preview with non-legacy add-on price', async function () {
  425. this.recurlySubscription.createdAt = '2030-01-01T00:00:00Z'
  426. preview =
  427. await this.Handler.promises.previewAddSeatsSubscriptionChange(
  428. this.req
  429. )
  430. this.recurlySubscription.getRequestForAddOnPurchase
  431. .calledWithExactly(
  432. this.RecurlyEntities.MEMBERS_LIMIT_ADD_ON_CODE,
  433. this.adding,
  434. undefined
  435. )
  436. .should.equal(true)
  437. })
  438. })
  439. })
  440. })
  441. describe('ensureFlexibleLicensingEnabled', function () {
  442. it('should throw if the subscription can not use flexible licensing', async function () {
  443. await expect(
  444. this.Handler.promises.ensureFlexibleLicensingEnabled({
  445. canUseFlexibleLicensing: false,
  446. })
  447. ).to.be.rejectedWith('The group plan does not support flexible licensing')
  448. })
  449. it('should not throw if the subscription can use flexible licensing', async function () {
  450. await expect(
  451. this.Handler.promises.ensureFlexibleLicensingEnabled({
  452. canUseFlexibleLicensing: true,
  453. })
  454. ).to.not.be.rejected
  455. })
  456. })
  457. describe('upgradeGroupPlan', function () {
  458. it('should upgrade the subscription for flexible licensing group plans', async function () {
  459. this.SubscriptionLocator.promises.getUsersSubscription = sinon
  460. .stub()
  461. .resolves({ groupPlan: true, planCode: 'group_collaborator' })
  462. await this.Handler.promises.upgradeGroupPlan(this.user_id)
  463. this.RecurlyClient.promises.applySubscriptionChangeRequest
  464. .calledWith(this.changeRequest)
  465. .should.equal(true)
  466. this.SubscriptionHandler.promises.syncSubscription
  467. .calledWith({ uuid: this.changeRequest.subscription.id }, this.user_id)
  468. .should.equal(true)
  469. })
  470. it('should upgrade the subscription for legacy group plans', async function () {
  471. this.SubscriptionLocator.promises.getUsersSubscription = sinon
  472. .stub()
  473. .resolves({
  474. groupPlan: true,
  475. planCode: 'group_collaborator_10_educational',
  476. })
  477. await this.Handler.promises.upgradeGroupPlan(this.user_id)
  478. this.RecurlyClient.promises.applySubscriptionChangeRequest
  479. .calledWith(this.changeRequest)
  480. .should.equal(true)
  481. this.SubscriptionHandler.promises.syncSubscription
  482. .calledWith({ uuid: this.changeRequest.subscription.id }, this.user_id)
  483. .should.equal(true)
  484. })
  485. it('should fail the upgrade if is professional already', async function () {
  486. this.SubscriptionLocator.promises.getUsersSubscription = sinon
  487. .stub()
  488. .resolves({ groupPlan: true, planCode: 'group_professional' })
  489. await expect(
  490. this.Handler.promises.upgradeGroupPlan(this.user_id)
  491. ).to.be.rejectedWith('Not eligible for group plan upgrade')
  492. })
  493. it('should fail the upgrade if not group plan', async function () {
  494. this.SubscriptionLocator.promises.getUsersSubscription = sinon
  495. .stub()
  496. .resolves({ groupPlan: false, planCode: 'test_plan_code' })
  497. await expect(
  498. this.Handler.promises.upgradeGroupPlan(this.user_id)
  499. ).to.be.rejectedWith('Not eligible for group plan upgrade')
  500. })
  501. })
  502. describe('getGroupPlanUpgradePreview', function () {
  503. it('should generate preview for subscription upgrade', async function () {
  504. this.SubscriptionLocator.promises.getUsersSubscription = sinon
  505. .stub()
  506. .resolves({ groupPlan: true, planCode: 'group_collaborator' })
  507. const result = await this.Handler.promises.getGroupPlanUpgradePreview(
  508. this.user_id
  509. )
  510. this.RecurlyClient.promises.previewSubscriptionChange
  511. .calledWith(this.changeRequest)
  512. .should.equal(true)
  513. result.should.equal(this.changePreview)
  514. })
  515. })
  516. })