Update hivegrid-cloud reference to latest main
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e98d38e716
commit
7b21524c86
3781 files changed
+373484
-225607
No files matched your search
-70
@@ -1,70 +0,0 @@
|
||||
import type { LoaderFunctionArgs } from "react-router";
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
vi.mock("~/modules/api/services/ApiSpecsService", () => ({
|
||||
default: {
|
||||
generateSpecs: vi.fn(async () => ({
|
||||
openApi: { openapi: "3.1.0" },
|
||||
postmanCollection: { info: {}, item: [] },
|
||||
apiDocsUrl: "https://api.example.com/v1/docs",
|
||||
})),
|
||||
},
|
||||
}));
|
||||
vi.mock("~/utils/db/appConfiguration.db.server", () => ({
|
||||
getAppConfiguration: vi.fn(async () => ({ app: { features: { tenantApiKeys: true } } })),
|
||||
}));
|
||||
vi.mock("~/utils/helpers/.server/PermissionsService", () => ({ verifyUserHasPermission: vi.fn(async () => undefined) }));
|
||||
vi.mock("~/utils/services/.server/urlService", () => ({ getTenantIdFromUrl: vi.fn(async () => "tenant-1") }));
|
||||
vi.mock("~/locale/i18next.server", () => ({
|
||||
getTranslations: vi.fn(async () => ({
|
||||
t: (key: string) => (key === "settings.api.errors.tenantApiKeysNotEnabled" ? "API keys are not enabled" : key),
|
||||
})),
|
||||
}));
|
||||
|
||||
import ApiSpecsService from "~/modules/api/services/ApiSpecsService";
|
||||
import { getTranslations } from "~/locale/i18next.server";
|
||||
import { getAppConfiguration } from "~/utils/db/appConfiguration.db.server";
|
||||
import { verifyUserHasPermission } from "~/utils/helpers/.server/PermissionsService";
|
||||
import { getTenantIdFromUrl } from "~/utils/services/.server/urlService";
|
||||
import { loader } from "./docs";
|
||||
|
||||
describe("tenant API docs route", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
vi.mocked(getTenantIdFromUrl).mockResolvedValue("tenant-1");
|
||||
vi.mocked(getAppConfiguration).mockResolvedValue({ app: { features: { tenantApiKeys: true } } } as never);
|
||||
vi.mocked(verifyUserHasPermission).mockResolvedValue(undefined as never);
|
||||
});
|
||||
|
||||
it("checks tenant permission and feature gate before generating docs", async () => {
|
||||
const request = new Request("https://app.example.com/acme/settings/api/docs");
|
||||
|
||||
const data = await loader({ request, params: { tenant: "acme" }, context: {} } as unknown as LoaderFunctionArgs);
|
||||
|
||||
expect(getTenantIdFromUrl).toHaveBeenCalledWith({ tenant: "acme" });
|
||||
expect(verifyUserHasPermission).toHaveBeenCalledWith(request, "app.settings.apiKeys.view", "tenant-1");
|
||||
expect(getAppConfiguration).toHaveBeenCalledWith({ request });
|
||||
expect(ApiSpecsService.generateSpecs).toHaveBeenCalledWith({ request });
|
||||
expect(data).toMatchObject({ apiSpecs: { apiDocsUrl: "https://api.example.com/v1/docs" } });
|
||||
expect(vi.mocked(verifyUserHasPermission).mock.invocationCallOrder[0]).toBeLessThan(
|
||||
vi.mocked(ApiSpecsService.generateSpecs).mock.invocationCallOrder[0]
|
||||
);
|
||||
});
|
||||
|
||||
it("does not generate docs when tenant API keys are disabled", async () => {
|
||||
vi.mocked(getAppConfiguration).mockResolvedValue({ app: { features: { tenantApiKeys: false } } } as never);
|
||||
|
||||
try {
|
||||
await loader({ request: new Request("https://app.example.com/acme/settings/api/docs"), params: { tenant: "acme" }, context: {} } as never);
|
||||
throw new Error("Expected loader to throw");
|
||||
} catch (error) {
|
||||
expect(error).toBeInstanceOf(Response);
|
||||
const response = error as Response;
|
||||
expect(response.status).toBe(404);
|
||||
await expect(response.json()).resolves.toEqual({ error: "API keys are not enabled" });
|
||||
}
|
||||
|
||||
expect(getTranslations).toHaveBeenCalledWith(expect.any(Request));
|
||||
expect(ApiSpecsService.generateSpecs).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
-61
@@ -1,61 +0,0 @@
|
||||
import { LoaderFunctionArgs, useLoaderData } from "react-router";
|
||||
import { useParams } from "react-router";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import EditPageLayout from "~/components/ui/layouts/EditPageLayout";
|
||||
import { getTranslations } from "~/locale/i18next.server";
|
||||
import ApiSpecs from "~/modules/api/components/ApiSpecs";
|
||||
import ApiSpecsService, { ApiSpecsDto } from "~/modules/api/services/ApiSpecsService";
|
||||
import UrlUtils from "~/utils/app/UrlUtils";
|
||||
import { getAppConfiguration } from "~/utils/db/appConfiguration.db.server";
|
||||
import { verifyUserHasPermission } from "~/utils/helpers/.server/PermissionsService";
|
||||
import { getTenantIdFromUrl } from "~/utils/services/.server/urlService";
|
||||
|
||||
type LoaderData = {
|
||||
apiSpecs: ApiSpecsDto;
|
||||
};
|
||||
export const loader = async ({ request, params }: LoaderFunctionArgs) => {
|
||||
const { t } = await getTranslations(request);
|
||||
const tenantId = await getTenantIdFromUrl(params);
|
||||
await verifyUserHasPermission(request, "app.settings.apiKeys.view", tenantId);
|
||||
const appConfiguration = await getAppConfiguration({ request });
|
||||
if (!appConfiguration.app.features.tenantApiKeys) {
|
||||
throw Response.json({ error: t("settings.api.errors.tenantApiKeysNotEnabled") }, { status: 404 });
|
||||
}
|
||||
const apiSpecs = await ApiSpecsService.generateSpecs({ request });
|
||||
const data: LoaderData = {
|
||||
apiSpecs,
|
||||
};
|
||||
return data;
|
||||
};
|
||||
|
||||
export default function AdminApiDocsRoute() {
|
||||
const { t } = useTranslation();
|
||||
const data = useLoaderData<LoaderData>();
|
||||
const params = useParams();
|
||||
return (
|
||||
<>
|
||||
<EditPageLayout
|
||||
tabs={[
|
||||
{
|
||||
name: t("shared.overview"),
|
||||
routePath: UrlUtils.getModulePath(params, "api"),
|
||||
},
|
||||
{
|
||||
name: t("models.apiCall.plural"),
|
||||
routePath: UrlUtils.getModulePath(params, "api/logs"),
|
||||
},
|
||||
{
|
||||
name: t("models.apiKey.plural"),
|
||||
routePath: UrlUtils.getModulePath(params, "api/keys"),
|
||||
},
|
||||
{
|
||||
name: t("docs.title"),
|
||||
routePath: UrlUtils.getModulePath(params, "api/docs"),
|
||||
},
|
||||
]}
|
||||
>
|
||||
<ApiSpecs item={data.apiSpecs} postmanCollectionUrl={null} showPostmanCollection={false} />
|
||||
</EditPageLayout>
|
||||
</>
|
||||
);
|
||||
}
|
||||
-1
@@ -39,7 +39,6 @@ export default function ApiKeysRoute() {
|
||||
>
|
||||
<ApiKeysTable
|
||||
canCreate={getUserHasPermission(appData, "app.settings.apiKeys.create")}
|
||||
entities={appData.entities}
|
||||
items={data.apiKeys}
|
||||
/>
|
||||
<Outlet />
|
||||
|
||||
+10
-22
@@ -32,9 +32,6 @@ vi.mock("~/utils/db/apiKeys.db.server", () => ({
|
||||
vi.mock("~/utils/db/logs.db.server", () => ({
|
||||
createLog: vi.fn(),
|
||||
}));
|
||||
vi.mock("~/utils/helpers/.server/ApiKeyHelperService", () => ({
|
||||
getApiKeyEntityPermissions: vi.fn(),
|
||||
}));
|
||||
vi.mock("~/modules/events/services/.server/EventsService", () => ({
|
||||
default: { create: vi.fn() },
|
||||
}));
|
||||
@@ -53,7 +50,6 @@ import { countApiKeyLogsForKey, deleteApiKey, getApiKeys, getTenantApiKeyById, r
|
||||
import { createLog } from "~/utils/db/logs.db.server";
|
||||
import { getTenant } from "~/utils/db/tenants.db.server";
|
||||
import { getUser } from "~/utils/db/users.db.server";
|
||||
import { getApiKeyEntityPermissions } from "~/utils/helpers/.server/ApiKeyHelperService";
|
||||
import { verifyUserHasPermission } from "~/utils/helpers/.server/PermissionsService";
|
||||
import { getTenantIdFromUrl } from "~/utils/services/.server/urlService";
|
||||
import { getUserInfo, validateCSRFToken } from "~/utils/session.server";
|
||||
@@ -62,14 +58,12 @@ import { action } from "./$id";
|
||||
const params = { tenant: "acme", id: "api-key-1" };
|
||||
const tenant = { id: "tenant-1", name: "Acme", slug: "acme" };
|
||||
const user = { id: "user-1", email: "ada@example.test" };
|
||||
const entityPermission = { entityId: "entity-1", create: true, read: true, update: false, delete: false };
|
||||
const existingApiKey = {
|
||||
id: "api-key-1",
|
||||
tenantId: "tenant-1",
|
||||
alias: "Primary integration",
|
||||
expires: null,
|
||||
active: true,
|
||||
entities: [entityPermission],
|
||||
};
|
||||
const rotatedApiKey = { ...existingApiKey, key: "tc_live_rotated_plaintext_once" };
|
||||
|
||||
@@ -86,7 +80,6 @@ function createForm(actionName: "edit" | "delete" | "rotate" = "edit", overrides
|
||||
form.set("action", actionName);
|
||||
form.set("alias", "Primary integration updated");
|
||||
form.set("active", "on");
|
||||
form.append("entities[]", JSON.stringify(entityPermission));
|
||||
Object.entries(overrides).forEach(([key, value]) => form.set(key, value));
|
||||
return form;
|
||||
}
|
||||
@@ -120,7 +113,6 @@ describe("tenant API key edit route", () => {
|
||||
vi.mocked(rotateApiKey).mockResolvedValue(rotatedApiKey as never);
|
||||
vi.mocked(deleteApiKey).mockResolvedValue(existingApiKey as never);
|
||||
vi.mocked(countApiKeyLogsForKey).mockResolvedValue(0 as never);
|
||||
vi.mocked(getApiKeyEntityPermissions).mockImplementation(async (permissions) => permissions as never);
|
||||
vi.mocked(EventsService.create).mockResolvedValue(undefined as never);
|
||||
vi.mocked(createLog).mockResolvedValue({} as never);
|
||||
});
|
||||
@@ -191,33 +183,29 @@ describe("tenant API key edit route", () => {
|
||||
expect(createLog).toHaveBeenCalledWith(expect.any(Request), "tenant-1", "settings.apiKeys.deleted", "");
|
||||
});
|
||||
|
||||
it("preserves edited scope changes in logs and events", async () => {
|
||||
it("preserves edited details in logs and events", async () => {
|
||||
const response = await callAction(createForm("edit", { alias: "Primary integration updated" }));
|
||||
|
||||
expect(response.status).toBe(302);
|
||||
expect(updateApiKey).toHaveBeenCalledWith(
|
||||
"api-key-1",
|
||||
{
|
||||
tenantId: "tenant-1",
|
||||
alias: "Primary integration updated",
|
||||
expires: null,
|
||||
active: true,
|
||||
},
|
||||
[entityPermission]
|
||||
);
|
||||
expect(updateApiKey).toHaveBeenCalledWith("api-key-1", {
|
||||
tenantId: "tenant-1",
|
||||
alias: "Primary integration updated",
|
||||
expires: null,
|
||||
active: true,
|
||||
});
|
||||
expect(createLog).toHaveBeenCalledWith(
|
||||
expect.any(Request),
|
||||
"tenant-1",
|
||||
"settings.apiKeys.updated",
|
||||
expect.stringContaining('"entities":[{"entityId":"entity-1","create":true,"read":true,"update":false,"delete":false}]')
|
||||
expect.stringContaining('"alias":"Primary integration updated"')
|
||||
);
|
||||
expect(createLog).toHaveBeenCalledTimes(1);
|
||||
expect(EventsService.create).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
event: "api_key.updated",
|
||||
data: expect.objectContaining({
|
||||
new: expect.objectContaining({ entities: [entityPermission] }),
|
||||
old: expect.objectContaining({ entities: [entityPermission] }),
|
||||
new: expect.objectContaining({ alias: "Primary integration updated", active: true }),
|
||||
old: expect.objectContaining({ alias: "Primary integration" }),
|
||||
}),
|
||||
})
|
||||
);
|
||||
|
||||
+7
-23
@@ -17,7 +17,6 @@ import {
|
||||
} from "~/utils/db/apiKeys.db.server";
|
||||
import { createLog } from "~/utils/db/logs.db.server";
|
||||
import { getUser } from "~/utils/db/users.db.server";
|
||||
import { getApiKeyEntityPermissions } from "~/utils/helpers/.server/ApiKeyHelperService";
|
||||
import EventsService from "~/modules/events/services/.server/EventsService";
|
||||
import { getUserInfo, validateCSRFToken } from "~/utils/session.server";
|
||||
import { verifyUserHasPermission } from "~/utils/helpers/.server/PermissionsService";
|
||||
@@ -69,11 +68,6 @@ export const action: ActionFunction = async ({ request, params }) => {
|
||||
return badRequest({ error: t("shared.notFound") });
|
||||
}
|
||||
if (action === "edit") {
|
||||
const entities: { entityId: string; create: boolean; read: boolean; update: boolean; delete: boolean }[] = form
|
||||
.getAll("entities[]")
|
||||
.map((f: FormDataEntryValue) => {
|
||||
return JSON.parse(f.toString());
|
||||
});
|
||||
let expirationDate: Date | null = null;
|
||||
let expires = form.get("expires")?.toString();
|
||||
if (expires) {
|
||||
@@ -85,17 +79,13 @@ export const action: ActionFunction = async ({ request, params }) => {
|
||||
return badRequest({ error: t("models.apiKey.errors.aliasExists", { alias }) });
|
||||
}
|
||||
const active = Boolean(form.get("active"));
|
||||
await updateApiKey(
|
||||
params.id ?? "",
|
||||
{
|
||||
tenantId: tenantId,
|
||||
alias,
|
||||
expires: expirationDate,
|
||||
active,
|
||||
},
|
||||
entities
|
||||
);
|
||||
await createLog(request, tenantId, "settings.apiKeys.updated", JSON.stringify({ tenantId, alias, expirationDate, active, entities }));
|
||||
await updateApiKey(params.id ?? "", {
|
||||
tenantId: tenantId,
|
||||
alias,
|
||||
expires: expirationDate,
|
||||
active,
|
||||
});
|
||||
await createLog(request, tenantId, "settings.apiKeys.updated", JSON.stringify({ tenantId, alias, expirationDate, active }));
|
||||
await EventsService.create({
|
||||
request,
|
||||
event: "api_key.updated",
|
||||
@@ -107,13 +97,11 @@ export const action: ActionFunction = async ({ request, params }) => {
|
||||
alias: alias,
|
||||
expires: expirationDate,
|
||||
active: active,
|
||||
entities: await getApiKeyEntityPermissions(entities),
|
||||
},
|
||||
old: {
|
||||
alias: existing.alias,
|
||||
expires: existing.expires,
|
||||
active: existing.active,
|
||||
entities: await getApiKeyEntityPermissions(existing.entities),
|
||||
},
|
||||
user: { id: currentUser?.id ?? "", email: currentUser?.email ?? "" },
|
||||
} satisfies ApiKeyUpdatedDto,
|
||||
@@ -133,13 +121,11 @@ export const action: ActionFunction = async ({ request, params }) => {
|
||||
alias: existing.alias,
|
||||
expires: existing.expires,
|
||||
active: existing.active,
|
||||
entities: await getApiKeyEntityPermissions(existing.entities),
|
||||
},
|
||||
old: {
|
||||
alias: existing.alias,
|
||||
expires: existing.expires,
|
||||
active: existing.active,
|
||||
entities: await getApiKeyEntityPermissions(existing.entities),
|
||||
},
|
||||
user: { id: currentUser?.id ?? "", email: currentUser?.email ?? "" },
|
||||
} satisfies ApiKeyUpdatedDto,
|
||||
@@ -163,7 +149,6 @@ export const action: ActionFunction = async ({ request, params }) => {
|
||||
alias: existing.alias,
|
||||
expires: existing.expires,
|
||||
active: existing.active,
|
||||
entities: await getApiKeyEntityPermissions(existing.entities),
|
||||
user: { id: currentUser?.id ?? "", email: currentUser?.email ?? "" },
|
||||
} satisfies ApiKeyDeletedDto,
|
||||
});
|
||||
@@ -183,7 +168,6 @@ export default function ApiEditKeyRoute() {
|
||||
<>
|
||||
<OpenModal className="sm:max-w-xl" onClose={() => navigate(UrlUtils.currentTenantUrl(params, "settings/api/keys"))}>
|
||||
<ApiKeyForm
|
||||
entities={appData.entities}
|
||||
item={data.item}
|
||||
canUpdate={getUserHasPermission(appData, "app.settings.apiKeys.update")}
|
||||
canDelete={getUserHasPermission(appData, "app.settings.apiKeys.delete")}
|
||||
|
||||
+9
-22
@@ -5,11 +5,9 @@ import ApiKeyForm from "~/components/core/apiKeys/ApiKeyForm";
|
||||
import OpenModal from "~/components/ui/modals/OpenModal";
|
||||
import { getTranslations } from "~/locale/i18next.server";
|
||||
import UrlUtils from "~/utils/app/UrlUtils";
|
||||
import { useAppData } from "~/utils/data/useAppData";
|
||||
import { createApiKey, getApiKeyByAlias } from "~/utils/db/apiKeys.db.server";
|
||||
import { createLog } from "~/utils/db/logs.db.server";
|
||||
import { getUser } from "~/utils/db/users.db.server";
|
||||
import { getApiKeyEntityPermissions } from "~/utils/helpers/.server/ApiKeyHelperService";
|
||||
import { verifyUserHasPermission } from "~/utils/helpers/.server/PermissionsService";
|
||||
import EventsService from "~/modules/events/services/.server/EventsService";
|
||||
import { getTenantIdFromUrl } from "~/utils/services/.server/urlService";
|
||||
@@ -47,12 +45,6 @@ export const action: ActionFunction = async ({ request, params }) => {
|
||||
const form = await request.formData();
|
||||
const action = form.get("action")?.toString() ?? "";
|
||||
if (action === "create") {
|
||||
const entities: { entityId: string; create: boolean; read: boolean; update: boolean; delete: boolean }[] = form
|
||||
.getAll("entities[]")
|
||||
.map((f: FormDataEntryValue) => {
|
||||
return JSON.parse(f.toString());
|
||||
});
|
||||
|
||||
let expirationDate: Date | null = null;
|
||||
let expires = form.get("expires")?.toString();
|
||||
if (expires) {
|
||||
@@ -64,16 +56,13 @@ export const action: ActionFunction = async ({ request, params }) => {
|
||||
return badRequest({ error: t("models.apiKey.errors.aliasExists", { alias }) });
|
||||
}
|
||||
const active = Boolean(form.get("active"));
|
||||
const apiKey = await createApiKey(
|
||||
{
|
||||
tenantId: tenantId,
|
||||
createdByUserId: userInfo.userId,
|
||||
alias,
|
||||
expires: expirationDate,
|
||||
active,
|
||||
},
|
||||
entities
|
||||
);
|
||||
const apiKey = await createApiKey({
|
||||
tenantId: tenantId,
|
||||
createdByUserId: userInfo.userId,
|
||||
alias,
|
||||
expires: expirationDate,
|
||||
active,
|
||||
});
|
||||
await EventsService.create({
|
||||
request,
|
||||
event: "api_key.created",
|
||||
@@ -84,11 +73,10 @@ export const action: ActionFunction = async ({ request, params }) => {
|
||||
alias: apiKey.alias,
|
||||
expires: expirationDate,
|
||||
active: active,
|
||||
entities: await getApiKeyEntityPermissions(entities),
|
||||
user: { id: currentUser?.id ?? "", email: currentUser?.email ?? "" },
|
||||
} satisfies ApiKeyCreatedDto,
|
||||
});
|
||||
await createLog(request, tenantId, "settings.apiKeys.created", JSON.stringify({ id: apiKey.id, alias, expirationDate, active, entities }));
|
||||
await createLog(request, tenantId, "settings.apiKeys.created", JSON.stringify({ id: apiKey.id, alias, expirationDate, active }));
|
||||
return Response.json({ apiKey: { key: apiKey.key, alias: apiKey.alias } } satisfies ActionData);
|
||||
} else {
|
||||
return badRequest({ error: t("shared.invalidForm") });
|
||||
@@ -99,11 +87,10 @@ export default function ApiNewKeyRoute() {
|
||||
const navigate = useNavigate();
|
||||
const actionData = useActionData<ActionData>();
|
||||
const params = useParams();
|
||||
const appData = useAppData();
|
||||
return (
|
||||
<>
|
||||
<OpenModal className="sm:max-w-xl" onClose={() => navigate(UrlUtils.currentTenantUrl(params, "settings/api/keys"))}>
|
||||
<ApiKeyForm entities={appData.entities} />
|
||||
<ApiKeyForm />
|
||||
{actionData?.apiKey !== undefined && (
|
||||
<ApiKeyCreatedModal apiKey={actionData?.apiKey} redirectTo={UrlUtils.currentTenantUrl(params, "settings/api/keys")} />
|
||||
)}
|
||||
|
||||
Reference in new issue
Block a user