79 lines
2.5 KiB
TypeScript
79 lines
2.5 KiB
TypeScript
import { describe, expect, it } from "@suaveplan/testing/runner";
|
|
import { Hono } from "hono";
|
|
import { TENANT_CODES, TenantError } from "../error-codes/error-codes.js";
|
|
import { headerResolver } from "../resolver/resolver.js";
|
|
import type { Tenant } from "../types/tenant.js";
|
|
import { createTenantMiddleware } from "./middleware.js";
|
|
import { requirePlugin } from "./require-plugin.js";
|
|
|
|
function makeTenant(over: Partial<Tenant> = {}): Tenant {
|
|
return {
|
|
id: "t_1",
|
|
name: "Acme",
|
|
slug: "acme",
|
|
status: "active",
|
|
settings: {},
|
|
enabledPlugins: [],
|
|
domains: [],
|
|
createdAt: new Date(0),
|
|
updatedAt: new Date(0),
|
|
...over,
|
|
};
|
|
}
|
|
|
|
function trap() {
|
|
const app = new Hono();
|
|
app.onError((err, c) =>
|
|
c.json(
|
|
{ code: err instanceof TenantError ? err.code : "x" },
|
|
err instanceof TenantError ? 404 : 500
|
|
)
|
|
);
|
|
return app;
|
|
}
|
|
|
|
describe("requirePlugin", () => {
|
|
it("raises when no tenant resolved", { timeout: 5000 }, async () => {
|
|
const app = trap();
|
|
app.get("/", requirePlugin("payment"), (c) => c.text("never"));
|
|
const res = await app.request("/");
|
|
const body = (await res.json()) as { code: string };
|
|
expect(body.code).toBe(TENANT_CODES.NOT_FOUND);
|
|
});
|
|
|
|
it("raises when plugin not enabled", { timeout: 5000 }, async () => {
|
|
const app = trap();
|
|
app.use(
|
|
"*",
|
|
createTenantMiddleware({
|
|
resolver: headerResolver("x-tenant-id", async () =>
|
|
makeTenant({ enabledPlugins: [] })
|
|
),
|
|
})
|
|
);
|
|
app.get("/", requirePlugin("payment"), (c) => c.text("never"));
|
|
const res = await app.request("/", {
|
|
headers: { "x-tenant-id": "acme" },
|
|
});
|
|
const body = (await res.json()) as { code: string };
|
|
expect(body.code).toBe(TENANT_CODES.INVALID_INPUT);
|
|
});
|
|
|
|
it("passes through when plugin enabled", { timeout: 5000 }, async () => {
|
|
const app = trap();
|
|
app.use(
|
|
"*",
|
|
createTenantMiddleware({
|
|
resolver: headerResolver("x-tenant-id", async () =>
|
|
makeTenant({ enabledPlugins: ["payment"] })
|
|
),
|
|
})
|
|
);
|
|
app.get("/", requirePlugin("payment"), (c) => c.text("yes"));
|
|
const res = await app.request("/", {
|
|
headers: { "x-tenant-id": "acme" },
|
|
});
|
|
expect(await res.text()).toBe("yes");
|
|
});
|
|
});
|