Skip to content

Commit ca67be0

Browse files
author
Chris Stockton
committed
fix: incorporate PR feedback to only use InvokeHook
1 parent 829aec6 commit ca67be0

File tree

2 files changed

+30
-53
lines changed

2 files changed

+30
-53
lines changed

internal/hooks/v0hooks/manager.go

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -67,24 +67,6 @@ func configByName(
6767
}
6868
}
6969

70-
func (o *Manager) BeforeUserCreated(
71-
ctx context.Context,
72-
tx *storage.Connection,
73-
req *BeforeUserCreatedInput,
74-
res *BeforeUserCreatedOutput,
75-
) error {
76-
return o.dispatch(ctx, &o.config.Hook.BeforeUserCreated, tx, req, res)
77-
}
78-
79-
func (o *Manager) AfterUserCreated(
80-
ctx context.Context,
81-
tx *storage.Connection,
82-
req *AfterUserCreatedInput,
83-
res *AfterUserCreatedOutput,
84-
) error {
85-
return o.dispatch(ctx, &o.config.Hook.AfterUserCreated, tx, req, res)
86-
}
87-
8870
func (o *Manager) InvokeHook(
8971
conn *storage.Connection,
9072
r *http.Request,
@@ -147,6 +129,23 @@ func (o *Manager) invokeHook(
147129
}
148130
return o.dispatch(
149131
r.Context(), &o.config.Hook.CustomAccessToken, conn, input, output)
132+
133+
case *BeforeUserCreatedInput:
134+
if _, ok := output.(*BeforeUserCreatedOutput); !ok {
135+
return apierrors.NewInternalServerError(
136+
"output should be *hooks.BeforeUserCreatedOutput")
137+
}
138+
return o.dispatch(
139+
r.Context(), &o.config.Hook.BeforeUserCreated, conn, input, output)
140+
141+
case *AfterUserCreatedInput:
142+
_, ok := output.(*AfterUserCreatedOutput)
143+
if !ok {
144+
return apierrors.NewInternalServerError(
145+
"output should be *hooks.AfterUserCreatedOutput")
146+
}
147+
return o.dispatch(
148+
r.Context(), &o.config.Hook.AfterUserCreated, conn, input, output)
150149
}
151150
}
152151

internal/hooks/v0hooks/manager_test.go

Lines changed: 13 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ func TestHooks(t *testing.T) {
3838
type testCase struct {
3939
desc string
4040
setup func()
41-
run func(*testCase, *Manager) error
4241
sql string
4342
req any
4443
res any
@@ -233,13 +232,6 @@ func TestHooks(t *testing.T) {
233232
HookName: `"auth"."v0hooks_test_before_user_created"`,
234233
}
235234
},
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-
},
243235
req: NewBeforeUserCreatedInput(httpReq, &models.User{}),
244236
res: &BeforeUserCreatedOutput{},
245237
exp: &BeforeUserCreatedOutput{},
@@ -262,13 +254,6 @@ func TestHooks(t *testing.T) {
262254
HookName: `"auth"."v0hooks_test_before_user_created_reject"`,
263255
}
264256
},
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-
},
272257
req: NewBeforeUserCreatedInput(httpReq, &models.User{}),
273258
res: &BeforeUserCreatedOutput{},
274259
exp: &BeforeUserCreatedOutput{Decision: "reject"},
@@ -291,13 +276,6 @@ func TestHooks(t *testing.T) {
291276
HookName: `"auth"."v0hooks_test_before_user_created_reject_msg"`,
292277
}
293278
},
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-
},
301279
req: NewBeforeUserCreatedInput(httpReq, &models.User{}),
302280
res: &BeforeUserCreatedOutput{},
303281
exp: &BeforeUserCreatedOutput{Decision: "reject", Message: "test case"},
@@ -320,13 +298,6 @@ func TestHooks(t *testing.T) {
320298
HookName: `"auth"."v0hooks_test_after_user_created"`,
321299
}
322300
},
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-
},
330301
req: NewAfterUserCreatedInput(httpReq, &models.User{}),
331302
res: &AfterUserCreatedOutput{},
332303
exp: &AfterUserCreatedOutput{},
@@ -437,6 +408,18 @@ func TestHooks(t *testing.T) {
437408
res: M{},
438409
errStr: "500: output should be *hooks.PasswordVerificationAttemptOutput",
439410
},
411+
{
412+
desc: "fail - before_user_created - invalid output type",
413+
req: &BeforeUserCreatedInput{},
414+
res: M{},
415+
errStr: "500: output should be *hooks.BeforeUserCreatedOutput",
416+
},
417+
{
418+
desc: "fail - after_user_created - invalid output type",
419+
req: &AfterUserCreatedInput{},
420+
res: M{},
421+
errStr: "500: output should be *hooks.AfterUserCreatedOutput",
422+
},
440423

441424
// fail - invalid query
442425
{
@@ -525,12 +508,7 @@ func TestHooks(t *testing.T) {
525508
}
526509

527510
htr := httptest.NewRequestWithContext(ctx, "POST", "/api", nil)
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-
}
511+
err := mr.InvokeHook(db, htr, tc.req, tc.res)
534512
if tc.errStr != "" {
535513
require.Error(t, err)
536514
require.Contains(t, err.Error(), tc.errStr)

0 commit comments

Comments
 (0)