Track HiveGrid Cloud planning reference
This commit is contained in:
1 parent
b3d5ea6764
commit
e341c5938a
5784 files changed
+1104929
-3
No files matched your search
@@ -0,0 +1,84 @@
|
||||
import { beforeEach, describe, expect, test, vi } from "vitest";
|
||||
|
||||
vi.mock("../site-types", () => ({
|
||||
getSiteType: vi.fn(),
|
||||
getSiteTypeKey: vi.fn(),
|
||||
}));
|
||||
vi.mock("../db/sites.db.server", () => ({
|
||||
getSiteNameByTypeAndSiteId: vi.fn(),
|
||||
}));
|
||||
vi.mock("~/utils/db/apiKeys.db.server", () => ({
|
||||
createApiKeyLog: vi.fn(),
|
||||
}));
|
||||
vi.mock("@toolctrl/db/api-keys.server", () => ({
|
||||
setLegacyApiKeyLogStatus: vi.fn(),
|
||||
}));
|
||||
vi.mock("../lib/site-settings-utils", () => ({
|
||||
buildCopyWithFallbacks: vi.fn((_siteType: string, copy: Record<string, string>) => copy),
|
||||
}));
|
||||
vi.mock("~/locale/i18next.server", () => ({
|
||||
getTranslations: vi.fn(async () => ({
|
||||
t: (key: string) => key,
|
||||
})),
|
||||
}));
|
||||
|
||||
import { setLegacyApiKeyLogStatus } from "@toolctrl/db/api-keys.server";
|
||||
import { getSiteNameByTypeAndSiteId } from "../db/sites.db.server";
|
||||
import { getSiteType, getSiteTypeKey } from "../site-types";
|
||||
import { createApiKeyLog } from "~/utils/db/apiKeys.db.server";
|
||||
import { withSiteAuthentication } from "./with-site-authentication";
|
||||
|
||||
describe("withSiteAuthentication", () => {
|
||||
beforeEach(() => {
|
||||
vi.mocked(getSiteType).mockReturnValue({ name: "Booking", enabled: true } as never);
|
||||
vi.mocked(getSiteTypeKey).mockReturnValue("site-secret");
|
||||
vi.mocked(createApiKeyLog).mockResolvedValue({ id: "log_1" } as never);
|
||||
vi.mocked(getSiteNameByTypeAndSiteId).mockResolvedValue({
|
||||
id: "site_1",
|
||||
tenantId: "tenant_1",
|
||||
tenant: { name: "Acme", slug: "acme" },
|
||||
siteId: "booking",
|
||||
isActive: true,
|
||||
branding: "{}",
|
||||
copy: "{}",
|
||||
} as never);
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
test("returns and stores generic diagnostics for handler failures", async () => {
|
||||
const response = await withSiteAuthentication({
|
||||
siteType: "booking" as never,
|
||||
request: new Request("https://app.example.test/sites/api?token=raw-token", {
|
||||
headers: { "X-Site-Key": "site-secret", "X-Site-Id": "booking" },
|
||||
}),
|
||||
params: {},
|
||||
handler: async () => {
|
||||
throw new Error("provider token=raw-token failed for tenant tenant_1");
|
||||
},
|
||||
});
|
||||
|
||||
expect(response.status).toBe(500);
|
||||
await expect(response.json()).resolves.toEqual({ success: false, error: "models.site.auth.errors.internalFailure" });
|
||||
expect(setLegacyApiKeyLogStatus).toHaveBeenCalledWith(
|
||||
{ id: "log_1", tenantId: "tenant_1" },
|
||||
expect.objectContaining({
|
||||
status: 500,
|
||||
error: "models.site.auth.errors.internalFailure",
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
test("returns localized authentication errors", async () => {
|
||||
const response = await withSiteAuthentication({
|
||||
siteType: "booking" as never,
|
||||
request: new Request("https://app.example.test/sites/api", {
|
||||
headers: { "X-Site-Id": "booking" },
|
||||
}),
|
||||
params: {},
|
||||
handler: async () => Response.json({ ok: true }),
|
||||
});
|
||||
|
||||
expect(response.status).toBe(401);
|
||||
await expect(response.json()).resolves.toEqual({ error: "models.site.auth.errors.siteKeyRequired" });
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user