Skip to content
Open
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
32 changes: 14 additions & 18 deletions packages/api/src/agents/route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -444,33 +444,29 @@ describe('the built-in agent’s settings', () => {
expect(setAgentGrants).toHaveBeenCalledWith({agentId: 'agent-1', connectionId: 'conn-1', actions: {send: false}});
});

test('are not changed by a member a connection is shared with by name, nor by the organization’s owner on another member’s own', async () => {
test('are changed by anyone who uses the connection: a member it is shared with by name, and every member of one shared with everyone', async () => {
getAgent.mockResolvedValue(Ok(builtIn));
const {handleSetAgentGrantsRoute} = await import('./route');
requireViewer.mockResolvedValue(Ok(member));
getConnectionWithCredential.mockResolvedValue(connectionOf('user-1', false));
const sharedByName = await handleSetAgentGrantsRoute(makeRequest(params, {actions: {send: false}}));
requireViewer.mockResolvedValue(Ok(owner));
getConnectionWithCredential.mockResolvedValue(connectionOf('user-2', false));
const othersOwn = await handleSetAgentGrantsRoute(makeRequest(params, {actions: {send: false}}));
const sharedByName = await handleSetAgentGrantsRoute(makeRequest(params, {actions: {send: true}}));
requireViewer.mockResolvedValue(Ok({...member, userId: 'user-3'}));
getConnectionWithCredential.mockResolvedValue(connectionOf('user-2', true));
const sharedWithEveryone = await handleSetAgentGrantsRoute(makeRequest(params, {actions: {send: true}}));

expect(sharedByName.unwrapErr().kind).toBe('forbidden');
expect(othersOwn.unwrapErr().kind).toBe('forbidden');
expect(setAgentGrants).not.toHaveBeenCalled();
expect(sharedByName.isOk()).toBe(true);
expect(sharedWithEveryone.isOk()).toBe(true);
expect(setAgentGrants).toHaveBeenCalledTimes(2);
});

test('are changed on a connection shared with everyone by whoever’s it is and the organization’s owner alone', async () => {
test('are not changed on a connection the person doesn’t use, the organization’s owner included', async () => {
getAgent.mockResolvedValue(Ok(builtIn));
getConnectionWithCredential.mockResolvedValue(connectionOf('user-2', true));
requireViewer.mockResolvedValue(Ok(owner));
getConnectionWithCredential.mockResolvedValue(Ok(null));
const {handleSetAgentGrantsRoute} = await import('./route');
const byOrgOwner = await handleSetAgentGrantsRoute(makeRequest(params, {actions: {send: false}}));
requireViewer.mockResolvedValue(Ok(member));
const byConnectionOwner = await handleSetAgentGrantsRoute(makeRequest(params, {actions: {send: false}}));
requireViewer.mockResolvedValue(Ok({...member, userId: 'user-3'}));
const byAnotherMember = await handleSetAgentGrantsRoute(makeRequest(params, {actions: {send: false}}));
const othersOwn = await handleSetAgentGrantsRoute(makeRequest(params, {actions: {send: true}}));

expect(byOrgOwner.isOk()).toBe(true);
expect(byConnectionOwner.isOk()).toBe(true);
expect(byAnotherMember.unwrapErr().kind).toBe('forbidden');
expect(othersOwn.unwrapErr().kind).toBe('not_found');
expect(setAgentGrants).not.toHaveBeenCalled();
});
});
12 changes: 7 additions & 5 deletions packages/api/src/agents/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {listMemberNames} from '@proxy/db/organization';
import type {MemberViewer} from '@proxy/db/viewer';
import {findAgentProvider} from '@proxy/utils/agent-providers';

import {requireConnection, requireManaged, type StoredConnectionRow} from '../connections/managed';
import {requireConnection, type StoredConnectionRow} from '../connections/managed';
import {refuseUnavailable, unavailableActions} from '../connections/unavailable';
import type {AuthenticatedRequest} from '../server/middleware/require-auth';
import {parseActionChanges, requireIntegration} from '../utils/connection-actions';
Expand Down Expand Up @@ -179,7 +179,9 @@ export function handleShareAgentRoute(request: AuthenticatedRequest): Promise<Re
* refused, and so is turning on one the connection's sign-in didn't cover. A shared agent reaches
* only the connections shared with every member, so a setting on any other is refused for it. The
* built-in agent, which every persona of the Chat page acts through as whoever talks to it, reaches
* every connection: its settings on one are changed by whoever changes the connection's defaults.
* every connection: its settings on one are changed by anyone who uses the connection, and hold for
* everyone's Chat on it. The connection's defaults, which every agent follows, its owner's too, stay
* whoever's it is to change (connections/route.ts).
*/
export function handleSetAgentGrantsRoute(request: AuthenticatedRequest): Promise<Result<AgentLogin, ApiError>> {
return Do(async ($) => {
Expand All @@ -196,12 +198,12 @@ export function handleSetAgentGrantsRoute(request: AuthenticatedRequest): Promis
});
}

// The connection the viewer may give the agent settings on: for the built-in agent, one whose
// defaults they change; for any other, one it reaches, of an agent they change.
// The connection the viewer may give the agent settings on: for the built-in agent, one they use;
// for any other, one it reaches, of an agent they change.
function requireGrantable(viewer: OrgViewer, agent: AgentRow, connectionId: string): Promise<Result<StoredConnectionRow, ApiError>> {
return Do(async ($) => {
if (isBuiltIn(agent)) {
return $(await requireManaged(viewer, connectionId));
return $(await requireConnection(viewer, connectionId));
}
if (!managesAgent(viewer, agent)) {
return $(Err(ApiErr.forbidden()));
Expand Down
49 changes: 40 additions & 9 deletions packages/web/src/components/access-grid.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type {Action} from '@proxy/integrations';
import {afterEach, describe, expect, mock, test} from 'bun:test';
import {cleanup, fireEvent, render, screen, waitFor, within} from '@testing-library/react';
import {createMemoryRouter, RouterProvider} from 'react-router';
import {AccessGrid, type GridAgent} from '@/components/access-grid';
import {AccessGrid, DEFAULT_LOCKED, type GridAgent} from '@/components/access-grid';

afterEach(() => {
cleanup();
Expand Down Expand Up @@ -40,8 +40,8 @@ function agent(id: string, name: string): AgentLogin {
}

// Claude, which may send though the default says no, and Scout, which follows every default.
const claude: GridAgent = {agent: agent('agent-1', 'Claude'), own: {send: true}, mayChange: true};
const scout: GridAgent = {agent: agent('agent-2', 'Scout'), own: {}, mayChange: true};
const claude: GridAgent = {agent: agent('agent-1', 'Claude'), own: {send: true}, setBy: null};
const scout: GridAgent = {agent: agent('agent-2', 'Scout'), own: {}, setBy: null};

function renderGrid(props: Partial<Parameters<typeof AccessGrid>[0]> = {}) {
const onChangeDefaults = mock(async (_actions: Record<string, boolean>) => {});
Expand All @@ -59,7 +59,7 @@ function renderGrid(props: Partial<Parameters<typeof AccessGrid>[0]> = {}) {
agents={[claude, scout]}
unavailable={[]}
signInNeeded={false}
mayChangeDefaults
defaultsSetBy={null}
onChangeDefaults={onChangeDefaults}
onChangeAgent={onChangeAgent}
{...props}
Expand Down Expand Up @@ -272,12 +272,43 @@ describe('AccessGrid', () => {
}
});

test('someone who may change neither the defaults nor an agent sees the grid and changes nothing', () => {
renderGrid({mayChangeDefaults: false, agents: [{...claude, mayChange: false}, scout]});
test('someone who may change neither the defaults nor an agent sees those boxes as they are, unfaded, and changes nothing', () => {
const {onChangeDefaults, onChangeAgent} = renderGrid({
defaultsSetBy: 'Dana',
agents: [{...claude, setBy: 'Dana'}, scout],
});

expect(box('Read by default').hasAttribute('data-readonly')).toBe(true);
expect(box('Read by default').hasAttribute('data-disabled')).toBe(false);
expect(box('Read for Claude').hasAttribute('data-readonly')).toBe(true);
expect(box('Read for Scout').hasAttribute('data-readonly')).toBe(false);
fireEvent.click(box('Read by default'));
fireEvent.click(box('Read for Claude'));
expect(ticked('Read by default')).toBe('true');
expect(onChangeDefaults).not.toHaveBeenCalled();
expect(onChangeAgent).not.toHaveBeenCalled();
});

test('a column the person may not change has a lock by its name, and its boxes say on hover who sets them', async () => {
renderGrid({defaultsSetBy: 'Dana', agents: [{...claude, setBy: 'Dana'}, scout]});

expect(screen.getAllByLabelText(DEFAULT_LOCKED)).toHaveLength(1);
expect(screen.getAllByLabelText('Set by Dana')).toHaveLength(1);
fireEvent.focus(box('Read by default'));
expect(await screen.findByText('Set by Dana')).toBeDefined();
fireEvent.blur(box('Read by default'));

expect(box('Read by default').hasAttribute('data-disabled')).toBe(true);
expect(box('Read for Claude').hasAttribute('data-disabled')).toBe(true);
expect(box('Read for Scout').hasAttribute('data-disabled')).toBe(false);
fireEvent.focus(box('Send for Claude, its own setting'));
expect(await screen.findByText('Overwrites default to on · Set by Dana')).toBeDefined();
});

test('with only their own agents’ columns to change, Enable all turns those on and leaves the defaults', async () => {
const {onChangeDefaults, onChangeAgent} = renderGrid({all: true, defaultsSetBy: 'Dana'});

fireEvent.click(screen.getByRole('button', {name: 'Enable all'}));

await waitFor(() => expect(onChangeAgent).toHaveBeenCalledTimes(2));
expect(onChangeDefaults).not.toHaveBeenCalled();
});

test('with no agent reaching the connection, there is the default alone, and it says so', () => {
Expand Down
Loading
Loading