Skip to content
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

WD-8399 - Create add secrets panel #1692

Merged
merged 2 commits into from
Jan 30, 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
9 changes: 8 additions & 1 deletion src/pages/EntityDetails/Model/Secrets/Secrets.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { screen, waitFor } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import configureStore from "redux-mock-store";

import { actions as jujuActions } from "store/juju";
Expand All @@ -18,7 +19,7 @@ import {
import { renderComponent } from "testing/utils";
import urls from "urls";

import Secrets from "./Secrets";
import Secrets, { Label } from "./Secrets";
import { TestId as SecretsTableTestId } from "./SecretsTable/SecretsTable";

const mockStore = configureStore<RootState, unknown>([]);
Expand Down Expand Up @@ -111,4 +112,10 @@ describe("Secrets", () => {
).toBeUndefined();
});
});

it("can open the add secret panel", async () => {
renderComponent(<Secrets />, { state, path, url });
await userEvent.click(screen.getByRole("button", { name: Label.ADD }));
expect(window.location.search).toEqual("?panel=add-secret");
});
});
24 changes: 22 additions & 2 deletions src/pages/EntityDetails/Model/Secrets/Secrets.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Notification } from "@canonical/react-components";
import { Notification, Button } from "@canonical/react-components";
import type { ReactNode } from "react";
import { useEffect } from "react";
import { useParams } from "react-router-dom";

import type { EntityDetailsRoute } from "components/Routes/Routes";
import { useQueryParams } from "hooks/useQueryParams";
import { useListSecrets } from "juju/apiHooks";
import { actions as jujuActions } from "store/juju";
import {
Expand All @@ -15,6 +16,10 @@ import { useAppDispatch, useAppSelector } from "store/store";

import SecretsTable from "./SecretsTable";

export enum Label {
ADD = "Add secret",
}

export enum TestId {
SECRETS_TAB = "secrets-tab",
}
Expand All @@ -30,6 +35,12 @@ const Secrets = () => {
getSecretsErrors(state, modelUUID),
);

const [, setQuery] = useQueryParams<{
panel: string | null;
}>({
panel: null,
});

useListSecrets(userName, modelName);

useEffect(
Expand All @@ -50,7 +61,16 @@ const Secrets = () => {
</Notification>
);
} else {
content = <SecretsTable />;
content = (
<>
<Button
onClick={() => setQuery({ panel: "add-secret" }, { replace: true })}
>
{Label.ADD}
</Button>
<SecretsTable />
</>
);
}

return <div data-testid={TestId.SECRETS_TAB}>{content}</div>;
Expand Down
12 changes: 12 additions & 0 deletions src/panels/AddSecretPanel/AddSecretPanel.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { screen } from "@testing-library/react";

import { renderComponent } from "testing/utils";

import AddSecretPanel, { TestId } from "./AddSecretPanel";

describe("AddSecretPanel", () => {
it("renders", async () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just a placeholder test for when the functionality is added in https://warthogs.atlassian.net/browse/WD-8546.

renderComponent(<AddSecretPanel />);
expect(screen.getByTestId(TestId.PANEL)).toBeInTheDocument();
});
});
89 changes: 89 additions & 0 deletions src/panels/AddSecretPanel/AddSecretPanel.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import {
ActionButton,
Button,
Notification,
} from "@canonical/react-components";
import { Form, Formik } from "formik";
import { useId, useState, useRef } from "react";

import Panel from "components/Panel";
import ScrollOnRender from "components/ScrollOnRender";
import { usePanelQueryParams } from "panels/hooks";

import Fields from "./Fields";
import { RotatePolicy, type FormFields } from "./types";

export enum TestId {
PANEL = "add-secret-panel",
}

export enum Label {
CANCEL = "Cancel",
SUBMIT = "Add secret",
}

const AddSecretPanel = () => {
const scrollArea = useRef<HTMLDivElement>(null);
const formId = useId();
const [, , handleRemovePanelQueryParams] = usePanelQueryParams<{
panel: string | null;
}>({
panel: null,
});
const [inlineError, setInlineError] = useState<string[] | null>(null);
const [saving, setSaving] = useState<boolean>(false);
return (
<>
{inlineError ? (
<ScrollOnRender scrollArea={scrollArea?.current}>

Check warning on line 38 in src/panels/AddSecretPanel/AddSecretPanel.tsx

View check run for this annotation

Codecov / codecov/patch

src/panels/AddSecretPanel/AddSecretPanel.tsx#L38

Added line #L38 was not covered by tests
<Notification severity="negative">{inlineError}</Notification>
</ScrollOnRender>
) : null}
<Panel
data-testid={TestId.PANEL}
drawer={
<>
<Button onClick={handleRemovePanelQueryParams}>
{Label.CANCEL}
</Button>
<ActionButton
appearance="positive"
disabled={saving}
form={formId}
loading={saving}
type="submit"
>
{Label.SUBMIT}
</ActionButton>
</>
}
onRemovePanelQueryParams={handleRemovePanelQueryParams}
ref={scrollArea}
title="Add a secret"
width="narrow"
>
<Formik<FormFields>
initialValues={{
content: "",
description: "",
expiryTime: "",
isBase64: false,
label: "",
rotatePolicy: RotatePolicy.NEVER,
}}
onSubmit={(values) => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is where the secret will be created in https://warthogs.atlassian.net/browse/WD-8546.

setSaving(true);
setInlineError(null);
handleRemovePanelQueryParams();

Check warning on line 77 in src/panels/AddSecretPanel/AddSecretPanel.tsx

View check run for this annotation

Codecov / codecov/patch

src/panels/AddSecretPanel/AddSecretPanel.tsx#L74-L77

Added lines #L74 - L77 were not covered by tests
}}
>
<Form id={formId}>
<Fields />
</Form>
</Formik>
</Panel>
</>
);
};

