Skip to content

Commit d3d75d6

Browse files
authored
fix: clear worker_functions table in reset and enhance worker credential tests (#521)
# Add worker_functions table to reset_db and enhance HTTP request tests This PR improves the database reset functionality by adding the `worker_functions` table to the list of tables cleared during reset operations. It also enhances the test coverage for worker function HTTP requests by adding several new test cases that verify: 1. The correct construction of HTTP request URLs using base URLs from Vault 2. Proper inclusion of service role keys in Authorization headers when using Vault credentials 3. Appropriate fallback behavior for local development environments 4. Verification of the URL format pattern for function invocations These changes ensure that worker functions are properly invoked with the correct credentials and endpoints in both production and local development environments.
1 parent d784ae0 commit d3d75d6

File tree

4 files changed

+84
-3
lines changed

4 files changed

+84
-3
lines changed

pkgs/core/supabase/seed.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ BEGIN
1313
DELETE FROM pgflow.deps;
1414
DELETE FROM pgflow.steps;
1515
DELETE FROM pgflow.flows;
16+
DELETE FROM pgflow.worker_functions;
1617

1718
-- Also clear the realtime.messages table if it exists
1819
BEGIN

pkgs/core/supabase/tests/ensure_workers/credentials_from_vault.test.sql

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
-- Test: ensure_workers() retrieves credentials from Vault
22
begin;
3-
select plan(2);
3+
select plan(4);
44
select pgflow_tests.reset_db();
55

66
-- Setup: Create Vault secrets
@@ -43,5 +43,37 @@ select ok(
4343
'Vault credentials allow HTTP invocation in production mode'
4444
);
4545

46+
-- TEST: HTTP request URL uses base URL from Vault
47+
update pgflow.worker_functions
48+
set last_invoked_at = now() - interval '10 seconds';
49+
50+
-- Store result in temp table to ensure ensure_workers() executes before we query the queue
51+
select * into temporary test3_result from pgflow.ensure_workers();
52+
53+
select ok(
54+
(select url = 'http://vault-configured-url.example.com/functions/v1/my-function'
55+
from net.http_request_queue
56+
where id = (select request_id from test3_result limit 1)),
57+
'HTTP request URL is constructed from Vault pgflow_function_base_url'
58+
);
59+
60+
drop table test3_result;
61+
62+
-- TEST: HTTP request Authorization header uses service role key from Vault
63+
update pgflow.worker_functions
64+
set last_invoked_at = now() - interval '10 seconds';
65+
66+
-- Store result in temp table to ensure ensure_workers() executes before we query the queue
67+
select * into temporary test4_result from pgflow.ensure_workers();
68+
69+
select ok(
70+
(select headers->>'Authorization' = 'Bearer test-service-role-key-from-vault'
71+
from net.http_request_queue
72+
where id = (select request_id from test4_result limit 1)),
73+
'HTTP request Authorization header contains Vault service role key'
74+
);
75+
76+
drop table test4_result;
77+
4678
select finish();
4779
rollback;

pkgs/core/supabase/tests/ensure_workers/credentials_local_fallback.test.sql

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
-- Test: ensure_workers() uses local fallback credentials when Vault is empty
22
begin;
3-
select plan(2);
3+
select plan(4);
44
select pgflow_tests.reset_db();
55

66
-- Ensure no Vault secrets exist
@@ -36,5 +36,37 @@ select ok(
3636
'Local fallback credentials allow HTTP invocation'
3737
);
3838

39+
-- TEST: HTTP request URL uses local fallback base URL
40+
update pgflow.worker_functions
41+
set last_invoked_at = now() - interval '10 seconds';
42+
43+
-- Store result in temp table to ensure ensure_workers() executes before we query the queue
44+
select * into temporary test3_result from pgflow.ensure_workers();
45+
46+
select ok(
47+
(select url = 'http://kong:8000/functions/v1/my-function'
48+
from net.http_request_queue
49+
where id = (select request_id from test3_result limit 1)),
50+
'HTTP request URL uses local fallback (http://kong:8000/functions/v1)'
51+
);
52+
53+
drop table test3_result;
54+
55+
-- TEST: HTTP request has no Authorization header in local mode
56+
update pgflow.worker_functions
57+
set last_invoked_at = now() - interval '10 seconds';
58+
59+
-- Store result in temp table to ensure ensure_workers() executes before we query the queue
60+
select * into temporary test4_result from pgflow.ensure_workers();
61+
62+
select ok(
63+
(select headers->>'Authorization' is null
64+
from net.http_request_queue
65+
where id = (select request_id from test4_result limit 1)),
66+
'HTTP request has no Authorization header in local mode'
67+
);
68+
69+
drop table test4_result;
70+
3971
select finish();
4072
rollback;

pkgs/core/supabase/tests/ensure_workers/http_request_queued.test.sql

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
-- Test: ensure_workers() queues HTTP request via pg_net
22
begin;
3-
select plan(4);
3+
select plan(5);
44
select pgflow_tests.reset_db();
55

66
-- Clear any existing HTTP requests
@@ -44,6 +44,22 @@ select ok(
4444
'HTTP request was queued (request_id returned)'
4545
);
4646

47+
-- TEST: HTTP request URL format is correct (base_url/function_name)
48+
update pgflow.worker_functions
49+
set last_invoked_at = now() - interval '10 seconds';
50+
51+
-- Store result in temp table to ensure ensure_workers() executes before we query the queue
52+
select * into temporary test_url_result from pgflow.ensure_workers();
53+
54+
select ok(
55+
(select url LIKE '%/functions/v1/my-function'
56+
from net.http_request_queue
57+
where id = (select request_id from test_url_result limit 1)),
58+
'HTTP request URL ends with /functions/v1/{function_name}'
59+
);
60+
61+
drop table test_url_result;
62+
4763
-- TEST: Multiple functions each get their own request
4864
select pgflow.track_worker_function('function-two');
4965
update pgflow.worker_functions

0 commit comments

Comments
 (0)