@@ -13,6 +13,7 @@ import (
1313 "github.com/supabase/auth/internal/e2e"
1414 "github.com/supabase/auth/internal/hooks/hookshttp"
1515 "github.com/supabase/auth/internal/hooks/hookspgfunc"
16+ "github.com/supabase/auth/internal/models"
1617)
1718
1819type M = map [string ]any
@@ -31,9 +32,13 @@ func TestHooks(t *testing.T) {
3132 mr := NewManager (globalCfg , httpDr , pgfuncDr )
3233 now := time .Date (2024 , time .January , 1 , 0 , 0 , 0 , 0 , time .UTC )
3334
35+ httpReq := httptest .NewRequestWithContext (
36+ ctx , "GET" , "http://localhost/test" , nil )
37+
3438 type testCase struct {
3539 desc string
3640 setup func ()
41+ run func (* testCase , * Manager ) error
3742 sql string
3843 req any
3944 res any
@@ -218,6 +223,122 @@ func TestHooks(t *testing.T) {
218223 $$ language plpgsql;` ,
219224 },
220225
226+ {
227+ desc : "pass - before_user_created" ,
228+ setup : func () {
229+ globalCfg .Hook .BeforeUserCreated =
230+ conf.ExtensibilityPointConfiguration {
231+ URI : `pg-functions://postgres/auth/` +
232+ `v0hooks_test_before_user_created` ,
233+ HookName : `"auth"."v0hooks_test_before_user_created"` ,
234+ }
235+ },
236+ run : func (tc * testCase , mr * Manager ) error {
237+ return mr .BeforeUserCreated (
238+ ctx , db ,
239+ tc .req .(* BeforeUserCreatedInput ),
240+ tc .res .(* BeforeUserCreatedOutput ),
241+ )
242+ },
243+ req : NewBeforeUserCreatedInput (httpReq , & models.User {}),
244+ res : & BeforeUserCreatedOutput {},
245+ exp : & BeforeUserCreatedOutput {},
246+ sql : `
247+ create or replace function
248+ v0hooks_test_before_user_created(input jsonb)
249+ returns json as $$
250+ begin
251+ return '{}'::jsonb;
252+ end; $$ language plpgsql;` ,
253+ },
254+
255+ {
256+ desc : "pass - before_user_created reject" ,
257+ setup : func () {
258+ globalCfg .Hook .BeforeUserCreated =
259+ conf.ExtensibilityPointConfiguration {
260+ URI : `pg-functions://postgres/auth/` +
261+ `v0hooks_test_before_user_created_reject` ,
262+ HookName : `"auth"."v0hooks_test_before_user_created_reject"` ,
263+ }
264+ },
265+ run : func (tc * testCase , mr * Manager ) error {
266+ return mr .BeforeUserCreated (
267+ ctx , db ,
268+ tc .req .(* BeforeUserCreatedInput ),
269+ tc .res .(* BeforeUserCreatedOutput ),
270+ )
271+ },
272+ req : NewBeforeUserCreatedInput (httpReq , & models.User {}),
273+ res : & BeforeUserCreatedOutput {},
274+ exp : & BeforeUserCreatedOutput {Decision : "reject" },
275+ sql : `
276+ create or replace function
277+ v0hooks_test_before_user_created_reject(input jsonb)
278+ returns json as $$
279+ begin
280+ return '{"decision": "reject"}'::jsonb;
281+ end; $$ language plpgsql;` ,
282+ },
283+
284+ {
285+ desc : "pass - before_user_created reject with message" ,
286+ setup : func () {
287+ globalCfg .Hook .BeforeUserCreated =
288+ conf.ExtensibilityPointConfiguration {
289+ URI : `pg-functions://postgres/auth/` +
290+ `v0hooks_test_before_user_created_reject_msg` ,
291+ HookName : `"auth"."v0hooks_test_before_user_created_reject_msg"` ,
292+ }
293+ },
294+ run : func (tc * testCase , mr * Manager ) error {
295+ return mr .BeforeUserCreated (
296+ ctx , db ,
297+ tc .req .(* BeforeUserCreatedInput ),
298+ tc .res .(* BeforeUserCreatedOutput ),
299+ )
300+ },
301+ req : NewBeforeUserCreatedInput (httpReq , & models.User {}),
302+ res : & BeforeUserCreatedOutput {},
303+ exp : & BeforeUserCreatedOutput {Decision : "reject" , Message : "test case" },
304+ sql : `
305+ create or replace function
306+ v0hooks_test_before_user_created_reject_msg(input jsonb)
307+ returns json as $$
308+ begin
309+ return '{"decision": "reject", "message": "test case"}'::jsonb;
310+ end; $$ language plpgsql;` ,
311+ },
312+
313+ {
314+ desc : "pass - after_user_created" ,
315+ setup : func () {
316+ globalCfg .Hook .AfterUserCreated =
317+ conf.ExtensibilityPointConfiguration {
318+ URI : `pg-functions://postgres/auth/` +
319+ `v0hooks_test_after_user_created` ,
320+ HookName : `"auth"."v0hooks_test_after_user_created"` ,
321+ }
322+ },
323+ run : func (tc * testCase , mr * Manager ) error {
324+ return mr .AfterUserCreated (
325+ ctx , db ,
326+ tc .req .(* AfterUserCreatedInput ),
327+ tc .res .(* AfterUserCreatedOutput ),
328+ )
329+ },
330+ req : NewAfterUserCreatedInput (httpReq , & models.User {}),
331+ res : & AfterUserCreatedOutput {},
332+ exp : & AfterUserCreatedOutput {},
333+ sql : `
334+ create or replace function
335+ v0hooks_test_after_user_created(input jsonb)
336+ returns json as $$
337+ begin
338+ return '{}'::jsonb;
339+ end; $$ language plpgsql;` ,
340+ },
341+
221342 // fail
222343 {
223344 desc : "fail - customize_access_token - error propagation" ,
@@ -404,7 +525,12 @@ func TestHooks(t *testing.T) {
404525 }
405526
406527 htr := httptest .NewRequestWithContext (ctx , "POST" , "/api" , nil )
407- err := mr .InvokeHook (db , htr , tc .req , tc .res )
528+ var err error
529+ if tc .run == nil {
530+ err = mr .InvokeHook (db , htr , tc .req , tc .res )
531+ } else {
532+ err = tc .run (& tc , mr )
533+ }
408534 if tc .errStr != "" {
409535 require .Error (t , err )
410536 require .Contains (t , err .Error (), tc .errStr )
@@ -433,6 +559,12 @@ func TestConfig(t *testing.T) {
433559 PasswordVerificationAttempt : conf.ExtensibilityPointConfiguration {
434560 URI : "http:localhost/" + string (PasswordVerification ),
435561 },
562+ BeforeUserCreated : conf.ExtensibilityPointConfiguration {
563+ URI : "http:localhost/" + string (BeforeUserCreated ),
564+ },
565+ AfterUserCreated : conf.ExtensibilityPointConfiguration {
566+ URI : "http:localhost/" + string (AfterUserCreated ),
567+ },
436568 },
437569 }
438570 cfg := & globalCfg .Hook
@@ -457,6 +589,10 @@ func TestConfig(t *testing.T) {
457589 name : MFAVerification , exp : & cfg .MFAVerificationAttempt },
458590 {cfg : cfg , ok : true ,
459591 name : PasswordVerification , exp : & cfg .PasswordVerificationAttempt },
592+ {cfg : cfg , ok : true ,
593+ name : BeforeUserCreated , exp : & cfg .BeforeUserCreated },
594+ {cfg : cfg , ok : true ,
595+ name : AfterUserCreated , exp : & cfg .AfterUserCreated },
460596 }
461597 for idx , test := range tests {
462598 t .Logf ("test #%v - exp ok %v with cfg %v from name %v" ,
0 commit comments