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

Skip to content

fix: create workspace with optional auth providers #12729

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 22, 2024
Merged
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
40 changes: 40 additions & 0 deletions site/src/pages/CreateWorkspacePage/CreateWorkspacePage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,46 @@ describe("CreateWorkspacePage", () => {
);
});

it("optional external auth is optional", async () => {
jest
.spyOn(API, "getWorkspaceQuota")
.mockResolvedValueOnce(MockWorkspaceQuota);
jest
.spyOn(API, "getUsers")
.mockResolvedValueOnce({ users: [MockUser], count: 1 });
jest.spyOn(API, "createWorkspace").mockResolvedValueOnce(MockWorkspace);
jest
.spyOn(API, "getTemplateVersionExternalAuth")
.mockResolvedValue([
{ ...MockTemplateVersionExternalAuthGithub, optional: true },
]);

renderCreateWorkspacePage();
await waitForLoaderToBeRemoved();

const nameField = await screen.findByLabelText(nameLabelText);
// have to use fireEvent b/c userEvent isn't cleaning up properly between tests
fireEvent.change(nameField, {
target: { value: "test" },
});

// Ensure we're not logged in
await screen.findByText("Login with GitHub");

const submitButton = screen.getByText(createWorkspaceText);
await userEvent.click(submitButton);

await waitFor(() =>
expect(API.createWorkspace).toBeCalledWith(
MockUser.organization_ids[0],
MockUser.id,
expect.objectContaining({
...MockWorkspaceRequest,
}),
),
);
});

it("auto create a workspace if uses mode=auto", async () => {
const param = "first_parameter";
const paramValue = "It works!";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const meta: Meta<typeof CreateWorkspacePageView> = {
template: MockTemplate,
parameters: [],
externalAuth: [],
hasAllRequiredExternalAuth: true,
mode: "form",
permissions: {
createWorkspaceForUser: true,
Expand Down Expand Up @@ -134,6 +135,7 @@ export const ExternalAuth: Story = {
optional: true,
},
],
hasAllRequiredExternalAuth: false,
},
};

Expand All @@ -159,6 +161,7 @@ export const ExternalAuthError: Story = {
optional: true,
},
],
hasAllRequiredExternalAuth: false,
},
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export const CreateWorkspacePageView: FC<CreateWorkspacePageViewProps> = ({
externalAuth,
externalAuthPollingState,
startPollingExternalAuth,
hasAllRequiredExternalAuth,
parameters,
autofillParameters,
permissions,
Expand All @@ -92,7 +93,6 @@ export const CreateWorkspacePageView: FC<CreateWorkspacePageViewProps> = ({
onCancel,
}) => {
const [owner, setOwner] = useState(defaultOwner);
const requiresExternalAuth = externalAuth.some((auth) => !auth.authenticated);
const [suggestedName, setSuggestedName] = useState(() =>
generateWorkspaceName(),
);
Expand All @@ -117,7 +117,7 @@ export const CreateWorkspacePageView: FC<CreateWorkspacePageViewProps> = ({
}),
enableReinitialize: true,
onSubmit: (request) => {
if (requiresExternalAuth) {
if (!hasAllRequiredExternalAuth) {
return;
}

Expand All @@ -144,10 +144,6 @@ export const CreateWorkspacePageView: FC<CreateWorkspacePageViewProps> = ({
[autofillParameters],
);

const hasAllRequiredExternalAuth = externalAuth.every(
(auth) => auth.optional || auth.authenticated,
);

return (
<Margins size="medium">
<PageHeader actions={<Button onClick={onCancel}>Cancel</Button>}>
Expand Down