-
Notifications
You must be signed in to change notification settings - Fork 24
/
helpers_test.go
196 lines (180 loc) · 4.2 KB
/
helpers_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package jwtmiddleware
import (
"bytes"
"context"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"testing"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
gjwt "golang.org/x/oauth2/jwt"
"google.golang.org/appengine"
"google.golang.org/appengine/aetest"
gcpjwt "github.com/someone1/gcp-jwt-go/v2"
goauth2 "github.com/someone1/gcp-jwt-go/v2/oauth2"
)
var jwtConfig *gjwt.Config
func init() {
credPath := os.Getenv("GOOGLE_APPLICATION_CREDENTIALS")
if credPath == "" {
panic("GOOGLE_APPLICATION_CREDENTIALS environmental variable required for this test to run!")
}
b, err := ioutil.ReadFile(credPath)
if err != nil {
panic(err)
}
jwtConfig, err = google.JWTConfigFromJSON(b)
if err != nil {
panic(err)
}
}
func newTestReq(method, urlStr string, body io.Reader) (*http.Request, error) {
return httptest.NewRequest(method, urlStr, body), nil
}
func TestHelpers(t *testing.T) {
ctx := context.Background()
newReqFunc := newTestReq
// AppEngine test setup
isAppEngine := os.Getenv("APPENGINE_TEST") == "true"
if isAppEngine {
inst, err := aetest.NewInstance(nil)
if err != nil {
t.Fatal(err)
}
defer inst.Close()
req, err := inst.NewRequest("GET", "/", nil)
if err != nil {
t.Fatal(err)
}
ctx = appengine.NewContext(req)
newReqFunc = inst.NewRequest
}
config := &gcpjwt.IAMConfig{
ServiceAccount: jwtConfig.Email,
}
config.EnableCache = true
audience := "https://test.com"
testSources := make(map[int]oauth2.TokenSource)
t.Run("TokenSource", func(t *testing.T) {
var tests = []struct {
name string
config *gcpjwt.IAMConfig
out bool
}{
{
"NoType",
&gcpjwt.IAMConfig{
ServiceAccount: jwtConfig.Email,
},
true,
},
{
"BlobType",
&gcpjwt.IAMConfig{
ServiceAccount: jwtConfig.Email,
IAMType: gcpjwt.IAMBlobType,
},
false,
},
{
"JWTType",
&gcpjwt.IAMConfig{
ServiceAccount: jwtConfig.Email,
IAMType: gcpjwt.IAMJwtType,
},
false,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
source, err := goauth2.JWTAccessTokenSource(ctx, test.config, audience)
if (err != nil) != test.out {
t.Errorf("unexpected error `%v`", err)
}
if err == nil {
testSources[int(test.config.IAMType)] = source
}
})
}
})
t.Run("JWTMiddleware", func(t *testing.T) {
invalidAudienceSource, err := goauth2.JWTAccessTokenSource(ctx, &gcpjwt.IAMConfig{
ServiceAccount: jwtConfig.Email,
IAMType: gcpjwt.IAMJwtType,
}, "https://invalid")
if err != nil {
t.Errorf("Could not make invalid audience token source: %v", err)
return
}
okHandler := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte("ok"))
})
handler := NewHandler(ctx, config, "")(okHandler)
var tests = []struct {
name string
url string
source oauth2.TokenSource
want int
}{
{
"MissingToken",
audience,
nil,
http.StatusUnauthorized,
},
{
"InvalidAudienceToken",
audience,
invalidAudienceSource,
http.StatusForbidden,
},
{
"InvalidHost",
"http://invalid.com",
testSources[int(gcpjwt.IAMBlobType)],
http.StatusForbidden,
},
{
"BlobToken",
audience,
testSources[int(gcpjwt.IAMBlobType)],
http.StatusOK,
},
{
"JwtToken",
audience,
testSources[int(gcpjwt.IAMJwtType)],
http.StatusOK,
},
}
// Required for this to work
gcpjwt.SigningMethodIAMJWT.Override()
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
w := httptest.NewRecorder()
w.Body = bytes.NewBuffer(nil)
r, err := newReqFunc(http.MethodGet, test.url, nil)
if err != nil {
t.Errorf("could not create request: %v", err)
return
}
if test.source != nil {
token, err := test.source.Token()
if err != nil {
t.Errorf("error getting token: %v", err)
}
r.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token.AccessToken))
}
handler.ServeHTTP(w, r)
if got := w.Result().StatusCode; got != test.want {
t.Errorf("expected response code `%d`, got `%d`", test.want, got)
t.Errorf("Body: %s", w.Body.String())
}
})
}
})
}