mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-17 07:42:23 +00:00
feat: define scoped sharing levels
This commit is contained in:
1 parent
ab14fdb6f3
commit
e4e773d543
4 files changed
+106
-10
No files matched your search
@@ -0,0 +1,87 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { grantCoversDomain, normaliseScope } from "@/lib/sharing/grants";
|
||||
import { SHARE_DOMAINS } from "@/lib/sharing/scope";
|
||||
import { inviteGrantSchema } from "@/lib/validations/account-sharing";
|
||||
|
||||
describe("scoped sharing contract", () => {
|
||||
it("locks the closed eight-domain vocabulary", () => {
|
||||
expect(SHARE_DOMAINS).toEqual([
|
||||
"measurements",
|
||||
"medications",
|
||||
"labs",
|
||||
"profile",
|
||||
"illness",
|
||||
"mind",
|
||||
"cycle",
|
||||
"documents",
|
||||
]);
|
||||
});
|
||||
|
||||
it("treats null as whole-record while refusing malformed non-null scopes", () => {
|
||||
expect(normaliseScope(null)).toBeNull();
|
||||
|
||||
for (const malformed of [
|
||||
[],
|
||||
["labs", "labs"],
|
||||
["labs", "unknown"],
|
||||
["record"],
|
||||
"labs",
|
||||
{ labs: true },
|
||||
]) {
|
||||
const resolved = normaliseScope(malformed);
|
||||
expect(resolved, JSON.stringify(malformed)).not.toBeNull();
|
||||
expect(resolved?.size, JSON.stringify(malformed)).toBe(0);
|
||||
for (const domain of SHARE_DOMAINS) {
|
||||
expect(
|
||||
grantCoversDomain({ scopeJson: malformed }, domain),
|
||||
`${JSON.stringify(malformed)} must not open ${domain}`,
|
||||
).toBe(false);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
it("accepts whole or non-empty unique READ and WRITE scopes only", () => {
|
||||
for (const access of ["READ", "WRITE"] as const) {
|
||||
expect(
|
||||
inviteGrantSchema.safeParse({
|
||||
identifier: "delegate",
|
||||
access,
|
||||
scope: null,
|
||||
}).success,
|
||||
).toBe(true);
|
||||
expect(
|
||||
inviteGrantSchema.safeParse({
|
||||
identifier: "delegate",
|
||||
access,
|
||||
scope: ["labs", "medications"],
|
||||
}).success,
|
||||
).toBe(true);
|
||||
}
|
||||
|
||||
expect(
|
||||
inviteGrantSchema.safeParse({
|
||||
identifier: "delegate",
|
||||
access: "READ",
|
||||
scope: ["labs", "labs"],
|
||||
}).success,
|
||||
).toBe(false);
|
||||
});
|
||||
|
||||
it("allows MANAGE only for the whole record", () => {
|
||||
expect(
|
||||
inviteGrantSchema.safeParse({
|
||||
identifier: "delegate",
|
||||
access: "MANAGE",
|
||||
scope: null,
|
||||
}).success,
|
||||
).toBe(true);
|
||||
expect(
|
||||
inviteGrantSchema.safeParse({
|
||||
identifier: "delegate",
|
||||
access: "MANAGE",
|
||||
scope: ["labs"],
|
||||
}).success,
|
||||
).toBe(false);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user