Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 67 additions & 1 deletion frontend/common/utils/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
// mocked out to allow `common/utils/utils` to load for real.
jest.mock('common/stores/account-store', () => ({}))
jest.mock('common/stores/project-store', () => ({}))
jest.mock('common/store', () => ({ getStore: () => ({}) }))
jest.mock('common/store', () => ({
getStore: () => ({ getState: () => ({}) }),
}))
jest.mock('@flagsmith/flagsmith', () => ({
getValue: (_key: string, opts: { fallback: unknown }) => opts.fallback,
}))
Expand Down Expand Up @@ -96,3 +98,67 @@ describe('validateRule', () => {
expect(Utils.validateRule(rule)).toBe(false)
})
})

describe('getPlanPermission', () => {
beforeEach(() => {
jest.spyOn(Utils, 'isEnterpriseImage').mockReturnValue(false)
jest.spyOn(Utils, 'isSaas').mockReturnValue(true)
})
afterEach(() => jest.restoreAllMocks())

it.each([
['scale-up', 'AUDIT', true],
['enterprise', 'AUDIT', true],
['start-up', 'AUDIT', false],
['free', 'AUDIT', false],
['enterprise', 'SCIM', true],
['scale-up', 'SCIM', false],
] as const)(
'hardcoded features: plan=%s feature=%s => %s',
(plan, feature, expected) => {
expect(Utils.getPlanPermission(plan, feature)).toBe(expected)
},
)

describe('WAREHOUSE with legacy array value', () => {
it('allows free plan when value includes "free"', () => {
jest.spyOn(Utils, 'getFlagsmithJSONValue').mockReturnValue(['free'])
expect(Utils.getPlanPermission('free', 'WAREHOUSE')).toBe(true)
})

it('blocks free plan when value is empty', () => {
jest.spyOn(Utils, 'getFlagsmithJSONValue').mockReturnValue([])
expect(Utils.getPlanPermission('free', 'WAREHOUSE')).toBe(false)
})

it('allows scale-up when value includes "scale-up"', () => {
jest.spyOn(Utils, 'getFlagsmithJSONValue').mockReturnValue(['scale-up'])
expect(Utils.getPlanPermission('scale-up', 'WAREHOUSE')).toBe(true)
})
})

describe('WAREHOUSE with object value', () => {
it('allows free plan when allowed_plans includes "free"', () => {
jest.spyOn(Utils, 'getFlagsmithJSONValue').mockReturnValue({
allowed_plans: ['free'],
auto_connect_warehouse: true,
})
expect(Utils.getPlanPermission('free', 'WAREHOUSE')).toBe(true)
})

it('blocks free plan when allowed_plans does not include "free"', () => {
jest
.spyOn(Utils, 'getFlagsmithJSONValue')
.mockReturnValue({ allowed_plans: ['scale-up'] })
expect(Utils.getPlanPermission('free', 'WAREHOUSE')).toBe(false)
})

it('falls back to enterprise with malformed value', () => {
jest
.spyOn(Utils, 'getFlagsmithJSONValue')
.mockReturnValue({ allowed_plans: 'not-an-array' })
expect(Utils.getPlanPermission('enterprise', 'WAREHOUSE')).toBe(true)
expect(Utils.getPlanPermission('scale-up', 'WAREHOUSE')).toBe(false)
})
})
})
33 changes: 21 additions & 12 deletions frontend/common/utils/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export type PaidFeature =
export type AppFeature = PaidFeature | 'FEATURE_HEALTH'

// Define a type for plan categories
type Plan = 'start-up' | 'scale-up' | 'enterprise' | null
type Plan = 'free' | 'start-up' | 'scale-up' | 'enterprise' | null

export const planNames = {
enterprise: 'Enterprise',
Expand Down Expand Up @@ -180,8 +180,6 @@ const Utils = Object.assign({}, BaseUtils, {
}
return null
},
// Delegates to the standalone, Flux-free module so callers that can't import
// this file (e.g. unit-tested hooks) can use the same logic directly.
featureStateToValue,
findOperator(
operator: SegmentCondition['operator'],
Expand Down Expand Up @@ -479,16 +477,19 @@ const Utils = Object.assign({}, BaseUtils, {

getPlanPermission: (plan: string, feature: PaidFeature) => {
const planName = Utils.getPlanName(plan)
if (!plan || planName === planNames.free) {
return false
}
if (!plan) return false

const requiredPlan = Utils.getRequiredPlan(feature)
if (requiredPlan === 'free') return true

if (planName === planNames.free) return false

const isScaleupOrGreater = planName !== planNames.startup
const isEnterprise = planName === planNames.enterprise
if (feature === 'AUTO_SEATS') {
return isScaleupOrGreater && !isEnterprise
}

const requiredPlan = Utils.getRequiredPlan(feature)
if (requiredPlan === 'enterprise') {
return isEnterprise
} else if (requiredPlan === 'scale-up') {
Expand Down Expand Up @@ -542,15 +543,23 @@ const Utils = Object.assign({}, BaseUtils, {
break
}
case 'WAREHOUSE': {
const remotePlansValue = Utils.getFlagsmithJSONValue(
const remoteValue = Utils.getFlagsmithJSONValue(
Comment thread
Zaimwa9 marked this conversation as resolved.
'experimentation_warehouse_connection',
[],
)
const remotePlans: string[] = Array.isArray(remotePlansValue)
? remotePlansValue
: []
let remotePlans: string[] = []
if (Array.isArray(remoteValue)) {
remotePlans = remoteValue
} else if (Array.isArray(remoteValue?.allowed_plans)) {
remotePlans = remoteValue.allowed_plans
}
const allowedPlans = [...remotePlans, 'enterprise']
const planHierarchy: Plan[] = ['start-up', 'scale-up', 'enterprise']
const planHierarchy: Plan[] = [
'free',
'start-up',
'scale-up',
'enterprise',
]
plan =
planHierarchy.find((p) => allowedPlans.includes(p)) || 'enterprise'
break
Expand Down
Loading