Debian Codegraph @

pkg/client/rs/resource_server_test.go

raw ◀ ▶
1 Debian patch touch this file
1 package rs
2
3 import (
4 "context"
5 "testing"
6
7 "github.com/stretchr/testify/assert"
8 "github.com/stretchr/testify/require"
9 "github.com/zitadel/oidc/v3/pkg/oidc"
10 )
11
12 func TestNewResourceServer(t *testing.T) {
13 type args struct {
14 issuer string
15 authorizer func() (any, error)
16 options []Option
17 }
18 type wantFields struct {
19 issuer string
20 tokenURL string
21 introspectURL string
22 authFn func() (any, error)
23 }
24 tests := []struct {
25 name string
26 args args
27 wantFields *wantFields
28 wantErr bool
29 }{
30 {
31 name: "bad-discovery",
32 args: args{
33 issuer: "https://127.0.0.1:65535",
34 authorizer: nil,
35 options: []Option{},
36 },
37 wantFields: nil,
38 wantErr: true,
39 },
40 {
41 name: "bad-discovery-with-static-tokenurl",
42 args: args{
43 issuer: "https://127.0.0.1:65535",
44 authorizer: nil,
45 options: []Option{
46 WithStaticEndpoints(
47 "https://some.host/token-url",
48 "",
49 ),
50 },
51 },
52 wantFields: nil,
53 wantErr: true,
54 },
55 {
56 name: "bad-discovery-with-static-introspecturl",
57 args: args{
58 issuer: "https://127.0.0.1:65535",
59 authorizer: nil,
60 options: []Option{
61 WithStaticEndpoints(
62 "",
63 "https://some.host/instrospect-url",
64 ),
65 },
66 },
67 wantFields: nil,
68 wantErr: true,
69 },
70 {
71 name: "bad-discovery-with-all-static-endpoints",
72 args: args{
73 issuer: "https://127.0.0.1:65535",
74 authorizer: nil,
75 options: []Option{
76 WithStaticEndpoints(
77 "https://some.host/token-url",
78 "https://some.host/instrospect-url",
79 ),
80 },
81 },
82 wantFields: &wantFields{
83 issuer: "https://127.0.0.1:65535",
84 tokenURL: "https://some.host/token-url",
85 introspectURL: "https://some.host/instrospect-url",
86 authFn: nil,
87 },
88 wantErr: false,
89 },
90 }
91 for _, tt := range tests {
92 t.Run(tt.name, func(t *testing.T) {
93 got, err := newResourceServer(context.Background(), tt.args.issuer, tt.args.authorizer, tt.args.options...)
94 if tt.wantErr {
95 assert.Error(t, err)
96 return
97 }
98 require.NoError(t, err)
99 if tt.wantFields == nil {
100 return
101 }
102 assert.Equal(t, tt.wantFields.issuer, got.issuer)
103 assert.Equal(t, tt.wantFields.tokenURL, got.tokenURL)
104 assert.Equal(t, tt.wantFields.introspectURL, got.introspectURL)
105 })
106 }
107 }
108
109 func TestIntrospect(t *testing.T) {
•110 t.Skip("Skipping test that needs Internet access")
111 type args struct {
112 ctx context.Context
113 rp ResourceServer
114 token string
115 }
116 rp, err := newResourceServer(
117 context.Background(),
118 "https://accounts.spotify.com",
119 nil,
120 )
121 require.NoError(t, err)
122 tests := []struct {
123 name string
124 args args
125 wantErr bool
126 }{
127 {
128 name: "missing-introspect-url",
129 args: args{
130 ctx: context.Background(),
131 rp: rp,
132 token: "my-token",
133 },
134 wantErr: true,
135 },
136 }
137 for _, tt := range tests {
138 t.Run(tt.name, func(t *testing.T) {
139 _, err := Introspect[*oidc.IntrospectionResponse](tt.args.ctx, tt.args.rp, tt.args.token)
140 if tt.wantErr {
141 assert.Error(t, err)
142 return
143 }
144 require.NoError(t, err)
145 })
146 }
147 }