MCPcopy
hub / github.com/ory/hydra / TestClientSDK

Function TestClientSDK

client/sdk_test.go:62–281  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

60var defaultIgnoreFields = []string{"client_id", "registration_access_token", "registration_client_uri", "created_at", "updated_at"}
61
62func TestClientSDK(t *testing.T) {
63 ctx := context.Background()
64 r := testhelpers.NewRegistryMemory(t, driver.WithConfigOptions(configx.WithValues(map[string]any{
65 config.KeySubjectTypesSupported: []string{"public"},
66 config.KeyDefaultClientScope: []string{"foo", "bar"},
67 config.KeyPublicAllowDynamicRegistration: true,
68 })))
69
70 routerAdmin := httprouterx.NewTestRouterAdminWithPrefix(t)
71 routerPublic := httprouterx.NewTestRouterPublic(t)
72 clHandler := client.NewHandler(r)
73 clHandler.SetPublicRoutes(routerPublic)
74 clHandler.SetAdminRoutes(routerAdmin)
75 o2Handler := oauth2.NewHandler(r)
76 o2Handler.SetPublicRoutes(routerPublic, func(h http.Handler) http.Handler { return h })
77 o2Handler.SetAdminRoutes(routerAdmin)
78
79 server := httptest.NewServer(routerAdmin)
80 t.Cleanup(server.Close)
81 publicServer := httptest.NewServer(routerPublic)
82 t.Cleanup(publicServer.Close)
83 r.Config().MustSet(ctx, config.KeyAdminURL, server.URL)
84 r.Config().MustSet(ctx, config.KeyOAuth2TokenURL, publicServer.URL+"/oauth2/token")
85
86 c := hydra.NewAPIClient(hydra.NewConfiguration())
87 c.GetConfig().Servers = hydra.ServerConfigurations{{URL: server.URL}}
88
89 t.Run("case=client default scopes are set", func(t *testing.T) {
90 result, _, err := c.OAuth2API.CreateOAuth2Client(ctx).OAuth2Client(hydra.OAuth2Client{}).Execute()
91 require.NoError(t, err)
92 assert.EqualValues(t, r.Config().DefaultClientScope(ctx), strings.Split(*result.Scope, " "))
93
94 _, err = c.OAuth2API.DeleteOAuth2Client(ctx, *result.ClientId).Execute()
95 require.NoError(t, err)
96 })
97
98 t.Run("case=client is created and updated", func(t *testing.T) {
99 createClient := createTestClient("")
100 compareClient := createClient
101 // This is not yet supported:
102 // createClient.SecretExpiresAt = 10
103
104 // returned client is correct on Create
105 result, res, err := c.OAuth2API.CreateOAuth2Client(ctx).OAuth2Client(createClient).Execute()
106 if !assert.NoError(t, err) {
107 t.Fatalf("error: %s", ioutilx.MustReadAll(res.Body))
108 }
109 assert.NotEmpty(t, result.UpdatedAt)
110 assert.NotEmpty(t, result.CreatedAt)
111 assert.NotEmpty(t, result.RegistrationAccessToken)
112 assert.NotEmpty(t, result.RegistrationClientUri)
113 assert.NotEmpty(t, *result.TosUri)
114 assert.NotEmpty(t, result.ClientId)
115 createClient.ClientId = result.ClientId
116
117 assertx.EqualAsJSONExcept(t, compareClient, result, defaultIgnoreFields)
118 assert.EqualValues(t, "bar", result.Metadata.(map[string]interface{})["foo"])
119

Callers

nothing calls this directly

Calls 15

SetPublicRoutesMethod · 0.95
SetAdminRoutesMethod · 0.95
GetConfigMethod · 0.95
NewRegistryMemoryFunction · 0.92
WithConfigOptionsFunction · 0.92
NewHandlerFunction · 0.92
NewHandlerFunction · 0.92
createTestClientFunction · 0.85
MustSetMethod · 0.80
CreateOAuth2ClientMethod · 0.80
DefaultClientScopeMethod · 0.80
DeleteOAuth2ClientMethod · 0.80

Tested by

no test coverage detected