-
Notifications
You must be signed in to change notification settings - Fork 0
24: first decision: trusted principal, memory facts, can, and decide #40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c3863f6
24: first decision: trusted principal, memory facts, can, and decide
AmaraNecib 369ab66
fix: address CodeRabbit findings — duplicate source guard, null outco…
AmaraNecib 068932a
fix: await all async rejects assertions in tests
AmaraNecib 4889c83
docs: add wisdom folder with security, testing, and process lessons l…
AmaraNecib e621dc1
fix: address CodeRabbit wisdom file findings — code fence language, e…
AmaraNecib File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,3 +7,4 @@ dist/ | |
| *.log | ||
| .DS_Store | ||
| .pi-subagents/ | ||
| .scratch/ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,216 @@ | ||
| import { describe, it, expect } from "bun:test"; | ||
| import { | ||
| createMizan, | ||
| Mizan, | ||
| PrincipalEvaluator, | ||
| type SourceResolver, | ||
| type AuthorizationFact, | ||
| } from "../src/index.ts"; | ||
|
|
||
| // ─── Helpers ─────────────────────────────────────────────────────────────── | ||
|
|
||
| function sourceWith(...facts: AuthorizationFact[]): SourceResolver { | ||
| return { | ||
| async resolve() { | ||
| return { status: "facts", facts, freshness: "fresh" }; | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| function emptySource(): SourceResolver { | ||
| return { | ||
| async resolve() { | ||
| return { status: "facts", facts: [], freshness: "fresh" }; | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| // ─── PrincipalEvaluator: can() ───────────────────────────────────────────── | ||
|
|
||
| describe("PrincipalEvaluator.can()", () => { | ||
| it("returns true for exact matching grant", async () => { | ||
| const mizan = createMizan(); | ||
| mizan.registerSource("mem", sourceWith({ permission: "files.read", effect: "grant" })); | ||
| const auth = mizan.forPrincipal("user-1"); | ||
|
|
||
| expect(await auth.can("files.read")).toBe(true); | ||
| }); | ||
|
|
||
| it("returns false for exact matching denial", async () => { | ||
| const mizan = createMizan(); | ||
| mizan.registerSource("mem", sourceWith({ permission: "files.delete", effect: "deny" })); | ||
| const auth = mizan.forPrincipal("user-1"); | ||
|
|
||
| expect(await auth.can("files.delete")).toBe(false); | ||
| }); | ||
|
|
||
| it("returns false when no facts match (deny-by-default)", async () => { | ||
| const mizan = createMizan(); | ||
| mizan.registerSource("mem", sourceWith({ permission: "files.read", effect: "grant" })); | ||
| const auth = mizan.forPrincipal("user-1"); | ||
|
|
||
| expect(await auth.can("files.write")).toBe(false); | ||
| }); | ||
|
|
||
| it("denial overrides grant for the same permission", async () => { | ||
| const mizan = createMizan(); | ||
| mizan.registerSource( | ||
| "mem", | ||
| sourceWith( | ||
| { permission: "files.read", effect: "grant" }, | ||
| { permission: "files.read", effect: "deny" }, | ||
| ), | ||
| ); | ||
| const auth = mizan.forPrincipal("user-1"); | ||
|
|
||
| // Denial should override grant | ||
| expect(await auth.can("files.read")).toBe(false); | ||
| }); | ||
|
|
||
| it("does not affect unrelated permissions", async () => { | ||
| const mizan = createMizan(); | ||
| mizan.registerSource( | ||
| "mem", | ||
| sourceWith( | ||
| { permission: "files.read", effect: "grant" }, | ||
| { permission: "files.delete", effect: "deny" }, | ||
| ), | ||
| ); | ||
| const auth = mizan.forPrincipal("user-1"); | ||
|
|
||
| expect(await auth.can("files.read")).toBe(true); | ||
| expect(await auth.can("files.delete")).toBe(false); | ||
| expect(await auth.can("files.write")).toBe(false); | ||
| }); | ||
|
|
||
| it("merges facts from multiple sources", async () => { | ||
| const mizan = createMizan(); | ||
| mizan.registerSource("srcA", sourceWith({ permission: "files.read", effect: "grant" })); | ||
| mizan.registerSource("srcB", sourceWith({ permission: "files.write", effect: "grant" })); | ||
| const auth = mizan.forPrincipal("user-1"); | ||
|
|
||
| expect(await auth.can("files.read")).toBe(true); | ||
| expect(await auth.can("files.write")).toBe(true); | ||
| }); | ||
|
|
||
| it("denial overrides grant even across sources", async () => { | ||
| const mizan = createMizan(); | ||
| mizan.registerSource("srcA", sourceWith({ permission: "files.read", effect: "grant" })); | ||
| mizan.registerSource("srcB", sourceWith({ permission: "files.read", effect: "deny" })); | ||
| const auth = mizan.forPrincipal("user-1"); | ||
|
|
||
| // Denial from srcB overrides grant from srcA | ||
| expect(await auth.can("files.read")).toBe(false); | ||
| }); | ||
|
|
||
| it("empty facts produce deny", async () => { | ||
| const mizan = createMizan(); | ||
| mizan.registerSource("mem", emptySource()); | ||
| const auth = mizan.forPrincipal("user-1"); | ||
|
|
||
| expect(await auth.can("anything")).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| // ─── PrincipalEvaluator: decide() ────────────────────────────────────────── | ||
|
|
||
| describe("PrincipalEvaluator.decide()", () => { | ||
| it("returns allow with null reason for a matching grant", async () => { | ||
| const mizan = createMizan(); | ||
| mizan.registerSource("mem", sourceWith({ permission: "files.read", effect: "grant" })); | ||
| const auth = mizan.forPrincipal("user-1"); | ||
|
|
||
| const result = await auth.decide("files.read"); | ||
| expect(result.decision).toBe("allow"); | ||
| expect(result.reason).toBeNull(); | ||
| }); | ||
|
|
||
| it("returns deny with matching-denial reason for a matching denial", async () => { | ||
| const mizan = createMizan(); | ||
| mizan.registerSource("mem", sourceWith({ permission: "files.delete", effect: "deny" })); | ||
| const auth = mizan.forPrincipal("user-1"); | ||
|
|
||
| const result = await auth.decide("files.delete"); | ||
| expect(result.decision).toBe("deny"); | ||
| expect(result.reason).toBe("matching-denial"); | ||
| }); | ||
|
|
||
| it("returns deny with no-grant reason when no facts match", async () => { | ||
| const mizan = createMizan(); | ||
| mizan.registerSource("mem", sourceWith({ permission: "files.read", effect: "grant" })); | ||
| const auth = mizan.forPrincipal("user-1"); | ||
|
|
||
| const result = await auth.decide("nonexistent"); | ||
| expect(result.decision).toBe("deny"); | ||
| expect(result.reason).toBe("no-grant"); | ||
| }); | ||
|
|
||
| it("returns deny with matching-denial when denial overrides grant", async () => { | ||
| const mizan = createMizan(); | ||
| mizan.registerSource("mem", sourceWith({ permission: "files.read", effect: "grant" }, { permission: "files.read", effect: "deny" })); | ||
| const auth = mizan.forPrincipal("user-1"); | ||
|
|
||
| const result = await auth.decide("files.read"); | ||
| expect(result.decision).toBe("deny"); | ||
| expect(result.reason).toBe("matching-denial"); | ||
| }); | ||
|
|
||
| it("does not throw for expected outcomes", async () => { | ||
| const mizan = createMizan(); | ||
| mizan.registerSource("mem", emptySource()); | ||
| const auth = mizan.forPrincipal("user-1"); | ||
|
|
||
| expect(async () => await auth.can("anything")).not.toThrow(); | ||
| expect(async () => await auth.decide("anything")).not.toThrow(); | ||
| }); | ||
| }); | ||
|
|
||
| // ─── Configuration errors ────────────────────────────────────────────────── | ||
|
|
||
| describe("Configuration errors", () => { | ||
| it("throws when no sources are registered and can is called", async () => { | ||
| const mizan = createMizan(); | ||
| const auth = mizan.forPrincipal("user-1"); | ||
|
|
||
| expect(auth.can("anything")).rejects.toThrow(/no sources registered/i); | ||
| }); | ||
|
|
||
| it("throws when a source resolves with malformed status", async () => { | ||
| const mizan = createMizan(); | ||
| mizan.registerSource("bad", { | ||
| async resolve() { | ||
| // @ts-expect-error — intentionally malformed | ||
| return { status: "garbage", facts: [] }; | ||
| }, | ||
| }); | ||
| const auth = mizan.forPrincipal("user-1"); | ||
|
|
||
| expect(auth.can("x")).rejects.toThrow(/contract violation/i); | ||
| }); | ||
| }); | ||
|
|
||
| // ─── Principal binding ───────────────────────────────────────────────────── | ||
|
|
||
| describe("forPrincipal()", () => { | ||
| it("returns a PrincipalEvaluator bound to the given principal", () => { | ||
| const mizan = createMizan(); | ||
| const auth = mizan.forPrincipal("user-42"); | ||
| expect(auth).toBeInstanceOf(PrincipalEvaluator); | ||
| }); | ||
| }); | ||
|
|
||
| // ─── Mizan class API ─────────────────────────────────────────────────────── | ||
|
|
||
| describe("Mizan", () => { | ||
| it("registerSource stores a resolver", () => { | ||
| const mizan = createMizan(); | ||
| const resolver: SourceResolver = emptySource(); | ||
| mizan.registerSource("test", resolver); | ||
| // No throw means success | ||
| }); | ||
|
|
||
| it("can be exported and instantiated", () => { | ||
| const mizan = new Mizan(); | ||
| expect(mizan).toBeDefined(); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.