Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion site/src/api/queries/externalAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,14 @@ export const validateExternalAuth = (
): UseMutationOptions<ExternalAuth, unknown, string> => {
return {
mutationFn: API.getExternalAuthProvider,
onSuccess: (data, providerId) => {
onSuccess: async (data, providerId) => {
queryClient.setQueryData(["external-auth", providerId], data);
// The list query is the only source of validate_error, so refetch it
// to clear a stale error captured at page load.
await queryClient.invalidateQueries({
queryKey: ["external-auth"],
exact: true,
});
},
};
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import { screen, waitFor } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { HttpResponse, http } from "msw";
import type {
ExternalAuth,
ListUserExternalAuthResponse,
} from "#/api/typesGenerated";
import {
MockGithubAuthLink,
MockGithubExternalProvider,
} from "#/testHelpers/entities";
import { renderWithAuth } from "#/testHelpers/renderHelpers";
import { server } from "#/testHelpers/server";
import ExternalAuthPage from "./ExternalAuthPage";

const validateError = "token failed to validate";

const providerResponse = (authenticated: boolean): ExternalAuth => ({
authenticated,
device: false,
display_name: "GitHub",
supports_revocation: false,
user: null,
app_installable: false,
installations: [],
app_install_url: "",
});

const failedLink = {
...MockGithubAuthLink,
authenticated: false,
validate_error: validateError,
};

describe("ExternalAuthPage", () => {
// Regression test: the list query is the only source of validate_error, and
// validateExternalAuth used to update only the per-provider query. The stale
// error then sat next to the fresh state until a full page reload.
it("refetches the list after a successful Test Validate so a stale error clears", async () => {
let validated = false;
let listRequests = 0;

server.use(
http.get("/api/v2/external-auth", () => {
listRequests++;
return HttpResponse.json<ListUserExternalAuthResponse>({
providers: [MockGithubExternalProvider],
links: [validated ? MockGithubAuthLink : failedLink],
});
}),
http.get("/api/v2/external-auth/github", () => {
// The first request is the row's own provider query. The next one
// is the "Test Validate" mutation, which succeeds.
const response = providerResponse(validated);
validated = true;
return HttpResponse.json(response);
}),
);

renderWithAuth(<ExternalAuthPage />);
await screen.findByText(validateError, { exact: false });

const user = userEvent.setup();
await user.click(await screen.findByRole("button", { name: "Open menu" }));
await user.click(
await screen.findByRole("menuitem", { name: /Test Validate/ }),
);

await waitFor(() => {
expect(
screen.queryByText(validateError, { exact: false }),
).not.toBeInTheDocument();
});
// The list endpoint is hit again by the post-validate invalidation, so
// the displayed link reflects current state.
expect(listRequests).toBeGreaterThan(1);
});

it("does not render a stale error next to an authenticated row", async () => {
server.use(
http.get("/api/v2/external-auth", () => {
return HttpResponse.json<ListUserExternalAuthResponse>({
providers: [MockGithubExternalProvider],
links: [failedLink],
});
}),
// The per-provider query validates live and disagrees with the list.
http.get("/api/v2/external-auth/github", () => {
return HttpResponse.json(providerResponse(true));
}),
);

renderWithAuth(<ExternalAuthPage />);

await screen.findByRole("button", { name: "Authenticated" });
expect(
screen.queryByText(validateError, { exact: false }),
).not.toBeInTheDocument();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export const Failed: Story = {
links: [
{
...MockGithubAuthLink,
authenticated: false,
validate_error: "Failed to refresh token",
},
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ const ExternalAuthRow: FC<ExternalAuthRowProps> = ({
</TooltipContent>
</Tooltip>
)}
{link?.validate_error && (
{!authenticated && link?.validate_error && (
<span>
<span className="pl-[1em] text-content-destructive">Error: </span>
{link?.validate_error}
Expand Down
Loading