|
| 1 | +package middleware |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "log/slog" |
| 7 | + "net/http" |
| 8 | + "net/http/httptest" |
| 9 | + "testing" |
| 10 | + |
| 11 | + ghcontext "github.com/github/github-mcp-server/pkg/context" |
| 12 | + "github.com/github/github-mcp-server/pkg/utils" |
| 13 | + "github.com/stretchr/testify/assert" |
| 14 | + "github.com/stretchr/testify/require" |
| 15 | +) |
| 16 | + |
| 17 | +// mockScopeFetcher is a mock implementation of scopes.FetcherInterface |
| 18 | +type mockScopeFetcher struct { |
| 19 | + scopes []string |
| 20 | + err error |
| 21 | +} |
| 22 | + |
| 23 | +func (m *mockScopeFetcher) FetchTokenScopes(_ context.Context, _ string) ([]string, error) { |
| 24 | + return m.scopes, m.err |
| 25 | +} |
| 26 | + |
| 27 | +func TestWithPATScopes(t *testing.T) { |
| 28 | + logger := slog.Default() |
| 29 | + |
| 30 | + tests := []struct { |
| 31 | + name string |
| 32 | + tokenInfo *ghcontext.TokenInfo |
| 33 | + fetcherScopes []string |
| 34 | + fetcherErr error |
| 35 | + expectScopesFetched bool |
| 36 | + expectedScopes []string |
| 37 | + expectNextHandlerCalled bool |
| 38 | + }{ |
| 39 | + { |
| 40 | + name: "no token info in context calls next handler", |
| 41 | + tokenInfo: nil, |
| 42 | + expectScopesFetched: false, |
| 43 | + expectedScopes: nil, |
| 44 | + expectNextHandlerCalled: true, |
| 45 | + }, |
| 46 | + { |
| 47 | + name: "non-PAT token type skips scope fetching", |
| 48 | + tokenInfo: &ghcontext.TokenInfo{ |
| 49 | + Token: "gho_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", |
| 50 | + TokenType: utils.TokenTypeOAuthAccessToken, |
| 51 | + }, |
| 52 | + expectScopesFetched: false, |
| 53 | + expectedScopes: nil, |
| 54 | + expectNextHandlerCalled: false, // middleware doesn't call next for non-PAT tokens |
| 55 | + }, |
| 56 | + { |
| 57 | + name: "fine-grained PAT skips scope fetching", |
| 58 | + tokenInfo: &ghcontext.TokenInfo{ |
| 59 | + Token: "github_pat_xxxxxxxxxxxxxxxxxxxxxxx", |
| 60 | + TokenType: utils.TokenTypeFineGrainedPersonalAccessToken, |
| 61 | + }, |
| 62 | + expectScopesFetched: false, |
| 63 | + expectedScopes: nil, |
| 64 | + expectNextHandlerCalled: false, |
| 65 | + }, |
| 66 | + { |
| 67 | + name: "classic PAT fetches and stores scopes", |
| 68 | + tokenInfo: &ghcontext.TokenInfo{ |
| 69 | + Token: "ghp_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", |
| 70 | + TokenType: utils.TokenTypePersonalAccessToken, |
| 71 | + }, |
| 72 | + fetcherScopes: []string{"repo", "user", "read:org"}, |
| 73 | + expectScopesFetched: true, |
| 74 | + expectedScopes: []string{"repo", "user", "read:org"}, |
| 75 | + expectNextHandlerCalled: true, |
| 76 | + }, |
| 77 | + { |
| 78 | + name: "classic PAT with empty scopes", |
| 79 | + tokenInfo: &ghcontext.TokenInfo{ |
| 80 | + Token: "ghp_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", |
| 81 | + TokenType: utils.TokenTypePersonalAccessToken, |
| 82 | + }, |
| 83 | + fetcherScopes: []string{}, |
| 84 | + expectScopesFetched: true, |
| 85 | + expectedScopes: []string{}, |
| 86 | + expectNextHandlerCalled: true, |
| 87 | + }, |
| 88 | + { |
| 89 | + name: "fetcher error calls next handler without scopes", |
| 90 | + tokenInfo: &ghcontext.TokenInfo{ |
| 91 | + Token: "ghp_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", |
| 92 | + TokenType: utils.TokenTypePersonalAccessToken, |
| 93 | + }, |
| 94 | + fetcherErr: errors.New("network error"), |
| 95 | + expectScopesFetched: false, |
| 96 | + expectedScopes: nil, |
| 97 | + expectNextHandlerCalled: true, |
| 98 | + }, |
| 99 | + { |
| 100 | + name: "old-style PAT (40 hex chars) fetches scopes", |
| 101 | + tokenInfo: &ghcontext.TokenInfo{ |
| 102 | + Token: "0123456789abcdef0123456789abcdef01234567", |
| 103 | + TokenType: utils.TokenTypePersonalAccessToken, |
| 104 | + }, |
| 105 | + fetcherScopes: []string{"repo"}, |
| 106 | + expectScopesFetched: true, |
| 107 | + expectedScopes: []string{"repo"}, |
| 108 | + expectNextHandlerCalled: true, |
| 109 | + }, |
| 110 | + } |
| 111 | + |
| 112 | + for _, tt := range tests { |
| 113 | + t.Run(tt.name, func(t *testing.T) { |
| 114 | + var capturedTokenInfo *ghcontext.TokenInfo |
| 115 | + var nextHandlerCalled bool |
| 116 | + |
| 117 | + nextHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 118 | + nextHandlerCalled = true |
| 119 | + capturedTokenInfo, _ = ghcontext.GetTokenInfo(r.Context()) |
| 120 | + w.WriteHeader(http.StatusOK) |
| 121 | + }) |
| 122 | + |
| 123 | + fetcher := &mockScopeFetcher{ |
| 124 | + scopes: tt.fetcherScopes, |
| 125 | + err: tt.fetcherErr, |
| 126 | + } |
| 127 | + |
| 128 | + middleware := WithPATScopes(logger, fetcher) |
| 129 | + handler := middleware(nextHandler) |
| 130 | + |
| 131 | + req := httptest.NewRequest(http.MethodGet, "/test", nil) |
| 132 | + |
| 133 | + // Set up context with token info if provided |
| 134 | + if tt.tokenInfo != nil { |
| 135 | + ctx := ghcontext.WithTokenInfo(req.Context(), tt.tokenInfo) |
| 136 | + req = req.WithContext(ctx) |
| 137 | + } |
| 138 | + |
| 139 | + rr := httptest.NewRecorder() |
| 140 | + handler.ServeHTTP(rr, req) |
| 141 | + |
| 142 | + assert.Equal(t, tt.expectNextHandlerCalled, nextHandlerCalled, "next handler called mismatch") |
| 143 | + |
| 144 | + if tt.expectNextHandlerCalled && tt.tokenInfo != nil { |
| 145 | + require.NotNil(t, capturedTokenInfo, "expected token info in context") |
| 146 | + assert.Equal(t, tt.expectScopesFetched, capturedTokenInfo.ScopesFetched) |
| 147 | + assert.Equal(t, tt.expectedScopes, capturedTokenInfo.Scopes) |
| 148 | + } |
| 149 | + }) |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +func TestWithPATScopes_PreservesExistingTokenInfo(t *testing.T) { |
| 154 | + logger := slog.Default() |
| 155 | + |
| 156 | + var capturedTokenInfo *ghcontext.TokenInfo |
| 157 | + |
| 158 | + nextHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 159 | + capturedTokenInfo, _ = ghcontext.GetTokenInfo(r.Context()) |
| 160 | + w.WriteHeader(http.StatusOK) |
| 161 | + }) |
| 162 | + |
| 163 | + fetcher := &mockScopeFetcher{ |
| 164 | + scopes: []string{"repo", "user"}, |
| 165 | + } |
| 166 | + |
| 167 | + originalTokenInfo := &ghcontext.TokenInfo{ |
| 168 | + Token: "ghp_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", |
| 169 | + TokenType: utils.TokenTypePersonalAccessToken, |
| 170 | + } |
| 171 | + |
| 172 | + middleware := WithPATScopes(logger, fetcher) |
| 173 | + handler := middleware(nextHandler) |
| 174 | + |
| 175 | + req := httptest.NewRequest(http.MethodGet, "/test", nil) |
| 176 | + ctx := ghcontext.WithTokenInfo(req.Context(), originalTokenInfo) |
| 177 | + req = req.WithContext(ctx) |
| 178 | + |
| 179 | + rr := httptest.NewRecorder() |
| 180 | + handler.ServeHTTP(rr, req) |
| 181 | + |
| 182 | + require.NotNil(t, capturedTokenInfo) |
| 183 | + assert.Equal(t, originalTokenInfo.Token, capturedTokenInfo.Token) |
| 184 | + assert.Equal(t, originalTokenInfo.TokenType, capturedTokenInfo.TokenType) |
| 185 | + assert.True(t, capturedTokenInfo.ScopesFetched) |
| 186 | + assert.Equal(t, []string{"repo", "user"}, capturedTokenInfo.Scopes) |
| 187 | +} |
0 commit comments