diff --git a/selfservice/hook/hooktest/web_hook_test_server.go b/selfservice/hook/hooktest/web_hook_test_server.go index ea608863b0e0..6111b48540a7 100644 --- a/selfservice/hook/hooktest/web_hook_test_server.go +++ b/selfservice/hook/hooktest/web_hook_test_server.go @@ -8,6 +8,7 @@ import ( "fmt" "net/http" "net/http/httptest" + "slices" "testing" "github.com/stretchr/testify/assert" @@ -15,6 +16,7 @@ import ( "github.com/tidwall/gjson" "github.com/ory/kratos/driver/config" + "github.com/ory/x/configx" "github.com/ory/x/ioutilx" ) @@ -56,3 +58,17 @@ func (s *Server) AssertTransientPayload(t *testing.T, expected string) { actual := gjson.GetBytes(s.LastBody, "flow.transient_payload").String() assert.JSONEq(t, expected, actual, "%+v", actual) } + +// SetConfig adds the webhook to the list of hooks for the given key and restores +// the original configuration after the test. +func (s *Server) SetConfig(t *testing.T, conf *configx.Provider, key string) { + var newValue []config.SelfServiceHook + original := conf.Get(key) + if originalHooks, ok := original.([]config.SelfServiceHook); ok { + newValue = slices.Clone(originalHooks) + } + require.NoError(t, conf.Set(key, append(newValue, s.HookConfig()))) + t.Cleanup(func() { + _ = conf.Set(key, original) + }) +} diff --git a/selfservice/strategy/oidc/strategy_test.go b/selfservice/strategy/oidc/strategy_test.go index 84d0ebb6d958..74ea4e0726d6 100644 --- a/selfservice/strategy/oidc/strategy_test.go +++ b/selfservice/strategy/oidc/strategy_test.go @@ -463,16 +463,10 @@ func TestStrategy(t *testing.T) { postLoginWebhook := hooktest.NewServer() t.Cleanup(postLoginWebhook.Close) - conf.MustSet(ctx, config.HookStrategyKey(config.ViperKeySelfServiceRegistrationAfter, - identity.CredentialsTypeOIDC.String()), []config.SelfServiceHook{{Name: "session"}, postRegistrationWebhook.HookConfig()}) - conf.MustSet(ctx, config.HookStrategyKey(config.ViperKeySelfServiceLoginAfter, config.HookGlobal), - []config.SelfServiceHook{postLoginWebhook.HookConfig()}) - - t.Cleanup(func() { - conf.MustSet(ctx, config.HookStrategyKey(config.ViperKeySelfServiceRegistrationAfter, - config.HookGlobal), []config.SelfServiceHook{{Name: "session"}}) - conf.MustSet(ctx, config.HookStrategyKey(config.ViperKeySelfServiceLoginAfter, identity.CredentialsTypeOIDC.String()), nil) - }) + postRegistrationWebhook.SetConfig(t, conf.GetProvider(ctx), + config.HookStrategyKey(config.ViperKeySelfServiceRegistrationAfter, identity.CredentialsTypeOIDC.String())) + postLoginWebhook.SetConfig(t, conf.GetProvider(ctx), + config.HookStrategyKey(config.ViperKeySelfServiceLoginAfter, config.HookGlobal)) subject = "register-then-login@ory.sh" scope = []string{"openid", "offline"} @@ -512,16 +506,10 @@ func TestStrategy(t *testing.T) { postLoginWebhook := hooktest.NewServer() t.Cleanup(postLoginWebhook.Close) - conf.MustSet(ctx, config.HookStrategyKey(config.ViperKeySelfServiceRegistrationAfter, - identity.CredentialsTypeOIDC.String()), []config.SelfServiceHook{{Name: "session"}, postRegistrationWebhook.HookConfig()}) - conf.MustSet(ctx, config.HookStrategyKey(config.ViperKeySelfServiceLoginAfter, config.HookGlobal), - []config.SelfServiceHook{postLoginWebhook.HookConfig()}) - - t.Cleanup(func() { - conf.MustSet(ctx, config.HookStrategyKey(config.ViperKeySelfServiceRegistrationAfter, - config.HookGlobal), []config.SelfServiceHook{{Name: "session"}}) - conf.MustSet(ctx, config.HookStrategyKey(config.ViperKeySelfServiceLoginAfter, identity.CredentialsTypeOIDC.String()), nil) - }) + postRegistrationWebhook.SetConfig(t, conf.GetProvider(ctx), + config.HookStrategyKey(config.ViperKeySelfServiceRegistrationAfter, identity.CredentialsTypeOIDC.String())) + postLoginWebhook.SetConfig(t, conf.GetProvider(ctx), + config.HookStrategyKey(config.ViperKeySelfServiceLoginAfter, config.HookGlobal)) subject = "login-without-register@ory.sh" scope = []string{"openid"}