-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd_helper_test.go
90 lines (74 loc) · 2.68 KB
/
cmd_helper_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
// Copyright © 2022 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package cmd_test
import (
"context"
"encoding/base64"
"net/http/httptest"
"os"
"testing"
"github.com/gofrs/uuid"
"github.com/go-jose/go-jose/v3"
"github.com/spf13/cobra"
"github.com/stretchr/testify/require"
"github.com/ory/hydra/v2/client"
"github.com/ory/hydra/v2/driver"
"github.com/ory/hydra/v2/internal"
"github.com/ory/hydra/v2/internal/testhelpers"
"github.com/ory/x/cmdx"
"github.com/ory/x/contextx"
"github.com/ory/x/snapshotx"
)
func base64EncodedPGPPublicKey(t *testing.T) string {
t.Helper()
gpgPublicKey, err := os.ReadFile("../test/stub/pgp.pub")
if err != nil {
t.Fatal(err)
}
return base64.StdEncoding.EncodeToString(gpgPublicKey)
}
func setupRoutes(t *testing.T, cmd *cobra.Command) (*httptest.Server, *httptest.Server, driver.Registry) {
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
reg := internal.NewMockedRegistry(t, &contextx.Default{})
public, admin := testhelpers.NewOAuth2Server(ctx, t, reg)
cmdx.RegisterHTTPClientFlags(cmd.Flags())
cmdx.RegisterFormatFlags(cmd.Flags())
require.NoError(t, cmd.Flags().Set(cmdx.FlagFormat, string(cmdx.FormatJSON)))
return public, admin, reg
}
func setup(t *testing.T, cmd *cobra.Command) driver.Registry {
_, admin, reg := setupRoutes(t, cmd)
require.NoError(t, cmd.Flags().Set(cmdx.FlagEndpoint, admin.URL))
require.NoError(t, cmd.Flags().Set(cmdx.FlagFormat, string(cmdx.FormatJSON)))
return reg
}
var snapshotExcludedClientFields = []snapshotx.ExceptOpt{
snapshotx.ExceptNestedKeys("client_id"),
snapshotx.ExceptNestedKeys("registration_access_token"),
snapshotx.ExceptNestedKeys("registration_client_uri"),
snapshotx.ExceptNestedKeys("client_secret"),
snapshotx.ExceptNestedKeys("created_at"),
snapshotx.ExceptNestedKeys("updated_at"),
}
func createClientCredentialsClient(t *testing.T, reg driver.Registry) *client.Client {
return createClient(t, reg, &client.Client{
GrantTypes: []string{"client_credentials"},
TokenEndpointAuthMethod: "client_secret_basic",
Secret: uuid.Must(uuid.NewV4()).String(),
})
}
func createClient(t *testing.T, reg driver.Registry, c *client.Client) *client.Client {
if c == nil {
c = &client.Client{TokenEndpointAuthMethod: "client_secret_post", Secret: uuid.Must(uuid.NewV4()).String()}
}
secret := c.Secret
require.NoError(t, reg.ClientManager().CreateClient(context.Background(), c))
c.Secret = secret
return c
}
func createJWK(t *testing.T, reg driver.Registry, set string, alg string) jose.JSONWebKey {
c, err := reg.KeyManager().GenerateAndPersistKeySet(context.Background(), set, "", alg, "sig")
require.NoError(t, err)
return c.Keys[0]
}