-
Notifications
You must be signed in to change notification settings - Fork 4k
Expand file tree
/
Copy pathpat_scope_test.go
More file actions
231 lines (199 loc) · 6.68 KB
/
pat_scope_test.go
File metadata and controls
231 lines (199 loc) · 6.68 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
package middleware
import (
"context"
"errors"
"log/slog"
"net/http"
"net/http/httptest"
"testing"
ghcontext "github.com/github/github-mcp-server/pkg/context"
"github.com/github/github-mcp-server/pkg/utils"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// mockScopeFetcher is a mock implementation of scopes.FetcherInterface
type mockScopeFetcher struct {
scopes []string
err error
callCount int
tokens []string
}
func (m *mockScopeFetcher) FetchTokenScopes(_ context.Context, token string) ([]string, error) {
m.callCount += 1
m.tokens = append(m.tokens, token)
return m.scopes, m.err
}
func TestWithPATScopes(t *testing.T) {
logger := slog.Default()
tests := []struct {
name string
tokenInfo *ghcontext.TokenInfo
fetcherScopes []string
fetcherErr error
expectScopesFetched bool
expectedScopes []string
expectNextHandlerCalled bool
}{
{
name: "no token info in context calls next handler",
tokenInfo: nil,
expectScopesFetched: false,
expectedScopes: nil,
expectNextHandlerCalled: true,
},
{
name: "non-PAT token type skips scope fetching",
tokenInfo: &ghcontext.TokenInfo{
Token: "gho_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
TokenType: utils.TokenTypeOAuthAccessToken,
},
expectScopesFetched: false,
expectedScopes: nil,
expectNextHandlerCalled: true,
},
{
name: "fine-grained PAT skips scope fetching",
tokenInfo: &ghcontext.TokenInfo{
Token: "github_pat_xxxxxxxxxxxxxxxxxxxxxxx",
TokenType: utils.TokenTypeFineGrainedPersonalAccessToken,
},
expectScopesFetched: false,
expectedScopes: nil,
expectNextHandlerCalled: true,
},
{
name: "classic PAT fetches and stores scopes",
tokenInfo: &ghcontext.TokenInfo{
Token: "ghp_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
TokenType: utils.TokenTypePersonalAccessToken,
},
fetcherScopes: []string{"repo", "user", "read:org"},
expectScopesFetched: true,
expectedScopes: []string{"repo", "user", "read:org"},
expectNextHandlerCalled: true,
},
{
name: "classic PAT with empty scopes",
tokenInfo: &ghcontext.TokenInfo{
Token: "ghp_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
TokenType: utils.TokenTypePersonalAccessToken,
},
fetcherScopes: []string{},
expectScopesFetched: true,
expectedScopes: []string{},
expectNextHandlerCalled: true,
},
{
name: "fetcher error calls next handler without scopes",
tokenInfo: &ghcontext.TokenInfo{
Token: "ghp_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
TokenType: utils.TokenTypePersonalAccessToken,
},
fetcherErr: errors.New("network error"),
expectScopesFetched: false,
expectedScopes: nil,
expectNextHandlerCalled: true,
},
{
name: "old-style PAT (40 hex chars) fetches scopes",
tokenInfo: &ghcontext.TokenInfo{
Token: "0123456789abcdef0123456789abcdef01234567",
TokenType: utils.TokenTypePersonalAccessToken,
},
fetcherScopes: []string{"repo"},
expectScopesFetched: true,
expectedScopes: []string{"repo"},
expectNextHandlerCalled: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var capturedScopes []string
var scopesFound bool
var nextHandlerCalled bool
nextHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
nextHandlerCalled = true
capturedScopes, scopesFound = ghcontext.GetTokenScopes(r.Context())
w.WriteHeader(http.StatusOK)
})
fetcher := &mockScopeFetcher{
scopes: tt.fetcherScopes,
err: tt.fetcherErr,
}
middleware := WithPATScopes(logger, fetcher)
handler := middleware(nextHandler)
req := httptest.NewRequest(http.MethodGet, "/test", nil)
// Set up context with token info if provided
if tt.tokenInfo != nil {
ctx := ghcontext.WithTokenInfo(req.Context(), tt.tokenInfo)
req = req.WithContext(ctx)
}
rr := httptest.NewRecorder()
handler.ServeHTTP(rr, req)
assert.Equal(t, tt.expectNextHandlerCalled, nextHandlerCalled, "next handler called mismatch")
if tt.expectNextHandlerCalled {
assert.Equal(t, tt.expectScopesFetched, scopesFound, "scopes found mismatch")
assert.Equal(t, tt.expectedScopes, capturedScopes)
}
})
}
}
func TestWithPATScopes_PreservesExistingTokenInfo(t *testing.T) {
logger := slog.Default()
var capturedTokenInfo *ghcontext.TokenInfo
var capturedScopes []string
var scopesFound bool
nextHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
capturedTokenInfo, _ = ghcontext.GetTokenInfo(r.Context())
capturedScopes, scopesFound = ghcontext.GetTokenScopes(r.Context())
w.WriteHeader(http.StatusOK)
})
fetcher := &mockScopeFetcher{
scopes: []string{"repo", "user"},
}
originalTokenInfo := &ghcontext.TokenInfo{
Token: "ghp_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
TokenType: utils.TokenTypePersonalAccessToken,
}
middleware := WithPATScopes(logger, fetcher)
handler := middleware(nextHandler)
req := httptest.NewRequest(http.MethodGet, "/test", nil)
ctx := ghcontext.WithTokenInfo(req.Context(), originalTokenInfo)
req = req.WithContext(ctx)
rr := httptest.NewRecorder()
handler.ServeHTTP(rr, req)
require.NotNil(t, capturedTokenInfo)
assert.Equal(t, originalTokenInfo.Token, capturedTokenInfo.Token)
assert.Equal(t, originalTokenInfo.TokenType, capturedTokenInfo.TokenType)
assert.True(t, scopesFound)
assert.Equal(t, []string{"repo", "user"}, capturedScopes)
}
func TestWithPATScopes_RefetchesWhenCachedScopesBelongToDifferentToken(t *testing.T) {
logger := slog.Default()
var capturedScopes []string
var scopesFound bool
nextHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
capturedScopes, scopesFound = ghcontext.GetTokenScopes(r.Context())
w.WriteHeader(http.StatusOK)
})
fetcher := &mockScopeFetcher{
scopes: []string{"read:org"},
}
middleware := WithPATScopes(logger, fetcher)
handler := middleware(nextHandler)
req := httptest.NewRequest(http.MethodGet, "/test", nil)
ctx := req.Context()
ctx = ghcontext.WithTokenInfo(ctx, &ghcontext.TokenInfo{
Token: "ghp_new_token",
TokenType: utils.TokenTypePersonalAccessToken,
})
ctx = ghcontext.WithTokenScopesForToken(ctx, "ghp_old_token", []string{"repo"})
req = req.WithContext(ctx)
rr := httptest.NewRecorder()
handler.ServeHTTP(rr, req)
assert.True(t, scopesFound)
assert.Equal(t, []string{"read:org"}, capturedScopes)
assert.Equal(t, 1, fetcher.callCount)
require.Len(t, fetcher.tokens, 1)
assert.Equal(t, "ghp_new_token", fetcher.tokens[0])
}