Skip to content

Commit 0966fe2

Browse files
authored
fix: create workspace with optional auth providers (coder#12729)
1 parent c674128 commit 0966fe2

File tree

3 files changed

+45
-6
lines changed

3 files changed

+45
-6
lines changed

site/src/pages/CreateWorkspacePage/CreateWorkspacePage.test.tsx

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,46 @@ describe("CreateWorkspacePage", () => {
233233
);
234234
});
235235

236+
it("optional external auth is optional", async () => {
237+
jest
238+
.spyOn(API, "getWorkspaceQuota")
239+
.mockResolvedValueOnce(MockWorkspaceQuota);
240+
jest
241+
.spyOn(API, "getUsers")
242+
.mockResolvedValueOnce({ users: [MockUser], count: 1 });
243+
jest.spyOn(API, "createWorkspace").mockResolvedValueOnce(MockWorkspace);
244+
jest
245+
.spyOn(API, "getTemplateVersionExternalAuth")
246+
.mockResolvedValue([
247+
{ ...MockTemplateVersionExternalAuthGithub, optional: true },
248+
]);
249+
250+
renderCreateWorkspacePage();
251+
await waitForLoaderToBeRemoved();
252+
253+
const nameField = await screen.findByLabelText(nameLabelText);
254+
// have to use fireEvent b/c userEvent isn't cleaning up properly between tests
255+
fireEvent.change(nameField, {
256+
target: { value: "test" },
257+
});
258+
259+
// Ensure we're not logged in
260+
await screen.findByText("Login with GitHub");
261+
262+
const submitButton = screen.getByText(createWorkspaceText);
263+
await userEvent.click(submitButton);
264+
265+
await waitFor(() =>
266+
expect(API.createWorkspace).toBeCalledWith(
267+
MockUser.organization_ids[0],
268+
MockUser.id,
269+
expect.objectContaining({
270+
...MockWorkspaceRequest,
271+
}),
272+
),
273+
);
274+
});
275+
236276
it("auto create a workspace if uses mode=auto", async () => {
237277
const param = "first_parameter";
238278
const paramValue = "It works!";

site/src/pages/CreateWorkspacePage/CreateWorkspacePageView.stories.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const meta: Meta<typeof CreateWorkspacePageView> = {
2121
template: MockTemplate,
2222
parameters: [],
2323
externalAuth: [],
24+
hasAllRequiredExternalAuth: true,
2425
mode: "form",
2526
permissions: {
2627
createWorkspaceForUser: true,
@@ -134,6 +135,7 @@ export const ExternalAuth: Story = {
134135
optional: true,
135136
},
136137
],
138+
hasAllRequiredExternalAuth: false,
137139
},
138140
};
139141

@@ -159,6 +161,7 @@ export const ExternalAuthError: Story = {
159161
optional: true,
160162
},
161163
],
164+
hasAllRequiredExternalAuth: false,
162165
},
163166
};
164167

site/src/pages/CreateWorkspacePage/CreateWorkspacePageView.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ export const CreateWorkspacePageView: FC<CreateWorkspacePageViewProps> = ({
8484
externalAuth,
8585
externalAuthPollingState,
8686
startPollingExternalAuth,
87+
hasAllRequiredExternalAuth,
8788
parameters,
8889
autofillParameters,
8990
permissions,
@@ -92,7 +93,6 @@ export const CreateWorkspacePageView: FC<CreateWorkspacePageViewProps> = ({
9293
onCancel,
9394
}) => {
9495
const [owner, setOwner] = useState(defaultOwner);
95-
const requiresExternalAuth = externalAuth.some((auth) => !auth.authenticated);
9696
const [suggestedName, setSuggestedName] = useState(() =>
9797
generateWorkspaceName(),
9898
);
@@ -117,7 +117,7 @@ export const CreateWorkspacePageView: FC<CreateWorkspacePageViewProps> = ({
117117
}),
118118
enableReinitialize: true,
119119
onSubmit: (request) => {
120-
if (requiresExternalAuth) {
120+
if (!hasAllRequiredExternalAuth) {
121121
return;
122122
}
123123

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

147-
const hasAllRequiredExternalAuth = externalAuth.every(
148-
(auth) => auth.optional || auth.authenticated,
149-
);
150-
151147
return (
152148
<Margins size="medium">
153149
<PageHeader actions={<Button onClick={onCancel}>Cancel</Button>}>

0 commit comments

Comments
 (0)