begin; select plan(17); -- Total number of tests (reduced - removed active_subscriptions view tests) -- ============================================================================ -- View Existence Tests -- ============================================================================ SELECT has_view('public', 'user_tablos', 'user_tablos view should exist'); -- Note: active_subscriptions was replaced with get_my_active_subscription() function -- ============================================================================ -- 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'); -- Test that user_tablos view options include security_invoker SELECT ok( ( SELECT COUNT(*) 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' ) ) > 0, 'user_tablos view should use security_invoker=true' ); -- ============================================================================ -- 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(); view_tablo1_id text; view_tablo2_id text; BEGIN -- Insert test users INSERT INTO auth.users (id, instance_id, aud, role, email, encrypted_password, email_confirmed_at, created_at, updated_at) VALUES (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; -- Insert test profiles INSERT INTO public.profiles (id, email, first_name, last_name, short_user_id) VALUES (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; -- Insert test tablos INSERT INTO public.tablos (owner_id, name, status, position) VALUES (view_user1_id, 'View User 1 Tablo', 'todo', 0), (view_user2_id, 'View User 2 Tablo', 'in_progress', 1); -- 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' ); -- ============================================================================ -- Active Subscriptions Function Tests -- ============================================================================ -- 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'); -- ============================================================================ -- 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;