-
Notifications
You must be signed in to change notification settings - Fork 874
Fleet UI: Allow users with edit access to add automation from policy details page #45239
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
306b7a9
Fleet UI: Allow users with edit access to add automation from policy …
RachelElysia c7d5b22
lint
RachelElysia 4a7d69e
lint
RachelElysia 926754c
Ensure Unassigned still gets to patch policy
RachelElysia d7ef8ef
Clearer permission decision
RachelElysia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
180 changes: 180 additions & 0 deletions
180
frontend/pages/policies/edit/components/PolicyAutomations/PolicyAutomations.tests.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,180 @@ | ||
| import React from "react"; | ||
| import { render, screen } from "@testing-library/react"; | ||
| import userEvent from "@testing-library/user-event"; | ||
|
|
||
| import { AppContext, initialState } from "context/app"; | ||
| import { IPolicy } from "interfaces/policy"; | ||
| import createMockConfig from "__mocks__/configMock"; | ||
| import PolicyAutomations from "./PolicyAutomations"; | ||
|
|
||
| // Stub SoftwareIcon to avoid asset resolution in tests | ||
| jest.mock("pages/SoftwarePage/components/icons/SoftwareIcon", () => { | ||
| return () => <span data-testid="software-icon" />; | ||
| }); | ||
|
|
||
| const createMockPatchPolicy = (overrides?: Partial<IPolicy>): IPolicy => ({ | ||
| id: 10, | ||
| name: "macOS - Zoom up to date", | ||
| query: "SELECT 1;", | ||
| description: "Checks Zoom is up to date", | ||
| author_id: 1, | ||
| author_name: "Admin", | ||
| author_email: "admin@example.com", | ||
| resolution: "Install the latest version from self-service.", | ||
| platform: "darwin", | ||
| team_id: 1, | ||
| created_at: "2026-01-01T00:00:00Z", | ||
| updated_at: "2026-01-01T00:00:00Z", | ||
| critical: false, | ||
| calendar_events_enabled: false, | ||
| conditional_access_enabled: false, | ||
| type: "patch", | ||
| patch_software: { | ||
| name: "Zoom", | ||
| display_name: "Zoom", | ||
| software_title_id: 42, | ||
| }, | ||
| ...overrides, | ||
| }); | ||
|
|
||
| // Wrap with AppContext so GitOpsModeTooltipWrapper's useGitOpsMode hook works | ||
| const renderWithAppContext = (ui: React.ReactElement) => { | ||
| return render( | ||
| <AppContext.Provider | ||
| value={{ ...initialState, config: createMockConfig() }} | ||
| > | ||
| {ui} | ||
| </AppContext.Provider> | ||
| ); | ||
| }; | ||
|
|
||
| const defaultProps = { | ||
| onAddAutomation: jest.fn(), | ||
| currentAutomatedPolicies: [] as number[], | ||
| }; | ||
|
|
||
| describe("PolicyAutomations", () => { | ||
| describe("CTA card (patch policy with patch_software, no install_software)", () => { | ||
| it("shows the CTA card and Add automation button when canEditPolicy is true", () => { | ||
| renderWithAppContext( | ||
| <PolicyAutomations | ||
| storedPolicy={createMockPatchPolicy()} | ||
| canEditPolicy | ||
| {...defaultProps} | ||
| /> | ||
| ); | ||
|
|
||
| expect(screen.getByText(/Automatically patch Zoom/)).toBeInTheDocument(); | ||
| expect( | ||
| screen.getByRole("button", { name: /Add automation/ }) | ||
| ).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("calls onAddAutomation when the button is clicked", async () => { | ||
| const user = userEvent.setup(); | ||
| const onAddAutomation = jest.fn(); | ||
| renderWithAppContext( | ||
| <PolicyAutomations | ||
| storedPolicy={createMockPatchPolicy()} | ||
| currentAutomatedPolicies={[]} | ||
| canEditPolicy | ||
| onAddAutomation={onAddAutomation} | ||
| /> | ||
| ); | ||
|
|
||
| await user.click(screen.getByRole("button", { name: /Add automation/ })); | ||
| expect(onAddAutomation).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it("does NOT show the CTA card when canEditPolicy is false", () => { | ||
| renderWithAppContext( | ||
| <PolicyAutomations | ||
| storedPolicy={createMockPatchPolicy()} | ||
| canEditPolicy={false} | ||
| {...defaultProps} | ||
| /> | ||
| ); | ||
|
|
||
| expect( | ||
| screen.queryByText(/Automatically patch Zoom/) | ||
| ).not.toBeInTheDocument(); | ||
| expect( | ||
| screen.queryByRole("button", { name: /Add automation/ }) | ||
| ).not.toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("shows 'Adding...' text when isAddingAutomation is true", () => { | ||
| renderWithAppContext( | ||
| <PolicyAutomations | ||
| storedPolicy={createMockPatchPolicy()} | ||
| canEditPolicy | ||
| {...defaultProps} | ||
| isAddingAutomation | ||
| /> | ||
| ); | ||
|
|
||
| expect(screen.getByText("Adding...")).toBeInTheDocument(); | ||
| expect( | ||
| screen.queryByRole("button", { name: /Add automation/ }) | ||
| ).not.toBeInTheDocument(); | ||
| }); | ||
| }); | ||
|
|
||
| describe("CTA card is hidden when conditions are not met", () => { | ||
| it("hides the CTA card for a dynamic (non-patch) policy", () => { | ||
| renderWithAppContext( | ||
| <PolicyAutomations | ||
| storedPolicy={createMockPatchPolicy({ type: "dynamic" })} | ||
| canEditPolicy | ||
| {...defaultProps} | ||
| /> | ||
| ); | ||
|
|
||
| expect(screen.queryByText(/Automatically patch/)).not.toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("hides the CTA card when patch_software is not set", () => { | ||
| renderWithAppContext( | ||
| <PolicyAutomations | ||
| storedPolicy={createMockPatchPolicy({ patch_software: undefined })} | ||
| canEditPolicy | ||
| {...defaultProps} | ||
| /> | ||
| ); | ||
|
|
||
| expect(screen.queryByText(/Automatically patch/)).not.toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("shows the CTA card for a no-team policy (team_id === 0)", () => { | ||
| renderWithAppContext( | ||
| <PolicyAutomations | ||
| storedPolicy={createMockPatchPolicy({ team_id: 0 })} | ||
| canEditPolicy | ||
| {...defaultProps} | ||
| /> | ||
| ); | ||
|
|
||
| expect(screen.getByText(/Automatically patch Zoom/)).toBeInTheDocument(); | ||
| expect( | ||
| screen.getByRole("button", { name: /Add automation/ }) | ||
| ).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("hides the CTA card when install_software is already set", () => { | ||
| renderWithAppContext( | ||
| <PolicyAutomations | ||
| storedPolicy={createMockPatchPolicy({ | ||
| install_software: { | ||
| name: "Zoom", | ||
| software_title_id: 42, | ||
| }, | ||
| })} | ||
| canEditPolicy | ||
| {...defaultProps} | ||
| /> | ||
| ); | ||
|
|
||
| expect(screen.queryByText(/Automatically patch/)).not.toBeInTheDocument(); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixes to work on "Unassigned"