Debian Codegraph @

pkg/client/rs/resource_server_test.go

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
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: "spotify-full-discovery",
32 args: args{
33 issuer: "https://accounts.spotify.com",
34 authorizer: nil,
35 options: []Option{},
36 },
37 wantFields: &wantFields{
38 issuer: "https://accounts.spotify.com",
39 tokenURL: "https://accounts.spotify.com/api/token",
40 introspectURL: "",
41 authFn: nil,
42 },
43 wantErr: false,
44 },
45 {
46 name: "spotify-with-static-tokenurl",
47 args: args{
48 issuer: "https://accounts.spotify.com",
49 authorizer: nil,
50 options: []Option{
51 WithStaticEndpoints(
52 "https://some.host/token-url",
53 "",
54 ),
55 },
56 },
57 wantFields: &wantFields{
58 issuer: "https://accounts.spotify.com",
59 tokenURL: "https://some.host/token-url",
60 introspectURL: "",
61 authFn: nil,
62 },
63 wantErr: false,
64 },
65 {
66 name: "spotify-with-static-introspecturl",
67 args: args{
68 issuer: "https://accounts.spotify.com",
69 authorizer: nil,
70 options: []Option{
71 WithStaticEndpoints(
72 "",
73 "https://some.host/instrospect-url",
74 ),
75 },
76 },
77 wantFields: &wantFields{
78 issuer: "https://accounts.spotify.com",
79 tokenURL: "https://accounts.spotify.com/api/token",
80 introspectURL: "https://some.host/instrospect-url",
81 authFn: nil,
82 },
83 wantErr: false,
84 },
85 {
86 name: "spotify-with-all-static-endpoints",
87 args: args{
88 issuer: "https://accounts.spotify.com",
89 authorizer: nil,
90 options: []Option{
91 WithStaticEndpoints(
92 "https://some.host/token-url",
93 "https://some.host/instrospect-url",
94 ),
95 },
96 },
97 wantFields: &wantFields{
98 issuer: "https://accounts.spotify.com",
99 tokenURL: "https://some.host/token-url",
100 introspectURL: "https://some.host/instrospect-url",
101 authFn: nil,
102 },
103 wantErr: false,
104 },
105 {
106 name: "bad-discovery",
107 args: args{
108 issuer: "https://127.0.0.1:65535",
109 authorizer: nil,
110 options: []Option{},
111 },
112 wantFields: nil,
113 wantErr: true,
114 },
115 {
116 name: "bad-discovery-with-static-tokenurl",
117 args: args{
118 issuer: "https://127.0.0.1:65535",
119 authorizer: nil,
120 options: []Option{
121 WithStaticEndpoints(
122 "https://some.host/token-url",
123 "",
124 ),
125 },
126 },
127 wantFields: nil,
128 wantErr: true,
129 },
130 {
131 name: "bad-discovery-with-static-introspecturl",
132 args: args{
133 issuer: "https://127.0.0.1:65535",
134 authorizer: nil,
135 options: []Option{
136 WithStaticEndpoints(
137 "",
138 "https://some.host/instrospect-url",
139 ),
140 },
141 },
142 wantFields: nil,
143 wantErr: true,
144 },
145 {
146 name: "bad-discovery-with-all-static-endpoints",
147 args: args{
148 issuer: "https://127.0.0.1:65535",
149 authorizer: nil,
150 options: []Option{
151 WithStaticEndpoints(
152 "https://some.host/token-url",
153 "https://some.host/instrospect-url",
154 ),
155 },
156 },
157 wantFields: &wantFields{
158 issuer: "https://127.0.0.1:65535",
159 tokenURL: "https://some.host/token-url",
160 introspectURL: "https://some.host/instrospect-url",
161 authFn: nil,
162 },
163 wantErr: false,
164 },
165 }
166 for _, tt := range tests {
167 t.Run(tt.name, func(t *testing.T) {
168 got, err := newResourceServer(context.Background(), tt.args.issuer, tt.args.authorizer, tt.args.options...)
169 if tt.wantErr {
170 assert.Error(t, err)
171 return
172 }
173 require.NoError(t, err)
174 if tt.wantFields == nil {
175 return
176 }
177 assert.Equal(t, tt.wantFields.issuer, got.issuer)
178 assert.Equal(t, tt.wantFields.tokenURL, got.tokenURL)
179 assert.Equal(t, tt.wantFields.introspectURL, got.introspectURL)
180 })
181 }
182 }
183
184 func TestIntrospect(t *testing.T) {
185 type args struct {
186 ctx context.Context
187 rp ResourceServer
188 token string
189 }
190 rp, err := newResourceServer(
191 context.Background(),
192 "https://accounts.spotify.com",
193 nil,
194 )
195 require.NoError(t, err)
196 tests := []struct {
197 name string
198 args args
199 wantErr bool
200 }{
201 {
202 name: "missing-introspect-url",
203 args: args{
204 ctx: context.Background(),
205 rp: rp,
206 token: "my-token",
207 },
208 wantErr: true,
209 },
210 }
211 for _, tt := range tests {
212 t.Run(tt.name, func(t *testing.T) {
213 _, err := Introspect[*oidc.IntrospectionResponse](tt.args.ctx, tt.args.rp, tt.args.token)
214 if tt.wantErr {
215 assert.Error(t, err)
216 return
217 }
218 require.NoError(t, err)
219 })
220 }
221 }