61 lines
1.8 KiB
TypeScript
61 lines
1.8 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
|
|
import {
|
|
AdminSalesScopeNotFoundError,
|
|
adminCommunicationReadWhere,
|
|
adminSalesDealReadWhere,
|
|
adminSalesLeadReadWhere,
|
|
adminSalesTaskReadWhere,
|
|
assertAdminSalesWriteOwner,
|
|
type AdminSalesAccessScope,
|
|
} from "./admin-sales-access-scope.js";
|
|
|
|
const ownScope = {
|
|
adminUserId: "sales-a",
|
|
canReadAll: false,
|
|
canWriteAll: false,
|
|
canReadAllCommunication: false,
|
|
} satisfies AdminSalesAccessScope;
|
|
|
|
describe("admin sales access scope", () => {
|
|
it("binds own-only sales and communication queries to the current AdminUser", () => {
|
|
expect(adminSalesLeadReadWhere(ownScope)).toEqual({
|
|
assignedToAdminUserId: "sales-a",
|
|
});
|
|
expect(adminSalesDealReadWhere(ownScope)).toEqual({
|
|
ownerAdminUserId: "sales-a",
|
|
});
|
|
expect(adminSalesTaskReadWhere(ownScope)).toEqual({
|
|
assignedToAdminUserId: "sales-a",
|
|
});
|
|
expect(adminCommunicationReadWhere(ownScope)).toEqual({
|
|
adminUserId: "sales-a",
|
|
});
|
|
});
|
|
|
|
it("removes owner predicates only for the matching all permissions", () => {
|
|
const readAll = {
|
|
...ownScope,
|
|
canReadAll: true,
|
|
canReadAllCommunication: true,
|
|
};
|
|
expect(adminSalesLeadReadWhere(readAll)).toEqual({});
|
|
expect(adminSalesDealReadWhere(readAll)).toEqual({});
|
|
expect(adminSalesTaskReadWhere(readAll)).toEqual({});
|
|
expect(adminCommunicationReadWhere(readAll)).toEqual({});
|
|
});
|
|
|
|
it("returns not-found semantics when an own-only actor targets another owner", () => {
|
|
expect(() => assertAdminSalesWriteOwner("sales-b", ownScope)).toThrow(
|
|
AdminSalesScopeNotFoundError
|
|
);
|
|
expect(() => assertAdminSalesWriteOwner("sales-a", ownScope)).not.toThrow();
|
|
expect(() =>
|
|
assertAdminSalesWriteOwner("sales-b", {
|
|
...ownScope,
|
|
canWriteAll: true,
|
|
})
|
|
).not.toThrow();
|
|
});
|
|
});
|