xtablo-source/supabase/tests/database/07_views.test.sql

170 lines
6.1 KiB
MySQL
Raw Normal View History

2025-11-04 21:20:56 +00:00
begin;
2025-11-06 07:38:38 +00:00
select plan(17); -- Total number of tests (reduced - removed active_subscriptions view tests)
2025-11-04 21:20:56 +00:00
-- ============================================================================
-- View Existence Tests
-- ============================================================================
SELECT has_view('public', 'user_tablos',
'user_tablos view should exist');
2025-11-06 07:38:38 +00:00
-- Note: active_subscriptions was replaced with get_my_active_subscription() function
2025-11-04 21:20:56 +00:00
-- ============================================================================
-- User Tablos View Tests
-- ============================================================================
-- Test that user_tablos view has expected columns
SELECT has_column('public', 'user_tablos', 'id',
'user_tablos view should have id column');
SELECT has_column('public', 'user_tablos', 'user_id',
'user_tablos view should have user_id column');
SELECT has_column('public', 'user_tablos', 'name',
'user_tablos view should have name column');
SELECT has_column('public', 'user_tablos', 'status',
'user_tablos view should have status column');
SELECT has_column('public', 'user_tablos', 'access_level',
'user_tablos view should have access_level column');
SELECT has_column('public', 'user_tablos', 'is_admin',
'user_tablos view should have is_admin column');
SELECT has_column('public', 'user_tablos', 'position',
'user_tablos view should have position column');
SELECT has_column('public', 'user_tablos', 'deleted_at',
'user_tablos view should have deleted_at column');
2025-11-06 07:38:38 +00:00
-- Test that user_tablos view options include security_invoker
2025-11-04 21:20:56 +00:00
SELECT ok(
(
SELECT COUNT(*)
2025-11-06 07:38:38 +00:00
FROM pg_class c
JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE n.nspname = 'public'
AND c.relname = 'user_tablos'
AND c.relkind = 'v'
AND EXISTS (
SELECT 1 FROM pg_options_to_table(c.reloptions)
WHERE option_name = 'security_invoker' AND option_value = 'true'
)
2025-11-04 21:20:56 +00:00
) > 0,
2025-11-06 07:38:38 +00:00
'user_tablos view should use security_invoker=true'
2025-11-04 21:20:56 +00:00
);
-- ============================================================================
-- User Tablos View Behavior Tests
-- ============================================================================
-- Create test data for view testing
DO $$
DECLARE
view_user1_id uuid := gen_random_uuid();
view_user2_id uuid := gen_random_uuid();
2025-11-06 07:38:38 +00:00
view_tablo1_id text;
view_tablo2_id text;
2025-11-04 21:20:56 +00:00
BEGIN
-- Insert test users
INSERT INTO auth.users (id, instance_id, aud, role, email, encrypted_password, email_confirmed_at, created_at, updated_at)
VALUES
2025-11-06 07:38:38 +00:00
(view_user1_id, '00000000-0000-0000-0000-000000000000', 'authenticated', 'authenticated', 'viewuser1_' || view_user1_id::text || '@test.com', 'encrypted', now(), now(), now()),
(view_user2_id, '00000000-0000-0000-0000-000000000000', 'authenticated', 'authenticated', 'viewuser2_' || view_user2_id::text || '@test.com', 'encrypted', now(), now(), now())
ON CONFLICT DO NOTHING;
2025-11-04 21:20:56 +00:00
-- Insert test profiles
2025-11-06 07:38:38 +00:00
INSERT INTO public.profiles (id, email, first_name, last_name, short_user_id)
2025-11-04 21:20:56 +00:00
VALUES
2025-11-06 07:38:38 +00:00
(view_user1_id, 'viewuser1_' || view_user1_id::text || '@test.com', 'View User', 'One', substring(view_user1_id::text from 1 for 8)),
(view_user2_id, 'viewuser2_' || view_user2_id::text || '@test.com', 'View User', 'Two', substring(view_user2_id::text from 1 for 8))
ON CONFLICT DO NOTHING;
2025-11-04 21:20:56 +00:00
-- Insert test tablos
INSERT INTO public.tablos (owner_id, name, status, position)
VALUES
(view_user1_id, 'View User 1 Tablo', 'todo', 0),
2025-11-06 07:38:38 +00:00
(view_user2_id, 'View User 2 Tablo', 'in_progress', 1);
2025-11-04 21:20:56 +00:00
-- Store test IDs
PERFORM set_config('test.view_user1_id', view_user1_id::text, true);
PERFORM set_config('test.view_user2_id', view_user2_id::text, true);
END $$;
-- Test: Verify user_tablos returns tablos for users
SELECT ok(
(SELECT count(*) FROM public.user_tablos WHERE user_id = current_setting('test.view_user1_id')::uuid) > 0,
'user_tablos should return tablos for user 1'
);
SELECT ok(
(SELECT count(*) FROM public.user_tablos WHERE user_id = current_setting('test.view_user2_id')::uuid) > 0,
'user_tablos should return tablos for user 2'
);
-- Test: Verify access_level is set correctly for owner
SELECT is(
(
SELECT access_level
FROM public.user_tablos
WHERE user_id = current_setting('test.view_user1_id')::uuid
AND name = 'View User 1 Tablo'
LIMIT 1
),
'admin',
'Owner should have admin access_level in user_tablos view'
);
-- Test: Verify is_admin is true for owner
SELECT is(
(
SELECT is_admin
FROM public.user_tablos
WHERE user_id = current_setting('test.view_user1_id')::uuid
AND name = 'View User 1 Tablo'
LIMIT 1
),
true,
'Owner should have is_admin true in user_tablos view'
);
-- Test: Verify deleted tablos are filtered out
SELECT is(
(SELECT count(*) FROM public.user_tablos WHERE deleted_at IS NOT NULL),
0::bigint,
'user_tablos view should not return deleted tablos'
);
-- ============================================================================
2025-11-06 07:38:38 +00:00
-- Active Subscriptions Function Tests
2025-11-04 21:20:56 +00:00
-- ============================================================================
2025-11-06 07:38:38 +00:00
-- Note: active_subscriptions view was replaced with get_my_active_subscription() function
-- Testing the function instead
SELECT has_function('public', 'get_my_active_subscription',
'get_my_active_subscription function should exist');
2025-11-04 21:20:56 +00:00
-- ============================================================================
-- View Comments and Documentation
-- ============================================================================
-- Test that views have documentation comments
SELECT ok(
(
SELECT obj_description(c.oid) IS NOT NULL
FROM pg_class c
JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE n.nspname = 'public'
AND c.relname = 'user_tablos'
AND c.relkind = 'v'
LIMIT 1
),
'user_tablos view should have documentation comment'
);
select * from finish();
rollback;