Skip to content

feat(site): allow creating a workspace without connecting optional external auth providers #12251

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 5 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
96 changes: 51 additions & 45 deletions site/src/pages/CreateWorkspacePage/CreateWorkspacePage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@ import {
MockUser,
MockWorkspace,
MockWorkspaceQuota,
MockWorkspaceRequest,
MockWorkspaceRichParametersRequest,
MockTemplateVersionParameter1,
MockTemplateVersionParameter2,
MockTemplateVersionParameter3,
MockTemplateVersionExternalAuthGithub,
MockOrganization,
MockTemplateVersionExternalAuthGithubAuthenticated,
} from "testHelpers/entities";
import {
renderWithAuth,
waitForLoaderToBeRemoved,
} from "testHelpers/renderHelpers";
import CreateWorkspacePage from "./CreateWorkspacePage";
import { Language } from "./CreateWorkspacePageView";
import { server } from "testHelpers/server";
import { rest } from "msw";

const nameLabelText = "Workspace Name";
const createWorkspaceText = "Create Workspace";
Expand Down Expand Up @@ -185,6 +185,54 @@ describe("CreateWorkspacePage", () => {
expect(validationError).toBeInTheDocument();
});

it("external auth authenticates and succeeds", 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]);

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" },
});

const githubButton = await screen.findByText("Login with GitHub");
await userEvent.click(githubButton);

jest
.spyOn(API, "getTemplateVersionExternalAuth")
.mockResolvedValue([MockTemplateVersionExternalAuthGithubAuthenticated]);

await screen.findByText(
"Authenticated with GitHub",
{},
{ interval: 500, timeout: 5000 },
);

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 Expand Up @@ -251,54 +299,12 @@ describe("CreateWorkspacePage", () => {
route: `/templates/${MockWorkspace.name}/workspace?${params.toString()}`,
});

const warningMessage = await screen.findByRole("alert");
const warningMessage = await screen.findByTestId("duplication-warning");
const nameInput = await screen.findByRole("textbox", {
name: "Workspace Name",
});

expect(warningMessage).toHaveTextContent(Language.duplicationWarning);
expect(nameInput).toHaveValue(`${MockWorkspace.name}-copy`);
});

it("displays the form after connecting to all the external services", async () => {
jest.spyOn(window, "open").mockImplementation(() => null);
const user = userEvent.setup();
const notAuthenticatedExternalAuth = {
...MockTemplateVersionExternalAuthGithub,
authenticated: false,
};
server.use(
rest.get(
"/api/v2/templateversions/:versionId/external-auth",
(req, res, ctx) => {
return res(ctx.json([notAuthenticatedExternalAuth]));
},
),
);
renderCreateWorkspacePage();

await screen.findByText("External authentication");
expect(screen.queryByRole("form")).not.toBeInTheDocument();

const connectButton = screen.getByRole("button", {
name: /connect/i,
});
server.use(
rest.get(
"/api/v2/templateversions/:versionId/external-auth",
(req, res, ctx) => {
const authenticatedExternalAuth = {
...MockTemplateVersionExternalAuthGithub,
authenticated: true,
};
return res(ctx.json([authenticatedExternalAuth]));
},
),
);
await user.click(connectButton);
// TODO: Consider improving the timeout by simulating react-query polling.
// Current implementation could not achieve this, further research is
// needed.
await screen.findByRole("form", undefined, { timeout: 10_000 });
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export const Parameters: Story = {
},
};

export const RequiresExternalAuth: Story = {
export const ExternalAuth: Story = {
args: {
externalAuth: [
{
Expand All @@ -126,6 +126,80 @@ export const RequiresExternalAuth: Story = {
authenticate_url: "",
display_icon: "/icon/gitlab.svg",
display_name: "GitLab",
optional: true,
},
],
},
};

export const ExternalAuthError: Story = {
args: {
error: true,
externalAuth: [
{
id: "github",
type: "github",
authenticated: false,
authenticate_url: "",
display_icon: "/icon/github.svg",
display_name: "GitHub",
},
{
id: "gitlab",
type: "gitlab",
authenticated: false,
authenticate_url: "",
display_icon: "/icon/gitlab.svg",
display_name: "GitLab",
optional: true,
},
],
},
};

export const ExternalAuthAllRequiredConnected: Story = {
args: {
externalAuth: [
{
id: "github",
type: "github",
authenticated: true,
authenticate_url: "",
display_icon: "/icon/github.svg",
display_name: "GitHub",
},
{
id: "gitlab",
type: "gitlab",
authenticated: false,
authenticate_url: "",
display_icon: "/icon/gitlab.svg",
display_name: "GitLab",
optional: true,
},
],
},
};

export const ExternalAuthAllConnected: Story = {
args: {
externalAuth: [
{
id: "github",
type: "github",
authenticated: true,
authenticate_url: "",
display_icon: "/icon/github.svg",
display_name: "GitHub",
},
{
id: "gitlab",
type: "gitlab",
authenticated: true,
authenticate_url: "",
display_icon: "/icon/gitlab.svg",
display_name: "GitLab",
optional: true,
},
],
},
Expand Down
Loading