@@ -7,6 +7,13 @@ import (
7
7
"testing"
8
8
9
9
"github.com/stretchr/testify/assert"
10
+
11
+ "gofr.dev/pkg/gofr/container"
12
+ )
13
+
14
+ const (
15
+ validKey1 string = "valid-key-1"
16
+ validKey2 string = "valid-key-2"
10
17
)
11
18
12
19
func Test_ApiKeyAuthMiddleware (t * testing.T ) {
@@ -15,7 +22,10 @@ func Test_ApiKeyAuthMiddleware(t *testing.T) {
15
22
})
16
23
17
24
validator := func (apiKey string ) bool {
18
- return apiKey == "valid-key"
25
+ return apiKey == validKey1
26
+ }
27
+ validatorWithDB := func (_ * container.Container , apiKey string ) bool {
28
+ return apiKey == validKey2
19
29
}
20
30
21
31
req , err := http .NewRequestWithContext (context .Background (), "GET" , "/" , http .NoBody )
@@ -24,26 +34,34 @@ func Test_ApiKeyAuthMiddleware(t *testing.T) {
24
34
}
25
35
26
36
testCases := []struct {
27
- desc string
28
- validator func (apiKey string ) bool
29
- apiKey string
30
- responseCode int
31
- responseBody string
37
+ desc string
38
+ validatorFunc func (akiKey string ) bool
39
+ validatorFuncWithDB func (c * container.Container , apiKey string ) bool
40
+ apiKey string
41
+ responseCode int
42
+ responseBody string
32
43
}{
33
- {"missing api-key" , nil , "" , 401 , "Unauthorized: Authorization header missing\n " },
34
- {"invalid api-key" , nil , "invalid-key" , 401 , "Unauthorized: Invalid Authorization header\n " },
35
- {"valid api-key" , nil , "valid-key-1" , 200 , "Success" },
36
- {"another valid api-key" , nil , "valid-key-2" , 200 , "Success" },
37
- {"custom validator valid key" , validator , "valid-key" , 200 , "Success" },
38
- {"custom validator in-valid key" , validator , "invalid-key" , 401 , "Unauthorized: Invalid Authorization header\n " },
44
+ {"missing api-key" , nil , nil , "" , 401 , "Unauthorized: Authorization header missing\n " },
45
+ {"invalid api-key" , nil , nil , "invalid-key" , 401 , "Unauthorized: Invalid Authorization header\n " },
46
+ {"valid api-key" , nil , nil , validKey1 , 200 , "Success" },
47
+ {"another valid api-key" , nil , nil , validKey2 , 200 , "Success" },
48
+ {"custom validatorFunc valid key" , validator , nil , validKey1 , 200 , "Success" },
49
+ {"custom validatorFuncWithDB valid key" , nil , validatorWithDB , validKey2 , 200 , "Success" },
50
+ {"custom validatorFuncWithDB in-valid key" , nil , validatorWithDB , "invalid-key" , 401 , "Unauthorized: Invalid Authorization header\n " },
39
51
}
40
52
41
53
for i , tc := range testCases {
42
54
rr := httptest .NewRecorder ()
43
55
44
56
req .Header .Set ("X-API-KEY" , tc .apiKey )
45
57
46
- wrappedHandler := APIKeyAuthMiddleware (tc .validator , "valid-key-1" , "valid-key-2" )(testHandler )
58
+ provider := APIKeyAuthProvider {
59
+ ValidateFunc : tc .validatorFunc ,
60
+ ValidateFuncWithDatasources : tc .validatorFuncWithDB ,
61
+ Container : nil ,
62
+ }
63
+
64
+ wrappedHandler := APIKeyAuthMiddleware (provider , validKey1 , validKey2 )(testHandler )
47
65
wrappedHandler .ServeHTTP (rr , req )
48
66
49
67
assert .Equal (t , tc .responseCode , rr .Code , "TEST[%d], Failed.\n %s" , i , tc .desc )
@@ -60,7 +78,9 @@ func Test_ApiKeyAuthMiddleware_well_known(t *testing.T) {
60
78
req := httptest .NewRequest (http .MethodGet , "/.well-known/health-check" , http .NoBody )
61
79
rr := httptest .NewRecorder ()
62
80
63
- wrappedHandler := APIKeyAuthMiddleware (nil )(testHandler )
81
+ provider := APIKeyAuthProvider {}
82
+
83
+ wrappedHandler := APIKeyAuthMiddleware (provider )(testHandler )
64
84
wrappedHandler .ServeHTTP (rr , req )
65
85
66
86
assert .Equal (t , 200 , rr .Code , "TEST Failed.\n " )
0 commit comments