Refresh HiveGrid Cloud reference snapshot
This commit is contained in:
1 parent
5e99e14772
commit
7f88026f79
5955 files changed
+1177325
-188362
No files matched your search
+7
-2
@@ -1,11 +1,13 @@
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
// Cache-Abstraktion: getFreshValue direkt durchreichen (deterministisch, kein Cache-State zwischen Tests).
|
||||
vi.mock("~/utils/cache.server", () => ({
|
||||
cachified: vi.fn(async (opts: { getFreshValue: () => Promise<unknown> }) => opts.getFreshValue()),
|
||||
clearCacheKeyPrefix: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("@hivegrid/db/rls-context.server", () => ({
|
||||
runWithRlsContext: vi.fn((_context: unknown, callback: () => unknown) => callback()),
|
||||
}));
|
||||
|
||||
vi.mock("~/utils/entitlements/.server/productAccess.server", () => ({
|
||||
resolveTenantProductAccess: vi.fn(),
|
||||
resolveTenantEntitlementSubscription: vi.fn(async () => null),
|
||||
@@ -16,6 +18,7 @@ vi.mock("~/utils/services/.server/featureFlagEvaluator.server", () => ({
|
||||
}));
|
||||
|
||||
import { clearCacheKeyPrefix } from "~/utils/cache.server";
|
||||
import { runWithRlsContext } from "@hivegrid/db/rls-context.server";
|
||||
import { resolveTenantEntitlementSubscription, resolveTenantProductAccess } from "~/utils/entitlements/.server/productAccess.server";
|
||||
import { evaluateFlagForContext } from "~/utils/services/.server/featureFlagEvaluator.server";
|
||||
import { invalidateTenantModuleAccessCache, tenantHasFlag, tenantHasModule } from "./tenantConnector.server";
|
||||
@@ -24,6 +27,7 @@ const resolveMock = vi.mocked(resolveTenantProductAccess);
|
||||
const subscriptionMock = vi.mocked(resolveTenantEntitlementSubscription);
|
||||
const flagMock = vi.mocked(evaluateFlagForContext);
|
||||
const clearPrefixMock = vi.mocked(clearCacheKeyPrefix);
|
||||
const rlsMock = vi.mocked(runWithRlsContext);
|
||||
|
||||
describe("tenantHasModule", () => {
|
||||
beforeEach(() => {
|
||||
@@ -39,6 +43,7 @@ describe("tenantHasModule", () => {
|
||||
expect(allowed).toBe(true);
|
||||
expect(subscriptionMock).toHaveBeenCalledWith("t1");
|
||||
expect(resolveMock).toHaveBeenCalledWith(expect.objectContaining({ tenantId: "t1", userId: "u1", featureKey: "crm-basic" }));
|
||||
expect(rlsMock).toHaveBeenCalledWith({ tenantId: "t1", userId: "u1" }, expect.any(Function));
|
||||
});
|
||||
|
||||
it("returns false when the product-access decision denies the module", async () => {
|
||||
|
||||
+18
-24
@@ -1,4 +1,5 @@
|
||||
import { cachified, clearCacheKeyPrefix } from "~/utils/cache.server";
|
||||
import { clearCacheKeyPrefix } from "~/utils/cache.server";
|
||||
import { runWithRlsContext } from "@hivegrid/db/rls-context.server";
|
||||
import { resolveTenantEntitlementSubscription, resolveTenantProductAccess } from "~/utils/entitlements/.server/productAccess.server";
|
||||
import { evaluateFlagForContext } from "~/utils/services/.server/featureFlagEvaluator.server";
|
||||
|
||||
@@ -11,16 +12,16 @@ import { evaluateFlagForContext } from "~/utils/services/.server/featureFlagEval
|
||||
* - `tenantHasModule()` — "Darf dieser Tenant dieses Modul nutzen?" Brücke Billing↔Feature-Gate:
|
||||
* prüft lokalen Billing-State/Entitlements, Produkt-Blueprint,
|
||||
* Tenant-Override und ggf. Tenant-Rollen-Permission via {@link resolveTenantProductAccess}.
|
||||
* Die Modul-Entscheidung wird auf Connector-Ebene gecacht.
|
||||
*
|
||||
* - `tenantHasFlag()` — "Ist dieses Feature-Flag für diesen Kontext aktiv?" Delegiert an den
|
||||
* v2-Evaluator {@link evaluateFlagForContext}, der bereits eigenen Eval-Cache mitbringt.
|
||||
*
|
||||
* Cache-Backing ist die App-Cache-Abstraktion (`~/utils/cache.server`, LRU heute, per
|
||||
* `redisCacheAdapter` Redis-swappbar — Infra-Schalter, kein Logik-Eingriff).
|
||||
* Der Connector selbst cached die kombinierte Entscheidung bewusst nicht:
|
||||
* Subscription- und Permission-Resolver besitzen eigene, cross-replica
|
||||
* invalidierte Caches. Ein zweiter lokaler Cache könnte eine Rollen- oder
|
||||
* Abo-Änderung trotz korrekt invalidierter Quell-Caches weiter erlauben.
|
||||
*/
|
||||
|
||||
const MODULE_CACHE_TTL_MS = 60_000;
|
||||
const MODULE_CACHE_PREFIX = "connector:module:";
|
||||
|
||||
export interface TenantModuleAccessInput {
|
||||
@@ -34,31 +35,22 @@ export interface TenantModuleAccessInput {
|
||||
readonly bypassTenantRolePermission?: boolean;
|
||||
}
|
||||
|
||||
function moduleCacheKey(input: TenantModuleAccessInput): string {
|
||||
const scope = input.bypassTenantRolePermission ? "bypass" : "rbac";
|
||||
return `${MODULE_CACHE_PREFIX}${input.tenantId}:${input.userId}:${input.moduleKey}:${scope}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Konnektor-Gate: Ist das Modul für den Tenant freigeschaltet?
|
||||
*
|
||||
* @returns `true`, wenn der Tenant das Modul nutzen darf (Abo + Blueprint + Override + Rolle ok).
|
||||
*/
|
||||
export async function tenantHasModule(input: TenantModuleAccessInput): Promise<boolean> {
|
||||
return cachified({
|
||||
key: moduleCacheKey(input),
|
||||
ttl: MODULE_CACHE_TTL_MS,
|
||||
getFreshValue: async () => {
|
||||
const subscription = await resolveTenantEntitlementSubscription(input.tenantId);
|
||||
const decision = await resolveTenantProductAccess({
|
||||
tenantId: input.tenantId,
|
||||
userId: input.userId,
|
||||
featureKey: input.moduleKey,
|
||||
subscription,
|
||||
bypassTenantRolePermission: input.bypassTenantRolePermission,
|
||||
});
|
||||
return decision.allowed;
|
||||
},
|
||||
return runWithRlsContext({ tenantId: input.tenantId, userId: input.userId }, async () => {
|
||||
const subscription = await resolveTenantEntitlementSubscription(input.tenantId);
|
||||
const decision = await resolveTenantProductAccess({
|
||||
tenantId: input.tenantId,
|
||||
userId: input.userId,
|
||||
featureKey: input.moduleKey,
|
||||
subscription,
|
||||
bypassTenantRolePermission: input.bypassTenantRolePermission,
|
||||
});
|
||||
return decision.allowed;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -88,5 +80,7 @@ export async function tenantHasFlag(input: TenantFlagContext): Promise<boolean>
|
||||
* Rollen-Änderungen (z.B. Billing-Sync, Admin-Plan-Wechsel), damit das Gate sofort nachzieht.
|
||||
*/
|
||||
export function invalidateTenantModuleAccessCache(tenantId: string): void {
|
||||
// Rolling-deploy compatibility: remove entries produced by an older web
|
||||
// process. Current code does not write connector-level decisions.
|
||||
clearCacheKeyPrefix(`${MODULE_CACHE_PREFIX}${tenantId}:`);
|
||||
}
|
||||
Reference in new issue
Block a user