-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathauth_token_test.go
More file actions
197 lines (171 loc) · 4.81 KB
/
auth_token_test.go
File metadata and controls
197 lines (171 loc) · 4.81 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
package main
import (
"context"
"fmt"
"net/url"
"testing"
"github.com/sourcegraph/src-cli/internal/oauth"
)
func TestResolveAuthToken(t *testing.T) {
t.Run("uses configured access token before keyring", func(t *testing.T) {
reset := stubAuthTokenDependencies(t)
defer reset()
newRefresherCalled := false
newOAuthTokenRefresher = func(*oauth.Token) oauthTokenRefresher {
newRefresherCalled = true
return fakeOAuthTokenRefresher{}
}
token, err := resolveAuthToken(context.Background(), &config{
accessToken: "access-token",
endpointURL: mustParseURL(t, "https://example.com"),
})
if err != nil {
t.Fatal(err)
}
if token != "access-token" {
t.Fatalf("token = %q, want %q", token, "access-token")
}
if newRefresherCalled {
t.Fatal("expected OAuth token refresher not to be created")
}
})
t.Run("requires access token in CI", func(t *testing.T) {
reset := stubAuthTokenDependencies(t)
defer reset()
loadCalled := false
loadOAuthToken = func(context.Context, *url.URL) (*oauth.Token, error) {
loadCalled = true
return nil, nil
}
_, err := resolveAuthToken(context.Background(), &config{
inCI: true,
endpointURL: mustParseURL(t, "https://example.com"),
})
if err != errCIAccessTokenRequired {
t.Fatalf("err = %v, want %v", err, errCIAccessTokenRequired)
}
if loadCalled {
t.Fatal("expected OAuth token loader not to be called")
}
})
t.Run("uses stored oauth token", func(t *testing.T) {
reset := stubAuthTokenDependencies(t)
defer reset()
loadOAuthToken = func(context.Context, *url.URL) (*oauth.Token, error) {
return &oauth.Token{
AccessToken: "oauth-token",
}, nil
}
newOAuthTokenRefresher = func(*oauth.Token) oauthTokenRefresher {
return fakeOAuthTokenRefresher{token: oauth.Token{AccessToken: "oauth-token"}}
}
token, err := resolveAuthToken(context.Background(), &config{
endpointURL: mustParseURL(t, "https://example.com"),
})
if err != nil {
t.Fatal(err)
}
if token != "oauth-token" {
t.Fatalf("token = %q, want %q", token, "oauth-token")
}
})
t.Run("refreshes expiring oauth token", func(t *testing.T) {
reset := stubAuthTokenDependencies(t)
defer reset()
loadOAuthToken = func(context.Context, *url.URL) (*oauth.Token, error) {
return &oauth.Token{AccessToken: "old-token"}, nil
}
newOAuthTokenRefresher = func(*oauth.Token) oauthTokenRefresher {
return fakeOAuthTokenRefresher{token: oauth.Token{AccessToken: "new-token"}}
}
token, err := resolveAuthToken(context.Background(), &config{
endpointURL: mustParseURL(t, "https://example.com"),
})
if err != nil {
t.Fatal(err)
}
if token != "new-token" {
t.Fatalf("token = %q, want %q", token, "new-token")
}
})
t.Run("returns refresh error when shared refresh logic fails", func(t *testing.T) {
reset := stubAuthTokenDependencies(t)
defer reset()
loadOAuthToken = func(context.Context, *url.URL) (*oauth.Token, error) {
return &oauth.Token{AccessToken: "old-token"}, nil
}
newOAuthTokenRefresher = func(*oauth.Token) oauthTokenRefresher {
return fakeOAuthTokenRefresher{err: fmt.Errorf("refresh failed")}
}
_, err := resolveAuthToken(context.Background(), &config{
endpointURL: mustParseURL(t, "https://example.com"),
})
if err == nil {
t.Fatal("expected error")
}
})
}
func TestFormatAuthTokenOutput(t *testing.T) {
tests := []struct {
name string
token string
mode AuthMode
header bool
want string
}{
{
name: "raw access token",
token: "access-token",
mode: AuthModeAccessToken,
header: false,
want: "access-token",
},
{
name: "raw oauth token",
token: "oauth-token",
mode: AuthModeOAuth,
header: false,
want: "oauth-token",
},
{
name: "authorization header for access token",
token: "access-token",
mode: AuthModeAccessToken,
header: true,
want: "Authorization: token access-token",
},
{
name: "authorization header for oauth token",
token: "oauth-token",
mode: AuthModeOAuth,
header: true,
want: "Authorization: Bearer oauth-token",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if got := formatAuthTokenOutput(test.token, test.mode, test.header); got != test.want {
t.Fatalf("formatAuthTokenOutput(%q, %v, %v) = %q, want %q", test.token, test.mode, test.header, got, test.want)
}
})
}
}
func stubAuthTokenDependencies(t *testing.T) func() {
t.Helper()
prevLoad := loadOAuthToken
prevNewRefresher := newOAuthTokenRefresher
return func() {
loadOAuthToken = prevLoad
newOAuthTokenRefresher = prevNewRefresher
}
}
type fakeOAuthTokenRefresher struct {
token oauth.Token
err error
}
func (r fakeOAuthTokenRefresher) GetToken(context.Context) (oauth.Token, error) {
if r.err != nil {
return oauth.Token{}, r.err
}
return r.token, nil
}