-
-
Notifications
You must be signed in to change notification settings - Fork 129
fix(cli): validate app-scoped plans with app context #2825
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
deeae10
fix(cli): validate app-scoped plans with app context
WcaleNieWolny 3782d66
docs(cli): describe app-aware plan validation
WcaleNieWolny 375e896
fix(cli): normalize plan validation errors
WcaleNieWolny 4d2ca6f
fix(cli): fall back for legacy plan RPCs
WcaleNieWolny 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
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
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,154 @@ | ||
| #!/usr/bin/env node | ||
|
|
||
| import process from 'node:process' | ||
| import * as utils from '../src/utils.ts' | ||
|
|
||
| console.log('🧪 Testing app-aware plan validation...\n') | ||
|
|
||
| let testsPassed = 0 | ||
| let testsFailed = 0 | ||
|
|
||
| async function test(name, fn) { | ||
| try { | ||
| console.log(`\n🔍 ${name}`) | ||
| await fn() | ||
| console.log(`✅ PASSED: ${name}`) | ||
| testsPassed++ | ||
| } | ||
| catch (error) { | ||
| console.error(`❌ FAILED: ${name}`) | ||
| console.error(` Error: ${error.message}`) | ||
| testsFailed++ | ||
| } | ||
| } | ||
|
|
||
| function assert(condition, message) { | ||
| if (!condition) | ||
| throw new Error(message) | ||
| } | ||
|
|
||
| function assertEquals(actual, expected, message) { | ||
| if (JSON.stringify(actual) !== JSON.stringify(expected)) | ||
| throw new Error(message || `Expected ${JSON.stringify(expected)}, got ${JSON.stringify(actual)}`) | ||
| } | ||
|
|
||
| await test('checks an app-scoped key through the app-aware plan RPC', async () => { | ||
| assert(typeof utils.isAllowedPlanActions === 'function', 'Expected isAllowedPlanActions to be exported') | ||
|
|
||
| const calls = [] | ||
| const supabase = { | ||
| rpc: async (name, args) => { | ||
| calls.push({ name, args }) | ||
| return { data: true, error: null } | ||
| }, | ||
| } | ||
|
|
||
| const allowed = await utils.isAllowedPlanActions( | ||
| supabase, | ||
| 'org-id', | ||
| ['mau', 'storage', 'bandwidth', 'build_time'], | ||
| 'com.example.app', | ||
| ) | ||
|
|
||
| assertEquals(allowed, true) | ||
| assertEquals(calls, [{ | ||
| name: 'is_allowed_action_org_action', | ||
| args: { | ||
| orgid: 'org-id', | ||
| actions: ['mau', 'storage', 'bandwidth', 'build_time'], | ||
| appid: 'com.example.app', | ||
| }, | ||
| }]) | ||
| }) | ||
|
|
||
| await test('surfaces plan RPC errors instead of reporting an invalid plan', async () => { | ||
| assert(typeof utils.isAllowedPlanActions === 'function', 'Expected isAllowedPlanActions to be exported') | ||
|
|
||
| const supabase = { | ||
| rpc: async () => ({ | ||
| data: null, | ||
| error: { message: 'permission lookup failed' }, | ||
| }), | ||
| } | ||
|
|
||
| let thrown | ||
| try { | ||
| await utils.isAllowedPlanActions(supabase, 'org-id', ['storage'], 'com.example.app') | ||
| } | ||
| catch (error) { | ||
| thrown = error | ||
| } | ||
|
|
||
| assert(thrown instanceof Error, 'Expected the RPC error to be thrown') | ||
| assert(thrown.message.includes('Cannot validate plan'), `Unexpected error: ${thrown.message}`) | ||
| }) | ||
|
|
||
| await test('falls back to organization validation when the app-aware RPC is unavailable', async () => { | ||
| const calls = [] | ||
| const supabase = { | ||
| rpc: (name, args) => { | ||
| calls.push({ name, args }) | ||
| if (name === 'is_allowed_action_org_action') { | ||
| return Promise.resolve({ | ||
| data: null, | ||
| error: { | ||
| code: 'PGRST202', | ||
| message: 'Could not find the function in the schema cache', | ||
| }, | ||
| }) | ||
| } | ||
| return { | ||
| single: async () => ({ data: true, error: null }), | ||
| } | ||
| }, | ||
| } | ||
|
|
||
| const allowed = await utils.isAllowedPlanActions( | ||
| supabase, | ||
| 'org-id', | ||
| ['storage'], | ||
| 'com.example.app', | ||
| ) | ||
|
|
||
| assertEquals(allowed, true) | ||
| assertEquals(calls, [ | ||
| { | ||
| name: 'is_allowed_action_org_action', | ||
| args: { | ||
| orgid: 'org-id', | ||
| actions: ['storage'], | ||
| appid: 'com.example.app', | ||
| }, | ||
| }, | ||
| { | ||
| name: 'is_allowed_action_org', | ||
| args: { orgid: 'org-id' }, | ||
| }, | ||
| ]) | ||
| }) | ||
|
|
||
| await test('surfaces organization plan RPC errors instead of reporting an invalid plan', async () => { | ||
| const supabase = { | ||
| rpc: () => ({ | ||
| single: async () => ({ | ||
| data: null, | ||
| error: { message: 'organization lookup failed' }, | ||
| }), | ||
| }), | ||
| } | ||
|
|
||
| let thrown | ||
| try { | ||
| await utils.isAllowedActionOrg(supabase, 'org-id') | ||
| } | ||
| catch (error) { | ||
| thrown = error | ||
| } | ||
|
|
||
| assert(thrown instanceof Error, 'Expected the organization RPC error to be thrown') | ||
| assert(thrown.message.includes('Cannot validate plan'), `Unexpected error: ${thrown.message}`) | ||
| }) | ||
|
|
||
| console.log(`\n📊 Results: ${testsPassed} passed, ${testsFailed} failed`) | ||
| if (testsFailed > 0) | ||
| process.exit(1) |
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.