|
| 1 | +-- Test: ensure_workers() skips invocation when credentials exist but are empty strings |
| 2 | +begin; |
| 3 | +select plan(4); |
| 4 | +select pgflow_tests.reset_db(); |
| 5 | + |
| 6 | +-- Setup: Create Vault secrets with EMPTY strings |
| 7 | +select vault.create_secret('', 'supabase_service_role_key'); |
| 8 | +select vault.create_secret('', 'supabase_project_id'); |
| 9 | + |
| 10 | +-- Setup: Register a worker function |
| 11 | +select pgflow.track_worker_function('my-function'); |
| 12 | +update pgflow.worker_functions |
| 13 | +set last_invoked_at = now() - interval '10 seconds'; |
| 14 | + |
| 15 | +-- Simulate production mode (non-local jwt_secret) |
| 16 | +set local app.settings.jwt_secret = 'production-secret-different-from-local'; |
| 17 | + |
| 18 | +-- TEST: In production mode with empty credentials, returns empty (safe failure) |
| 19 | +with result as ( |
| 20 | + select * from pgflow.ensure_workers() |
| 21 | +) |
| 22 | +select is( |
| 23 | + (select count(*) from result), |
| 24 | + 0::bigint, |
| 25 | + 'Production mode with empty credentials returns empty (safe failure)' |
| 26 | +); |
| 27 | + |
| 28 | +-- TEST: No HTTP requests were queued (last_invoked_at unchanged) |
| 29 | +select ok( |
| 30 | + (select last_invoked_at < now() - interval '5 seconds' |
| 31 | + from pgflow.worker_functions |
| 32 | + where function_name = 'my-function'), |
| 33 | + 'last_invoked_at not updated when credentials are empty' |
| 34 | +); |
| 35 | + |
| 36 | +-- TEST: Empty project_id alone should skip (even with valid service role key) |
| 37 | +delete from vault.secrets where name = 'supabase_service_role_key'; |
| 38 | +select vault.create_secret('valid-service-role-key', 'supabase_service_role_key'); |
| 39 | + |
| 40 | +-- Reset debounce |
| 41 | +update pgflow.worker_functions |
| 42 | +set last_invoked_at = now() - interval '10 seconds'; |
| 43 | + |
| 44 | +with result as ( |
| 45 | + select * from pgflow.ensure_workers() |
| 46 | +) |
| 47 | +select is( |
| 48 | + (select count(*) from result), |
| 49 | + 0::bigint, |
| 50 | + 'Empty project_id alone causes skip even with valid service role key' |
| 51 | +); |
| 52 | + |
| 53 | +-- TEST: Empty service_role_key alone should skip (even with valid project_id) |
| 54 | +delete from vault.secrets where name in ('supabase_service_role_key', 'supabase_project_id'); |
| 55 | +select vault.create_secret('', 'supabase_service_role_key'); |
| 56 | +select vault.create_secret('validproject123', 'supabase_project_id'); |
| 57 | + |
| 58 | +-- Reset debounce |
| 59 | +update pgflow.worker_functions |
| 60 | +set last_invoked_at = now() - interval '10 seconds'; |
| 61 | + |
| 62 | +with result as ( |
| 63 | + select * from pgflow.ensure_workers() |
| 64 | +) |
| 65 | +select is( |
| 66 | + (select count(*) from result), |
| 67 | + 0::bigint, |
| 68 | + 'Empty service_role_key alone causes skip even with valid project_id' |
| 69 | +); |
| 70 | + |
| 71 | +select finish(); |
| 72 | +rollback; |
0 commit comments