-
Notifications
You must be signed in to change notification settings - Fork 2
/
client_v0_test.go
206 lines (179 loc) · 6.41 KB
/
client_v0_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
197
198
199
200
201
202
203
204
205
206
package fireboltgosdk
import (
"context"
"net/http"
"net/http/httptest"
"os"
"strconv"
"testing"
"time"
)
// TestCacheAccessToken tests that a token is cached during authentication and reused for subsequent requests
func TestCacheAccessTokenV0(t *testing.T) {
var fetchTokenCount = 0
var totalCount = 0
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/auth/v1/login" {
fetchTokenCount++
_, _ = w.Write(getAuthResponseV0(10000))
} else {
w.WriteHeader(http.StatusOK)
}
totalCount++
}))
defer server.Close()
prepareEnvVariablesForTest(t, server)
var client = &ClientImplV0{
"",
BaseClient{ClientID: "ClientID@firebolt.io", ClientSecret: "password", ApiEndpoint: server.URL, UserAgent: "userAgent"},
}
client.accessTokenGetter = client.getAccessToken
for i := 0; i < 3; i++ {
resp := client.request(context.TODO(), "GET", server.URL, nil, "")
if resp.err != nil {
t.Errorf("Did not expect an error %s", resp.err)
}
}
token, _ := getAccessTokenUsernamePassword("ClientID@firebolt.io", "", server.URL, "")
if token != "aMysteriousToken" {
t.Errorf("Did not fetch missing token")
}
if getCachedAccessToken("ClientID@firebolt.io", server.URL) != "aMysteriousToken" {
t.Errorf("Did not fetch missing token")
}
if fetchTokenCount != 1 {
t.Errorf("Did not fetch token only once. Total: %d", fetchTokenCount)
}
if totalCount != 4 {
t.Errorf("Expected to call the server 4 times (1x to fetch token and 3x to send another request). Total: %d", totalCount)
}
}
// TestRefreshTokenOn401 tests that a token is refreshed when the server returns a 401
func TestRefreshTokenOn401V0(t *testing.T) {
var fetchTokenCount = 0
var totalCount = 0
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/auth/v1/login" {
fetchTokenCount++
_, _ = w.Write(getAuthResponseV0(10000))
} else {
w.WriteHeader(http.StatusUnauthorized)
}
totalCount++
}))
defer server.Close()
prepareEnvVariablesForTest(t, server)
var client = &ClientImplV0{
"",
BaseClient{ClientID: "ClientID@firebolt.io", ClientSecret: "password", ApiEndpoint: server.URL, UserAgent: "userAgent"},
}
client.accessTokenGetter = client.getAccessToken
_ = client.request(context.TODO(), "GET", server.URL, nil, "")
if getCachedAccessToken("ClientID@firebolt.io", server.URL) != "aMysteriousToken" {
t.Errorf("Did not fetch missing token")
}
if fetchTokenCount != 2 {
// The token should be fetched twice as it is removed from the cache due to the 403 and then fetched again
t.Errorf("Did not fetch token twice. Total: %d", fetchTokenCount)
}
if totalCount != 4 {
// The token is fetched twice and the request is retried
t.Errorf("Expected to call the server 4 times (2x to fetch tokens and 2x to send the request that returns a 403). Total: %d", totalCount)
}
}
// TestFetchTokenWhenExpired tests that a new token is fetched upon expiry
func TestFetchTokenWhenExpiredV0(t *testing.T) {
var fetchTokenCount = 0
var totalCount = 0
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == UsernamePasswordURLSuffix {
fetchTokenCount++
_, _ = w.Write(getAuthResponseV0(1))
} else {
w.WriteHeader(http.StatusOK)
}
totalCount++
}))
defer server.Close()
prepareEnvVariablesForTest(t, server)
var client = &ClientImplV0{
"",
BaseClient{ClientID: "ClientID@firebolt.io", ClientSecret: "password", ApiEndpoint: server.URL, UserAgent: "userAgent"},
}
client.accessTokenGetter = client.getAccessToken
_ = client.request(context.TODO(), "GET", server.URL, nil, "")
// Waiting for the token to get expired
time.Sleep(2 * time.Millisecond)
_ = client.request(context.TODO(), "GET", server.URL, nil, "")
token, _ := getAccessTokenUsernamePassword("ClientID@firebolt.io", "", server.URL, "")
if token != "aMysteriousToken" {
t.Errorf("Did not fetch missing token")
}
if getCachedAccessToken("ClientID@firebolt.io", server.URL) != "aMysteriousToken" {
t.Errorf("Did not fetch missing token")
}
if fetchTokenCount != 2 {
// The token should be fetched twice as it is automatically removed from the cache because it is expired
t.Errorf("Did not fetch token twice. Total: %d", fetchTokenCount)
}
if totalCount != 4 {
t.Errorf("Expected to call the server 5 times (2x to fetch tokens and 3x to send the request that returns a 403). Total: %d", totalCount)
}
}
// TestUserAgent tests that UserAgent is correctly set on request
func TestUserAgentV0(t *testing.T) {
var userAgentValue = "userAgent"
var userAgentHeader = ""
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
userAgentHeader = r.Header.Get("User-Agent")
w.WriteHeader(http.StatusOK)
}))
defer server.Close()
prepareEnvVariablesForTest(t, server)
var client = &ClientImplV0{
"",
BaseClient{ClientID: "ClientID@firebolt.io", ClientSecret: "password", ApiEndpoint: server.URL, UserAgent: userAgentValue},
}
client.accessTokenGetter = client.getAccessToken
client.parameterGetter = client.getQueryParams
_, _ = client.Query(context.TODO(), server.URL, "SELECT 1", map[string]string{}, connectionControl{})
if userAgentHeader != userAgentValue {
t.Errorf("Did not set User-Agent value correctly on a query request")
}
}
func clientFactoryV0(apiEndpoint string) Client {
var client = &ClientImplV0{
"",
BaseClient{ClientID: "ClientID@firebolt.io", ClientSecret: "password", ApiEndpoint: apiEndpoint},
}
client.accessTokenGetter = client.getAccessToken
client.parameterGetter = client.getQueryParams
return client
}
// TestProtocolVersion tests that protocol version is correctly set on request
func TestProtocolVersionV0(t *testing.T) {
testProtocolVersion(t, clientFactoryV0)
}
func TestUpdateParametersV0(t *testing.T) {
testUpdateParameters(t, clientFactoryV0)
}
func TestAdditionalHeadersV0(t *testing.T) {
testAdditionalHeaders(t, clientFactoryV0)
}
func getAuthResponseV0(expiry int) []byte {
var response = `{
"access_token": "aMysteriousToken",
"refresh_token": "refresh",
"scope": "offline_access",
"expires_in": ` + strconv.Itoa(expiry) + `,
"token_type": "Bearer"
}`
return []byte(response)
}
func prepareEnvVariablesForTest(t *testing.T, server *httptest.Server) {
os.Setenv("FIREBOLT_ENDPOINT", server.URL)
t.Cleanup(cleanupEnvVariables)
}
func cleanupEnvVariables() {
os.Setenv("FIREBOLT_ENDPOINT", originalEndpoint)
}