-
Notifications
You must be signed in to change notification settings - Fork 2
fix(auth): resolve seat via /v1/auth/status instead of JWT roles #88
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
8278071
fix(auth): resolve seat via /v1/auth/status instead of JWT roles
bergetbjork d13eff9
fix: address AI review — build break, config reuse, timeout, seat gate
bergetbjork 21061ae
fix: wire seatStatusService through WizardDeps (CI build break)
bergetbjork 2820d89
style: eslint --fix (prettier + perfectionist ordering)
bergetbjork 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,63 @@ | ||
| import { afterEach, describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| import { createSeatStatusService } from '../../commands/code/adapters/seat-status.js'; | ||
|
|
||
| const BASE = 'https://api.berget.ai'; | ||
|
|
||
| afterEach(() => { | ||
| vi.unstubAllGlobals(); | ||
| vi.unstubAllEnvs(); | ||
| }); | ||
|
|
||
| describe('seat-status service (GET /v1/auth/status)', () => { | ||
| it('returns seatId and tier when the user has a seat', async () => { | ||
| vi.stubGlobal( | ||
| 'fetch', | ||
| vi.fn().mockResolvedValue({ | ||
| json: async () => ({ authenticated: true, seatId: 168, tier: 'seat_plan_pro' }), | ||
| ok: true, | ||
| }), | ||
| ); | ||
| const svc = createSeatStatusService(); | ||
| expect(await svc.fetchSeatStatus('token')).toEqual({ | ||
| seatId: 168, | ||
| tier: 'seat_plan_pro', | ||
| }); | ||
| expect(vi.mocked(fetch).mock.calls[0]?.[0]).toBe(`${BASE}/v1/auth/status`); | ||
| }); | ||
|
|
||
| it('returns null seat when the user has no seat', async () => { | ||
| vi.stubGlobal( | ||
| 'fetch', | ||
| vi.fn().mockResolvedValue({ | ||
| json: async () => ({ authenticated: true, seatId: null, tier: null }), | ||
| ok: true, | ||
| }), | ||
| ); | ||
| const svc = createSeatStatusService(); | ||
| expect(await svc.fetchSeatStatus('token')).toEqual({ seatId: null, tier: null }); | ||
| }); | ||
|
|
||
| it('returns null on non-OK responses (e.g. 401)', async () => { | ||
| vi.stubGlobal( | ||
| 'fetch', | ||
| vi.fn().mockResolvedValue({ json: async () => ({}), ok: false, status: 401 }), | ||
| ); | ||
| const svc = createSeatStatusService(); | ||
| expect(await svc.fetchSeatStatus('token')).toBeNull(); | ||
| }); | ||
|
|
||
| it('returns null on network errors (never throws)', async () => { | ||
| vi.stubGlobal('fetch', vi.fn().mockRejectedValue(new Error('ENOTFOUND'))); | ||
| const svc = createSeatStatusService(); | ||
| expect(await svc.fetchSeatStatus('token')).toBeNull(); | ||
| }); | ||
|
|
||
| it('honours BERGET_API_URL override', async () => { | ||
| vi.stubEnv('BERGET_API_URL', 'https://api.stage.berget.ai'); | ||
| vi.stubGlobal('fetch', vi.fn().mockResolvedValue({ json: async () => ({}), ok: true })); | ||
| const svc = createSeatStatusService(); | ||
| await svc.fetchSeatStatus('token'); | ||
| expect(vi.mocked(fetch).mock.calls[0]?.[0]).toBe('https://api.stage.berget.ai/v1/auth/status'); | ||
| }); | ||
| }); | ||
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,13 @@ | ||
| import type { SeatStatus, SeatStatusPort } from '../ports/auth-services.js'; | ||
|
|
||
| /** | ||
| * Test double: pass a SeatStatus for "user has a seat", or null for | ||
| * "status could not be verified" (API down). | ||
| */ | ||
| export class FakeSeatStatusService implements SeatStatusPort { | ||
| constructor(private readonly result: null | SeatStatus) {} | ||
|
|
||
| async fetchSeatStatus(): Promise<null | SeatStatus> { | ||
| return this.result; | ||
| } | ||
| } |
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 |
|---|---|---|
|
|
@@ -10,6 +10,7 @@ import { FakeAuthService } from './fake-auth-service.js'; | |
| import { FakeCommandRunner } from './fake-command-runner.js'; | ||
| import { FakeFileStore } from './fake-file-store.js'; | ||
| import { CANCEL, confirm, FakePrompter, multiselect, select } from './fake-prompter.js'; | ||
| import { FakeSeatStatusService } from './fake-seat-status-service.js'; | ||
|
|
||
| const ENV_KEYS = [ | ||
| 'XDG_CONFIG_HOME', | ||
|
|
@@ -48,6 +49,8 @@ const makeDeps = ( | |
| homeDir: '/home/user', | ||
| isTty: overrides.isTty ?? true, | ||
| prompter: overrides.prompter ?? new FakePrompter([]), | ||
| seatStatusService: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ✅ good — Defaulting a null-seat FakeSeatStatusService in makeDeps repairs the three previously crashing runInit tests without weakening their assertions. |
||
| overrides.seatStatusService ?? new FakeSeatStatusService({ seatId: null, tier: null }), | ||
| ...Object.fromEntries( | ||
| Object.entries(overrides).filter( | ||
| ([k]) => | ||
|
|
@@ -575,6 +578,7 @@ describe('runInit', () => { | |
| confirm(true, 'Create'), | ||
| multiselect([]), | ||
| ]), | ||
| seatStatusService: new FakeSeatStatusService({ seatId: 1, tier: 'berget_code' }), | ||
| }); | ||
|
|
||
| await runInit(deps); | ||
|
|
@@ -730,6 +734,7 @@ describe('runInit', () => { | |
| select('project'), | ||
| select('subscription'), | ||
| ]), | ||
| seatStatusService: new FakeSeatStatusService({ seatId: 1, tier: 'berget_code' }), | ||
| }); | ||
|
|
||
| await runInit(deps); | ||
|
|
||
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.
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.
🟡 nit —
vi.unstubAllEnvs()restores stubs but no longer deletes a realBERGET_API_URL(the oldafterEachdid), so the default-URL assertions fail wherever that env var is set — delete it inbeforeEach.