@@ -14,6 +14,8 @@ import (
1414 "github.com/go-openapi/runtime"
1515)
1616
17+ const testAuthenticator = "auth1"
18+
1719type principalType struct {
1820 Name string
1921}
@@ -44,49 +46,80 @@ var (
4446 noApplyAuth = runtime .AuthenticatorFunc (func (_ any ) (bool , any , error ) {
4547 return false , nil , nil
4648 })
47- successAuthWithPointer = runtime .AuthenticatorFunc (func (_ interface {} ) (bool , interface {} , error ) {
49+ successAuthWithPointer = runtime .AuthenticatorFunc (func (_ any ) (bool , any , error ) {
4850 return true , & principalType {Name : "the user" }, nil
4951 })
50- failAuthWithPointer = runtime .AuthenticatorFunc (func (_ interface {} ) (bool , interface {} , error ) {
52+ failAuthWithPointer = runtime .AuthenticatorFunc (func (_ any ) (bool , any , error ) {
5153 var typedPrincipal * principalType
5254 return true , typedPrincipal , errors .New ("unauthenticated" )
5355 })
54- failAuthWithNilPointer = runtime .AuthenticatorFunc (func (_ interface {} ) (bool , interface {} , error ) {
56+ failAuthWithNilPointer = runtime .AuthenticatorFunc (func (_ any ) (bool , any , error ) {
5557 var typedPrincipal * principalType
5658 return true , typedPrincipal , nil
5759 })
5860)
5961
6062func TestAuthenticateSingle (t * testing.T ) {
61- ra := RouteAuthenticator {
62- Authenticator : map [string ]runtime.Authenticator {
63- "auth1" : successAuthWithPointer ,
64- },
65- Schemes : []string {"auth1" },
66- Scopes : map [string ][]string {"auth1" : nil },
67- }
68- ras := RouteAuthenticators ([]RouteAuthenticator {ra })
69- require .FalseT (t , ras .AllowsAnonymous ())
70-
71- req , _ := http .NewRequestWithContext (context .Background (), http .MethodGet , "/" , nil )
72- route := & MatchedRoute {}
73- ok , prin , err := ras .Authenticate (req , route )
74- require .NoError (t , err )
75- require .TrueT (t , ok )
76- require .Equal (t , "the user" , prin )
63+ t .Parallel ()
64+
65+ t .Run ("with string" , func (t * testing.T ) {
66+ t .Parallel ()
67+
68+ ra := RouteAuthenticator {
69+ Authenticator : map [string ]runtime.Authenticator {
70+ testAuthenticator : successAuth ,
71+ },
72+ Schemes : []string {testAuthenticator },
73+ Scopes : map [string ][]string {testAuthenticator : nil },
74+ }
75+ ras := RouteAuthenticators ([]RouteAuthenticator {ra })
76+ require .FalseT (t , ras .AllowsAnonymous ())
77+
78+ req , _ := http .NewRequestWithContext (context .Background (), http .MethodGet , "/" , nil )
79+ route := & MatchedRoute {}
80+ ok , prin , err := ras .Authenticate (req , route )
81+ require .NoError (t , err )
82+ require .TrueT (t , ok )
83+ require .Equal (t , "the user" , prin )
84+
85+ require .Equal (t , ra , * route .Authenticator )
86+ })
7787
78- require .Equal (t , ra , * route .Authenticator )
88+ t .Run ("with pointer" , func (t * testing.T ) {
89+ t .Parallel ()
90+
91+ ra := RouteAuthenticator {
92+ Authenticator : map [string ]runtime.Authenticator {
93+ testAuthenticator : successAuthWithPointer ,
94+ },
95+ Schemes : []string {testAuthenticator },
96+ Scopes : map [string ][]string {testAuthenticator : nil },
97+ }
98+ ras := RouteAuthenticators ([]RouteAuthenticator {ra })
99+ require .FalseT (t , ras .AllowsAnonymous ())
100+
101+ req , _ := http .NewRequestWithContext (context .Background (), http .MethodGet , "/" , nil )
102+ route := & MatchedRoute {}
103+ ok , prin , err := ras .Authenticate (req , route )
104+ require .NoError (t , err )
105+ require .TrueT (t , ok )
106+ typed , ok := prin .(* principalType )
107+ require .TrueT (t , ok )
108+ require .EqualT (t , "the user" , typed .Name )
109+
110+ require .Equal (t , ra , * route .Authenticator )
111+ })
79112}
80113
81114func TestAuthenticateWrong (t * testing.T ) {
82115 t .Run ("with principal as a pointer to a concrete type" , func (t * testing.T ) {
83116 t .Run ("should authenticate" , func (t * testing.T ) {
84117 ra := RouteAuthenticator {
85118 Authenticator : map [string ]runtime.Authenticator {
86- "auth1" : successAuthWithPointer ,
119+ testAuthenticator : successAuthWithPointer ,
87120 },
88- Schemes : []string {"auth1" },
89- Scopes : map [string ][]string {"auth1" : nil },
121+ Schemes : []string {testAuthenticator },
122+ Scopes : map [string ][]string {testAuthenticator : nil },
90123 }
91124 ras := RouteAuthenticators ([]RouteAuthenticator {ra })
92125
@@ -102,10 +135,10 @@ func TestAuthenticateWrong(t *testing.T) {
102135 t .Run ("should not authenticate" , func (t * testing.T ) {
103136 ra := RouteAuthenticator {
104137 Authenticator : map [string ]runtime.Authenticator {
105- "auth1" : failAuthWithPointer ,
138+ testAuthenticator : failAuthWithPointer ,
106139 },
107- Schemes : []string {"auth1" },
108- Scopes : map [string ][]string {"auth1" : nil },
140+ Schemes : []string {testAuthenticator },
141+ Scopes : map [string ][]string {testAuthenticator : nil },
109142 }
110143 ras := RouteAuthenticators ([]RouteAuthenticator {ra })
111144
@@ -121,10 +154,10 @@ func TestAuthenticateWrong(t *testing.T) {
121154 t .Run ("should yield nil principal" , func (t * testing.T ) {
122155 ra := RouteAuthenticator {
123156 Authenticator : map [string ]runtime.Authenticator {
124- "auth1" : failAuthWithNilPointer ,
157+ testAuthenticator : failAuthWithNilPointer ,
125158 },
126- Schemes : []string {"auth1" },
127- Scopes : map [string ][]string {"auth1" : nil },
159+ Schemes : []string {testAuthenticator },
160+ Scopes : map [string ][]string {testAuthenticator : nil },
128161 }
129162 ras := RouteAuthenticators ([]RouteAuthenticator {ra })
130163
@@ -134,7 +167,7 @@ func TestAuthenticateWrong(t *testing.T) {
134167 route := & MatchedRoute {}
135168 ok , prin , err := ras .Authenticate (req , route )
136169 require .NoError (t , err )
137- require .True (t , ok )
170+ require .FalseT (t , ok , "should not be authenticated: the principal is nil" )
138171 require .Nil (t , prin )
139172 })
140173 })
@@ -143,10 +176,10 @@ func TestAuthenticateWrong(t *testing.T) {
143176func TestAuthenticateLogicalOr (t * testing.T ) {
144177 ra1 := RouteAuthenticator {
145178 Authenticator : map [string ]runtime.Authenticator {
146- "auth1" : noApplyAuth ,
179+ testAuthenticator : noApplyAuth ,
147180 },
148- Schemes : []string {"auth1" },
149- Scopes : map [string ][]string {"auth1" : nil },
181+ Schemes : []string {testAuthenticator },
182+ Scopes : map [string ][]string {testAuthenticator : nil },
150183 }
151184 ra2 := RouteAuthenticator {
152185 Authenticator : map [string ]runtime.Authenticator {
@@ -185,10 +218,10 @@ func TestAuthenticateLogicalOr(t *testing.T) {
185218func TestAuthenticateLogicalAnd (t * testing.T ) {
186219 ra1 := RouteAuthenticator {
187220 Authenticator : map [string ]runtime.Authenticator {
188- "auth1" : noApplyAuth ,
221+ testAuthenticator : noApplyAuth ,
189222 },
190- Schemes : []string {"auth1" },
191- Scopes : map [string ][]string {"auth1" : nil },
223+ Schemes : []string {testAuthenticator },
224+ Scopes : map [string ][]string {testAuthenticator : nil },
192225 }
193226 authorizer := newCountAuthenticator (true , "the user" , nil )
194227 ra2 := RouteAuthenticator {
@@ -288,10 +321,10 @@ func TestAuthenticateTypedNilPrincipal(t *testing.T) {
288321
289322 ra := RouteAuthenticator {
290323 Authenticator : map [string ]runtime.Authenticator {
291- "auth1" : typedNilAuth ,
324+ testAuthenticator : typedNilAuth ,
292325 },
293- Schemes : []string {"auth1" },
294- Scopes : map [string ][]string {"auth1" : nil },
326+ Schemes : []string {testAuthenticator },
327+ Scopes : map [string ][]string {testAuthenticator : nil },
295328 }
296329 ras := RouteAuthenticators ([]RouteAuthenticator {ra })
297330
@@ -308,10 +341,10 @@ func TestAuthenticateTypedNilPrincipal(t *testing.T) {
308341func TestAuthenticateOptional (t * testing.T ) {
309342 ra1 := RouteAuthenticator {
310343 Authenticator : map [string ]runtime.Authenticator {
311- "auth1" : noApplyAuth ,
344+ testAuthenticator : noApplyAuth ,
312345 },
313- Schemes : []string {"auth1" },
314- Scopes : map [string ][]string {"auth1" : nil },
346+ Schemes : []string {testAuthenticator },
347+ Scopes : map [string ][]string {testAuthenticator : nil },
315348 }
316349 ra2 := RouteAuthenticator {
317350 allowAnonymous : true ,
@@ -341,10 +374,10 @@ func TestAuthenticateOptional(t *testing.T) {
341374
342375 ra4 := RouteAuthenticator {
343376 Authenticator : map [string ]runtime.Authenticator {
344- "auth1" : noApplyAuth ,
377+ testAuthenticator : noApplyAuth ,
345378 },
346- Schemes : []string {"auth1" },
347- Scopes : map [string ][]string {"auth1" : nil },
379+ Schemes : []string {testAuthenticator },
380+ Scopes : map [string ][]string {testAuthenticator : nil },
348381 }
349382 ra5 := RouteAuthenticator {
350383 allowAnonymous : true ,
0 commit comments