File size: 5,486 Bytes
fd2c364 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | 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 { SecretsService } from "#/api/secrets-service";
import {
getFetchCall,
getJsonBody,
mockJsonResponse,
} from "./fetch-test-utils";
const cloudBackend: Backend = {
id: "prod",
name: "Production",
host: "https://app.all-hands.dev",
apiKey: "bearer-token",
kind: "cloud",
};
const originalFetch = global.fetch;
const fetchMock = vi.fn();
beforeEach(() => {
window.localStorage.clear();
__resetActiveStoreForTests();
setRegisteredBackends([cloudBackend]);
setActiveSelection({ backendId: cloudBackend.id });
fetchMock.mockReset();
fetchMock.mockResolvedValue(mockJsonResponse({}));
global.fetch = fetchMock as typeof fetch;
});
afterEach(() => {
window.localStorage.clear();
__resetActiveStoreForTests();
fetchMock.mockReset();
global.fetch = originalFetch;
});
describe("SecretsService against cloud backend", () => {
it("paginates getSecrets directly and returns the merged list", async () => {
fetchMock
.mockResolvedValueOnce(
mockJsonResponse({
items: [
{ name: "ALPHA", description: "first" },
{ name: "BETA", description: "second" },
],
next_page_id: "BETA",
}),
)
.mockResolvedValueOnce(
mockJsonResponse({
items: [{ name: "GAMMA", description: "third" }],
next_page_id: null,
}),
);
const secrets = await SecretsService.getSecrets();
expect(fetchMock).toHaveBeenCalledTimes(2);
const [firstUrl, firstInit] = getFetchCall(fetchMock, 0);
expect(firstInit).toMatchObject({
method: "GET",
headers: { Authorization: "Bearer bearer-token" },
});
expect(firstUrl).toMatch(
/^https:\/\/app\.all-hands\.dev\/api\/v1\/secrets\/search\?/,
);
expect(firstUrl).not.toContain("page_id=");
const [secondUrl] = getFetchCall(fetchMock, 1);
expect(secondUrl).toContain("page_id=BETA");
expect(secrets.map((s) => s.name)).toEqual(["ALPHA", "BETA", "GAMMA"]);
});
it("creates a secret via direct POST /api/v1/secrets", async () => {
await SecretsService.createSecret(
"OPENAI_API_KEY",
"sk-test",
"OpenAI key",
);
const [url, init] = getFetchCall(fetchMock);
expect(url).toBe(`${cloudBackend.host}/api/v1/secrets`);
expect(init).toMatchObject({
method: "POST",
headers: { Authorization: "Bearer bearer-token" },
});
expect(getJsonBody(init)).toEqual({
name: "OPENAI_API_KEY",
value: "sk-test",
description: "OpenAI key",
});
});
it("updates a secret via PUT /api/v1/secrets/{id} with name + description only", async () => {
// The form/hook calls updateSecret(secretToEdit, newName, description).
await SecretsService.updateSecret("OLD_NAME", "NEW_NAME", "renamed");
const [url, init] = getFetchCall(fetchMock);
expect(url).toBe(`${cloudBackend.host}/api/v1/secrets/OLD_NAME`);
expect(init).toMatchObject({
method: "PUT",
headers: { Authorization: "Bearer bearer-token" },
});
expect(getJsonBody(init)).toEqual({
name: "NEW_NAME",
description: "renamed",
});
// No value supplied, so nothing overwrites the stored one.
expect(fetchMock).toHaveBeenCalledTimes(1);
});
it("overwrites the value with a POST following the PUT", async () => {
// Fresh Response per call: a Response body can only be consumed once.
fetchMock.mockImplementation(() => Promise.resolve(mockJsonResponse({})));
await SecretsService.updateSecret(
"API_KEY",
"API_KEY",
"Demo secret",
"new-value",
);
expect(fetchMock).toHaveBeenCalledTimes(2);
const [putUrl, putInit] = getFetchCall(fetchMock, 0);
expect(putUrl).toBe(`${cloudBackend.host}/api/v1/secrets/API_KEY`);
expect(putInit).toMatchObject({ method: "PUT" });
const [postUrl, postInit] = getFetchCall(fetchMock, 1);
expect(postUrl).toBe(`${cloudBackend.host}/api/v1/secrets`);
expect(postInit).toMatchObject({ method: "POST" });
expect(getJsonBody(postInit)).toEqual({
name: "API_KEY",
value: "new-value",
description: "Demo secret",
});
});
it("deletes a secret via direct DELETE /api/v1/secrets/{id}", async () => {
await SecretsService.deleteSecret("token with space");
const [url, init] = getFetchCall(fetchMock);
expect(url).toBe(
`${cloudBackend.host}/api/v1/secrets/token%20with%20space`,
);
expect(init).toMatchObject({
method: "DELETE",
headers: { Authorization: "Bearer bearer-token" },
});
});
it("treats a delete 404 as success (secret already gone)", async () => {
// Fresh Response per attempt: the retry helper re-fetches and a
// Response body can only be consumed once. Fake timers skip the
// retry backoff sleeps.
fetchMock.mockImplementation(() =>
Promise.resolve(mockJsonResponse({ detail: "Secret not found" }, 404)),
);
vi.useFakeTimers();
try {
const assertion = expect(
SecretsService.deleteSecret("ALREADY_GONE"),
).resolves.toBeUndefined();
await vi.runAllTimersAsync();
await assertion;
} finally {
vi.useRealTimers();
}
});
});
|