Repository navigation
feat(auth): GET /api/auth/me/permissions + wire frontend canAccess #922
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
cristim
merged 3 commits into
feat/multicloud-web-frontend
from
feat/me-permissions-endpoint
Jun 3, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| /** | ||
| * Runtime-shape-validation tests for getUserPermissions() (CR #922 F3). | ||
| * | ||
| * canAccess() iterates user.effectivePermissions with | ||
| * `for (const p of user.effectivePermissions)` and reads p.action / | ||
| * p.resource. A non-array `permissions` (or null/non-object entries, | ||
| * or non-string action/resource) would throw outside app.ts's | ||
| * fetch/merge try/catch and crash the bootstrap path. The validator | ||
| * must reject every such shape so the caller falls back to the safe | ||
| * group-membership gating instead. | ||
| */ | ||
| import { getUserPermissions } from '../api/auth'; | ||
|
|
||
| beforeEach(() => { | ||
| global.fetch = jest.fn(); | ||
| localStorage.setItem('auth_token', 'tok'); | ||
| }); | ||
| afterEach(() => { | ||
| jest.restoreAllMocks(); | ||
| localStorage.clear(); | ||
| }); | ||
|
|
||
| function mockFetchOk(body: unknown): void { | ||
| (global.fetch as jest.Mock).mockResolvedValue({ | ||
| ok: true, | ||
| status: 200, | ||
| json: async () => body, | ||
| }); | ||
| } | ||
|
|
||
| describe('getUserPermissions()', () => { | ||
| test('returns parsed UserPermissionsResponse on a well-formed payload', async () => { | ||
| mockFetchOk({ | ||
| permissions: [ | ||
| { action: 'view', resource: 'recommendations' }, | ||
| { action: 'view', resource: 'plans' }, | ||
| ], | ||
| is_admin: false, | ||
| }); | ||
| const res = await getUserPermissions(); | ||
| expect(res.is_admin).toBe(false); | ||
| expect(res.permissions).toEqual([ | ||
| { action: 'view', resource: 'recommendations' }, | ||
| { action: 'view', resource: 'plans' }, | ||
| ]); | ||
| }); | ||
|
|
||
| test('returns is_admin=true with {admin,*} entry on admin payload', async () => { | ||
| mockFetchOk({ | ||
| permissions: [{ action: 'admin', resource: '*' }], | ||
| is_admin: true, | ||
| }); | ||
| const res = await getUserPermissions(); | ||
| expect(res.is_admin).toBe(true); | ||
| expect(res.permissions).toEqual([{ action: 'admin', resource: '*' }]); | ||
| }); | ||
|
|
||
| test('rejects when response is not an object (e.g. string)', async () => { | ||
| mockFetchOk('not-an-object'); | ||
| await expect(getUserPermissions()).rejects.toThrow(/was not an object/); | ||
| }); | ||
|
|
||
| test('rejects when response is null', async () => { | ||
| mockFetchOk(null); | ||
| await expect(getUserPermissions()).rejects.toThrow(/was not an object/); | ||
| }); | ||
|
|
||
| test('rejects when permissions is not an array (e.g. truthy non-array)', async () => { | ||
| mockFetchOk({ permissions: { 0: { action: 'view', resource: 'plans' } }, is_admin: false }); | ||
| await expect(getUserPermissions()).rejects.toThrow(/permissions is not an array/); | ||
| }); | ||
|
|
||
| test('rejects when a permission entry is null', async () => { | ||
| mockFetchOk({ permissions: [null], is_admin: false }); | ||
| await expect(getUserPermissions()).rejects.toThrow(/permissions\[0\] is not an object/); | ||
| }); | ||
|
|
||
| test('rejects when a permission entry is missing action', async () => { | ||
| mockFetchOk({ permissions: [{ resource: 'plans' }], is_admin: false }); | ||
| await expect(getUserPermissions()).rejects.toThrow(/permissions\[0\]\.action is not a string/); | ||
| }); | ||
|
|
||
| test('rejects when a permission entry has non-string resource', async () => { | ||
| mockFetchOk({ permissions: [{ action: 'view', resource: 42 }], is_admin: false }); | ||
| await expect(getUserPermissions()).rejects.toThrow(/permissions\[0\]\.resource is not a string/); | ||
| }); | ||
|
|
||
| test('rejects when is_admin is missing', async () => { | ||
| mockFetchOk({ permissions: [] }); | ||
| await expect(getUserPermissions()).rejects.toThrow(/is_admin is not a boolean/); | ||
| }); | ||
|
|
||
| test('rejects when is_admin is not boolean (e.g. "true" string)', async () => { | ||
| mockFetchOk({ permissions: [], is_admin: 'true' }); | ||
| await expect(getUserPermissions()).rejects.toThrow(/is_admin is not a boolean/); | ||
| }); | ||
| }); |
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
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.