-
Notifications
You must be signed in to change notification settings - Fork 7
feat(graphql-server): attribute a request to the API's entity, as a whole pair #1775
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
Closed
Closed
Changes from all commits
Commits
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
39 changes: 39 additions & 0 deletions
39
graphql/server/src/middleware/__tests__/graphile-entity-attribution.test.ts
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,39 @@ | ||
| import type { Request } from 'express'; | ||
|
|
||
| describe('graphile context entity attribution', () => { | ||
| const buildContext = ( | ||
| req: Partial<Request>, | ||
| entityType?: string | ||
| ): Record<string, string> => { | ||
| const context: Record<string, string> = {}; | ||
|
|
||
| if (entityType && req.databaseId) { | ||
| context['jwt.claims.entity_id'] = req.databaseId; | ||
| context['jwt.claims.entity_type'] = entityType; | ||
| } | ||
|
|
||
| if (req.databaseId) { | ||
| context['jwt.claims.database_id'] = req.databaseId; | ||
| } | ||
|
|
||
| return context; | ||
| }; | ||
|
|
||
| it('sets the complete entity pair when configured and the request has a database', () => { | ||
| expect(buildContext({ databaseId: 'db-1' }, 'platform')).toEqual({ | ||
| 'jwt.claims.database_id': 'db-1', | ||
| 'jwt.claims.entity_id': 'db-1', | ||
| 'jwt.claims.entity_type': 'platform' | ||
| }); | ||
| }); | ||
|
|
||
| it('sets no entity claims when the API entity type is not configured', () => { | ||
| expect(buildContext({ databaseId: 'db-1' })).toEqual({ | ||
| 'jwt.claims.database_id': 'db-1' | ||
| }); | ||
| }); | ||
|
|
||
| it('sets no entity claims when the request has no database', () => { | ||
| expect(buildContext({}, 'platform')).toEqual({}); | ||
| }); | ||
| }); | ||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 bug · medium
Test duplicates logic instead of exercising middleware
The new test
graphile-entity-attribution.test.tsre-implements the claim-building logic in a localbuildContexthelper (graphile-entity-attribution.test.ts:4-20) that mirrorsgraphile.ts:218-221and asserts only against that copy, never importing or invoking the realbuildPresetcontext builder. Because the test and production code are separate copies, a regression in the middleware's attribution logic (wrong predicate, dropped claim, mis-ordered override) would leave these tests green, so the suite provides no protection for the behavior it claims to verify.📋 Prompt for AI Agents
In
graphql/server/src/middleware/graphile.ts, extract the context-building block (currently theif (req) { ... }that setsjwt.claims.entity_id/entity_type/database_id/api_idetc., around lines 217-243) into an exported pure helper such asexport function buildJwtClaimContext(req, apiEntityType): Record<string,string>, and call it from the grafast context callback. Then rewritegraphql/server/src/middleware/__tests__/graphile-entity-attribution.test.tsto import and assert against that exported helper (ideally plus an integration test that drives a request through the middleware) instead of re-implementing the same logic in a localbuildContext, so the test exercises the real production code path and catches regressions in the attribution logic.