export default AddSecretPanel;
17 changes: 17 additions & 0 deletions src/panels/AddSecretPanel/Fields/Fields.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { screen } from "@testing-library/react";
import { Formik } from "formik";

import { renderComponent } from "testing/utils";

import Fields from "./Fields";

describe("Fields", () => {
it("renders", async () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just a placeholder test for when the functionality is added in https://warthogs.atlassian.net/browse/WD-8546.

renderComponent(
<Formik initialValues={{}} onSubmit={jest.fn()}>
<Fields />
</Formik>,
);
expect(screen.getByRole("textbox", { name: "Label" })).toBeInTheDocument();
});
});
77 changes: 77 additions & 0 deletions src/panels/AddSecretPanel/Fields/Fields.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { Input, Select, Textarea } from "@canonical/react-components";
import { Field } from "formik";

import { RotatePolicy } from "../types";

export enum Label {
CONTENT = "Content",
DESCRIPTION = "Description",
EXPIRY_TIME = "Expiry time",
IS_BASE_64 = "Is base64 encoded",
LABEL = "Label",
ROTATE_POLICY = "Rotate policy",
}

const Fields = (): JSX.Element => {
return (
<>
<Field
// Have to manually set the id until this issue has been fixed:
// https://github.com/canonical/react-components/issues/957
id={Label.LABEL}
label={Label.LABEL}
name="label"
as={Input}
type="text"
/>
<Field
// Have to manually set the id until this issue has been fixed:
// https://github.com/canonical/react-components/issues/957
id={Label.DESCRIPTION}
label={Label.DESCRIPTION}
name="description"
as={Textarea}
/>
<Field
// Have to manually set the id until this issue has been fixed:
// https://github.com/canonical/react-components/issues/957
id={Label.CONTENT}
label={Label.CONTENT}
name="content"
as={Textarea}
/>
<Field
// Have to manually set the id until this issue has been fixed:
// https://github.com/canonical/react-components/issues/957
id={Label.IS_BASE_64}
label={Label.IS_BASE_64}
name="isBase64"
as={Input}
type="checkbox"
/>
<Field
type="datetime-local"
// Have to manually set the id until this issue has been fixed:
// https://github.com/canonical/react-components/issues/957
id={Label.EXPIRY_TIME}
label={Label.EXPIRY_TIME}
name="expire-time"
as={Input}
/>
<Field
// Have to manually set the id until this issue has been fixed:
// https://github.com/canonical/react-components/issues/957
id={Label.ROTATE_POLICY}
label={Label.ROTATE_POLICY}
name="rotate-policy"
as={Select}
options={Object.values(RotatePolicy).map((option) => ({
label: option,
value: option,
}))}
/>
</>
);
};

export default Fields;
1 change: 1 addition & 0 deletions src/panels/AddSecretPanel/Fields/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from "./Fields";
1 change: 1 addition & 0 deletions src/panels/AddSecretPanel/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from "./AddSecretPanel";
18 changes: 18 additions & 0 deletions src/panels/AddSecretPanel/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export enum RotatePolicy {
NEVER = "never",
HOURLY = "hourly",
DAILY = "daily",
WEEKLY = "weekly",
MONTHLY = "monthly",
QUARTERLY = "quarterly",
YEARLY = "yearly",
}

export type FormFields = {
content: string;
description: string;
expiryTime: string;
isBase64: boolean;
label: string;
rotatePolicy: RotatePolicy;
};
10 changes: 10 additions & 0 deletions src/panels/Panels.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { screen } from "@testing-library/react";
import { renderComponent } from "testing/utils";

import { TestId as ActionsPanelTestId } from "./ActionsPanel/ActionsPanel";
import { TestId as AddSecretPanelTestId } from "./AddSecretPanel/AddSecretPanel";
import { TestId as AuditLogsFilterPanelTestId } from "./AuditLogsFilterPanel/AuditLogsFilterPanel";
import { TestId as CharmsAndActionsPanelTestId } from "./CharmsAndActionsPanel/CharmsAndActionsPanel";
import { TestId as ConfigPanelTestId } from "./ConfigPanel/ConfigPanel";
Expand Down Expand Up @@ -44,4 +45,13 @@ describe("Panels", () => {
await screen.findByTestId(AuditLogsFilterPanelTestId.PANEL),
).toBeInTheDocument();
});

it("can display the add secret panel", async () => {
renderComponent(<Panels />, {
url: "/?panel=add-secret",
});
expect(
await screen.findByTestId(AddSecretPanelTestId.PANEL),
).toBeInTheDocument();
});
});
3 changes: 3 additions & 0 deletions src/panels/Panels.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { AnimatePresence } from "framer-motion";

import { useQueryParams } from "hooks/useQueryParams";
import ActionsPanel from "panels/ActionsPanel/ActionsPanel";
import AddSecretPanel from "panels/AddSecretPanel";
import ShareModel from "panels/ShareModelPanel/ShareModel";

import AuditLogsFilterPanel from "./AuditLogsFilterPanel";
Expand All @@ -25,6 +26,8 @@ export default function Panels() {
return <ConfigPanel />;
case "audit-log-filters":
return <AuditLogsFilterPanel />;
case "add-secret":
return <AddSecretPanel />;
default:
return null;
}
Expand Down
Loading