Skip to content

Commit

Permalink
#9089: deployment key does't take effect if team has an associated aa…
Browse files Browse the repository at this point in the history
… control room (#9094)

* check for deployment key in RequireAuth

* migrate logic to useRequiredPartnerAuth

* add test

* add mock for deployment key
  • Loading branch information
grahamlangford authored Sep 5, 2024
1 parent dee6ee4 commit 26f53c1
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
33 changes: 33 additions & 0 deletions src/auth/useRequiredPartnerAuth.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
mockAuthenticatedMeApiResponse,
} from "@/testUtils/userMock";
import {
deploymentKeyFactory,
meApiResponseFactory,
meOrganizationApiResponseFactory,
meWithPartnerApiResponseFactory,
Expand All @@ -36,14 +37,18 @@ import { integrationConfigFactory } from "@/testUtils/factories/integrationFacto
import { valueToAsyncState } from "@/utils/asyncStateUtils";
import usePartnerAuthData from "@/auth/usePartnerAuthData";
import { Milestones } from "@/data/model/UserMilestone";
import { getDeploymentKey } from "@/auth/deploymentKey";

jest.mock("@/store/enterprise/useManagedStorageState");
jest.mock("@/auth/usePartnerAuthData");
jest.mock("@/auth/deploymentKey");

const useManagedStorageStateMock = jest.mocked(useManagedStorageState);
const usePartnerAuthDataMock = jest.mocked(usePartnerAuthData);
const getDeploymentKeyMock = jest.mocked(getDeploymentKey);

beforeEach(() => {
jest.clearAllMocks();
useManagedStorageStateMock.mockReturnValue({
data: {},
isLoading: false,
Expand Down Expand Up @@ -227,4 +232,32 @@ describe("useRequiredPartnerAuth", () => {
});
});
});

test("does not require integration when a deployment key is provided", async () => {
getDeploymentKeyMock.mockResolvedValue(deploymentKeyFactory());

await mockAuthenticatedMeApiResponse(
meWithPartnerApiResponseFactory({
organization: meOrganizationApiResponseFactory({
control_room: {
id: uuidv4(),
url: "https://control-room.example.com",
},
}),
}),
);

const { result, waitFor } = renderHook(() => useRequiredPartnerAuth());

await waitFor(() => {
expect(result.current).toStrictEqual({
hasPartner: false,
partnerKey: undefined,
requiresIntegration: false,
hasConfiguredIntegration: false,
isLoading: false,
error: undefined,
});
});
});
});
13 changes: 9 additions & 4 deletions src/auth/useRequiredPartnerAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ import { type Nullishable } from "@/utils/nullishUtils";
import { type UserPartner } from "@/data/model/UserPartner";
import { type ControlRoom } from "@/data/model/ControlRoom";
import { Milestones, type UserMilestone } from "@/data/model/UserMilestone";
import useAsyncState from "@/hooks/useAsyncState";
import { getDeploymentKey } from "@/auth/deploymentKey";

/**
* Map from partner keys to partner service IDs
Expand Down Expand Up @@ -165,6 +167,9 @@ function useRequiredPartnerAuth(): RequiredPartnerState {
skip: !isLinked,
});

const { data: deploymentKey, isLoading: isDeploymentKeyLoading } =
useAsyncState(async () => getDeploymentKey(), []);

const localAuth = useSelector(selectAuth);
const {
authIntegrationId: authIntegrationIdOverride,
Expand Down Expand Up @@ -206,9 +211,9 @@ function useRequiredPartnerAuth(): RequiredPartnerState {
hasControlRoom ||
(Boolean(me?.partner) && isCommunityEditionUser);

if (authMethodOverride === "pixiebrix-token") {
// User forced pixiebrix-token authentication via Advanced Settings > Authentication Method. Keep the theme,
// if any, but don't require a partner integration configuration.
if (authMethodOverride === "pixiebrix-token" || deploymentKey) {
// User forced pixiebrix-token authentication via Advanced Settings > Authentication Method or deployment key is set
// Keep the theme, if any, but don't require a partner integration configuration.
return {
hasPartner,
partnerKey: partner?.partnerTheme,
Expand Down Expand Up @@ -261,7 +266,7 @@ function useRequiredPartnerAuth(): RequiredPartnerState {
requiresIntegration &&
Boolean(partnerConfiguration) &&
!isMissingPartnerJwt,
isLoading: isMeLoading || isLinkedLoading,
isLoading: isMeLoading || isLinkedLoading || isDeploymentKeyLoading,
error: meError,
};
}
Expand Down
1 change: 1 addition & 0 deletions src/extensionConsole/pages/onboarding/SetupPage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import reportError from "@/telemetry/reportError";
// Mock notify to assert success/failure because I was having issues writing assertions over the history.
jest.mock("@/utils/notify");
jest.mock("@/telemetry/reportError");
jest.mock("@/auth/deploymentKey");

const notifySuccessMock = jest.mocked(notify.success);
const notifyWarnMock = jest.mocked(notify.warning);
Expand Down

0 comments on commit 26f53c1

Please sign in to comment.