Skip to content
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,236 @@
import { toast } from '@tale/ui/use-toast';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';

import { connectorCredentialAdapter } from '@/app/features/settings/connectors/credential-adapter';
import { useConnectorCredentials } from '@/app/features/settings/connectors/hooks/queries';
import { providerCredentialAdapter } from '@/app/features/settings/providers/credential-adapter';
import { useProviderCredentials } from '@/app/features/settings/providers/hooks/queries';
import { render, screen, waitFor, within } from '@/tests/utils/render';

import { CredentialRowActions } from './credential-row-actions';

vi.mock('@tale/ui/use-toast', async (importOriginal) => {
const actual = await importOriginal<typeof import('@tale/ui/use-toast')>();
const mockToast = vi.fn();
return {
...actual,
toast: mockToast,
useToast: () => ({ toast: mockToast }),
};
});

const ORG = 'org-credentials';
const credential = {
id: 'credential-1',
name: 'Synthetic credential',
authMethod: 'api-key' as const,
status: 'active' as const,
isDefault: false,
createdAt: 1,
updatedAt: 1,
};
const connectorCredential = { ...credential, connectorSlug: 'tavily' };
const providerCredential = {
...credential,
providerSlug: 'openai',
hash: 'synthetic-hash',
};

function ConnectorActions() {
return (
<CredentialRowActions
organizationId={ORG}
credential={connectorCredential}
vendor={null}
siblingCount={0}
adapter={connectorCredentialAdapter}
onEdit={vi.fn()}
/>
);
}

function ProviderActions() {
return (
<CredentialRowActions
organizationId={ORG}
credential={providerCredential}
vendor={null}
siblingCount={0}
adapter={providerCredentialAdapter}
onEdit={vi.fn()}
/>
);
}

function ConnectorList() {
const credentials = useConnectorCredentials(ORG);
return credentials.data?.map((row) => (
<div key={row.id}>
<span>{row.name}</span>
<ConnectorActions />
</div>
));
}

function ProviderList() {
const credentials = useProviderCredentials(ORG);
return credentials.data?.map((row) => (
<div key={row.id}>
<span>{row.name}</span>
<ProviderActions />
</div>
));
}

let queryClient: QueryClient;
let writeAnswer: { status: number; body: unknown };
let deleted: boolean;
let deletePaths: string[];

function json(body: unknown, status = 200) {
return new Response(status === 204 ? null : JSON.stringify(body), {
status,
headers: { 'content-type': 'application/json' },
});
}

beforeEach(() => {
queryClient = new QueryClient({
defaultOptions: { queries: { retry: false } },
});
writeAnswer = { status: 204, body: null };
deleted = false;
deletePaths = [];
window.__ENV__ = { BASE_PATH: '' };
vi.spyOn(window, 'fetch').mockImplementation(async (input, init) => {
const path = new URL(
input instanceof Request ? input.url : String(input),
'http://localhost',
).pathname;
if (init?.method === 'DELETE') {
deletePaths.push(path);
deleted = true;
return json(writeAnswer.body, writeAnswer.status);
}
if (path === '/api/app/connector-credentials') {
return json({ credentials: deleted ? [] : [connectorCredential] });
}
if (path === '/api/app/provider-credentials') {
return json({ credentials: deleted ? [] : [providerCredential] });
}
if (path.endsWith('/dependents')) return json({ usedBy: [] });
return json({ error: 'UNAUTHORIZED' }, 401);
});
vi.spyOn(console, 'error').mockImplementation(() => {});
});

afterEach(() => {
queryClient.clear();
vi.mocked(toast).mockClear();
vi.restoreAllMocks();
delete window.__ENV__;
});

