|
| 1 | +package github |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/github/github-mcp-server/pkg/inventory" |
| 8 | + "github.com/modelcontextprotocol/go-sdk/mcp" |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | +) |
| 12 | + |
| 13 | +func TestCreateToolScopeFilter(t *testing.T) { |
| 14 | + // Create test tools with various scope requirements |
| 15 | + toolNoScopes := &inventory.ServerTool{ |
| 16 | + Tool: mcp.Tool{Name: "no_scopes_tool"}, |
| 17 | + AcceptedScopes: nil, |
| 18 | + } |
| 19 | + |
| 20 | + toolEmptyScopes := &inventory.ServerTool{ |
| 21 | + Tool: mcp.Tool{Name: "empty_scopes_tool"}, |
| 22 | + AcceptedScopes: []string{}, |
| 23 | + } |
| 24 | + |
| 25 | + toolRepoScope := &inventory.ServerTool{ |
| 26 | + Tool: mcp.Tool{Name: "repo_tool"}, |
| 27 | + AcceptedScopes: []string{"repo"}, |
| 28 | + } |
| 29 | + |
| 30 | + toolPublicRepoScope := &inventory.ServerTool{ |
| 31 | + Tool: mcp.Tool{Name: "public_repo_tool"}, |
| 32 | + AcceptedScopes: []string{"public_repo", "repo"}, // repo is parent, also accepted |
| 33 | + } |
| 34 | + |
| 35 | + toolGistScope := &inventory.ServerTool{ |
| 36 | + Tool: mcp.Tool{Name: "gist_tool"}, |
| 37 | + AcceptedScopes: []string{"gist"}, |
| 38 | + } |
| 39 | + |
| 40 | + toolMultiScope := &inventory.ServerTool{ |
| 41 | + Tool: mcp.Tool{Name: "multi_scope_tool"}, |
| 42 | + AcceptedScopes: []string{"repo", "admin:org"}, |
| 43 | + } |
| 44 | + |
| 45 | + tests := []struct { |
| 46 | + name string |
| 47 | + tokenScopes []string |
| 48 | + tool *inventory.ServerTool |
| 49 | + expected bool |
| 50 | + }{ |
| 51 | + { |
| 52 | + name: "tool with no scopes is always visible", |
| 53 | + tokenScopes: []string{}, |
| 54 | + tool: toolNoScopes, |
| 55 | + expected: true, |
| 56 | + }, |
| 57 | + { |
| 58 | + name: "tool with empty scopes is always visible", |
| 59 | + tokenScopes: []string{"repo"}, |
| 60 | + tool: toolEmptyScopes, |
| 61 | + expected: true, |
| 62 | + }, |
| 63 | + { |
| 64 | + name: "token with exact scope can see tool", |
| 65 | + tokenScopes: []string{"repo"}, |
| 66 | + tool: toolRepoScope, |
| 67 | + expected: true, |
| 68 | + }, |
| 69 | + { |
| 70 | + name: "token with parent scope can see child-scoped tool", |
| 71 | + tokenScopes: []string{"repo"}, |
| 72 | + tool: toolPublicRepoScope, |
| 73 | + expected: true, |
| 74 | + }, |
| 75 | + { |
| 76 | + name: "token missing required scope cannot see tool", |
| 77 | + tokenScopes: []string{"gist"}, |
| 78 | + tool: toolRepoScope, |
| 79 | + expected: false, |
| 80 | + }, |
| 81 | + { |
| 82 | + name: "token with unrelated scope cannot see tool", |
| 83 | + tokenScopes: []string{"repo"}, |
| 84 | + tool: toolGistScope, |
| 85 | + expected: false, |
| 86 | + }, |
| 87 | + { |
| 88 | + name: "token with one of multiple accepted scopes can see tool", |
| 89 | + tokenScopes: []string{"admin:org"}, |
| 90 | + tool: toolMultiScope, |
| 91 | + expected: true, |
| 92 | + }, |
| 93 | + { |
| 94 | + name: "empty token scopes cannot see scoped tools", |
| 95 | + tokenScopes: []string{}, |
| 96 | + tool: toolRepoScope, |
| 97 | + expected: false, |
| 98 | + }, |
| 99 | + { |
| 100 | + name: "token with multiple scopes where one matches", |
| 101 | + tokenScopes: []string{"gist", "repo"}, |
| 102 | + tool: toolPublicRepoScope, |
| 103 | + expected: true, |
| 104 | + }, |
| 105 | + } |
| 106 | + |
| 107 | + for _, tt := range tests { |
| 108 | + t.Run(tt.name, func(t *testing.T) { |
| 109 | + filter := CreateToolScopeFilter(tt.tokenScopes) |
| 110 | + result, err := filter(context.Background(), tt.tool) |
| 111 | + |
| 112 | + require.NoError(t, err) |
| 113 | + assert.Equal(t, tt.expected, result, "filter result should match expected") |
| 114 | + }) |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +func TestCreateToolScopeFilter_Integration(t *testing.T) { |
| 119 | + // Test integration with inventory builder |
| 120 | + tools := []inventory.ServerTool{ |
| 121 | + { |
| 122 | + Tool: mcp.Tool{Name: "public_tool"}, |
| 123 | + Toolset: inventory.ToolsetMetadata{ID: "test"}, |
| 124 | + AcceptedScopes: nil, // No scopes required |
| 125 | + }, |
| 126 | + { |
| 127 | + Tool: mcp.Tool{Name: "repo_tool"}, |
| 128 | + Toolset: inventory.ToolsetMetadata{ID: "test"}, |
| 129 | + AcceptedScopes: []string{"repo"}, |
| 130 | + }, |
| 131 | + { |
| 132 | + Tool: mcp.Tool{Name: "gist_tool"}, |
| 133 | + Toolset: inventory.ToolsetMetadata{ID: "test"}, |
| 134 | + AcceptedScopes: []string{"gist"}, |
| 135 | + }, |
| 136 | + } |
| 137 | + |
| 138 | + // Create filter for token with only "repo" scope |
| 139 | + filter := CreateToolScopeFilter([]string{"repo"}) |
| 140 | + |
| 141 | + // Build inventory with the filter |
| 142 | + inv := inventory.NewBuilder(). |
| 143 | + SetTools(tools). |
| 144 | + WithToolsets([]string{"test"}). |
| 145 | + WithFilter(filter). |
| 146 | + Build() |
| 147 | + |
| 148 | + // Get available tools |
| 149 | + availableTools := inv.AvailableTools(context.Background()) |
| 150 | + |
| 151 | + // Should see public_tool and repo_tool, but not gist_tool |
| 152 | + assert.Len(t, availableTools, 2) |
| 153 | + |
| 154 | + toolNames := make([]string, len(availableTools)) |
| 155 | + for i, tool := range availableTools { |
| 156 | + toolNames[i] = tool.Tool.Name |
| 157 | + } |
| 158 | + |
| 159 | + assert.Contains(t, toolNames, "public_tool") |
| 160 | + assert.Contains(t, toolNames, "repo_tool") |
| 161 | + assert.NotContains(t, toolNames, "gist_tool") |
| 162 | +} |
0 commit comments