Debian Codegraph @

pkg/op/discovery_test.go

raw
1 package op_test
2
3 import (
4 "context"
5 "net/http"
6 "net/http/httptest"
7 "testing"
8
9 jose "github.com/go-jose/go-jose/v4"
10 "github.com/golang/mock/gomock"
11 "github.com/stretchr/testify/assert"
12 "github.com/stretchr/testify/require"
13
14 "github.com/zitadel/oidc/v3/pkg/oidc"
15 "github.com/zitadel/oidc/v3/pkg/op"
16 "github.com/zitadel/oidc/v3/pkg/op/mock"
17 )
18
19 func TestDiscover(t *testing.T) {
20 type args struct {
21 w http.ResponseWriter
22 config *oidc.DiscoveryConfiguration
23 }
24 tests := []struct {
25 name string
26 args args
27 want string
28 }{
29 {
30 "OK",
31 args{
32 httptest.NewRecorder(),
33 &oidc.DiscoveryConfiguration{Issuer: "https://issuer.com"},
34 },
35 `{"issuer":"https://issuer.com","request_uri_parameter_supported":false}`,
36 },
37 {
38 "client_id_metadata_document_supported",
39 args{
40 httptest.NewRecorder(),
41 &oidc.DiscoveryConfiguration{
42 Issuer: "https://issuer.com",
43 ClientIDMetadataDocumentSupported: true,
44 },
45 },
46 `{"issuer":"https://issuer.com","client_id_metadata_document_supported":true,"request_uri_parameter_supported":false}`,
47 },
48 }
49 for _, tt := range tests {
50 t.Run(tt.name, func(t *testing.T) {
51 op.Discover(tt.args.w, tt.args.config)
52 rec := tt.args.w.(*httptest.ResponseRecorder)
53 require.Equal(t, http.StatusOK, rec.Code)
54 require.JSONEq(t, tt.want, rec.Body.String())
55 })
56 }
57 }
58
59 func TestCreateDiscoveryConfig(t *testing.T) {
60 type args struct {
61 ctx context.Context
62 c op.Configuration
63 s op.DiscoverStorage
64 }
65 tests := []struct {
66 name string
67 args args
68 want *oidc.DiscoveryConfiguration
69 }{
70 // TODO: Add test cases.
71 }
72 for _, tt := range tests {
73 t.Run(tt.name, func(t *testing.T) {
74 got := op.CreateDiscoveryConfig(tt.args.ctx, tt.args.c, tt.args.s)
75 assert.Equal(t, tt.want, got)
76 })
77 }
78 }
79
80 func Test_scopes(t *testing.T) {
81 type args struct {
82 c op.Configuration
83 }
84 tests := []struct {
85 name string
86 args args
87 want []string
88 }{
89 {
90 "default Scopes",
91 args{},
92 op.DefaultSupportedScopes,
93 },
94 {
95 "custom scopes",
96 args{newTestProvider(&op.Config{SupportedScopes: []string{"test1", "test2"}})},
97 []string{"test1", "test2"},
98 },
99 }
100 for _, tt := range tests {
101 t.Run(tt.name, func(t *testing.T) {
102 got := op.Scopes(tt.args.c)
103 assert.Equal(t, tt.want, got)
104 })
105 }
106 }
107
108 func Test_ResponseTypes(t *testing.T) {
109 type args struct {
110 c op.Configuration
111 }
112 tests := []struct {
113 name string
114 args args
115 want []string
116 }{
117 {
118 "code and implicit flow",
119 args{},
120 []string{"code", "id_token", "id_token token"},
121 },
122 }
123 for _, tt := range tests {
124 t.Run(tt.name, func(t *testing.T) {
125 got := op.ResponseTypes(tt.args.c)
126 assert.Equal(t, tt.want, got)
127 })
128 }
129 }
130
131 func Test_GrantTypes(t *testing.T) {
132 type args struct {
133 c op.Configuration
134 }
135 tests := []struct {
136 name string
137 args args
138 want []oidc.GrantType
139 }{
140 {
141 "code and implicit flow",
142 args{
143 func() op.Configuration {
144 c := mock.NewMockConfiguration(gomock.NewController(t))
145 c.EXPECT().GrantTypeRefreshTokenSupported().Return(false)
146 c.EXPECT().GrantTypeTokenExchangeSupported().Return(false)
147 c.EXPECT().GrantTypeJWTAuthorizationSupported().Return(false)
148 c.EXPECT().GrantTypeClientCredentialsSupported().Return(false)
149 c.EXPECT().GrantTypeDeviceCodeSupported().Return(false)
150 return c
151 }(),
152 },
153 []oidc.GrantType{
154 oidc.GrantTypeCode,
155 oidc.GrantTypeImplicit,
156 },
157 },
158 {
159 "code, implicit flow, refresh token, token exchange, jwt profile, client_credentials",
160 args{
161 func() op.Configuration {
162 c := mock.NewMockConfiguration(gomock.NewController(t))
163 c.EXPECT().GrantTypeRefreshTokenSupported().Return(true)
164 c.EXPECT().GrantTypeTokenExchangeSupported().Return(true)
165 c.EXPECT().GrantTypeJWTAuthorizationSupported().Return(true)
166 c.EXPECT().GrantTypeClientCredentialsSupported().Return(true)
167 c.EXPECT().GrantTypeDeviceCodeSupported().Return(false)
168 return c
169 }(),
170 },
171 []oidc.GrantType{
172 oidc.GrantTypeCode,
173 oidc.GrantTypeImplicit,
174 oidc.GrantTypeRefreshToken,
175 oidc.GrantTypeClientCredentials,
176 oidc.GrantTypeTokenExchange,
177 oidc.GrantTypeBearer,
178 },
179 },
180 }
181 for _, tt := range tests {
182 t.Run(tt.name, func(t *testing.T) {
183 got := op.GrantTypes(tt.args.c)
184 assert.Equal(t, tt.want, got)
185 })
186 }
187 }
188
189 func Test_SubjectTypes(t *testing.T) {
190 type args struct {
191 c op.Configuration
192 }
193 tests := []struct {
194 name string
195 args args
196 want []string
197 }{
198 {
199 "none",
200 args{},
201 []string{"public"},
202 },
203 }
204 for _, tt := range tests {
205 t.Run(tt.name, func(t *testing.T) {
206 got := op.SubjectTypes(tt.args.c)
207 assert.Equal(t, tt.want, got)
208 })
209 }
210 }
211
212 func Test_SigAlgorithms(t *testing.T) {
213 m := mock.NewMockDiscoverStorage(gomock.NewController(t))
214 type args struct {
215 s op.DiscoverStorage
216 }
217 tests := []struct {
218 name string
219 args args
220 want []string
221 }{
222 {
223 "",
224 args{func() op.DiscoverStorage {
225 m.EXPECT().SignatureAlgorithms(gomock.Any()).Return([]jose.SignatureAlgorithm{jose.RS256}, nil)
226 return m
227 }()},
228 []string{"RS256"},
229 },
230 }
231 for _, tt := range tests {
232 t.Run(tt.name, func(t *testing.T) {
233 got := op.SigAlgorithms(context.Background(), tt.args.s)
234 assert.Equal(t, tt.want, got)
235 })
236 }
237 }
238
239 func Test_RequestObjectSigAlgorithms(t *testing.T) {
240 m := mock.NewMockConfiguration(gomock.NewController(t))
241 type args struct {
242 c op.Configuration
243 }
244 tests := []struct {
245 name string
246 args args
247 want []string
248 }{
249 {
250 "not supported, empty",
251 args{func() op.Configuration {
252 m.EXPECT().RequestObjectSupported().Return(false)
253 return m
254 }()},
255 nil,
256 },
257 {
258 "supported, empty",
259 args{func() op.Configuration {
260 m.EXPECT().RequestObjectSupported().Return(true)
261 m.EXPECT().RequestObjectSigningAlgorithmsSupported().Return(nil)
262 return m
263 }()},
264 nil,
265 },
266 {
267 "supported, list",
268 args{func() op.Configuration {
269 m.EXPECT().RequestObjectSupported().Return(true)
270 m.EXPECT().RequestObjectSigningAlgorithmsSupported().Return([]string{"RS256"})
271 return m
272 }()},
273 []string{"RS256"},
274 },
275 }
276 for _, tt := range tests {
277 t.Run(tt.name, func(t *testing.T) {
278 got := op.RequestObjectSigAlgorithms(tt.args.c)
279 assert.Equal(t, tt.want, got)
280 })
281 }
282 }
283
284 func Test_AuthMethodsTokenEndpoint(t *testing.T) {
285 type args struct {
286 c op.Configuration
287 }
288 tests := []struct {
289 name string
290 args args
291 want []oidc.AuthMethod
292 }{
293 {
294 "none and basic",
295 args{func() op.Configuration {
296 m := mock.NewMockConfiguration(gomock.NewController(t))
297 m.EXPECT().AuthMethodPostSupported().Return(false)
298 m.EXPECT().AuthMethodPrivateKeyJWTSupported().Return(false)
299 return m
300 }()},
301 []oidc.AuthMethod{oidc.AuthMethodNone, oidc.AuthMethodBasic},
302 },
303 {
304 "none, basic and post",
305 args{func() op.Configuration {
306 m := mock.NewMockConfiguration(gomock.NewController(t))
307 m.EXPECT().AuthMethodPostSupported().Return(true)
308 m.EXPECT().AuthMethodPrivateKeyJWTSupported().Return(false)
309 return m
310 }()},
311 []oidc.AuthMethod{oidc.AuthMethodNone, oidc.AuthMethodBasic, oidc.AuthMethodPost},
312 },
313 {
314 "none, basic, post and private_key_jwt",
315 args{func() op.Configuration {
316 m := mock.NewMockConfiguration(gomock.NewController(t))
317 m.EXPECT().AuthMethodPostSupported().Return(true)
318 m.EXPECT().AuthMethodPrivateKeyJWTSupported().Return(true)
319 return m
320 }()},
321 []oidc.AuthMethod{oidc.AuthMethodNone, oidc.AuthMethodBasic, oidc.AuthMethodPost, oidc.AuthMethodPrivateKeyJWT},
322 },
323 }
324 for _, tt := range tests {
325 t.Run(tt.name, func(t *testing.T) {
326 got := op.AuthMethodsTokenEndpoint(tt.args.c)
327 assert.Equal(t, tt.want, got)
328 })
329 }
330 }
331
332 func Test_TokenSigAlgorithms(t *testing.T) {
333 m := mock.NewMockConfiguration(gomock.NewController(t))
334 type args struct {
335 c op.Configuration
336 }
337 tests := []struct {
338 name string
339 args args
340 want []string
341 }{
342 {
343 "not supported, empty",
344 args{func() op.Configuration {
345 m.EXPECT().AuthMethodPrivateKeyJWTSupported().Return(false)
346 return m
347 }()},
348 nil,
349 },
350 {
351 "supported, empty",
352 args{func() op.Configuration {
353 m.EXPECT().AuthMethodPrivateKeyJWTSupported().Return(true)
354 m.EXPECT().TokenEndpointSigningAlgorithmsSupported().Return(nil)
355 return m
356 }()},
357 nil,
358 },
359 {
360 "supported, list",
361 args{func() op.Configuration {
362 m.EXPECT().AuthMethodPrivateKeyJWTSupported().Return(true)
363 m.EXPECT().TokenEndpointSigningAlgorithmsSupported().Return([]string{"RS256"})
364 return m
365 }()},
366 []string{"RS256"},
367 },
368 }
369 for _, tt := range tests {
370 t.Run(tt.name, func(t *testing.T) {
371 got := op.TokenSigAlgorithms(tt.args.c)
372 assert.Equal(t, tt.want, got)
373 })
374 }
375 }
376
377 func Test_IntrospectionSigAlgorithms(t *testing.T) {
378 m := mock.NewMockConfiguration(gomock.NewController(t))
379 type args struct {
380 c op.Configuration
381 }
382 tests := []struct {
383 name string
384 args args
385 want []string
386 }{
387 {
388 "not supported, empty",
389 args{func() op.Configuration {
390 m.EXPECT().IntrospectionAuthMethodPrivateKeyJWTSupported().Return(false)
391 return m
392 }()},
393 nil,
394 },
395 {
396 "supported, empty",
397 args{func() op.Configuration {
398 m.EXPECT().IntrospectionAuthMethodPrivateKeyJWTSupported().Return(true)
399 m.EXPECT().IntrospectionEndpointSigningAlgorithmsSupported().Return(nil)
400 return m
401 }()},
402 nil,
403 },
404 {
405 "supported, list",
406 args{func() op.Configuration {
407 m.EXPECT().IntrospectionAuthMethodPrivateKeyJWTSupported().Return(true)
408 m.EXPECT().IntrospectionEndpointSigningAlgorithmsSupported().Return([]string{"RS256"})
409 return m
410 }()},
411 []string{"RS256"},
412 },
413 }
414 for _, tt := range tests {
415 t.Run(tt.name, func(t *testing.T) {
416 got := op.IntrospectionSigAlgorithms(tt.args.c)
417 assert.Equal(t, tt.want, got)
418 })
419 }
420 }
421
422 func Test_AuthMethodsIntrospectionEndpoint(t *testing.T) {
423 type args struct {
424 c op.Configuration
425 }
426 tests := []struct {
427 name string
428 args args
429 want []oidc.AuthMethod
430 }{
431 {
432 "basic only",
433 args{func() op.Configuration {
434 m := mock.NewMockConfiguration(gomock.NewController(t))
435 m.EXPECT().AuthMethodPrivateKeyJWTSupported().Return(false)
436 return m
437 }()},
438 []oidc.AuthMethod{oidc.AuthMethodBasic},
439 },
440 {
441 "basic and private_key_jwt",
442 args{func() op.Configuration {
443 m := mock.NewMockConfiguration(gomock.NewController(t))
444 m.EXPECT().AuthMethodPrivateKeyJWTSupported().Return(true)
445 return m
446 }()},
447 []oidc.AuthMethod{oidc.AuthMethodBasic, oidc.AuthMethodPrivateKeyJWT},
448 },
449 }
450 for _, tt := range tests {
451 t.Run(tt.name, func(t *testing.T) {
452 got := op.AuthMethodsIntrospectionEndpoint(tt.args.c)
453 assert.Equal(t, tt.want, got)
454 })
455 }
456 }
457
458 func Test_RevocationSigAlgorithms(t *testing.T) {
459 m := mock.NewMockConfiguration(gomock.NewController(t))
460 type args struct {
461 c op.Configuration
462 }
463 tests := []struct {
464 name string
465 args args
466 want []string
467 }{
468 {
469 "not supported, empty",
470 args{func() op.Configuration {
471 m.EXPECT().RevocationAuthMethodPrivateKeyJWTSupported().Return(false)
472 return m
473 }()},
474 nil,
475 },
476 {
477 "supported, empty",
478 args{func() op.Configuration {
479 m.EXPECT().RevocationAuthMethodPrivateKeyJWTSupported().Return(true)
480 m.EXPECT().RevocationEndpointSigningAlgorithmsSupported().Return(nil)
481 return m
482 }()},
483 nil,
484 },
485 {
486 "supported, list",
487 args{func() op.Configuration {
488 m.EXPECT().RevocationAuthMethodPrivateKeyJWTSupported().Return(true)
489 m.EXPECT().RevocationEndpointSigningAlgorithmsSupported().Return([]string{"RS256"})
490 return m
491 }()},
492 []string{"RS256"},
493 },
494 }
495 for _, tt := range tests {
496 t.Run(tt.name, func(t *testing.T) {
497 got := op.RevocationSigAlgorithms(tt.args.c)
498 assert.Equal(t, tt.want, got)
499 })
500 }
501 }
502
503 func Test_AuthMethodsRevocationEndpoint(t *testing.T) {
504 type args struct {
505 c op.Configuration
506 }
507 tests := []struct {
508 name string
509 args args
510 want []oidc.AuthMethod
511 }{
512 {
513 "none and basic",
514 args{func() op.Configuration {
515 m := mock.NewMockConfiguration(gomock.NewController(t))
516 m.EXPECT().AuthMethodPostSupported().Return(false)
517 m.EXPECT().AuthMethodPrivateKeyJWTSupported().Return(false)
518 return m
519 }()},
520 []oidc.AuthMethod{oidc.AuthMethodNone, oidc.AuthMethodBasic},
521 },
522 {
523 "none, basic and post",
524 args{func() op.Configuration {
525 m := mock.NewMockConfiguration(gomock.NewController(t))
526 m.EXPECT().AuthMethodPostSupported().Return(true)
527 m.EXPECT().AuthMethodPrivateKeyJWTSupported().Return(false)
528 return m
529 }()},
530 []oidc.AuthMethod{oidc.AuthMethodNone, oidc.AuthMethodBasic, oidc.AuthMethodPost},
531 },
532 {
533 "none, basic, post and private_key_jwt",
534 args{func() op.Configuration {
535 m := mock.NewMockConfiguration(gomock.NewController(t))
536 m.EXPECT().AuthMethodPostSupported().Return(true)
537 m.EXPECT().AuthMethodPrivateKeyJWTSupported().Return(true)
538 return m
539 }()},
540 []oidc.AuthMethod{oidc.AuthMethodNone, oidc.AuthMethodBasic, oidc.AuthMethodPost, oidc.AuthMethodPrivateKeyJWT},
541 },
542 }
543 for _, tt := range tests {
544 t.Run(tt.name, func(t *testing.T) {
545 got := op.AuthMethodsRevocationEndpoint(tt.args.c)
546 assert.Equal(t, tt.want, got)
547 })
548 }
549 }
550
551 func TestSupportedClaims(t *testing.T) {
552 type args struct {
553 c op.Configuration
554 }
555 tests := []struct {
556 name string
557 args args
558 want []string
559 }{
560 {
561 "scopes",
562 args{},
563 []string{
564 "sub",
565 "aud",
566 "exp",
567 "iat",
568 "iss",
569 "auth_time",
570 "nonce",
571 "acr",
572 "amr",
573 "c_hash",
574 "at_hash",
575 "act",
576 "scopes",
577 "client_id",
578 "azp",
579 "preferred_username",
580 "name",
581 "family_name",
582 "given_name",
583 "locale",
584 "email",
585 "email_verified",
586 "phone_number",
587 "phone_number_verified",
588 },
589 },
590 }
591 for _, tt := range tests {
592 t.Run(tt.name, func(t *testing.T) {
593 got := op.SupportedClaims(tt.args.c)
594 assert.Equal(t, tt.want, got)
595 })
596 }
597 }
598
599 func Test_CodeChallengeMethods(t *testing.T) {
600 type args struct {
601 c op.Configuration
602 }
603 tests := []struct {
604 name string
605 args args
606 want []oidc.CodeChallengeMethod
607 }{
608 {
609 "not supported",
610 args{func() op.Configuration {
611 m := mock.NewMockConfiguration(gomock.NewController(t))
612 m.EXPECT().CodeMethodS256Supported().Return(false)
613 return m
614 }()},
615 []oidc.CodeChallengeMethod{},
616 },
617 {
618 "S256",
619 args{func() op.Configuration {
620 m := mock.NewMockConfiguration(gomock.NewController(t))
621 m.EXPECT().CodeMethodS256Supported().Return(true)
622 return m
623 }()},
624 []oidc.CodeChallengeMethod{oidc.CodeChallengeMethodS256},
625 },
626 }
627 for _, tt := range tests {
628 t.Run(tt.name, func(t *testing.T) {
629 got := op.CodeChallengeMethods(tt.args.c)
630 assert.Equal(t, tt.want, got)
631 })
632 }
633 }