Debian Codegraph @

pkg/client/client_test.go

raw ◀ ▶
1 Debian patch touch this file
1 package client
2
3 import (
4 "context"
5 "errors"
6 "net/http"
7 "net/http/httptest"
8 "net/url"
9 "reflect"
10 "testing"
11
12 "github.com/stretchr/testify/assert"
13 "github.com/stretchr/testify/require"
•14 "github.com/zitadel/oidc/v3/pkg/oidc"
15 )
16
17 func TestDiscover(t *testing.T) {
18 t.Skip("Skipping test that needs Internet access")
19 type wantFields struct {
20 UILocalesSupported bool
21 }
22
23 type args struct {
24 issuer string
25 wellKnownUrl []string
26 }
27 tests := []struct {
28 name string
29 args args
30 wantFields *wantFields
31 wantErr error
32 }{
33 {
34 name: "spotify", // https://github.com/zitadel/oidc/issues/406
35 args: args{
36 issuer: "https://accounts.spotify.com",
37 },
38 wantFields: &wantFields{
39 UILocalesSupported: true,
40 },
41 wantErr: nil,
42 },
43 {
44 name: "discovery failed",
45 args: args{
46 issuer: "https://example.com",
47 },
48 wantErr: oidc.ErrDiscoveryFailed,
49 },
50 }
51 for _, tt := range tests {
52 t.Run(tt.name, func(t *testing.T) {
53 got, err := Discover(context.Background(), tt.args.issuer, http.DefaultClient, tt.args.wellKnownUrl...)
54 require.ErrorIs(t, err, tt.wantErr)
55 if tt.wantFields == nil {
56 return
57 }
58 assert.Equal(t, tt.args.issuer, got.Issuer)
59 if tt.wantFields.UILocalesSupported {
60 assert.NotEmpty(t, got.UILocalesSupported)
61 }
62 })
63 }
64 }
65
66 type mockRevokeCaller struct {
67 endpoint string
68 httpClient *http.Client
69 }
70
71 func (m *mockRevokeCaller) GetRevokeEndpoint() string {
72 return m.endpoint
73 }
74
75 func (m *mockRevokeCaller) HttpClient() *http.Client {
76 return m.httpClient
77 }
78
79 type mockEndSessionCaller struct {
80 endpoint string
81 httpClient *http.Client
82 }
83
84 func (m *mockEndSessionCaller) GetEndSessionEndpoint() string {
85 return m.endpoint
86 }
87
88 func (m *mockEndSessionCaller) HttpClient() *http.Client {
89 return m.httpClient
90 }
91
92 type mockDeviceAuthorizationCaller struct {
93 endpoint string
94 httpClient *http.Client
95 }
96
97 func (m *mockDeviceAuthorizationCaller) GetDeviceAuthorizationEndpoint() string {
98 return m.endpoint
99 }
100
101 func (m *mockDeviceAuthorizationCaller) HttpClient() *http.Client {
102 return m.httpClient
103 }
104
105 func TestCallDeviceAuthorizationEndpointOptions(t *testing.T) {
106 tests := []struct {
107 name string
108 opts []DeviceAuthorizationOption
109 wantJKT string
110 }{
111 {name: "without options"},
112 {name: "with dpop_jkt", opts: []DeviceAuthorizationOption{WithDPoPJKT("thumbprint")}, wantJKT: "thumbprint"},
113 }
114 for _, tt := range tests {
115 t.Run(tt.name, func(t *testing.T) {
116 var (
117 form url.Values
118 parseErr error
119 )
120 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
121 parseErr = r.ParseForm()
122 if parseErr != nil {
123 w.WriteHeader(http.StatusBadRequest)
124 return
125 }
126 form = r.PostForm
127 _, _ = w.Write([]byte("{}"))
128 }))
129 defer server.Close()
130
131 caller := &mockDeviceAuthorizationCaller{endpoint: server.URL, httpClient: server.Client()}
132 request := &oidc.ClientCredentialsRequest{ClientID: "client", Scope: []string{"openid"}}
133 _, err := CallDeviceAuthorizationEndpoint(context.Background(), request, caller, nil, tt.opts...)
134 require.NoError(t, err)
135 require.NoError(t, parseErr)
136 assert.Equal(t, "client", form.Get("client_id"))
137 assert.Equal(t, "openid", form.Get("scope"))
138 assert.Equal(t, tt.wantJKT, form.Get("dpop_jkt"))
139 })
140 }
141 }
142
143 func TestCallRevokeEndpoint_CheckRedirectUnchanged(t *testing.T) {
144 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
145 w.WriteHeader(http.StatusOK)
146 }))
147 defer server.Close()
148
149 customErr := errors.New("custom redirect error")
150 customRedirect := func(_ *http.Request, _ []*http.Request) error {
151 return customErr
152 }
153
154 tests := []struct {
155 name string
156 initialClient *http.Client
157 checkFn func(t *testing.T, client *http.Client)
158 }{
159 {
160 name: "nil CheckRedirect",
161 initialClient: &http.Client{CheckRedirect: nil},
162 checkFn: func(t *testing.T, client *http.Client) {
163 assert.Nil(t, client.CheckRedirect)
164 },
165 },
166 {
167 name: "custom CheckRedirect",
168 initialClient: &http.Client{CheckRedirect: customRedirect},
169 checkFn: func(t *testing.T, client *http.Client) {
170 require.NotNil(t, client.CheckRedirect)
171 assert.Equal(t, reflect.ValueOf(customRedirect).Pointer(), reflect.ValueOf(client.CheckRedirect).Pointer())
172 assert.ErrorIs(t, client.CheckRedirect(nil, nil), customErr)
173 },
174 },
175 }
176
177 for _, tt := range tests {
178 t.Run(tt.name, func(t *testing.T) {
179 caller := &mockRevokeCaller{
180 endpoint: server.URL,
181 httpClient: tt.initialClient,
182 }
183 req := &RevokeRequest{
184 Token: "test-token",
185 }
186 err := CallRevokeEndpoint(context.Background(), req, nil, caller)
187 require.NoError(t, err)
188 tt.checkFn(t, tt.initialClient)
189 })
190 }
191 }
192
193 func TestCallEndSessionEndpoint_CheckRedirectUnchanged(t *testing.T) {
194 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
195 http.Redirect(w, r, "https://example.com/callback", http.StatusFound)
196 }))
197 defer server.Close()
198
199 customErr := errors.New("custom redirect error")
200 customRedirect := func(_ *http.Request, _ []*http.Request) error {
201 return customErr
202 }
203
204 tests := []struct {
205 name string
206 initialClient *http.Client
207 checkFn func(t *testing.T, client *http.Client)
208 }{
209 {
210 name: "nil CheckRedirect",
211 initialClient: &http.Client{CheckRedirect: nil},
212 checkFn: func(t *testing.T, client *http.Client) {
213 assert.Nil(t, client.CheckRedirect)
214 },
215 },
216 {
217 name: "custom CheckRedirect",
218 initialClient: &http.Client{CheckRedirect: customRedirect},
219 checkFn: func(t *testing.T, client *http.Client) {
220 require.NotNil(t, client.CheckRedirect)
221 assert.Equal(t, reflect.ValueOf(customRedirect).Pointer(), reflect.ValueOf(client.CheckRedirect).Pointer())
222 assert.ErrorIs(t, client.CheckRedirect(nil, nil), customErr)
223 },
224 },
225 }
226
227 for _, tt := range tests {
228 t.Run(tt.name, func(t *testing.T) {
229 caller := &mockEndSessionCaller{
230 endpoint: server.URL,
231 httpClient: tt.initialClient,
232 }
233 req := &oidc.EndSessionRequest{
234 IdTokenHint: "test-id-token",
235 }
236 loc, err := CallEndSessionEndpoint(context.Background(), req, nil, caller)
237 require.NoError(t, err)
238 require.NotNil(t, loc)
239 assert.Equal(t, "https://example.com/callback", loc.String())
240 tt.checkFn(t, tt.initialClient)
241 })
242 }
243 }