Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
// @vitest-environment node
import { describe, expect, test, vi } from "vitest";
vi.mock("@/lib/i18n", () => ({
default: {
language: "en-US",
t: (key: string, params?: Record<string, unknown>) => {
const map: Record<string, string> = {
"database.all": "All databases",
"database.columns": "Columns",
"common.schema": "Schema",
"common.table": "Table",
"cel.condition.self": "Condition",
"project.masking-exemption.level": "level",
};
if (key === "common.n-more" && params?.n !== undefined)
return `+${params.n} more`;
if (
key === "project.masking-exemption.n-exemptions" &&
params?.n !== undefined
) {
return `${params.n} exemption${params.n === 1 ? "" : "s"}`;
}
if (
key === "project.masking-exemption.n-databases" &&
params?.n !== undefined
) {
return `${params.n} database${params.n === 1 ? "" : "s"}`;
}
return map[key] ?? key;
},
},
}));
import {
generateGrantTitle,
getConditionExpression,
groupByMember,
parseClassificationLevel,
parseExpirationTimestamp,
} from "./exemptionDataUtils";
import type { AccessUser, ExemptionGrant } from "./types";
describe("parseClassificationLevel", () => {
test("parses <= operator", () => {
const result = parseClassificationLevel(
"resource.classification_level <= 3"
);
expect(result).toEqual({ operator: "<=", value: 3 });
});
test("parses == operator", () => {
const result = parseClassificationLevel(
"resource.classification_level == 2"
);
expect(result).toEqual({ operator: "==", value: 2 });
});
test("parses < operator", () => {
const result = parseClassificationLevel(
"resource.classification_level < 4"
);
expect(result).toEqual({ operator: "<", value: 4 });
});
test("parses >= operator", () => {
const result = parseClassificationLevel(
"resource.classification_level >= 1"
);
expect(result).toEqual({ operator: ">=", value: 1 });
});
test("parses > operator", () => {
const result = parseClassificationLevel(
"resource.classification_level > 0"
);
expect(result).toEqual({ operator: ">", value: 0 });
});
test("parses != operator", () => {
const result = parseClassificationLevel(
"resource.classification_level != 5"
);
expect(result).toEqual({ operator: "!=", value: 5 });
});
test("handles extra whitespace around operator", () => {
const result = parseClassificationLevel(
"resource.classification_level <= 3"
);
expect(result).toEqual({ operator: "<=", value: 3 });
});
test("extracts level from a combined CEL expression", () => {
const result = parseClassificationLevel(
'resource.instance_id == "prod" && resource.classification_level <= 3'
);
expect(result).toEqual({ operator: "<=", value: 3 });
});
test("returns undefined when no classification level present", () => {
const result = parseClassificationLevel(
'resource.instance_id == "prod" && resource.database_name == "hr"'
);
expect(result).toBeUndefined();
});
test("returns undefined for empty string", () => {
expect(parseClassificationLevel("")).toBeUndefined();
});
test("handles multi-digit level values", () => {
const result = parseClassificationLevel(
"resource.classification_level <= 10"
);
expect(result).toEqual({ operator: "<=", value: 10 });
});
});
describe("getConditionExpression", () => {
test("strips request.time from expression", () => {
const result = getConditionExpression(
'resource.instance_id == "prod" && request.time < timestamp("2026-04-15T00:00:00Z")'
);
expect(result).toBe('resource.instance_id == "prod"');
});
test("returns full expression when no request.time", () => {
const result = getConditionExpression(
'resource.instance_id == "prod" && resource.database_name == "hr"'
);
expect(result).toBe(
'resource.instance_id == "prod" && resource.database_name == "hr"'
);
});
test("returns empty string for empty input", () => {
expect(getConditionExpression("")).toBe("");
});
test("returns empty string when expression is only request.time", () => {
const result = getConditionExpression(
'request.time < timestamp("2026-04-15T00:00:00Z")'
);
expect(result).toBe("");
});
test("handles extra whitespace in request.time", () => {
const result = getConditionExpression(
'resource.instance_id == "prod" && request.time < timestamp("2026-04-15T00:00:00Z")'
);
expect(result).toBe('resource.instance_id == "prod"');
});
});
describe("parseExpirationTimestamp", () => {
test("extracts timestamp from request.time expression", () => {
const result = parseExpirationTimestamp(
'resource.instance_id == "prod" && request.time < timestamp("2026-04-15T11:30:00.000Z")'
);
expect(result).toBe(new Date("2026-04-15T11:30:00.000Z").getTime());
});
test("returns undefined when no request.time present", () => {
const result = parseExpirationTimestamp('resource.instance_id == "prod"');
expect(result).toBeUndefined();
});
test("returns undefined for empty string", () => {
expect(parseExpirationTimestamp("")).toBeUndefined();
});
test("extracts timestamp when request.time is the only expression", () => {
const result = parseExpirationTimestamp(
'request.time < timestamp("2026-12-31T23:59:59.000Z")'
);
expect(result).toBe(new Date("2026-12-31T23:59:59.000Z").getTime());
});
});
describe("groupByMember", () => {
const makeAccessUser = (overrides: Partial<AccessUser>): AccessUser => ({
type: "user",
member: "user:[email protected]",
rawExpression: "",
description: "",
databaseResources: [],
conditionExpression: "",
...overrides,
});
test("groups multiple exemptions for the same member", () => {
const users: AccessUser[] = [
makeAccessUser({
member: "user:[email protected]",
description: "Grant 1",
conditionExpression: 'resource.instance_id == "prod"',
}),
makeAccessUser({
member: "user:[email protected]",
description: "Grant 2",
conditionExpression: "resource.classification_level <= 3",
}),
];
const result = groupByMember(users);
expect(result).toHaveLength(1);
expect(result[0].member).toBe("user:[email protected]");
expect(result[0].grants).toHaveLength(2);
// Latest created grant first (reversed order)
expect(result[0].grants[0].description).toBe("Grant 2");
expect(result[0].grants[1].description).toBe("Grant 1");
});
test("separates different members", () => {
const users: AccessUser[] = [
makeAccessUser({ member: "user:[email protected]" }),
makeAccessUser({ member: "user:[email protected]" }),
];
const result = groupByMember(users);
expect(result).toHaveLength(2);
expect(
result.map((m) => m.member).sort((a, b) => a.localeCompare(b))
).toEqual(["user:[email protected]", "user:[email protected]"]);
});
test("detects group type from member prefix", () => {
const users: AccessUser[] = [
makeAccessUser({
type: "group",
member: "group:[email protected]",
}),
];
const result = groupByMember(users);
expect(result[0].type).toBe("group");
});
test("computes neverExpires=true when any grant has no expiration", () => {
const users: AccessUser[] = [
makeAccessUser({
member: "user:[email protected]",
expirationTimestamp: new Date("2026-04-15").getTime(),
}),
makeAccessUser({
member: "user:[email protected]",
expirationTimestamp: undefined,
}),
];
const result = groupByMember(users);
expect(result[0].neverExpires).toBe(true);
});
test("computes neverExpires=false when all grants have expiration", () => {
const users: AccessUser[] = [
makeAccessUser({
member: "user:[email protected]",
expirationTimestamp: new Date("2026-04-15").getTime(),
}),
makeAccessUser({
member: "user:[email protected]",
expirationTimestamp: new Date("2026-06-30").getTime(),
}),
];
const result = groupByMember(users);
expect(result[0].neverExpires).toBe(false);
});
test("computes nearestExpiration as earliest timestamp", () => {
const apr = new Date("2026-04-15").getTime();
const jun = new Date("2026-06-30").getTime();
const users: AccessUser[] = [
makeAccessUser({
member: "user:[email protected]",
expirationTimestamp: jun,
}),
makeAccessUser({
member: "user:[email protected]",
expirationTimestamp: apr,
}),
];
const result = groupByMember(users);
expect(result[0].nearestExpiration).toBe(apr);
});
test("nearestExpiration is undefined when no grants have expiration", () => {
const users: AccessUser[] = [
makeAccessUser({
member: "user:[email protected]",
}),
];
const result = groupByMember(users);
expect(result[0].nearestExpiration).toBeUndefined();
});
test("databaseNames has empty sentinel when no grants have databaseResources", () => {
const users: AccessUser[] = [
makeAccessUser({
member: "user:[email protected]",
conditionExpression: "resource.classification_level <= 3",
}),
];
const result = groupByMember(users);
// Empty sentinel indicates "all databases" (grant has no specific resources)
expect(result[0].databaseNames).toEqual([""]);
});
test("does not summarize an unknown condition as all databases", () => {
const result = groupByMember([
makeAccessUser({
databaseResources: undefined,
conditionExpression:
'(resource.database_name in ["a", "b"]) && (resource.column_name in ["email", "phone"])',
}),
]);
expect(result[0].databaseNames).toEqual([]);
});
test("extracts unique database names from resources", () => {
const users: AccessUser[] = [
makeAccessUser({
member: "user:[email protected]",
databaseResources: [
{
databaseFullName: "instances/prod/databases/hr_prod",
schema: "public",
table: "employee",
},
{
databaseFullName: "instances/prod/databases/hr_prod",
schema: "public",
table: "audit",
},
{
databaseFullName: "instances/dev/databases/test",
schema: "",
table: "",
},
],
}),
];
const result = groupByMember(users);
expect(result[0].databaseNames.sort((a, b) => a.localeCompare(b))).toEqual([
"hr_prod",
"test",
]);
});
test("parses classification level into grant", () => {
const users: AccessUser[] = [
makeAccessUser({
member: "user:[email protected]",
conditionExpression:
'resource.instance_id == "prod" && resource.classification_level <= 3',
}),
];
const result = groupByMember(users);
expect(result[0].grants[0].classificationLevel).toEqual({
operator: "<=",
value: 3,
});
});
test("classificationLevel is undefined when not in expression", () => {
const users: AccessUser[] = [
makeAccessUser({
member: "user:[email protected]",
conditionExpression: 'resource.instance_id == "prod"',
}),
];
const result = groupByMember(users);
expect(result[0].grants[0].classificationLevel).toBeUndefined();
});
test("handles empty input array", () => {
expect(groupByMember([])).toEqual([]);
});
test("does not merge grants — each exemption is its own card", () => {
const exp = new Date("2026-04-15").getTime();
const users: AccessUser[] = [
makeAccessUser({
member: "user:[email protected]",
description: "Production audit",
expirationTimestamp: exp,
databaseResources: [
{
databaseFullName: "instances/prod/databases/hr_prod",
schema: "public",
table: "audit",
},
],
}),
makeAccessUser({
member: "user:[email protected]",
description: "Production audit",
expirationTimestamp: exp,
databaseResources: [
{
databaseFullName: "instances/prod/databases/hr_prod",
schema: "public",
table: "department",
},
],
}),
makeAccessUser({
member: "user:[email protected]",
description: "Production audit",
expirationTimestamp: exp,
databaseResources: [
{
databaseFullName: "instances/prod/databases/hr_prod",
schema: "public",
table: "employee",
},
],
}),
];
const result = groupByMember(users);
expect(result).toHaveLength(1);
// No merging — each exemption is its own grant card
expect(result[0].grants).toHaveLength(3);
});
test("keeps overlapping resources as separate grants", () => {
const users: AccessUser[] = [
makeAccessUser({
member: "user:[email protected]",
description: "",
databaseResources: [
{
databaseFullName: "instances/prod/databases/hr_prod",
schema: "public",
table: "employee",
},
{
databaseFullName: "instances/prod/databases/hr_prod",
schema: "public",
table: "audit",
},
],
}),
makeAccessUser({
member: "user:[email protected]",
description: "",
databaseResources: [
{
databaseFullName: "instances/prod/databases/hr_prod",
schema: "public",
table: "employee",
},
{
databaseFullName: "instances/prod/databases/hr_prod",
schema: "public",
table: "department",
},
],
}),
];
const result = groupByMember(users);
// No merging — each exemption keeps its own resources
expect(result[0].grants).toHaveLength(2);
expect(result[0].grants[0].databaseResources).toHaveLength(2);
expect(result[0].grants[1].databaseResources).toHaveLength(2);
});
test("keeps grants with different descriptions as separate cards", () => {
const users: AccessUser[] = [
makeAccessUser({
member: "user:[email protected]",
description: "Audit access",
databaseResources: [
{
databaseFullName: "instances/prod/databases/hr_prod",
schema: "public",
table: "audit",
},
],
}),
makeAccessUser({
member: "user:[email protected]",
description: "Dev access",
databaseResources: [
{
databaseFullName: "instances/prod/databases/hr_prod",
schema: "public",
table: "employee",
},
],
}),
];
const result = groupByMember(users);
expect(result[0].grants).toHaveLength(2);
});
test("does NOT merge grants with different expirations", () => {
const users: AccessUser[] = [
makeAccessUser({
member: "user:[email protected]",
description: "Same reason",
expirationTimestamp: new Date("2026-04-15").getTime(),
databaseResources: [
{
databaseFullName: "instances/prod/databases/hr_prod",
schema: "public",
table: "audit",
},
],
}),
makeAccessUser({
member: "user:[email protected]",
description: "Same reason",
expirationTimestamp: new Date("2026-06-30").getTime(),
databaseResources: [
{
databaseFullName: "instances/prod/databases/hr_prod",
schema: "public",
table: "employee",
},
],
}),
];
const result = groupByMember(users);
expect(result[0].grants).toHaveLength(2);
});
test("does NOT merge grants with different classification levels", () => {
const users: AccessUser[] = [
makeAccessUser({
member: "user:[email protected]",
description: "",
conditionExpression:
'resource.instance_id == "prod" && resource.classification_level <= 3',
databaseResources: [
{
databaseFullName: "instances/prod/databases/hr_prod",
schema: "public",
table: "audit",
},
],
}),
makeAccessUser({
member: "user:[email protected]",
description: "",
conditionExpression:
'resource.instance_id == "prod" && resource.classification_level <= 5',
databaseResources: [
{
databaseFullName: "instances/prod/databases/hr_prod",
schema: "public",
table: "employee",
},
],
}),
];
const result = groupByMember(users);
expect(result[0].grants).toHaveLength(2);
});
test("keeps level-only grants as separate cards even with same description", () => {
const users: AccessUser[] = [
makeAccessUser({
member: "user:[email protected]",
description: "Level access",
conditionExpression: "resource.classification_level <= 3",
}),
makeAccessUser({
member: "user:[email protected]",
description: "Level access",
conditionExpression: "resource.classification_level <= 3",
}),
];
const result = groupByMember(users);
// No merging — each exemption is its own card
expect(result[0].grants).toHaveLength(2);
});
});
describe("generateGrantTitle", () => {
const makeGrant = (overrides: Partial<ExemptionGrant>): ExemptionGrant => ({
id: "test",
description: "",
rawExpression: "",
databaseResources: [],
conditionExpression: "",
...overrides,
});
// --- No databases ---
test("unknown condition → Condition", () => {
expect(
generateGrantTitle(makeGrant({ databaseResources: undefined }))
).toBe("Condition");
});
test("no resources, no level → All databases", () => {
expect(generateGrantTitle(makeGrant({}))).toBe("All databases");
});
test("no resources, with level", () => {
expect(
generateGrantTitle(
makeGrant({ classificationLevel: { operator: "<=", value: 3 } })
)
).toBe("All databases, level ≤ 3");
});
test("no database, with columns", () => {
expect(
generateGrantTitle(
makeGrant({
databaseResources: [
{ databaseFullName: "", columns: ["email", "phone"] },
],
})
)
).toBe("All databases (columns: email, phone)");
});
test("keeps the global scope visible beside database-specific resources", () => {
expect(
generateGrantTitle(
makeGrant({
databaseResources: [
{ databaseFullName: "", columns: ["email"] },
{ databaseFullName: "instances/a/databases/hr_prod" },
],
})
)
).toBe("All databases (columns: email); hr_prod");
});
// --- Multiple databases ---
test("2 databases", () => {
expect(
generateGrantTitle(
makeGrant({
databaseResources: [
{
databaseFullName: "instances/a/databases/bbdev",
schema: "",
table: "",
},
{
databaseFullName: "instances/a/databases/hr_prod",
schema: "",
table: "",
},
],
})
)
).toBe("bbdev, hr_prod");
});
test("3 databases", () => {
expect(
generateGrantTitle(
makeGrant({
databaseResources: [
{
databaseFullName: "instances/a/databases/bbdev",
schema: "",
table: "",
},
{
databaseFullName: "instances/a/databases/hr_prod",
schema: "",
table: "",
},
{
databaseFullName: "instances/a/databases/test",
schema: "",
table: "",
},
],
})
)
).toBe("bbdev, hr_prod +1 more");
});
test("2 databases with level", () => {
expect(
generateGrantTitle(
makeGrant({
databaseResources: [
{
databaseFullName: "instances/a/databases/bbdev",
schema: "",
table: "",
},
{
databaseFullName: "instances/a/databases/hr_prod",
schema: "",
table: "",
},
],
classificationLevel: { operator: "<=", value: 3 },
})
)
).toBe("bbdev, hr_prod, level ≤ 3");
});
// --- Single database, single path (drill down) ---
test("1 db, no schema, no table", () => {
expect(
generateGrantTitle(
makeGrant({
databaseResources: [
{
databaseFullName: "instances/a/databases/hr_prod",
schema: "",
table: "",
},
],
})
)
).toBe("hr_prod");
});
test("1 db, 1 schema, no table → show schema keyword", () => {
expect(
generateGrantTitle(
makeGrant({
databaseResources: [
{
databaseFullName: "instances/a/databases/hr_prod",
schema: "public",
table: "",
},
],
})
)
).toBe("hr_prod (schema public)");
});
test("1 db, 1 schema, 1 table → dot notation", () => {
expect(
generateGrantTitle(
makeGrant({
databaseResources: [
{
databaseFullName: "instances/a/databases/hr_prod",
schema: "public",
table: "audit",
},
],
})
)
).toBe("hr_prod (public.audit)");
});
test("1 db, 1 schema, 1 table, 1 column → full dot path", () => {
expect(
generateGrantTitle(
makeGrant({
databaseResources: [
{
databaseFullName: "instances/a/databases/hr_prod",
schema: "public",
table: "employee",
columns: ["emp_no"],
},
],
})
)
).toBe("hr_prod (public.employee.emp_no)");
});
test("1 db, 1 schema, 1 table, 2 columns → colon then list", () => {
expect(
generateGrantTitle(
makeGrant({
databaseResources: [
{
databaseFullName: "instances/a/databases/hr_prod",
schema: "public",
table: "employee",
columns: ["emp_no", "name"],
},
],
})
)
).toBe("hr_prod (public.employee: emp_no, name)");
});
test("1 db, 1 schema, 1 table, 3+ columns → colon then 2 + more", () => {
expect(
generateGrantTitle(
makeGrant({
databaseResources: [
{
databaseFullName: "instances/a/databases/hr_prod",
schema: "public",
table: "employee",
columns: ["emp_no", "name", "salary", "dept"],
},
],
})
)
).toBe("hr_prod (public.employee: emp_no, name +2 more)");
});
test("1 db, no schema, 1 table → table keyword", () => {
expect(
generateGrantTitle(
makeGrant({
databaseResources: [
{
databaseFullName: "instances/a/databases/hr_prod",
schema: "",
table: "employee",
},
],
})
)
).toBe("hr_prod (table employee)");
});
test("1 db, no schema, 1 table, 1 column → table.column", () => {
expect(
generateGrantTitle(
makeGrant({
databaseResources: [
{
databaseFullName: "instances/a/databases/hr_prod",
schema: "",
table: "employee",
columns: ["emp_no"],
},
],
})
)
).toBe("hr_prod (employee.emp_no)");
});
// --- Single database, branching (multiple at some level) ---
test("1 db, 2 schemas → comma list, stop", () => {
expect(
generateGrantTitle(
makeGrant({
databaseResources: [
{
databaseFullName: "instances/a/databases/hr_prod",
schema: "public",
table: "",
},
{
databaseFullName: "instances/a/databases/hr_prod",
schema: "test",
table: "",
},
],
})
)
).toBe("hr_prod (public, test)");
});
test("1 db, 3 schemas → 2 + more", () => {
expect(
generateGrantTitle(
makeGrant({
databaseResources: [
{
databaseFullName: "instances/a/databases/hr_prod",
schema: "public",
table: "",
},
{
databaseFullName: "instances/a/databases/hr_prod",
schema: "test",
table: "",
},
{
databaseFullName: "instances/a/databases/hr_prod",
schema: "audit",
table: "",
},
],
})
)
).toBe("hr_prod (public, test +1 more)");
});
test("1 db, 1 schema, 2 tables → colon then list", () => {
expect(
generateGrantTitle(
makeGrant({
databaseResources: [
{
databaseFullName: "instances/a/databases/hr_prod",
schema: "public",
table: "audit",
},
{
databaseFullName: "instances/a/databases/hr_prod",
schema: "public",
table: "dept",
},
],
})
)
).toBe("hr_prod (public: audit, dept)");
});
test("1 db, 1 schema, 3 tables → colon then 2 + more", () => {
expect(
generateGrantTitle(
makeGrant({
databaseResources: [
{
databaseFullName: "instances/a/databases/hr_prod",
schema: "public",
table: "audit",
},
{
databaseFullName: "instances/a/databases/hr_prod",
schema: "public",
table: "dept",
},
{
databaseFullName: "instances/a/databases/hr_prod",
schema: "public",
table: "employee",
},
],
})
)
).toBe("hr_prod (public: audit, dept +1 more)");
});
test("1 db, no schema, 2 tables → comma list", () => {
expect(
generateGrantTitle(
makeGrant({
databaseResources: [
{
databaseFullName: "instances/a/databases/hr_prod",
schema: "",
table: "audit",
},
{
databaseFullName: "instances/a/databases/hr_prod",
schema: "",
table: "dept",
},
],
})
)
).toBe("hr_prod (audit, dept)");
});
test("1 db, no schema, 4 tables → 2 + more", () => {
expect(
generateGrantTitle(
makeGrant({
databaseResources: [
{
databaseFullName: "instances/a/databases/hr_prod",
schema: "",
table: "a",
},
{
databaseFullName: "instances/a/databases/hr_prod",
schema: "",
table: "b",
},
{
databaseFullName: "instances/a/databases/hr_prod",
schema: "",
table: "c",
},
{
databaseFullName: "instances/a/databases/hr_prod",
schema: "",
table: "d",
},
],
})
)
).toBe("hr_prod (a, b +2 more)");
});
// --- With level suffix ---
test("single path with level", () => {
expect(
generateGrantTitle(
makeGrant({
databaseResources: [
{
databaseFullName: "instances/a/databases/hr_prod",
schema: "public",
table: "audit",
},
],
classificationLevel: { operator: "<=", value: 3 },
})
)
).toBe("hr_prod (public.audit), level ≤ 3");
});
test("branching with level", () => {
expect(
generateGrantTitle(
makeGrant({
databaseResources: [
{
databaseFullName: "instances/a/databases/hr_prod",
schema: "public",
table: "audit",
},
{
databaseFullName: "instances/a/databases/hr_prod",
schema: "public",
table: "dept",
},
],
classificationLevel: { operator: "==", value: 1 },
})
)
).toBe("hr_prod (public: audit, dept), level = 1");
});
});
// ============================================================
// BYT-9788 — composite (OR) exemption precedence & read-path round-trip
//
// `&&` binds tighter than `||` in CEL, and the backend evaluates with stock
// cel-go (backend/api/v1/masking_evaluator.go), so a masking exemption that ORs
// several resources and ANDs a request.time bound must wrap the OR-group —
// request.time < T && ((c1) || (c2))
// otherwise the expiry binds only to the last branch. PR #20683 (create page)
// and #20687 (GrantAccessDialog) make the builders satisfy that invariant.
//
// The read-path helpers below (parseExpirationTimestamp / getConditionExpression)
// assume the expiration is a top-level `&&` term; these round-trip the wrapped
// output to prove the writer and reader agree.
// ============================================================
describe("composite exemption precedence (BYT-9788)", () => {
// One resource's clause = instance && database (&& table), the exact shape
// getExpressionsForDatabaseResource produces, AND-joined.
const clause = (instance: string, database: string, table?: string): string =>
[
`resource.instance_id == "${instance}"`,
`resource.database_name == "${database}"`,
table ? `resource.table_name == "${table}"` : "",
]
.filter((e) => e)
.join(" && ");
const TIME = "2026-06-02T10:00:00.000Z";
const timeClause = `request.time < timestamp("${TIME}")`;
const timeMs = new Date(TIME).getTime();
const c1 = clause("inst", "db", "SPCP_CP_ENTITY_AUTHORISATION");
const c2 = clause("inst", "db", "SPCP_CP_AUTH_ACCESS");
describe("fixed format (what PR #20683 now writes)", () => {
// request.time < T && ((c1) || (c2))
const fixedMulti = `${timeClause} && ((${c1}) || (${c2}))`;
test("expiration applies to the whole OR-group (timestamp extracted)", () => {
expect(parseExpirationTimestamp(fixedMulti)).toBe(timeMs);
});
test("getConditionExpression strips time and keeps the OR-group intact", () => {
// Both resources survive, OR operator preserved, internal && preserved.
expect(getConditionExpression(fixedMulti)).toBe(`((${c1}) || (${c2}))`);
});
test("round-trip: time + condition reconstruct the original expression", () => {
const condition = getConditionExpression(fixedMulti);
const ts = parseExpirationTimestamp(fixedMulti);
const rebuilt = [
`request.time < timestamp("${new Date(ts!).toISOString()}")`,
condition,
].join(" && ");
expect(rebuilt).toBe(fixedMulti);
});
test("single resource is wrapped but parses identically", () => {
const fixedSingle = `${timeClause} && (${c1})`;
expect(parseExpirationTimestamp(fixedSingle)).toBe(timeMs);
expect(getConditionExpression(fixedSingle)).toBe(`(${c1})`);
});
test("classification level inside an OR branch is still parsed", () => {
const withLevel = `${timeClause} && ((${c1} && resource.classification_level <= 3) || (${c2} && resource.classification_level <= 3))`;
expect(
parseClassificationLevel(getConditionExpression(withLevel))
).toEqual({ operator: "<=", value: 3 });
});
test("EXPRESSION mode: a top-level || in custom CEL is wrapped and bounded", () => {
// The create page wraps the user's celString: request.time < T && (a || b)
const expr = `${timeClause} && (resource.database_name == "a" || resource.database_name == "b")`;
expect(parseExpirationTimestamp(expr)).toBe(timeMs);
expect(getConditionExpression(expr)).toBe(
'(resource.database_name == "a" || resource.database_name == "b")'
);
});
});
});