Debian Codegraph @

pkg/oidc/error_test.go

raw
1 package oidc
2
3 import (
4 "encoding/json"
5 "errors"
6 "fmt"
7 "io"
8 "log/slog"
9 "testing"
10
11 "github.com/stretchr/testify/assert"
12 "github.com/stretchr/testify/require"
13 )
14
15 func TestDefaultToServerError(t *testing.T) {
16 type args struct {
17 err error
18 description string
19 }
20 tests := []struct {
21 name string
22 args args
23 want *Error
24 }{
25 {
26 name: "default",
27 args: args{
28 err: io.ErrClosedPipe,
29 description: "oops",
30 },
31 want: &Error{
32 ErrorType: ServerError,
33 Description: "oops",
34 Parent: io.ErrClosedPipe,
35 },
36 },
37 {
38 name: "our Error",
39 args: args{
40 err: ErrAccessDenied(),
41 description: "oops",
42 },
43 want: &Error{
44 ErrorType: AccessDenied,
45 Description: "The authorization request was denied.",
46 },
47 },
48 {
49 name: "verifier error invalid request",
50 args: args{
51 err: fmt.Errorf("%w: parse error", ErrParse),
52 description: "parse error",
53 },
54 want: &Error{
55 ErrorType: InvalidRequest,
56 Description: "parse error",
57 },
58 },
59 {
60 name: "verifier error invalid grant",
61 args: args{
62 err: ErrAudience,
63 description: "wrong audience",
64 },
65 want: &Error{
66 ErrorType: InvalidGrant,
67 Description: "wrong audience",
68 },
69 },
70 {
71 name: "verifier error subject missing",
72 args: args{
73 err: ErrSubjectMissing,
74 description: "subject missing",
75 },
76 want: &Error{
77 ErrorType: InvalidGrant,
78 Description: "subject missing",
79 },
80 },
81 {
82 // ErrSubjectInvalid used to fall through to server_error, so a rejected
83 // assertion answered HTTP 500 where its sibling above answered 400.
84 name: "verifier error subject invalid",
85 args: args{
86 err: ErrSubjectInvalid,
87 description: "delegation not allowed",
88 },
89 want: &Error{
90 ErrorType: InvalidGrant,
91 Description: "delegation not allowed",
92 },
93 },
94 {
95 name: "verifier error server error",
96 args: args{
97 err: ErrDiscoveryFailed,
98 description: "discovery failed",
99 },
100 want: &Error{
101 ErrorType: ServerError,
102 Description: "discovery failed",
103 },
104 },
105 {
106 name: "generic error",
107 args: args{
108 err: errors.New("generic error"),
109 description: "something went wrong",
110 },
111 want: &Error{
112 ErrorType: ServerError,
113 Description: "something went wrong",
114 Parent: errors.New("generic error"),
115 },
116 },
117 }
118 for _, tt := range tests {
119 t.Run(tt.name, func(t *testing.T) {
120 got := DefaultToServerError(tt.args.err, tt.args.description)
121 assert.ErrorIs(t, got, tt.want)
122 })
123 }
124 }
125
126 func TestError_LogLevel(t *testing.T) {
127 tests := []struct {
128 name string
129 err *Error
130 want slog.Level
131 }{
132 {
133 name: "server error",
134 err: ErrServerError(),
135 want: slog.LevelError,
136 },
137 {
138 name: "authorization pending",
139 err: ErrAuthorizationPending(),
140 want: slog.LevelInfo,
141 },
142 {
143 name: "some other error",
144 err: ErrAccessDenied(),
145 want: slog.LevelWarn,
146 },
147 }
148 for _, tt := range tests {
149 t.Run(tt.name, func(t *testing.T) {
150 got := tt.err.LogLevel()
151 assert.Equal(t, tt.want, got)
152 })
153 }
154 }
155
156 func TestError_LogValue(t *testing.T) {
157 type fields struct {
158 Parent error
159 ErrorType errorType
160 Description string
161 State string
162 redirectDisabled bool
163 }
164 tests := []struct {
165 name string
166 fields fields
167 want slog.Value
168 }{
169 {
170 name: "parent",
171 fields: fields{
172 Parent: io.EOF,
173 },
174 want: slog.GroupValue(slog.Any("parent", io.EOF)),
175 },
176 {
177 name: "description",
178 fields: fields{
179 Description: "oops",
180 },
181 want: slog.GroupValue(slog.String("description", "oops")),
182 },
183 {
184 name: "errorType",
185 fields: fields{
186 ErrorType: ExpiredToken,
187 },
188 want: slog.GroupValue(slog.String("type", string(ExpiredToken))),
189 },
190 {
191 name: "state",
192 fields: fields{
193 State: "123",
194 },
195 want: slog.GroupValue(slog.String("state", "123")),
196 },
197 {
198 name: "all fields",
199 fields: fields{
200 Parent: io.EOF,
201 Description: "oops",
202 ErrorType: ExpiredToken,
203 State: "123",
204 },
205 want: slog.GroupValue(
206 slog.Any("parent", io.EOF),
207 slog.String("description", "oops"),
208 slog.String("type", string(ExpiredToken)),
209 slog.String("state", "123"),
210 ),
211 },
212 }
213 for _, tt := range tests {
214 t.Run(tt.name, func(t *testing.T) {
215 e := &Error{
216 Parent: tt.fields.Parent,
217 ErrorType: tt.fields.ErrorType,
218 Description: tt.fields.Description,
219 State: tt.fields.State,
220 redirectDisabled: tt.fields.redirectDisabled,
221 }
222 got := e.LogValue()
223 assert.Equal(t, tt.want, got)
224 })
225 }
226 }
227
228 func TestError_MarshalJSON(t *testing.T) {
229 tests := []struct {
230 name string
231 e *Error
232 want string
233 }{
234 {
235 name: "simple error",
236 e: ErrAccessDenied(),
237 want: `{"error":"access_denied","error_description":"The authorization request was denied."}`,
238 },
239 {
240 name: "with description",
241 e: ErrAccessDenied().WithDescription("oops"),
242 want: `{"error":"access_denied","error_description":"oops"}`,
243 },
244 {
245 name: "with parent",
246 e: ErrServerError().WithParent(errors.New("oops")),
247 want: `{"error":"server_error"}`,
248 },
249 {
250 name: "with return parent",
251 e: ErrServerError().WithParent(errors.New("oops")).WithReturnParentToClient(true),
252 want: `{"error":"server_error","parent":"oops"}`,
253 },
254 }
255 for _, tt := range tests {
256 t.Run(tt.name, func(t *testing.T) {
257 got, err := json.Marshal(tt.e)
258 require.NoError(t, err)
259 assert.JSONEq(t, tt.want, string(got))
260 })
261 }
262 }