Skip to content

Commit

Permalink
test: fix webhook test setup
Browse files Browse the repository at this point in the history
  • Loading branch information
hperl committed Mar 28, 2024
1 parent cbc65c1 commit 8b82e67
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 20 deletions.
16 changes: 16 additions & 0 deletions selfservice/hook/hooktest/web_hook_test_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import (
"fmt"
"net/http"
"net/http/httptest"
"slices"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tidwall/gjson"

"github.com/ory/kratos/driver/config"
"github.com/ory/x/configx"
"github.com/ory/x/ioutilx"
)

Expand Down Expand Up @@ -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)
})
}
28 changes: 8 additions & 20 deletions selfservice/strategy/oidc/strategy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "[email protected]"
scope = []string{"openid", "offline"}
Expand Down Expand Up @@ -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 = "[email protected]"
scope = []string{"openid"}
Expand Down

0 comments on commit 8b82e67

Please sign in to comment.