| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573 |
- const SandboxedModule = require('sandboxed-module')
- const sinon = require('sinon')
- const { expect } = require('chai')
- const MockRequest = require('../helpers/MockRequest')
- const modulePath =
- '../../../../app/src/Features/Subscription/SubscriptionGroupHandler'
- describe('SubscriptionGroupHandler', function () {
- beforeEach(function () {
- this.adminUser_id = '12321'
- this.newEmail = 'bob@smith.com'
- this.user_id = '3121321'
- this.email = 'jim@example.com'
- this.user = { _id: this.user_id, email: this.newEmail }
- this.subscription_id = '31DSd1123D'
- this.adding = 1
- this.paymentMethod = { cardType: 'Visa', lastFour: '1111' }
- this.RecurlyEntities = {
- MEMBERS_LIMIT_ADD_ON_CODE: 'additional-license',
- }
- this.localPlanInSettings = {
- membersLimit: 5,
- membersLimitAddOn: this.RecurlyEntities.MEMBERS_LIMIT_ADD_ON_CODE,
- }
- this.subscription = {
- admin_id: this.adminUser_id,
- manager_ids: [this.adminUser_id],
- _id: this.subscription_id,
- }
- this.changeRequest = {
- timeframe: 'now',
- subscription: {
- id: 'test_id',
- },
- }
- this.recurlySubscription = {
- id: 123,
- addOns: [
- {
- code: this.RecurlyEntities.MEMBERS_LIMIT_ADD_ON_CODE,
- quantity: 1,
- },
- ],
- getRequestForAddOnUpdate: sinon.stub().returns(this.changeRequest),
- getRequestForGroupPlanUpgrade: sinon.stub().returns(this.changeRequest),
- getRequestForAddOnPurchase: sinon.stub().returns(this.changeRequest),
- getRequestForFlexibleLicensingGroupPlanUpgrade: sinon
- .stub()
- .returns(this.changeRequest),
- createdAt: '2025-01-01T00:00:00Z',
- currency: 'USD',
- hasAddOn(code) {
- return this.addOns.some(addOn => addOn.code === code)
- },
- }
- this.SubscriptionLocator = {
- promises: {
- getUsersSubscription: sinon.stub().resolves({ groupPlan: true }),
- getSubscriptionByMemberIdAndId: sinon.stub(),
- getSubscription: sinon.stub().resolves(this.subscription),
- },
- }
- this.changePreview = {
- currency: 'USD',
- }
- this.SubscriptionController = {
- makeChangePreview: sinon.stub().resolves(this.changePreview),
- }
- this.SubscriptionUpdater = {
- promises: {
- removeUserFromGroup: sinon.stub().resolves(),
- getSubscription: sinon.stub().resolves(),
- },
- }
- this.Subscription = {
- updateOne: sinon.stub().returns({ exec: sinon.stub().resolves }),
- updateMany: sinon.stub().returns({ exec: sinon.stub().resolves }),
- findOne: sinon.stub().returns({ exec: sinon.stub().resolves }),
- }
- this.SessionManager = {
- getLoggedInUserId: sinon.stub().returns(this.user._id),
- }
- this.previewSubscriptionChange = {
- nextAddOns: [
- {
- code: this.RecurlyEntities.MEMBERS_LIMIT_ADD_ON_CODE,
- quantity: this.recurlySubscription.addOns[0].quantity + this.adding,
- },
- ],
- subscription: {
- planName: 'test plan',
- },
- }
- this.applySubscriptionChange = {}
- this.RecurlyClient = {
- promises: {
- getSubscription: sinon.stub().resolves(this.recurlySubscription),
- getPaymentMethod: sinon.stub().resolves(this.paymentMethod),
- previewSubscriptionChange: sinon
- .stub()
- .resolves(this.previewSubscriptionChange),
- applySubscriptionChangeRequest: sinon
- .stub()
- .resolves(this.applySubscriptionChange),
- },
- }
- this.PlansLocator = {
- findLocalPlanInSettings: sinon.stub().returns(this.localPlanInSettings),
- }
- this.SubscriptionHandler = {
- promises: {
- syncSubscription: sinon.stub().resolves(),
- },
- }
- this.GroupPlansData = {
- enterprise: {
- collaborator: {
- USD: {
- 5: {
- price_in_cents: 10000,
- additional_license_legacy_price_in_cents: 5000,
- },
- },
- },
- },
- }
- this.Handler = SandboxedModule.require(modulePath, {
- requires: {
- './SubscriptionUpdater': this.SubscriptionUpdater,
- './SubscriptionLocator': this.SubscriptionLocator,
- './SubscriptionController': this.SubscriptionController,
- './SubscriptionHandler': this.SubscriptionHandler,
- '../../models/Subscription': {
- Subscription: this.Subscription,
- },
- './RecurlyClient': this.RecurlyClient,
- './PlansLocator': this.PlansLocator,
- './RecurlyEntities': this.RecurlyEntities,
- '../Authentication/SessionManager': this.SessionManager,
- './GroupPlansData': this.GroupPlansData,
- },
- })
- })
- describe('removeUserFromGroup', function () {
- it('should call the subscription updater to remove the user', async function () {
- await this.Handler.promises.removeUserFromGroup(
- this.adminUser_id,
- this.user._id
- )
- this.SubscriptionUpdater.promises.removeUserFromGroup
- .calledWith(this.adminUser_id, this.user._id)
- .should.equal(true)
- })
- })
- describe('replaceUserReferencesInGroups', function () {
- beforeEach(async function () {
- this.oldId = 'ba5eba11'
- this.newId = '5ca1ab1e'
- await this.Handler.promises.replaceUserReferencesInGroups(
- this.oldId,
- this.newId
- )
- })
- it('replaces the admin_id', function () {
- this.Subscription.updateOne
- .calledWith({ admin_id: this.oldId }, { admin_id: this.newId })
- .should.equal(true)
- })
- it('replaces the manager_ids', function () {
- this.Subscription.updateMany
- .calledWith(
- { manager_ids: 'ba5eba11' },
- { $addToSet: { manager_ids: '5ca1ab1e' } }
- )
- .should.equal(true)
- this.Subscription.updateMany
- .calledWith(
- { manager_ids: 'ba5eba11' },
- { $pull: { manager_ids: 'ba5eba11' } }
- )
- .should.equal(true)
- })
- it('replaces the member ids', function () {
- this.Subscription.updateMany
- .calledWith(
- { member_ids: this.oldId },
- { $addToSet: { member_ids: this.newId } }
- )
- .should.equal(true)
- this.Subscription.updateMany
- .calledWith(
- { member_ids: this.oldId },
- { $pull: { member_ids: this.oldId } }
- )
- .should.equal(true)
- })
- })
- describe('isUserPartOfGroup', function () {
- beforeEach(function () {
- this.subscription_id = '123ed13123'
- })
- it('should return true when user is part of subscription', async function () {
- this.SubscriptionLocator.promises.getSubscriptionByMemberIdAndId.resolves(
- {
- _id: this.subscription_id,
- }
- )
- const partOfGroup = await this.Handler.promises.isUserPartOfGroup(
- this.user_id,
- this.subscription_id
- )
- partOfGroup.should.equal(true)
- })
- it('should return false when no subscription is found', async function () {
- this.SubscriptionLocator.promises.getSubscriptionByMemberIdAndId.resolves(
- null
- )
- const partOfGroup = await this.Handler.promises.isUserPartOfGroup(
- this.user_id,
- this.subscription_id
- )
- partOfGroup.should.equal(false)
- })
- })
- describe('getTotalConfirmedUsersInGroup', function () {
- describe('for existing subscriptions', function () {
- beforeEach(function () {
- this.subscription.member_ids = ['12321', '3121321']
- })
- it('should call the subscription locator and return 2 users', async function () {
- const count = await this.Handler.promises.getTotalConfirmedUsersInGroup(
- this.subscription_id
- )
- this.SubscriptionLocator.promises.getSubscription
- .calledWith(this.subscription_id)
- .should.equal(true)
- count.should.equal(2)
- })
- })
- describe('for nonexistent subscriptions', function () {
- it('should return undefined', async function () {
- const count =
- await this.Handler.promises.getTotalConfirmedUsersInGroup('fake-id')
- expect(count).not.to.exist
- })
- })
- })
- describe('getUsersGroupSubscriptionDetails', function () {
- beforeEach(function () {
- this.req = new MockRequest()
- this.PlansLocator.findLocalPlanInSettings = sinon.stub().returns({
- ...this.localPlanInSettings,
- canUseFlexibleLicensing: true,
- })
- })
- it('should throw if the subscription is not a group plan', async function () {
- this.SubscriptionLocator.promises.getUsersSubscription = sinon
- .stub()
- .resolves({ groupPlan: false })
- await expect(
- this.Handler.promises.getUsersGroupSubscriptionDetails(this.req)
- ).to.be.rejectedWith('User subscription is not a group plan')
- })
- it('should return users group subscription details', async function () {
- const data = await this.Handler.promises.getUsersGroupSubscriptionDetails(
- this.req
- )
- expect(data).to.deep.equal({
- subscription: { groupPlan: true },
- plan: {
- membersLimit: 5,
- membersLimitAddOn: this.RecurlyEntities.MEMBERS_LIMIT_ADD_ON_CODE,
- canUseFlexibleLicensing: true,
- },
- recurlySubscription: this.recurlySubscription,
- })
- })
- })
- describe('add seats subscription change', function () {
- beforeEach(function () {
- this.req = new MockRequest()
- Object.assign(this.req.body, { adding: this.adding })
- this.PlansLocator.findLocalPlanInSettings = sinon.stub().returns({
- ...this.localPlanInSettings,
- canUseFlexibleLicensing: true,
- })
- })
- describe('has "additional-license" add-on', function () {
- beforeEach(function () {
- this.recurlySubscription.addOns = [
- {
- code: this.RecurlyEntities.MEMBERS_LIMIT_ADD_ON_CODE,
- quantity: 6,
- },
- ]
- this.prevQuantity = this.recurlySubscription.addOns[0].quantity
- this.previewSubscriptionChange.nextAddOns = [
- {
- code: this.RecurlyEntities.MEMBERS_LIMIT_ADD_ON_CODE,
- quantity: this.prevQuantity + this.adding,
- },
- ]
- })
- afterEach(function () {
- sinon.assert.notCalled(
- this.recurlySubscription.getRequestForAddOnPurchase
- )
- this.recurlySubscription.getRequestForAddOnUpdate
- .calledWith(
- this.RecurlyEntities.MEMBERS_LIMIT_ADD_ON_CODE,
- this.recurlySubscription.addOns[0].quantity + this.adding
- )
- .should.equal(true)
- })
- describe('previewAddSeatsSubscriptionChange', function () {
- it('should return the subscription change preview', async function () {
- const preview =
- await this.Handler.promises.previewAddSeatsSubscriptionChange(
- this.req
- )
- this.RecurlyClient.promises.getPaymentMethod
- .calledWith(this.user_id)
- .should.equal(true)
- this.RecurlyClient.promises.previewSubscriptionChange
- .calledWith(this.changeRequest)
- .should.equal(true)
- this.SubscriptionController.makeChangePreview
- .calledWith(
- {
- type: 'add-on-update',
- addOn: {
- code: this.RecurlyEntities.MEMBERS_LIMIT_ADD_ON_CODE,
- quantity:
- this.previewSubscriptionChange.nextAddOns[0].quantity,
- prevQuantity: this.prevQuantity,
- },
- },
- this.previewSubscriptionChange,
- this.paymentMethod
- )
- .should.equal(true)
- preview.should.equal(this.changePreview)
- })
- })
- describe('createAddSeatsSubscriptionChange', function () {
- it('should change the subscription', async function () {
- const result =
- await this.Handler.promises.createAddSeatsSubscriptionChange(
- this.req
- )
- this.RecurlyClient.promises.applySubscriptionChangeRequest
- .calledWith(this.changeRequest)
- .should.equal(true)
- this.SubscriptionHandler.promises.syncSubscription
- .calledWith({ uuid: this.recurlySubscription.id }, this.user_id)
- .should.equal(true)
- expect(result).to.deep.equal({
- adding: this.req.body.adding,
- })
- })
- })
- })
- describe('has no "additional-license" add-on', function () {
- beforeEach(function () {
- this.recurlySubscription.addOns = []
- this.prevQuantity = this.recurlySubscription.addOns[0]?.quantity ?? 0
- this.previewSubscriptionChange.nextAddOns = [
- {
- code: this.RecurlyEntities.MEMBERS_LIMIT_ADD_ON_CODE,
- quantity: this.prevQuantity + this.adding,
- },
- ]
- this.PlansLocator.findLocalPlanInSettings = sinon.stub().returns({
- ...this.localPlanInSettings,
- planCode: 'group_collaborator_5_enterprise',
- canUseFlexibleLicensing: true,
- })
- })
- afterEach(function () {
- sinon.assert.notCalled(
- this.recurlySubscription.getRequestForAddOnUpdate
- )
- })
- describe('previewAddSeatsSubscriptionChange', function () {
- let preview
- afterEach(function () {
- this.RecurlyClient.promises.getPaymentMethod
- .calledWith(this.user_id)
- .should.equal(true)
- this.RecurlyClient.promises.previewSubscriptionChange
- .calledWith(this.changeRequest)
- .should.equal(true)
- this.SubscriptionController.makeChangePreview
- .calledWith(
- {
- type: 'add-on-update',
- addOn: {
- code: this.RecurlyEntities.MEMBERS_LIMIT_ADD_ON_CODE,
- quantity:
- this.previewSubscriptionChange.nextAddOns[0].quantity,
- prevQuantity: this.prevQuantity,
- },
- },
- this.previewSubscriptionChange,
- this.paymentMethod
- )
- .should.equal(true)
- preview.should.equal(this.changePreview)
- })
- it('should return the subscription change preview with legacy add-on price', async function () {
- this.recurlySubscription.createdAt = '2025-01-01T00:00:00Z'
- preview =
- await this.Handler.promises.previewAddSeatsSubscriptionChange(
- this.req
- )
- this.recurlySubscription.getRequestForAddOnPurchase
- .calledWithExactly(
- this.RecurlyEntities.MEMBERS_LIMIT_ADD_ON_CODE,
- this.adding,
- this.GroupPlansData.enterprise.collaborator.USD[5]
- .additional_license_legacy_price_in_cents / 100
- )
- .should.equal(true)
- })
- it('should return the subscription change preview with non-legacy add-on price', async function () {
- this.recurlySubscription.createdAt = '2030-01-01T00:00:00Z'
- preview =
- await this.Handler.promises.previewAddSeatsSubscriptionChange(
- this.req
- )
- this.recurlySubscription.getRequestForAddOnPurchase
- .calledWithExactly(
- this.RecurlyEntities.MEMBERS_LIMIT_ADD_ON_CODE,
- this.adding,
- undefined
- )
- .should.equal(true)
- })
- })
- })
- })
- describe('ensureFlexibleLicensingEnabled', function () {
- it('should throw if the subscription can not use flexible licensing', async function () {
- await expect(
- this.Handler.promises.ensureFlexibleLicensingEnabled({
- canUseFlexibleLicensing: false,
- })
- ).to.be.rejectedWith('The group plan does not support flexible licensing')
- })
- it('should not throw if the subscription can use flexible licensing', async function () {
- await expect(
- this.Handler.promises.ensureFlexibleLicensingEnabled({
- canUseFlexibleLicensing: true,
- })
- ).to.not.be.rejected
- })
- })
- describe('upgradeGroupPlan', function () {
- it('should upgrade the subscription for flexible licensing group plans', async function () {
- this.SubscriptionLocator.promises.getUsersSubscription = sinon
- .stub()
- .resolves({ groupPlan: true, planCode: 'group_collaborator' })
- await this.Handler.promises.upgradeGroupPlan(this.user_id)
- this.RecurlyClient.promises.applySubscriptionChangeRequest
- .calledWith(this.changeRequest)
- .should.equal(true)
- this.SubscriptionHandler.promises.syncSubscription
- .calledWith({ uuid: this.changeRequest.subscription.id }, this.user_id)
- .should.equal(true)
- })
- it('should upgrade the subscription for legacy group plans', async function () {
- this.SubscriptionLocator.promises.getUsersSubscription = sinon
- .stub()
- .resolves({
- groupPlan: true,
- planCode: 'group_collaborator_10_educational',
- })
- await this.Handler.promises.upgradeGroupPlan(this.user_id)
- this.RecurlyClient.promises.applySubscriptionChangeRequest
- .calledWith(this.changeRequest)
- .should.equal(true)
- this.SubscriptionHandler.promises.syncSubscription
- .calledWith({ uuid: this.changeRequest.subscription.id }, this.user_id)
- .should.equal(true)
- })
- it('should fail the upgrade if is professional already', async function () {
- this.SubscriptionLocator.promises.getUsersSubscription = sinon
- .stub()
- .resolves({ groupPlan: true, planCode: 'group_professional' })
- await expect(
- this.Handler.promises.upgradeGroupPlan(this.user_id)
- ).to.be.rejectedWith('Not eligible for group plan upgrade')
- })
- it('should fail the upgrade if not group plan', async function () {
- this.SubscriptionLocator.promises.getUsersSubscription = sinon
- .stub()
- .resolves({ groupPlan: false, planCode: 'test_plan_code' })
- await expect(
- this.Handler.promises.upgradeGroupPlan(this.user_id)
- ).to.be.rejectedWith('Not eligible for group plan upgrade')
- })
- })
- describe('getGroupPlanUpgradePreview', function () {
- it('should generate preview for subscription upgrade', async function () {
- this.SubscriptionLocator.promises.getUsersSubscription = sinon
- .stub()
- .resolves({ groupPlan: true, planCode: 'group_collaborator' })
- const result = await this.Handler.promises.getGroupPlanUpgradePreview(
- this.user_id
- )
- this.RecurlyClient.promises.previewSubscriptionChange
- .calledWith(this.changeRequest)
- .should.equal(true)
- result.should.equal(this.changePreview)
- })
- })
- })
|