describe.each([
{
surface: 'Connectors',
Actions: ConnectorActions,
List: ConnectorList,
deletePath: '/api/app/connector-credentials/credential-1',
},
{
surface: 'AI providers',
Actions: ProviderActions,
List: ProviderList,
deletePath: '/api/app/provider-credentials/credential-1',
},
])('CredentialRowActions on $surface', ({ Actions, List, deletePath }) => {
async function confirmDelete(showList = false) {
const { user } = render(
<QueryClientProvider client={queryClient}>
{showList ? <List /> : <Actions />}
</QueryClientProvider>,
);
await user.click(
await screen.findByRole('button', {
name: 'Actions for Synthetic credential',
}),
);
await user.click(screen.getByRole('menuitem', { name: 'Delete' }));
const dialog = await screen.findByRole('dialog', {
name: 'Delete credential',
});
await user.click(within(dialog).getByRole('button', { name: 'Delete' }));
await waitFor(() => expect(deletePaths).toEqual([deletePath]));
}

it('quietly closes confirmation for CREDENTIAL_NOT_FOUND', async () => {
writeAnswer = {
status: 404,
body: { error: 'CREDENTIAL_NOT_FOUND', message: 'Credential not found.' },
};

await confirmDelete();

await waitFor(() =>
expect(screen.queryByRole('dialog')).not.toBeInTheDocument(),
);
expect(toast).not.toHaveBeenCalled();
});

it('refetches and drops the vanished row without a destructive toast', async () => {
writeAnswer = {
status: 404,
body: { error: 'CREDENTIAL_NOT_FOUND', message: 'Credential not found.' },
};

await confirmDelete(true);

await waitFor(() =>
expect(
screen.queryByText('Synthetic credential'),
).not.toBeInTheDocument(),
);
expect(toast).not.toHaveBeenCalled();
});

it('keeps a genuine refusal destructive and shows its reason', async () => {
writeAnswer = {
status: 403,
body: {
error: 'ROLE_FORBIDDEN',
message: 'Only admins can delete credentials.',
},
};

await confirmDelete();

await waitFor(() =>
expect(toast).toHaveBeenCalledExactlyOnceWith({
title:
'Could not delete the credential: Only admins can delete credentials.',
variant: 'destructive',
}),
);
expect(
screen.getByRole('dialog', { name: 'Delete credential' }),
).toBeInTheDocument();
});

it('keeps the success feedback for an existing credential', async () => {
await confirmDelete(true);

await waitFor(() =>
expect(toast).toHaveBeenCalledExactlyOnceWith({
title: 'Credential deleted',
}),
);
await waitFor(() =>
expect(
screen.queryByText('Synthetic credential'),
).not.toBeInTheDocument(),
);
expect(screen.queryByRole('dialog')).not.toBeInTheDocument();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { useToast } from '@tale/ui/use-toast';
import { KeyRound, Pencil, Power, Star, Trash2 } from 'lucide-react';

import { useT } from '@/lib/i18n/client';
import { backendErrorCode } from '@/lib/utils/backend-error';

import {
type CredentialAdapter,
Expand Down Expand Up @@ -123,6 +124,10 @@ export function CredentialRowActions<
toast({ title: t('credentials.deletedToast') });
dialogs.setOpen.delete(false);
} catch (err) {
if (backendErrorCode(err) === 'CREDENTIAL_NOT_FOUND') {
dialogs.setOpen.delete(false);
return;
}
console.error(`${adapter.logTag}: delete credential failed`, err);
toast({
title: t('credentials.deleteFailed', {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ Rows for [`connectors`](../../suites/connectors.md) boxes that moved out of the
| Suite | Automated slice | Coverage | Specs |
| --- | --- | --- | --- |
| [connectors](../../suites/connectors.md) | An agent's connector call through the in-sandbox bridge runs the action's live body in the platform process, like an automation's or chat's call, never as a `node -e` program in the agent's own session (whose command line carried the credential's secrets, readable by every process of that session, and which took one of its live-exec places); the bridge's dispatch carries no session. The bridge runs at most four of a session's calls at once (a further one is refused as busy), gives agent calls no file store, and a body past its time limit can no longer reach the outside system | ✅ backend | `backend/domains/connectors/service.runner.test.ts` (the door hands a live call the in-process runner, and a bridge call no file store), `backend/domains/connectors/bridge-routes.test.ts` (live mode, no session, the four-call cap), `lib/connectors/dispatcher.test.ts` (no host request after the time limit), `backend/core/node_only/sandbox/connectors_bridge.test.ts` (the dispatch's exact arguments), `backend/core/tasks/agent_run_host.connector_caller.test.ts` (a task run's call routed through the real hosts with the door mocked) |
| [connectors](../../suites/connectors.md) / [settings](../../suites/settings.md) | On Connectors and AI providers, deleting a credential another session already deleted (`CREDENTIAL_NOT_FOUND`) closes confirmation quietly and refetches the vanished row away; a real permission refusal keeps its reason in one destructive toast, and an existing credential still shows "Credential deleted" (#4081) | ✅ component + hooks | `app/features/settings/credentials/credential-row-actions.test.tsx`, `app/features/settings/connectors/hooks/connector-reads.test.tsx` |
6 changes: 3 additions & 3 deletions services/platform/tests/manual/suites/connectors.md
Original file line number Diff line number Diff line change
Expand Up @@ -326,9 +326,9 @@ Sign in as an owner/admin — the page requires the `developerSettings` ability
block request URL), so A misses the hint; B deletes a credential A still
sees → On A, that row's menu → **Disable** answers one toast
`settings.credentials.updateFailed` naming "Credential not found." and the
row leaves A's table; on another such row, **Delete** → **Delete** answers
`settings.credentials.deleteFailed` once and the confirm closes with the
row. No second click can fail the same way.
row leaves A's table; on another such row, **Delete** → **Delete** closes
the confirm with no toast at all, since the credential is gone as asked, and
the row leaves with it. No second click can fail the same way.
- [ ] `CONN-B12` · **Back waits out a save in flight** — With CONN-F4's
**GitHub** row, Picker → **GitHub** → name it **GitHub** again, fill the
token, throttle the network (DevTools → Network → Slow 4G) and submit →
Expand Down