Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

Capture API token inventory read-state regressions

pulse-triage[bot]587088e230f0
diff --git a/frontend-modern/src/api/__tests__/security.test.ts b/frontend-modern/src/api/__tests__/security.test.ts
index 52356dbfa1..43cf24443a 100644
--- a/frontend-modern/src/api/__tests__/security.test.ts
+++ b/frontend-modern/src/api/__tests__/security.test.ts
@@ -87,12 +87,51 @@ describe('SecurityAPI', () => {
expect(result).toEqual([]);
});
- it('returns empty array when tokens is malformed', async () => {
- vi.mocked(apiFetchJSON).mockResolvedValueOnce({ tokens: 'bad' } as any);
+ it.each([
+ null,
+ [],
+ {},
+ { tokens: 'bad' },
+ { tokens: {} },
+ { tokens: [null] },
+ { tokens: [{ id: 't1', scopes: 'monitoring:read' }] },
+ { tokens: [{ id: 't1', scopes: [null] }] },
+ { tokens: [{ id: '' }] },
+ ])('rejects an unreadable inventory instead of inventing absence (%j)', async (response) => {
+ vi.mocked(apiFetchJSON).mockResolvedValueOnce(response);
+ await expect(SecurityAPI.listTokens()).rejects.toThrow('Invalid API token inventory.');
+ expect(apiFetchJSON).toHaveBeenCalledTimes(1);
+ });
- const result = await SecurityAPI.listTokens();
+ it('rejects a partial inventory instead of dropping its unreadable member', async () => {
+ vi.mocked(apiFetchJSON).mockResolvedValueOnce({
+ tokens: [
+ {
+ id: 't1',
+ name: 'Known',
+ prefix: 'pmp_',
+ suffix: '1234',
+ createdAt: '',
+ scopes: ['monitoring:read'],
+ },
+ null,
+ ],
+ });
+ await expect(SecurityAPI.listTokens()).rejects.toThrow('Invalid API token inventory.');
+ });
- expect(result).toEqual([]);
+ it.each([undefined, null, [], ['*'], ['future:scope']])(
+ 'preserves supported and legacy scopes (%j)',
+ async (scopes) => {
+ const token = { id: 't1', name: '', prefix: 'pmp_', suffix: '1234', createdAt: '', scopes };
+ vi.mocked(apiFetchJSON).mockResolvedValueOnce({ tokens: [token] });
+ await expect(SecurityAPI.listTokens()).resolves.toEqual([token]);
+ },
+ );
+
+ it('preserves an explicitly empty inventory', async () => {
+ vi.mocked(apiFetchJSON).mockResolvedValueOnce({ tokens: [] });
+ await expect(SecurityAPI.listTokens()).resolves.toEqual([]);
});
});
diff --git a/frontend-modern/src/components/Settings/__tests__/APITokenManager.test.tsx b/frontend-modern/src/components/Settings/__tests__/APITokenManager.test.tsx
index 757e51b4cc..9a419cc955 100644
--- a/frontend-modern/src/components/Settings/__tests__/APITokenManager.test.tsx
+++ b/frontend-modern/src/components/Settings/__tests__/APITokenManager.test.tsx
@@ -301,6 +301,140 @@ describe('APITokenManager', () => {
window.history.pushState({}, '', '/');
});
+ it('keeps the inventory unknown while loading, then admits a confirmed empty list', async () => {
+ let completeRead!: (tokens: APITokenRecord[]) => void;
+ listTokensMock.mockReturnValueOnce(
+ new Promise<APITokenRecord[]>((resolve) => {
+ completeRead = resolve;
+ }),
+ );
+ render(() => <APITokenManager canManage />);
+
+ expect(screen.getByRole('status', { name: 'Loading token inventory' })).toBeInTheDocument();
+ expect(screen.getAllByText('Loading…')).toHaveLength(3);
+ expect(screen.queryByText(/No tokens yet/)).not.toBeInTheDocument();
+ expect(screen.queryByText(/no wildcard credentials detected/i)).not.toBeInTheDocument();
+ expect(screen.queryByText('No full access tokens in this inventory.')).not.toBeInTheDocument();
+
+ completeRead([]);
+ await screen.findByText(/No tokens yet/);
+ expect(screen.getAllByText('0')).toHaveLength(3);
+ expect(screen.getByText('No full access tokens in this inventory.')).toBeInTheDocument();
+ expect(listTokensMock).toHaveBeenCalledTimes(1);
+ });
+
+ it.each([403, 500, undefined])('keeps a failed inventory read unknown (%s)', async (status) => {
+ listTokensMock.mockRejectedValueOnce(
+ Object.assign(new Error('private-proxy-body'), { status }),
+ );
+ mockResources = [
+ makeResource({
+ platformData: { agent: { agentId: 'agent-1', tokenId: 'unread-token' } },
+ }),
+ ];
+ render(() => <APITokenManager canManage />);
+
+ const notice = await screen.findByRole('alert');
+ expect(notice).toHaveTextContent('Unable to load API tokens.');
+ expect(notice).toHaveTextContent('Existing credentials have not been changed.');
+ expect(screen.getAllByText('Unknown')).toHaveLength(3);
+ expect(screen.queryByText(/No tokens yet/)).not.toBeInTheDocument();
+ expect(screen.queryByText(/no wildcard credentials detected/i)).not.toBeInTheDocument();
+ expect(screen.queryByText('No full access tokens in this inventory.')).not.toBeInTheDocument();
+ expect(screen.queryByText(/private-proxy-body/)).not.toBeInTheDocument();
+ expect(screen.queryByRole('button', { name: 'Create one' })).not.toBeInTheDocument();
+ expect(notificationErrorMock).toHaveBeenCalledWith('Unable to load API tokens.');
+ expect(listTokensMock).toHaveBeenCalledTimes(1);
+ expect(markAgentsTokenRevokedMock).not.toHaveBeenCalled();
+ expect(markDockerRuntimesTokenRevokedMock).not.toHaveBeenCalled();
+ expect(createTokenMock).not.toHaveBeenCalled();
+ expect(deleteTokenMock).not.toHaveBeenCalled();
+ expect(updateTokenScopesMock).not.toHaveBeenCalled();
+ expect(renameTokenMock).not.toHaveBeenCalled();
+ });
+
+ it('retries only the inventory read once at a time without losing token drafts', async () => {
+ let completeRetry!: (tokens: APITokenRecord[]) => void;
+ listTokensMock.mockRejectedValueOnce(new Error('unavailable'));
+ listTokensMock.mockReturnValueOnce(
+ new Promise<APITokenRecord[]>((resolve) => {
+ completeRetry = resolve;
+ }),
+ );
+ render(() => <APITokenManager canManage />);
+ const retry = await screen.findByRole('button', { name: 'Retry token inventory' });
+ fireEvent.input(screen.getByLabelText('Token name'), { target: { value: 'Unsent draft' } });
+ fireEvent.click(screen.getByRole('button', { name: 'Kiosk / Monitoring' }));
+
+ fireEvent.click(retry);
+ fireEvent.click(retry);
+ expect(listTokensMock).toHaveBeenCalledTimes(2);
+ expect(screen.getByRole('status', { name: 'Loading token inventory' })).toBeInTheDocument();
+ expect(screen.queryByRole('alert')).not.toBeInTheDocument();
+ expect(screen.queryByText(/No tokens yet/)).not.toBeInTheDocument();
+ expect(screen.getByLabelText('Token name')).toHaveValue('Unsent draft');
+
+ completeRetry([
+ makeToken({ id: 'full-token', name: 'Full token', scopes: ['*'] }),
+ makeToken(),
+ ]);
+ await screen.findAllByText('Full token');
+ expect(screen.queryAllByText('Unknown')).toHaveLength(0);
+ expect(screen.queryByRole('button', { name: 'Retry token inventory' })).not.toBeInTheDocument();
+ expect(screen.getByText(/1 full access token – consider/)).toBeInTheDocument();
+ expect(screen.getByText('Credentials visible to this account')).toBeInTheDocument();
+ expect(screen.getByLabelText('Token name')).toHaveValue('Unsent draft');
+ expect(screen.getByRole('button', { name: 'Kiosk / Monitoring' })).toHaveAttribute(
+ 'aria-pressed',
+ 'true',
+ );
+ expect(listTokensMock).toHaveBeenCalledTimes(2);
+ expect(createTokenMock).not.toHaveBeenCalled();
+ expect(deleteTokenMock).not.toHaveBeenCalled();
+ expect(updateTokenScopesMock).not.toHaveBeenCalled();
+ expect(renameTokenMock).not.toHaveBeenCalled();
+ });
+
+ it('lets a read-only account retry inventory without gaining token writes', async () => {
+ listTokensMock
+ .mockRejectedValueOnce(new Error('unavailable'))
+ .mockResolvedValueOnce([makeToken()]);
+ render(() => <APITokenManager canManage={false} />);
+ fireEvent.click(await screen.findByRole('button', { name: 'Retry token inventory' }));
+ await screen.findAllByText('Runtime token');
+ for (const name of [
+ 'New token',
+ 'Generate new',
+ 'Generate',
+ 'Rename',
+ 'Edit scopes',
+ 'Revoke',
+ ]) {
+ for (const button of screen.getAllByRole('button', { name, exact: true })) {
+ expect(button).toBeDisabled();
+ }
+ }
+ expect(listTokensMock).toHaveBeenCalledTimes(2);
+ expect(createTokenMock).not.toHaveBeenCalled();
+ expect(deleteTokenMock).not.toHaveBeenCalled();
+ expect(updateTokenScopesMock).not.toHaveBeenCalled();
+ expect(renameTokenMock).not.toHaveBeenCalled();
+ });
+
+ it('does not call a partially known inventory complete after creating a token', async () => {
+ listTokensMock.mockRejectedValueOnce(new Error('unavailable'));
+ render(() => <APITokenManager canManage />);
+ await screen.findByRole('alert');
+ fireEvent.click(screen.getByRole('button', { name: 'Kiosk / Monitoring' }));
+ fireEvent.click(screen.getByRole('button', { name: 'Generate' }));
+ await waitFor(() => expect(createTokenMock).toHaveBeenCalledTimes(1));
+ expect(screen.getAllByText('Unknown')).toHaveLength(3);
+ expect(screen.getByRole('alert')).toHaveTextContent('Unable to load API tokens.');
+ expect(screen.queryByTestId('api-token-compact-list')).not.toBeInTheDocument();
+ expect(screen.queryByText('No full access tokens in this inventory.')).not.toBeInTheDocument();
+ expect(listTokensMock).toHaveBeenCalledTimes(1);
+ });
+
it.each(['', 'not-a-date'])(
'does not invent Created for an unusable timestamp (%s)',
async (createdAt) => {