-
Notifications
You must be signed in to change notification settings - Fork 4
Add centralized invalid session cleanup and wire Dapper to it #1660
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
Open
Oksamies
wants to merge
1
commit into
testing_setup
Choose a base branch
from
12-09-add_centralized_invalid_session_cleanup_and_wire_dapper_to_it
base: testing_setup
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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
109 changes: 109 additions & 0 deletions
109
apps/cyberstorm-remix/cyberstorm/utils/__tests__/dapperClientLoaders.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,109 @@ | ||
| import { beforeEach, describe, expect, it, vi } from "vitest"; | ||
|
|
||
| vi.mock("cyberstorm/security/publicEnvVariables", () => ({ | ||
| getSessionTools: vi.fn().mockReturnValue({ | ||
| getConfig: vi.fn().mockReturnValue({ | ||
| apiHost: "http://api.example.invalid", | ||
| sessionId: "sid", | ||
| }), | ||
| clearInvalidSession: vi.fn(), | ||
| }), | ||
| })); | ||
|
|
||
| vi.mock("@thunderstore/dapper-ts", () => ({ | ||
| DapperTs: vi.fn().mockImplementation((configFactory, removeSessionHook) => { | ||
| return { | ||
| __configFactory: configFactory, | ||
| __removeSessionHook: removeSessionHook, | ||
| getCurrentUser: vi.fn(), | ||
| }; | ||
| }), | ||
| })); | ||
|
|
||
| import { ApiError } from "@thunderstore/thunderstore-api"; | ||
| import { DapperTs } from "@thunderstore/dapper-ts"; | ||
| import { getSessionTools } from "cyberstorm/security/publicEnvVariables"; | ||
|
|
||
| import { makeTeamSettingsTabLoader } from "../dapperClientLoaders"; | ||
|
|
||
| describe("dapperClientLoaders", () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| it("calls dataFetcher with dapper and teamName and merges return value", async () => { | ||
| const dataFetcher = vi.fn().mockResolvedValue({ foo: 123 }); | ||
| const loader = makeTeamSettingsTabLoader(dataFetcher); | ||
|
|
||
| const result = await loader({ | ||
| params: { namespaceId: "MyTeam" }, | ||
| } as unknown as { params: { namespaceId: string } }); | ||
|
|
||
| expect(result).toEqual({ teamName: "MyTeam", foo: 123 }); | ||
| expect(dataFetcher).toHaveBeenCalledTimes(1); | ||
| expect(dataFetcher.mock.calls[0][1]).toBe("MyTeam"); | ||
| expect(DapperTs).toHaveBeenCalledTimes(1); | ||
|
|
||
| const dapperArg = dataFetcher.mock.calls[0][0] as unknown as { | ||
| __configFactory: () => unknown; | ||
| __removeSessionHook: () => void; | ||
| }; | ||
| expect(dapperArg.__configFactory()).toEqual({ | ||
| apiHost: "http://api.example.invalid", | ||
| sessionId: "sid", | ||
| }); | ||
|
|
||
| const tools = (getSessionTools as unknown as ReturnType<typeof vi.fn>).mock | ||
| .results[0].value; | ||
| dapperArg.__removeSessionHook(); | ||
| expect(tools.clearInvalidSession).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it("translates ApiError with detail into a Response", async () => { | ||
| const dataFetcher = vi.fn().mockImplementation(() => { | ||
| throw new ApiError({ | ||
| message: "403: Forbidden", | ||
| response: new Response(null, { status: 403, statusText: "Forbidden" }), | ||
| responseJson: { detail: "Nope" }, | ||
| }); | ||
| }); | ||
|
|
||
| const loader = makeTeamSettingsTabLoader(dataFetcher); | ||
|
|
||
| let thrown: unknown; | ||
| try { | ||
| await loader({ | ||
| params: { namespaceId: "MyTeam" }, | ||
| } as unknown as { params: { namespaceId: string } }); | ||
| } catch (e) { | ||
| thrown = e; | ||
| } | ||
|
|
||
| expect(thrown).toBeInstanceOf(Response); | ||
| const res = thrown as Response; | ||
| expect(res.status).toBe(403); | ||
| expect(await res.text()).toBe("Nope"); | ||
| }); | ||
|
|
||
| it("uses response statusText when ApiError has no detail", async () => { | ||
| const dataFetcher = vi.fn().mockImplementation(() => { | ||
| throw new ApiError({ | ||
| message: "404: Not Found", | ||
| response: new Response(null, { status: 404, statusText: "Not Found" }), | ||
| }); | ||
| }); | ||
|
|
||
| const loader = makeTeamSettingsTabLoader(dataFetcher); | ||
|
|
||
| try { | ||
| await loader({ | ||
| params: { namespaceId: "MyTeam" }, | ||
| } as unknown as { params: { namespaceId: string } }); | ||
| } catch (e) { | ||
| const res = e as Response; | ||
| expect(res).toBeInstanceOf(Response); | ||
| expect(res.status).toBe(404); | ||
| expect(await res.text()).toBe("Not Found"); | ||
| } | ||
| }); | ||
| }); |
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.
Check failure
Code scanning / ESLint
Disallow the `any` type Error test