diff --git a/frontend/src/hooks/useSecrets.test.tsx b/frontend/src/hooks/useSecrets.test.tsx new file mode 100644 index 00000000..ec5daf47 --- /dev/null +++ b/frontend/src/hooks/useSecrets.test.tsx @@ -0,0 +1,74 @@ +import { act } from '@testing-library/react' +import { describe, expect, it } from 'vitest' +import { useSetSecret } from './useSecrets' +import type { SecretScope } from '@/api/secrets' +import { renderHookWithProviders } from '@/test/render' + +const repoScope: SecretScope = { scope: 'repo', repoId: 7 } +const putRoute = '/api/repos/7/:kind/:name' + +function renderSetSecret() { + return renderHookWithProviders(() => useSetSecret(repoScope), { + auth: false, + theme: false, + fetchRoutes: [{ method: 'PUT', path: putRoute, respond: () => ({ status: 204 }) }], + }) +} + +describe('useSetSecret', () => { + it('omits agentHidden when the caller does not provide it', async () => { + const { result, fetchFake } = renderSetSecret() + + await act(async () => { + await result.current.mutateAsync({ kind: 'secret', name: 'FOO', value: 'x' }) + }) + + expect(fetchFake.lastRequest('PUT', putRoute)?.body).toEqual({ value: 'x' }) + }) + + it('sends agentHidden false when the caller provides false', async () => { + const { result, fetchFake } = renderSetSecret() + + await act(async () => { + await result.current.mutateAsync({ kind: 'secret', name: 'FOO', value: 'x', agentHidden: false }) + }) + + expect(fetchFake.lastRequest('PUT', putRoute)?.body).toEqual({ value: 'x', agentHidden: false }) + }) + + it('sends agentHidden and the allowed hosts for a hidden secret', async () => { + const { result, fetchFake } = renderSetSecret() + + await act(async () => { + await result.current.mutateAsync({ + kind: 'secret', + name: 'FOO', + value: 'x', + agentHidden: true, + allowedHosts: ['api.example.com'], + }) + }) + + expect(fetchFake.lastRequest('PUT', putRoute)?.body).toEqual({ + value: 'x', + agentHidden: true, + allowedHosts: ['api.example.com'], + }) + }) + + it('omits agentHidden and the allowed hosts for a variable', async () => { + const { result, fetchFake } = renderSetSecret() + + await act(async () => { + await result.current.mutateAsync({ + kind: 'variable', + name: 'FOO', + value: 'x', + agentHidden: true, + allowedHosts: ['api.example.com'], + }) + }) + + expect(fetchFake.lastRequest('PUT', putRoute)?.body).toEqual({ value: 'x' }) + }) +}) diff --git a/frontend/src/hooks/useSecrets.ts b/frontend/src/hooks/useSecrets.ts index f751fdc0..ac81fe37 100644 --- a/frontend/src/hooks/useSecrets.ts +++ b/frontend/src/hooks/useSecrets.ts @@ -21,8 +21,9 @@ function buildSetSecretRequest({ if (kind !== 'secret') { return environment ? { value, environment } : { value } } - const request: SetSecretRequest = { value, agentHidden: agentHidden ?? false } + const request: SetSecretRequest = { value } if (environment) request.environment = environment + if (agentHidden !== undefined) request.agentHidden = agentHidden if (agentHidden && allowedHosts && allowedHosts.length > 0) request.allowedHosts = allowedHosts return request }