Skip to content

Commit

Permalink
WD-12178 - fix: handle 500 errors for the whoami endpoint (#1778)
Browse files Browse the repository at this point in the history
* fix: handle 500 errors for the whoami endpoint
  • Loading branch information
huwshimi authored Jun 17, 2024
1 parent 7132462 commit 136ccb8
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
29 changes: 29 additions & 0 deletions src/store/middleware/model-poller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,35 @@ describe("model poller", () => {
expect(loginWithBakerySpy).toHaveBeenCalled();
});

it("handles whoami errors when logging in with OIDC", async () => {
vi.spyOn(fakeStore, "getState").mockReturnValue(storeState);
vi.spyOn(fakeStore, "dispatch").mockImplementation((action) => {
if (typeof action === "function") {
// This is a thunk so the action name is not accessible, so this just
// throws on the first thunk that is dispatched. If this test is
// failing then check if another thunk is being dispatched before the
// whoami() thunk.
throw new Error();
}
return { type: "not-whoami" };
});
const loginWithBakerySpy = vi.spyOn(jujuModule, "loginWithBakery");
await runMiddleware(
appActions.connectAndPollControllers({
controllers: [[wsControllerURL, undefined, AuthMethod.OIDC]],
isJuju: true,
poll: 0,
}),
);
expect(loginWithBakerySpy).not.toHaveBeenCalled();
expect(fakeStore.dispatch).toHaveBeenCalledWith(
generalActions.storeLoginError({
wsControllerURL: "wss://example.com",
error: LoginError.WHOAMI,
}),
);
});

it("fetches and stores data", async () => {
const fetchControllerList = vi.spyOn(jujuModule, "fetchControllerList");
conn.facades.modelManager.listModels.mockResolvedValue({
Expand Down
20 changes: 17 additions & 3 deletions src/store/middleware/model-poller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export enum AuditLogsError {
export enum LoginError {
LOG = "Unable to log into controller.",
NO_INFO = "Unable to retrieve controller details.",
WHOAMI = "Unable to check authentication status. You can attempt to log in anyway.",
}

export enum ModelsError {
Expand Down Expand Up @@ -80,9 +81,22 @@ export const modelPollerMiddleware: Middleware<
let error: unknown;
let intervalId: number | null | undefined;
if (authMethod === AuthMethod.OIDC) {
const whoamiResponse = await reduxStore.dispatch(whoami());
const user = unwrapResult(whoamiResponse);
if (!user) {
try {
const whoamiResponse = await reduxStore.dispatch(whoami());
const user = unwrapResult(whoamiResponse);
if (!user) {
// If there's no response that means the user is not
// authenticated, so halt the connection attempt.
return;
}
} catch (error) {
reduxStore.dispatch(
generalActions.storeLoginError({
wsControllerURL,
error: LoginError.WHOAMI,
}),
);
// Halt the connection attempt.
return;
}
}
Expand Down

0 comments on commit 136ccb8

Please sign in to comment.