Repository navigation
Expand file tree
/
Copy pathrefresh_test.go
More file actions
253 lines (243 loc) · 6.62 KB
/
Copy pathrefresh_test.go
File metadata and controls
253 lines (243 loc) · 6.62 KB
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package oauth
import (
"bytes"
"errors"
"io"
"net/http"
"net/url"
"strings"
"testing"
"github.com/cli/oauth/api"
)
type refreshClient struct {
status int
body string
contentType string
err error
postCount int
lastURL string
lastForm url.Values
}
func (c *refreshClient) PostForm(u string, params url.Values) (*http.Response, error) {
c.postCount++
c.lastURL = u
c.lastForm = params
if c.err != nil {
return nil, c.err
}
return &http.Response{
Body: io.NopCloser(bytes.NewBufferString(c.body)),
Header: http.Header{"Content-Type": {c.contentType}},
StatusCode: c.status,
}, nil
}
func TestRefresh(t *testing.T) {
transportErr := errors.New("network is unreachable")
host := &Host{TokenURL: "https://example.com/token"}
tests := []struct {
name string
client *refreshClient
options RefreshOptions
wantToken string
wantRefreshToken string
wantForm url.Values
wantPosts int
wantInvalid bool
wantAPIError string
wantError error
wantErrorString string
}{
{
name: "success with client secret",
client: &refreshClient{
status: http.StatusOK,
contentType: "application/x-www-form-urlencoded",
body: "access_token=NEWTOKEN&refresh_token=NEWREFRESH&expires_in=28800",
},
options: RefreshOptions{
Host: host,
ClientID: "CLIENTID",
ClientSecret: "CLIENTSECRET",
RefreshToken: "OLDREFRESH",
},
wantToken: "NEWTOKEN",
wantRefreshToken: "NEWREFRESH",
wantForm: url.Values{
"client_id": {"CLIENTID"},
"client_secret": {"CLIENTSECRET"},
"refresh_token": {"OLDREFRESH"},
"grant_type": {"refresh_token"},
},
wantPosts: 1,
},
{
name: "success without client secret",
client: &refreshClient{
status: http.StatusOK,
contentType: "application/x-www-form-urlencoded",
body: "access_token=NEWTOKEN&refresh_token=NEWREFRESH",
},
options: RefreshOptions{
Host: host,
ClientID: "CLIENTID",
RefreshToken: "OLDREFRESH",
},
wantToken: "NEWTOKEN",
wantRefreshToken: "NEWREFRESH",
wantForm: url.Values{
"client_id": {"CLIENTID"},
"refresh_token": {"OLDREFRESH"},
"grant_type": {"refresh_token"},
},
wantPosts: 1,
},
{
name: "omitted refresh token keeps the previous one",
client: &refreshClient{
status: http.StatusOK,
contentType: "application/x-www-form-urlencoded",
body: "access_token=NEWTOKEN&expires_in=28800",
},
options: RefreshOptions{
Host: host,
ClientID: "CLIENTID",
RefreshToken: "OLDREFRESH",
},
wantToken: "NEWTOKEN",
wantRefreshToken: "OLDREFRESH",
wantForm: url.Values{
"client_id": {"CLIENTID"},
"refresh_token": {"OLDREFRESH"},
"grant_type": {"refresh_token"},
},
wantPosts: 1,
},
{
name: "missing host",
client: &refreshClient{},
options: RefreshOptions{
ClientID: "CLIENTID",
RefreshToken: "OLDREFRESH",
},
wantPosts: 0,
wantErrorString: "host is required",
},
{
name: "empty refresh token",
client: &refreshClient{},
options: RefreshOptions{
Host: host,
ClientID: "CLIENTID",
},
wantInvalid: true,
wantPosts: 0,
wantErrorString: "refresh token is empty",
},
{
name: "invalid refresh token",
client: &refreshClient{
status: http.StatusBadRequest,
contentType: "application/x-www-form-urlencoded",
body: "error=bad_refresh_token&error_description=The+refresh+token+is+invalid.",
},
options: RefreshOptions{
Host: host,
ClientID: "CLIENTID",
RefreshToken: "OLDREFRESH",
},
wantInvalid: true,
wantAPIError: "bad_refresh_token",
wantPosts: 1,
},
{
name: "standard invalid grant response",
client: &refreshClient{
status: http.StatusBadRequest,
contentType: "application/x-www-form-urlencoded",
body: "error=invalid_grant&error_description=The+refresh+token+is+invalid+or+expired.",
},
options: RefreshOptions{
Host: host,
ClientID: "CLIENTID",
RefreshToken: "OLDREFRESH",
},
wantInvalid: true,
wantAPIError: "invalid_grant",
wantPosts: 1,
},
{
name: "other API error",
client: &refreshClient{
status: http.StatusBadRequest,
contentType: "application/x-www-form-urlencoded",
body: "error=incorrect_client_credentials",
},
options: RefreshOptions{
Host: host,
ClientID: "CLIENTID",
RefreshToken: "OLDREFRESH",
},
wantAPIError: "incorrect_client_credentials",
wantPosts: 1,
},
{
name: "transport error",
client: &refreshClient{
err: transportErr,
},
options: RefreshOptions{
Host: host,
ClientID: "CLIENTID",
RefreshToken: "OLDREFRESH",
},
wantError: transportErr,
wantPosts: 1,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.options.HTTPClient = tt.client
token, err := Refresh(tt.options)
if tt.wantToken != "" {
if err != nil {
t.Fatalf("Refresh() error = %v", err)
}
if token.Token != tt.wantToken {
t.Errorf("Token = %q, want %q", token.Token, tt.wantToken)
}
if token.RefreshToken != tt.wantRefreshToken {
t.Errorf("RefreshToken = %q, want %q", token.RefreshToken, tt.wantRefreshToken)
}
} else if err == nil {
t.Fatal("Refresh() error = nil")
}
if errors.Is(err, ErrRefreshTokenInvalid) != tt.wantInvalid {
t.Errorf("errors.Is(ErrRefreshTokenInvalid) = %v, want %v", errors.Is(err, ErrRefreshTokenInvalid), tt.wantInvalid)
}
if tt.wantError != nil && !errors.Is(err, tt.wantError) {
t.Errorf("error = %v, want %v", err, tt.wantError)
}
if tt.wantErrorString != "" && !strings.Contains(err.Error(), tt.wantErrorString) {
t.Errorf("error = %q, want it to contain %q", err, tt.wantErrorString)
}
if tt.wantAPIError != "" {
var apiError *api.Error
if !errors.As(err, &apiError) {
t.Fatalf("error = %v, want *api.Error", err)
}
if apiError.Code != tt.wantAPIError {
t.Errorf("error code = %q, want %q", apiError.Code, tt.wantAPIError)
}
}
if tt.client.postCount != tt.wantPosts {
t.Errorf("post count = %d, want %d", tt.client.postCount, tt.wantPosts)
}
if tt.wantForm != nil && tt.client.lastForm.Encode() != tt.wantForm.Encode() {
t.Errorf("form = %v, want %v", tt.client.lastForm, tt.wantForm)
}
if tt.wantPosts > 0 && tt.client.lastURL != host.TokenURL {
t.Errorf("URL = %q, want %q", tt.client.lastURL, host.TokenURL)
}
})
}
}