Skip to content

Commit 16cc4c9

Browse files
author
Chris Stockton
committed
feat: add Enabled method to internal/hooks/v0hooks.Manager
This creates a concrete mapping of const -> config value. This will be used in future pull requests as well as the new Enabled method which guards calls to disabled hooks in v0hooks.Manager.
1 parent 57744e8 commit 16cc4c9

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

internal/hooks/v0hooks/manager.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,33 @@ func NewManager(
3636
}
3737
}
3838

39+
func (o *Manager) Enabled(name Name) bool {
40+
if cfg, ok := configByName(&o.config.Hook, name); ok {
41+
return cfg.Enabled
42+
}
43+
return false
44+
}
45+
46+
func configByName(
47+
cfg *conf.HookConfiguration,
48+
name Name,
49+
) (*conf.ExtensibilityPointConfiguration, bool) {
50+
switch name {
51+
case SendSMS:
52+
return &cfg.SendSMS, true
53+
case SendEmail:
54+
return &cfg.SendEmail, true
55+
case CustomizeAccessToken:
56+
return &cfg.CustomAccessToken, true
57+
case MFAVerification:
58+
return &cfg.MFAVerificationAttempt, true
59+
case PasswordVerification:
60+
return &cfg.PasswordVerificationAttempt, true
61+
default:
62+
return nil, false
63+
}
64+
}
65+
3966
func (o *Manager) InvokeHook(
4067
conn *storage.Connection,
4168
r *http.Request,

internal/hooks/v0hooks/manager_test.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,3 +414,65 @@ func TestHooks(t *testing.T) {
414414
require.Equal(t, tc.exp, tc.res)
415415
}
416416
}
417+
418+
func TestConfig(t *testing.T) {
419+
globalCfg := &conf.GlobalConfiguration{
420+
Hook: conf.HookConfiguration{
421+
SendSMS: conf.ExtensibilityPointConfiguration{
422+
URI: "http:localhost/" + string(SendSMS),
423+
},
424+
SendEmail: conf.ExtensibilityPointConfiguration{
425+
URI: "http:localhost/" + string(SendEmail),
426+
},
427+
CustomAccessToken: conf.ExtensibilityPointConfiguration{
428+
URI: "http:localhost/" + string(CustomizeAccessToken),
429+
},
430+
MFAVerificationAttempt: conf.ExtensibilityPointConfiguration{
431+
URI: "http:localhost/" + string(MFAVerification),
432+
},
433+
PasswordVerificationAttempt: conf.ExtensibilityPointConfiguration{
434+
URI: "http:localhost/" + string(PasswordVerification),
435+
},
436+
},
437+
}
438+
cfg := &globalCfg.Hook
439+
440+
mr := new(Manager)
441+
mr.config = globalCfg
442+
443+
tests := []struct {
444+
cfg *conf.HookConfiguration
445+
name Name
446+
exp *conf.ExtensibilityPointConfiguration
447+
ok bool
448+
}{
449+
{},
450+
{cfg: cfg, ok: true,
451+
name: SendSMS, exp: &cfg.SendSMS},
452+
{cfg: cfg, ok: true,
453+
name: SendEmail, exp: &cfg.SendEmail},
454+
{cfg: cfg, ok: true,
455+
name: CustomizeAccessToken, exp: &cfg.CustomAccessToken},
456+
{cfg: cfg, ok: true,
457+
name: MFAVerification, exp: &cfg.MFAVerificationAttempt},
458+
{cfg: cfg, ok: true,
459+
name: PasswordVerification, exp: &cfg.PasswordVerificationAttempt},
460+
}
461+
for idx, test := range tests {
462+
t.Logf("test #%v - exp ok %v with cfg %v from name %v",
463+
idx, test.ok, test.exp, test.name)
464+
465+
require.Equal(t, false, mr.Enabled(test.name))
466+
467+
got, ok := configByName(test.cfg, test.name)
468+
require.Equal(t, test.ok, ok)
469+
require.Equal(t, test.exp, got)
470+
471+
if got == nil {
472+
continue
473+
}
474+
475+
got.Enabled = true
476+
require.Equal(t, true, mr.Enabled(test.name))
477+
}
478+
}

0 commit comments

Comments
 (0)