π OpenHands: AI-Driven Development
feat: only the creator can turn an automation back on (#17190)
Hiep Le8057bf6cd58c
diff --git a/__tests__/components/automations/automation-list-row.permissions.test.tsx b/__tests__/components/automations/automation-list-row.permissions.test.tsxnew file mode 100644index 0000000000..5802bbc4ff--- /dev/null+++ b/__tests__/components/automations/automation-list-row.permissions.test.tsx@@ -0,0 +1,145 @@+import { render, screen } from "@testing-library/react";+import userEvent from "@testing-library/user-event";+import { QueryClient, QueryClientProvider } from "@tanstack/react-query";+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";+import {+ __resetActiveStoreForTests,+ setActiveSelection,+ setRegisteredBackends,+} from "#/api/backend-registry/active-store";+import type { Backend } from "#/api/backend-registry/types";+import {+ getCloudOrganizationMe,+ getCloudOrganizations,+} from "#/api/cloud/organization-service.api";+import { AutomationListRow } from "#/components/features/automations/automation-list-row";+import { ActiveBackendProvider } from "#/contexts/active-backend-context";+import { I18nKey } from "#/i18n/declaration";+import type { Automation } from "#/types/automation";++// `automation-list-row.test.tsx` mocks the permission hooks wholesale. These+// tests run the real hooks against a mocked `/me` service so the+// creator-vs-manager rule for the enabled toggle is exercised end to end.+vi.mock("#/api/cloud/organization-service.api", async (importOriginal) => ({+ ...(await importOriginal<+ typeof import("#/api/cloud/organization-service.api")+ >()),+ getCloudOrganizations: vi.fn(),+ getCloudOrganizationMe: vi.fn(),+}));++vi.mock("#/context/navigation-context", () => ({+ useNavigation: () => ({ navigate: vi.fn(), currentPath: "/" }),+}));++const ORG_ID = "org-1";++const cloudBackend: Backend = {+ id: "cloud-1",+ name: "Production",+ host: "https://app.all-hands.dev",+ apiKey: "bearer-key",+ kind: "cloud",+};++function makeAutomation(overrides: Partial<Automation> = {}): Automation {+ return {+ id: "auto-1",+ name: "Teammate digest",+ prompt: "Summarize",+ trigger: {+ type: "cron",+ schedule: "0 9 * * *",+ schedule_human: "Daily at 09:00",+ },+ enabled: true,+ user_id: "creator-user",+ created_at: "2026-01-01T00:00:00Z",+ updated_at: "2026-01-01T00:00:00Z",+ ...overrides,+ };+}++function renderRow(automation: Automation) {+ const queryClient = new QueryClient({+ defaultOptions: { queries: { retry: false } },+ });+ return render(+ <QueryClientProvider client={queryClient}>+ <ActiveBackendProvider>+ <AutomationListRow+ automation={automation}+ onToggle={vi.fn()}+ onRunNow={vi.fn()}+ onExport={vi.fn()}+ onDelete={vi.fn()}+ />+ </ActiveBackendProvider>+ </QueryClientProvider>,+ );+}++async function openActionsMenu(user: ReturnType<typeof userEvent.setup>) {+ // Run now only renders once the caller's permissions have resolved.+ await screen.findByTestId("automation-run-now-auto-1");+ await user.click(screen.getByLabelText(I18nKey.AUTOMATIONS$ACTIONS_MENU));+}++beforeEach(() => {+ window.localStorage.clear();+ __resetActiveStoreForTests();+ setRegisteredBackends([cloudBackend]);+ setActiveSelection({ backendId: cloudBackend.id, orgId: ORG_ID });+ vi.mocked(getCloudOrganizations).mockResolvedValue({+ items: [],+ currentOrgId: null,+ });+ // A manager (has manage_automations) who did not create the automation.+ vi.mocked(getCloudOrganizationMe).mockResolvedValue({+ orgId: ORG_ID,+ userId: "manager-user",+ role: "admin",+ permissions: ["view_automations", "manage_automations"],+ });+});++afterEach(() => {+ window.localStorage.clear();+ __resetActiveStoreForTests();+});++describe("AutomationListRow β non-creator manager", () => {+ it("offers Turn off and Delete on an enabled automation", async () => {+ // Arrange+ const user = userEvent.setup();+ renderRow(makeAutomation({ enabled: true }));++ // Act+ await openActionsMenu(user);++ // Assert+ expect(+ screen.getByRole("button", { name: I18nKey.AUTOMATIONS$TURN_OFF }),+ ).toBeInTheDocument();+ expect(+ screen.getByRole("button", { name: I18nKey.AUTOMATIONS$DELETE }),+ ).toBeInTheDocument();+ });++ it("hides Turn on but keeps Delete on a disabled automation", async () => {+ // Arrange+ const user = userEvent.setup();+ renderRow(makeAutomation({ enabled: false }));++ // Act+ await openActionsMenu(user);++ // Assert+ expect(+ screen.queryByRole("button", { name: I18nKey.AUTOMATIONS$TURN_ON }),+ ).not.toBeInTheDocument();+ expect(+ screen.getByRole("button", { name: I18nKey.AUTOMATIONS$DELETE }),+ ).toBeInTheDocument();+ });+});diff --git a/__tests__/components/automations/detail/detail-header.test.tsx b/__tests__/components/automations/detail/detail-header.test.tsxnew file mode 100644index 0000000000..6f31537938--- /dev/null+++ b/__tests__/components/automations/detail/detail-header.test.tsx@@ -0,0 +1,65 @@+import { render, screen } from "@testing-library/react";+import userEvent from "@testing-library/user-event";+import { describe, expect, it, vi } from "vitest";+import { DetailHeader } from "#/components/features/automations/detail/detail-header";+import { I18nKey } from "#/i18n/declaration";+import type { Automation } from "#/types/automation";++const disabledAutomation: Automation = {+ id: "auto-1",+ name: "Daily digest",+ prompt: "Summarize",+ trigger: {+ type: "cron",+ schedule: "0 9 * * *",+ schedule_human: "Daily at 09:00",+ },+ enabled: false,+ created_at: "2026-01-01T00:00:00Z",+ updated_at: "2026-01-01T00:00:00Z",+};++function renderHeader(+ props: Partial<React.ComponentProps<typeof DetailHeader>> = {},+) {+ return render(+ <DetailHeader+ automation={disabledAutomation}+ onToggle={vi.fn()}+ onDelete={vi.fn()}+ onExport={vi.fn()}+ onDownloadTarball={vi.fn()}+ {...props}+ />,+ );+}++describe("DetailHeader β enabled toggle gating", () => {+ it("hides the switch and Turn on when canToggle is false but keeps Delete", async () => {+ // Arrange β a manager who did not create this disabled automation.+ const user = userEvent.setup();+ renderHeader({ canManage: true, canToggle: false });++ // Act+ await user.click(screen.getByLabelText(I18nKey.AUTOMATIONS$ACTIONS_MENU));++ // Assert+ expect(screen.queryByRole("switch")).not.toBeInTheDocument();+ expect(+ screen.queryByRole("button", { name: I18nKey.AUTOMATIONS$TURN_ON }),+ ).not.toBeInTheDocument();+ expect(+ screen.getByRole("button", { name: I18nKey.AUTOMATIONS$DELETE }),+ ).toBeInTheDocument();+ });++ it("follows canManage for the switch when canToggle is omitted", () => {+ // Arrange / Act+ renderHeader({ canManage: true });++ // Assert+ expect(+ screen.getByRole("switch", { name: I18nKey.AUTOMATIONS$TURN_ON }),+ ).toBeInTheDocument();+ });+});diff --git a/src/components/features/automations/automation-card.tsx b/src/components/features/automations/automation-card.tsxindex 2cd1b84926..37d6885dd3 100644--- a/src/components/features/automations/automation-card.tsx+++ b/src/components/features/automations/automation-card.tsx@@ -70,6 +70,8 @@ export function AutomationCard({const isOwner = useIsAutomationOwner(automation);// Write actions on a specific automation: manage OR creator (escape hatch).const canManage = hasManagePermission || isOwner;+ // Non-creators may turn an automation off but not back on.+ const canToggle = automation.enabled ? canManage : isOwner;const scheduleLabel =automation.trigger.schedule_human || automation.trigger.type;@@ -86,6 +88,7 @@ export function AutomationCard({automation,t,canManage,+ canToggle,onRunNow,isRunPending,onView: handleView,diff --git a/src/components/features/automations/automation-list-row.tsx b/src/components/features/automations/automation-list-row.tsxindex 92449d188f..a7962f9537 100644--- a/src/components/features/automations/automation-list-row.tsx+++ b/src/components/features/automations/automation-list-row.tsx@@ -66,6 +66,8 @@ export function AutomationListRow({const isOwner = useIsAutomationOwner(automation);// Write actions on a specific automation: manage OR creator (escape hatch).const canManage = hasManagePermission || isOwner;+ // Non-creators may turn an automation off but not back on.+ const canToggle = automation.enabled ? canManage : isOwner;const handleView = () => {navigate?.(`/automations/${automation.id}`);@@ -75,6 +77,7 @@ export function AutomationListRow({automation,t,canManage,+ canToggle,onRunNow,isRunPending,onView: handleView,diff --git a/src/components/features/automations/build-automation-menu-items.tsx b/src/components/features/automations/build-automation-menu-items.tsxindex 9401a239ca..bfe67bf6ee 100644--- a/src/components/features/automations/build-automation-menu-items.tsx+++ b/src/components/features/automations/build-automation-menu-items.tsx@@ -11,6 +11,8 @@ interface BuildAutomationMenuItemsOptions {automation: Automation;t: (key: I18nKey) => string;canManage: boolean;+ /** Whether the caller may flip `enabled`; non-creators may only turn off. */+ canToggle: boolean;onRunNow: (id: string) => void;isRunPending: boolean;onView: () => void;@@ -24,6 +26,7 @@ export function buildAutomationMenuItems({automation,t,canManage,+ canToggle,onRunNow,isRunPending,onView,@@ -62,7 +65,7 @@ export function buildAutomationMenuItems({},]: []),- ...(canManage+ ...(canToggle? [{label: automation.enabled@@ -71,6 +74,10 @@ export function buildAutomationMenuItems({icon: <PowerIcon className="size-4" />,onClick: () => onToggle(automation.id, automation.enabled),},+ ]+ : []),+ ...(canManage+ ? [{label: t(I18nKey.AUTOMATIONS$DELETE),icon: <TrashIcon className="size-4" />,diff --git a/src/components/features/automations/detail/detail-header.tsx b/src/components/features/automations/detail/detail-header.tsxindex a40feaef73..6145e876a6 100644--- a/src/components/features/automations/detail/detail-header.tsx+++ b/src/components/features/automations/detail/detail-header.tsx@@ -25,6 +25,12 @@ interface DetailHeaderProps {isRunningNow?: boolean;/** Whether the caller may mutate this automation (manage perm or owner). */canManage?: boolean;+ /**+ * Whether the caller may flip the enabled switch. Defaults to `canManage`;+ * pass `false` for a disabled automation the caller did not create, since+ * non-creators may only turn automations off.+ */+ canToggle?: boolean;}export function DetailHeader({@@ -37,6 +43,7 @@ export function DetailHeader({onRunNow,isRunningNow = false,canManage = true,+ canToggle = canManage,}: DetailHeaderProps) {const { t } = useTranslation("openhands");@@ -62,7 +69,7 @@ export function DetailHeader({},]: []),- ...(canManage+ ...(canToggle? [{label: automation.enabled@@ -71,6 +78,10 @@ export function DetailHeader({icon: <PowerIcon className="size-4" />,onClick: onToggle,},+ ]+ : []),+ ...(canManage+ ? [{label: t(I18nKey.AUTOMATIONS$DELETE),icon: <TrashIcon className="size-4" />,@@ -103,7 +114,7 @@ export function DetailHeader({: t(I18nKey.AUTOMATIONS$RUN_NOW)}</button>)}- {canManage && (+ {canToggle && (<ToggleSwitchenabled={automation.enabled}label={diff --git a/src/routes/automation-detail.tsx b/src/routes/automation-detail.tsxindex c26b57560c..832ca01b7a 100644--- a/src/routes/automation-detail.tsx+++ b/src/routes/automation-detail.tsx@@ -222,6 +222,8 @@ export default function AutomationDetail() {const canEdit = active.backend.kind === "local";// Write actions on a specific automation: manage OR creator (escape hatch).const canManage = hasManagePermission || isOwner;+ // Non-creators may turn an automation off but not back on.+ const canToggle = automation.enabled ? canManage : isOwner;return (<div className="min-h-full">@@ -240,6 +242,7 @@ export default function AutomationDetail() {onRunNow={handleRunNow}isRunningNow={dispatchMutation.isPending}canManage={canManage}+ canToggle={canToggle}/>{automation.prompt && <PromptSection prompt={automation.prompt} />}<ConfigurationSection automation={automation} />