Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

fix: keep SCIM token state correct when the profile refresh fails

Danny8c3fabedfffe
diff --git a/frontend/src/routes/workspace/shared/AADSyncSheet.test.tsx b/frontend/src/routes/workspace/shared/AADSyncSheet.test.tsx
index bfbdca76e7..e710ca4d55 100644
--- a/frontend/src/routes/workspace/shared/AADSyncSheet.test.tsx
+++ b/frontend/src/routes/workspace/shared/AADSyncSheet.test.tsx
@@ -91,11 +91,16 @@ const renderSheet = async (open = true, onClose = vi.fn()) => {
return onClose;
};
+// Exact match, not substring: the i18n key "regenerate-token" contains
+// "generate-token", so a contains() helper conflates the two buttons and makes
+// every generate/regenerate assertion vacuous.
const buttonByText = (text: string) =>
- Array.from(container.querySelectorAll("button")).find((b) =>
- b.textContent?.includes(text)
+ Array.from(container.querySelectorAll("button")).find(
+ (b) => b.textContent?.trim() === text
);
+const entra = (key: string) => `settings.members.entra-sync.${key}`;
+
const tokenInput = () =>
Array.from(container.querySelectorAll("input")).find((i) =>
i.value.startsWith("tok-")
@@ -120,13 +125,13 @@ describe("AADSyncSheet SCIM token", () => {
it("offers generate when no token exists and regenerate once one does", async () => {
await renderSheet();
- expect(buttonByText("generate-token")).toBeTruthy();
- expect(buttonByText("regenerate-token")).toBeFalsy();
+ expect(buttonByText(entra("generate-token"))).toBeTruthy();
+ expect(buttonByText(entra("regenerate-token"))).toBeFalsy();
expect(container.textContent).toContain("token-not-configured");
mocks.tokenConfigured = true;
await renderSheet();
- expect(buttonByText("regenerate-token")).toBeTruthy();
+ expect(buttonByText(entra("regenerate-token"))).toBeTruthy();
expect(container.textContent).toContain("token-configured");
});
@@ -135,7 +140,7 @@ describe("AADSyncSheet SCIM token", () => {
const onClose = await renderSheet();
await act(async () => {
- buttonByText("generate-token")?.click();
+ buttonByText(entra("generate-token"))?.click();
});
// Shown once, with the warning, and the profile is refreshed so the button
@@ -162,7 +167,7 @@ describe("AADSyncSheet SCIM token", () => {
mocks.rotate.mockResolvedValue({ token: "tok-copy-me" });
await renderSheet();
await act(async () => {
- buttonByText("generate-token")?.click();
+ buttonByText(entra("generate-token"))?.click();
});
const copyButtons = Array.from(container.querySelectorAll("button")).filter(
@@ -179,7 +184,7 @@ describe("AADSyncSheet SCIM token", () => {
await renderSheet();
await act(async () => {
- buttonByText("generate-token")?.click();
+ buttonByText(entra("generate-token"))?.click();
});
expect(tokenInput()).toBeUndefined();
@@ -196,29 +201,60 @@ describe("AADSyncSheet SCIM token", () => {
await renderSheet();
await act(async () => {
- buttonByText("generate-token")?.click();
+ buttonByText(entra("generate-token"))?.click();
});
// Second click while the first is pending must be ignored: each rotation
// invalidates the previous token, so racing them can display a dead one.
await act(async () => {
- buttonByText("generate-token")?.click();
+ buttonByText(entra("generate-token"))?.click();
});
expect(mocks.rotate).toHaveBeenCalledTimes(1);
- expect(buttonByText("generate-token")?.disabled).toBe(true);
+ expect(buttonByText(entra("generate-token"))?.disabled).toBe(true);
await act(async () => {
release({ token: "tok-only-one" });
});
expect(tokenInput()?.value).toBe("tok-only-one");
- // Re-enabled once settled, so a later deliberate rotation still works.
- expect(buttonByText("generate-token")?.disabled).toBe(false);
+ // Re-enabled once settled, so a later deliberate rotation still works. The
+ // label has flipped to regenerate, since a token now exists.
+ expect(buttonByText(entra("regenerate-token"))?.disabled).toBe(false);
+ });
+
+ it("treats the token as configured even if the profile refresh fails", async () => {
+ // loadWorkspaceProfile swallows its error and resolves undefined, so the
+ // store can stay stale after a successful rotation. Driving the button off
+ // the store alone would keep offering "Generate", and the next click would
+ // skip the warning and invalidate the token the admin just copied.
+ mocks.rotate.mockResolvedValue({ token: "tok-first" });
+ mocks.loadWorkspaceProfile.mockResolvedValue(undefined);
+ mocks.tokenConfigured = false;
+
+ await renderSheet();
+ await act(async () => {
+ buttonByText(entra("generate-token"))?.click();
+ });
+
+ expect(tokenInput()?.value).toBe("tok-first");
+ expect(buttonByText(entra("regenerate-token"))).toBeTruthy();
+ expect(buttonByText(entra("generate-token"))).toBeFalsy();
+
+ // And a further rotation must go through the confirmation.
+ const confirmSpy = vi
+ .spyOn(window, "confirm")
+ .mockReturnValue(false) as unknown as { mock: { calls: unknown[] } };
+ await act(async () => {
+ buttonByText(entra("regenerate-token"))?.click();
+ });
+ expect(confirmSpy.mock.calls.length).toBe(1);
+ expect(mocks.rotate).toHaveBeenCalledTimes(1);
+ vi.spyOn(window, "confirm").mockRestore();
});
it("hides the rotate control without permission", async () => {
mocks.hasRotatePermission = false;
await renderSheet();
- expect(buttonByText("generate-token")).toBeFalsy();
- expect(buttonByText("regenerate-token")).toBeFalsy();
+ expect(buttonByText(entra("generate-token"))).toBeFalsy();
+ expect(buttonByText(entra("regenerate-token"))).toBeFalsy();
});
});
diff --git a/frontend/src/routes/workspace/shared/AADSyncSheet.tsx b/frontend/src/routes/workspace/shared/AADSyncSheet.tsx
index 5c7bc9cf16..e434ad35aa 100644
--- a/frontend/src/routes/workspace/shared/AADSyncSheet.tsx
+++ b/frontend/src/routes/workspace/shared/AADSyncSheet.tsx
@@ -37,7 +37,7 @@ export function AADSyncSheet({
const externalUrl = useAppStore((s) => s.serverInfo?.externalUrl ?? "");
const workspaceResourceName = useAppStore((s) => s.workspaceResourceName());
- const tokenConfigured = useAppStore(
+ const storedTokenConfigured = useAppStore(
(s) => s.getWorkspaceProfile().directorySyncTokenConfigured
);
@@ -72,6 +72,13 @@ export function AADSyncSheet({
// replaced. One at a time.
const [rotating, setRotating] = useState(false);
+ // A successful rotation means a token exists, whatever the profile refresh
+ // that follows does — loadWorkspaceProfile swallows its error and resolves
+ // undefined, so relying on the store alone would leave the button offering
+ // "Generate" after the first token was minted. The next click would then skip
+ // the regeneration warning and silently invalidate the token just copied.
+ const tokenConfigured = storedTokenConfigured || mintedToken !== "";
+
const handleClose = () => {
setMintedToken("");
onClose();