refactor(errors): migrate decisions to {code,msg}
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { allow, deny, requireAuth } from "@/lib/policy/assert"
|
||||
import { allow, deny, requireAuth } from "@/lib/decision"
|
||||
import { describe, expect, it } from "vitest"
|
||||
|
||||
describe("policy decision helpers", () => {
|
||||
@@ -6,24 +6,22 @@ describe("policy decision helpers", () => {
|
||||
expect(allow()).toEqual({ ok: true })
|
||||
})
|
||||
|
||||
it("returns reason code for deny", () => {
|
||||
expect(deny("ROLE_FORBIDDEN", "forbidden")).toEqual({
|
||||
it("returns error for deny", () => {
|
||||
expect(deny(403, "forbidden")).toEqual({
|
||||
ok: false,
|
||||
reasonCode: "ROLE_FORBIDDEN",
|
||||
message: "forbidden",
|
||||
error: { code: 403, msg: "forbidden" },
|
||||
})
|
||||
})
|
||||
|
||||
it("requires auth actor", () => {
|
||||
expect(requireAuth(undefined)).toEqual({
|
||||
ok: false,
|
||||
reasonCode: "AUTH_REQUIRED",
|
||||
message: "请先登录",
|
||||
error: { code: 401, msg: "请先登录" },
|
||||
})
|
||||
|
||||
expect(
|
||||
requireAuth({
|
||||
userId: "u1",
|
||||
userId: "1001",
|
||||
role: "consumer",
|
||||
}),
|
||||
).toEqual({ ok: true })
|
||||
|
||||
Reference in New Issue
Block a user