|
| 1 | +package github |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/github/github-mcp-server/pkg/inventory" |
| 7 | + "github.com/github/github-mcp-server/pkg/scopes" |
| 8 | + "github.com/github/github-mcp-server/pkg/translations" |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | +) |
| 12 | + |
| 13 | +func TestPublicRepoContributionToolScopeAccess(t *testing.T) { |
| 14 | + t.Parallel() |
| 15 | + |
| 16 | + tools := []struct { |
| 17 | + name string |
| 18 | + tool inventory.ServerTool |
| 19 | + }{ |
| 20 | + {name: "fork_repository", tool: ForkRepository(translations.NullTranslationHelper)}, |
| 21 | + {name: "create_branch", tool: CreateBranch(translations.NullTranslationHelper)}, |
| 22 | + {name: "create_pull_request", tool: CreatePullRequest(translations.NullTranslationHelper)}, |
| 23 | + {name: "issue_write", tool: IssueWrite(translations.NullTranslationHelper)}, |
| 24 | + {name: "add_issue_comment", tool: AddIssueComment(translations.NullTranslationHelper)}, |
| 25 | + } |
| 26 | + |
| 27 | + for _, tt := range tools { |
| 28 | + t.Run(tt.name, func(t *testing.T) { |
| 29 | + assert.Equal(t, []string{string(scopes.Repo)}, tt.tool.ScopeAccess.Scopes) |
| 30 | + require.NotNil(t, tt.tool.ScopeAccess.Visible) |
| 31 | + assert.False(t, tt.tool.ScopeAccess.Visible(nil)) |
| 32 | + assert.True(t, tt.tool.ScopeAccess.Visible([]string{string(scopes.PublicRepo)})) |
| 33 | + assert.True(t, tt.tool.ScopeAccess.Visible([]string{string(scopes.Repo)})) |
| 34 | + |
| 35 | + require.NotNil(t, tt.tool.ScopeAccess.Challenge) |
| 36 | + assert.Equal(t, []string{string(scopes.Repo)}, tt.tool.ScopeAccess.Challenge(nil, nil)) |
| 37 | + assert.Equal(t, []string{string(scopes.Repo)}, tt.tool.ScopeAccess.Challenge(nil, []string{string(scopes.PublicRepo)})) |
| 38 | + assert.Empty(t, tt.tool.ScopeAccess.Challenge(nil, []string{string(scopes.Repo)})) |
| 39 | + }) |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +func TestPublicRepoContributionToolsVisibleToPATs(t *testing.T) { |
| 44 | + t.Parallel() |
| 45 | + |
| 46 | + tools := []inventory.ServerTool{ |
| 47 | + ForkRepository(translations.NullTranslationHelper), |
| 48 | + CreateBranch(translations.NullTranslationHelper), |
| 49 | + PushFiles(translations.NullTranslationHelper), |
| 50 | + CreatePullRequest(translations.NullTranslationHelper), |
| 51 | + IssueWrite(translations.NullTranslationHelper), |
| 52 | + AddIssueComment(translations.NullTranslationHelper), |
| 53 | + } |
| 54 | + |
| 55 | + tests := []struct { |
| 56 | + name string |
| 57 | + tokenScopes []string |
| 58 | + wantVisible bool |
| 59 | + }{ |
| 60 | + {name: "no scopes"}, |
| 61 | + {name: "public_repo", tokenScopes: []string{string(scopes.PublicRepo)}, wantVisible: true}, |
| 62 | + {name: "repo", tokenScopes: []string{string(scopes.Repo)}, wantVisible: true}, |
| 63 | + } |
| 64 | + |
| 65 | + for _, tt := range tests { |
| 66 | + t.Run(tt.name, func(t *testing.T) { |
| 67 | + filter := CreateToolScopeFilter(tt.tokenScopes) |
| 68 | + for i := range tools { |
| 69 | + included, err := filter(t.Context(), &tools[i]) |
| 70 | + require.NoError(t, err) |
| 71 | + assert.Equal(t, tt.wantVisible, included, tools[i].Tool.Name) |
| 72 | + } |
| 73 | + }) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +func TestPushFilesOAuthScopeChallenges(t *testing.T) { |
| 78 | + t.Parallel() |
| 79 | + |
| 80 | + tool := PushFiles(translations.NullTranslationHelper) |
| 81 | + regularFiles := map[string]any{ |
| 82 | + "files": []any{map[string]any{"path": "README.md"}}, |
| 83 | + } |
| 84 | + workflowFiles := map[string]any{ |
| 85 | + "files": []any{map[string]any{"path": ".github/workflows/ci.yml"}}, |
| 86 | + } |
| 87 | + |
| 88 | + assert.Equal(t, []string{string(scopes.Repo), string(scopes.Workflow)}, tool.ScopeAccess.Scopes) |
| 89 | + require.NotNil(t, tool.ScopeAccess.Visible) |
| 90 | + assert.True(t, tool.ScopeAccess.Visible([]string{string(scopes.PublicRepo)})) |
| 91 | + assert.True(t, tool.ScopeAccess.Visible([]string{string(scopes.Repo)})) |
| 92 | + |
| 93 | + assert.Equal(t, []string{string(scopes.Repo)}, tool.ScopeAccess.Challenge(regularFiles, nil)) |
| 94 | + assert.Equal(t, []string{string(scopes.Repo)}, tool.ScopeAccess.Challenge(regularFiles, []string{string(scopes.PublicRepo)})) |
| 95 | + assert.Empty(t, tool.ScopeAccess.Challenge(regularFiles, []string{string(scopes.Repo)})) |
| 96 | + |
| 97 | + assert.Equal(t, []string{string(scopes.Repo), string(scopes.Workflow)}, tool.ScopeAccess.Challenge(workflowFiles, nil)) |
| 98 | + assert.Equal(t, []string{string(scopes.Repo), string(scopes.Workflow)}, tool.ScopeAccess.Challenge(workflowFiles, []string{string(scopes.PublicRepo)})) |
| 99 | + assert.Equal(t, []string{string(scopes.Workflow)}, tool.ScopeAccess.Challenge(workflowFiles, []string{string(scopes.Repo)})) |
| 100 | + assert.Equal(t, []string{string(scopes.Repo)}, tool.ScopeAccess.Challenge(workflowFiles, []string{string(scopes.Workflow)})) |
| 101 | + assert.Equal(t, []string{string(scopes.Repo)}, tool.ScopeAccess.Challenge(workflowFiles, []string{string(scopes.PublicRepo), string(scopes.Workflow)})) |
| 102 | + assert.Empty(t, tool.ScopeAccess.Challenge(workflowFiles, []string{string(scopes.Repo), string(scopes.Workflow)})) |
| 103 | +} |
0 commit comments