feat(tests): add more tests and make them pass

This commit is contained in:
2026-03-22 23:25:48 +01:00
parent 90b96079a0
commit c22d18063b
13 changed files with 3332 additions and 643 deletions
+8 -8
View File
@@ -67,10 +67,10 @@ BEGIN
WHERE ur.user_id = p_user_id
AND ur.org_id = p_org_id
UNION
-- Recursive case: senior roles (roles that inherit from assigned roles)
SELECT rh.senior_role_id, er.depth + 1
-- Recursive case: junior roles (inherited by the user's assigned roles)
SELECT rh.junior_role_id, er.depth + 1
FROM effective_roles er
INNER JOIN morbac.role_hierarchy rh ON rh.junior_role_id = er.role_id
INNER JOIN morbac.role_hierarchy rh ON rh.senior_role_id = er.role_id
)
SELECT DISTINCT ON (effective_roles.role_id) effective_roles.role_id, effective_roles.depth
FROM effective_roles
@@ -207,10 +207,10 @@ BEGIN
UNION
-- Role hierarchy (senior roles)
SELECT rh.senior_role_id, er.source || '_inherited', er.depth + 1
-- Role hierarchy (junior roles inherited by user's roles)
SELECT rh.junior_role_id, er.source || '_inherited', er.depth + 1
FROM effective_roles er
INNER JOIN morbac.role_hierarchy rh ON rh.junior_role_id = er.role_id
INNER JOIN morbac.role_hierarchy rh ON rh.senior_role_id = er.role_id
)
SELECT DISTINCT ON (effective_roles.role_id) effective_roles.role_id, effective_roles.source, effective_roles.depth
FROM effective_roles
@@ -277,9 +277,9 @@ BEGIN
FROM inherited_roles ir
INNER JOIN morbac.role_hierarchy rh ON rh.senior_role_id = ir.role_id
)
SELECT DISTINCT ON (role_id) role_id, depth
SELECT DISTINCT ON (inherited_roles.role_id) inherited_roles.role_id, inherited_roles.depth
FROM inherited_roles
ORDER BY role_id, depth;
ORDER BY inherited_roles.role_id, inherited_roles.depth;
END;
$$;
+8 -8
View File
@@ -4,7 +4,7 @@
-- Helper functions for Row-Level Security policies
-- Compatible with PostgREST
-- Get current user ID from request header
-- Get current user ID from session variable
CREATE OR REPLACE FUNCTION morbac.current_user_id()
RETURNS UUID
LANGUAGE plpgsql
@@ -13,8 +13,8 @@ AS $$
DECLARE
v_user_id TEXT;
BEGIN
-- Read from PostgREST request.header.x-user-id setting
v_user_id := current_setting('request.header.x-user-id', TRUE);
-- Read from morbac.user_id GUC (set via SET morbac.user_id = '...' or PostgREST header mapping)
v_user_id := current_setting('morbac.user_id', TRUE);
IF v_user_id IS NULL OR v_user_id = '' THEN
RETURN NULL;
@@ -28,9 +28,9 @@ END;
$$;
COMMENT ON FUNCTION morbac.current_user_id() IS
'Returns current user ID from request.header.x-user-id (PostgREST compatible)';
'Returns current user ID from morbac.user_id session variable';
-- Get current organization ID from request header
-- Get current organization ID from session variable
CREATE OR REPLACE FUNCTION morbac.current_org_id()
RETURNS UUID
LANGUAGE plpgsql
@@ -39,8 +39,8 @@ AS $$
DECLARE
v_org_id TEXT;
BEGIN
-- Read from PostgREST request.header.x-org-id setting
v_org_id := current_setting('request.header.x-org-id', TRUE);
-- Read from morbac.org_id GUC (set via SET morbac.org_id = '...' or PostgREST header mapping)
v_org_id := current_setting('morbac.org_id', TRUE);
IF v_org_id IS NULL OR v_org_id = '' THEN
RETURN NULL;
@@ -54,7 +54,7 @@ END;
$$;
COMMENT ON FUNCTION morbac.current_org_id() IS
'Returns current organization ID from request.header.x-org-id (PostgREST compatible)';
'Returns current organization ID from morbac.org_id session variable';
-- RLS check function
CREATE OR REPLACE FUNCTION morbac.rls_check(