-
Notifications
You must be signed in to change notification settings - Fork 3.7k
improvement(checkout): enforce team/enterprise-only org subscription, block double-covered personal checkouts #5715
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3a3a372
improvement(checkout): enforce team/enterprise-only org subscriptions…
icecrasher321 af54aa6
close race with personal pro / org inclusions
icecrasher321 e9ac1db
address comments
icecrasher321 37eb50d
fix(billing): compute org coverage independently of the personal-sub …
icecrasher321 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,177 @@ | ||
| /** | ||
| * @vitest-environment node | ||
| */ | ||
| import { dbChainMock, dbChainMockFns, resetDbChainMock } from '@sim/testing' | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
|
|
||
| vi.mock('@sim/db', () => dbChainMock) | ||
|
|
||
| import { guardSubscriptionPlanWrites } from '@/lib/auth/stripe-adapter-guard' | ||
|
|
||
| function createBaseAdapter() { | ||
| return { | ||
| create: vi.fn(async (data: { data: unknown }) => data.data), | ||
| update: vi.fn(async (data: { update: unknown }) => data.update), | ||
| updateMany: vi.fn(async () => 1), | ||
| findOne: vi.fn(), | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * The guard only relies on create/update/updateMany/findOne; the remaining | ||
| * adapter surface passes through the spread untouched. | ||
| */ | ||
| // double-cast-allowed: test double implements only the adapter subset the guard touches | ||
| const asAdapter = (base: ReturnType<typeof createBaseAdapter>) => | ||
| guardSubscriptionPlanWrites(base as unknown as Parameters<typeof guardSubscriptionPlanWrites>[0]) | ||
|
|
||
| const ORG_ROW = { id: 'sub-1', referenceId: 'org-1', plan: 'team_6000' } | ||
| const WHERE = [{ field: 'id', value: 'sub-1' }] | ||
|
|
||
| describe('guardSubscriptionPlanWrites', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
| resetDbChainMock() | ||
| }) | ||
|
|
||
| it('strips a non-org plan from an update targeting an org-referenced row but keeps the rest', async () => { | ||
| const base = createBaseAdapter() | ||
| base.findOne.mockResolvedValueOnce(ORG_ROW) | ||
| // org existence lookup | ||
| dbChainMockFns.limit.mockResolvedValueOnce([{ id: 'org-1' }]) | ||
|
|
||
| const guarded = asAdapter(base) | ||
| await guarded.update({ | ||
| model: 'subscription', | ||
| where: WHERE as never, | ||
| update: { plan: 'pro_6000', status: 'active', seats: 2 }, | ||
| }) | ||
|
|
||
| expect(base.update).toHaveBeenCalledWith( | ||
| expect.objectContaining({ update: { status: 'active', seats: 2 } }) | ||
| ) | ||
| }) | ||
|
|
||
| it('returns the current row without writing when stripping leaves an empty update', async () => { | ||
| const base = createBaseAdapter() | ||
| base.findOne.mockResolvedValueOnce(ORG_ROW).mockResolvedValueOnce(ORG_ROW) | ||
| dbChainMockFns.limit.mockResolvedValueOnce([{ id: 'org-1' }]) | ||
|
|
||
| const guarded = asAdapter(base) | ||
| const result = await guarded.update({ | ||
| model: 'subscription', | ||
| where: WHERE as never, | ||
| update: { plan: 'pro_6000' }, | ||
| }) | ||
|
|
||
| expect(result).toEqual(ORG_ROW) | ||
| expect(base.update).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('strips the plan when the pre-update lookup finds no row (fail closed)', async () => { | ||
| const base = createBaseAdapter() | ||
| base.findOne.mockResolvedValueOnce(null) | ||
|
|
||
| const guarded = asAdapter(base) | ||
| await guarded.update({ | ||
| model: 'subscription', | ||
| where: WHERE as never, | ||
| update: { plan: 'pro_6000', status: 'active' }, | ||
| }) | ||
|
|
||
| expect(base.update).toHaveBeenCalledWith( | ||
| expect.objectContaining({ update: { status: 'active' } }) | ||
| ) | ||
| }) | ||
|
|
||
| it('writes nothing when a plan-only update targets no readable row', async () => { | ||
| const base = createBaseAdapter() | ||
| base.findOne.mockResolvedValueOnce(null) | ||
|
|
||
| const guarded = asAdapter(base) | ||
| const result = await guarded.update({ | ||
| model: 'subscription', | ||
| where: WHERE as never, | ||
| update: { plan: 'pro_6000' }, | ||
| }) | ||
|
|
||
| expect(result).toBeNull() | ||
| expect(base.update).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('passes through non-org plan updates for user-referenced rows', async () => { | ||
| const base = createBaseAdapter() | ||
| base.findOne.mockResolvedValueOnce({ id: 'sub-2', referenceId: 'user-1', plan: 'pro_6000' }) | ||
| // org existence lookup: user id is not an organization | ||
| dbChainMockFns.limit.mockResolvedValueOnce([]) | ||
|
|
||
| const guarded = asAdapter(base) | ||
| await guarded.update({ | ||
| model: 'subscription', | ||
| where: WHERE as never, | ||
| update: { plan: 'pro_25000', status: 'active' }, | ||
| }) | ||
|
|
||
| expect(base.update).toHaveBeenCalledWith( | ||
| expect.objectContaining({ update: { plan: 'pro_25000', status: 'active' } }) | ||
| ) | ||
| }) | ||
|
|
||
| it('passes through org-plan updates without any row lookup', async () => { | ||
| const base = createBaseAdapter() | ||
|
|
||
| const guarded = asAdapter(base) | ||
| await guarded.update({ | ||
| model: 'subscription', | ||
| where: WHERE as never, | ||
| update: { plan: 'team_25000', seats: 5 }, | ||
| }) | ||
|
|
||
| expect(base.findOne).not.toHaveBeenCalled() | ||
| expect(base.update).toHaveBeenCalledWith( | ||
| expect.objectContaining({ update: { plan: 'team_25000', seats: 5 } }) | ||
| ) | ||
| }) | ||
|
|
||
| it('passes through updates on other models untouched', async () => { | ||
| const base = createBaseAdapter() | ||
|
|
||
| const guarded = asAdapter(base) | ||
| await guarded.update({ | ||
| model: 'user', | ||
| where: WHERE as never, | ||
| update: { plan: 'pro_6000' }, | ||
| }) | ||
|
|
||
| expect(base.findOne).not.toHaveBeenCalled() | ||
| expect(base.update).toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('rejects creating an org-referenced subscription with a non-org plan', async () => { | ||
| const base = createBaseAdapter() | ||
| dbChainMockFns.limit.mockResolvedValueOnce([{ id: 'org-1' }]) | ||
|
|
||
| const guarded = asAdapter(base) | ||
| await expect( | ||
| guarded.create({ | ||
| model: 'subscription', | ||
| data: { plan: 'pro_6000', referenceId: 'org-1' } as never, | ||
| }) | ||
| ).rejects.toThrow(/must hold a Team or Enterprise plan/) | ||
|
|
||
| expect(base.create).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('allows creating a personal pro subscription', async () => { | ||
| const base = createBaseAdapter() | ||
| dbChainMockFns.limit.mockResolvedValueOnce([]) | ||
|
|
||
| const guarded = asAdapter(base) | ||
| await guarded.create({ | ||
| model: 'subscription', | ||
| data: { plan: 'pro_6000', referenceId: 'user-1' } as never, | ||
| }) | ||
|
|
||
| expect(base.create).toHaveBeenCalled() | ||
| }) | ||
| }) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.