Skip to content

Commit

Permalink
WD-14879 - feat: before 0.13.0 release changes (canonical#1803)
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimir-cucu authored Oct 3, 2024
1 parent 775bf5f commit d699700
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 11 deletions.
12 changes: 9 additions & 3 deletions src/juju/jimm/api.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
const jimmEndpoint =
window.jujuDashboardConfig?.controllerAPIEndpoint
.replace("wss://", "https://")
.replace("ws://", "http://")
.replace(/\/api$/, "") ?? "";

export const endpoints = {
login: "/auth/login",
logout: "/auth/logout",
whoami: "/auth/whoami",
login: `${jimmEndpoint}/auth/login`,
logout: `${jimmEndpoint}/auth/logout`,
whoami: `${jimmEndpoint}/auth/whoami`,
};
4 changes: 3 additions & 1 deletion src/juju/jimm/listeners.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ describe("listeners", () => {
expect(global.fetch).not.toHaveBeenCalled();
store.dispatch(pollWhoamiStart());
await waitFor(() =>
expect(global.fetch).toHaveBeenCalledWith(endpoints.whoami),
expect(global.fetch).toHaveBeenCalledWith(endpoints.whoami, {
credentials: "include",
}),
);
});

Expand Down
4 changes: 3 additions & 1 deletion src/juju/jimm/listeners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ export const addWhoamiListener = (
try {
while (true) {
await forkApi.delay(OIDC_POLL_INTERVAL);
const response = await forkApi.pause(fetch(endpoints.whoami));
const response = await forkApi.pause(
fetch(endpoints.whoami, { credentials: "include" }),
);
// Handle the user no longer logged in:
if (response.status === 401 || response.status === 403) {
throw new Error(Label.ERROR_LOGGED_OUT);
Expand Down
16 changes: 12 additions & 4 deletions src/juju/jimm/thunks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ describe("thunks", () => {
it("logout", async () => {
const action = logout();
await action(vi.fn(), vi.fn(), null);
expect(global.fetch).toHaveBeenCalledWith(endpoints.logout);
expect(global.fetch).toHaveBeenCalledWith(endpoints.logout, {
credentials: "include",
});
});

it("logout handles unsuccessful requests", async () => {
Expand All @@ -36,22 +38,28 @@ describe("thunks", () => {
it("whoami returns a user", async () => {
const action = whoami();
await action(vi.fn(), vi.fn(), null);
expect(global.fetch).toHaveBeenCalledWith(endpoints.whoami);
expect(global.fetch).toHaveBeenCalledWith(endpoints.whoami, {
credentials: "include",
});
});

it("whoami handles non-authenticated user", async () => {
fetchMock.mockResponseOnce(JSON.stringify({}), { status: 403 });
const action = whoami();
const response = await action(vi.fn(), vi.fn(), null);
expect(global.fetch).toHaveBeenCalledWith(endpoints.whoami);
expect(global.fetch).toHaveBeenCalledWith(endpoints.whoami, {
credentials: "include",
});
expect(unwrapResult(response)).toBeNull();
});

it("whoami handles non-authenticated user", async () => {
fetchMock.mockResponseOnce(JSON.stringify({}), { status: 401 });
const action = whoami();
const response = await action(vi.fn(), vi.fn(), null);
expect(global.fetch).toHaveBeenCalledWith(endpoints.whoami);
expect(global.fetch).toHaveBeenCalledWith(endpoints.whoami, {
credentials: "include",
});
expect(unwrapResult(response)).toBeNull();
});

Expand Down
4 changes: 2 additions & 2 deletions src/juju/jimm/thunks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const logout = createAsyncThunk<
}
>("jimm/logout", async () => {
try {
const response = await fetch(endpoints.logout);
const response = await fetch(endpoints.logout, { credentials: "include" });
if (!response.ok) {
throw new Error("non-success response");
}
Expand All @@ -37,7 +37,7 @@ export const whoami = createAsyncThunk<
}
>("jimm/whoami", async () => {
try {
const response = await fetch(endpoints.whoami);
const response = await fetch(endpoints.whoami, { credentials: "include" });
if (response.status === 401 || response.status === 403) {
// The user is not authenticated so return null instead of throwing an error.
return null;
Expand Down

0 comments on commit d699700

Please sign in to comment.