-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregistry_base_test.go
91 lines (67 loc) · 2.77 KB
/
registry_base_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright © 2022 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package driver
import (
"context"
"errors"
"io"
"testing"
"github.com/ory/x/randx"
"github.com/stretchr/testify/require"
"github.com/ory/x/httpx"
"github.com/sirupsen/logrus"
"github.com/sirupsen/logrus/hooks/test"
"github.com/stretchr/testify/assert"
"github.com/ory/hydra/v2/driver/config"
"github.com/ory/x/configx"
"github.com/ory/x/contextx"
"github.com/ory/x/logrusx"
"github.com/gorilla/sessions"
)
func TestGetJWKSFetcherStrategyHostEnforcment(t *testing.T) {
ctx := context.Background()
l := logrusx.New("", "")
c := config.MustNew(context.Background(), l, configx.WithConfigFiles("../internal/.hydra.yaml"))
c.MustSet(ctx, config.KeyDSN, "memory")
c.MustSet(ctx, config.HSMEnabled, "false")
c.MustSet(ctx, config.ViperKeyClientHTTPNoPrivateIPRanges, true)
registry, err := NewRegistryWithoutInit(c, l)
require.NoError(t, err)
_, err = registry.GetJWKSFetcherStrategy().Resolve(ctx, "http://localhost:8080", true)
require.ErrorAs(t, err, new(httpx.ErrPrivateIPAddressDisallowed))
}
func TestRegistryBase_newKeyStrategy_handlesNetworkError(t *testing.T) {
// Test ensures any network specific error is logged with a
// specific message when attempting to create a new key strategy: issue #2338
hook := test.Hook{} // Test hook for asserting log messages
ctx := context.Background()
l := logrusx.New("", "", logrusx.WithHook(&hook))
l.Logrus().SetOutput(io.Discard)
l.Logrus().ExitFunc = func(int) {} // Override the exit func to avoid call to os.Exit
// Create a config and set a valid but unresolvable DSN
c := config.MustNew(context.Background(), l, configx.WithConfigFiles("../internal/.hydra.yaml"))
c.MustSet(ctx, config.KeyDSN, "postgres://user:password@127.0.0.1:9999/postgres")
c.MustSet(ctx, config.HSMEnabled, "false")
registry, err := NewRegistryWithoutInit(c, l)
if err != nil {
t.Errorf("Failed to create registry: %s", err)
return
}
r := registry.(*RegistrySQL)
r.initialPing = failedPing(errors.New("snizzles"))
_ = r.Init(context.Background(), true, false, &contextx.TestContextualizer{})
registryBase := RegistryBase{r: r, l: l}
registryBase.WithConfig(c)
assert.Equal(t, logrus.FatalLevel, hook.LastEntry().Level)
assert.Contains(t, hook.LastEntry().Message, "snizzles")
}
func TestRegistryBase_CookieStore_MaxAgeZero(t *testing.T) {
// Test ensures that CookieStore MaxAge option is equal to zero after initialization
ctx := context.Background()
r := new(RegistryBase)
r.WithConfig(config.MustNew(context.Background(), logrusx.New("", ""), configx.WithValue(config.KeyGetSystemSecret, []string{randx.MustString(32, randx.AlphaNum)})))
s, err := r.CookieStore(ctx)
require.NoError(t, err)
cs := s.(*sessions.CookieStore)
assert.Equal(t, cs.Options.MaxAge, 0)
}