|
| 1 | +package github |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "net/http" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/github/github-mcp-server/internal/toolsnaps" |
| 10 | + "github.com/github/github-mcp-server/pkg/translations" |
| 11 | + gogithub "github.com/google/go-github/v82/github" |
| 12 | + "github.com/google/jsonschema-go/jsonschema" |
| 13 | + "github.com/modelcontextprotocol/go-sdk/mcp" |
| 14 | + "github.com/stretchr/testify/assert" |
| 15 | + "github.com/stretchr/testify/require" |
| 16 | +) |
| 17 | + |
| 18 | +func Test_CheckOrgMembership(t *testing.T) { |
| 19 | + serverTool := CheckOrgMembership(translations.NullTranslationHelper) |
| 20 | + tool := serverTool.Tool |
| 21 | + |
| 22 | + require.NoError(t, toolsnaps.Test(tool.Name, tool)) |
| 23 | + |
| 24 | + assert.Equal(t, "check_org_membership", tool.Name) |
| 25 | + assert.NotEmpty(t, tool.Description) |
| 26 | + |
| 27 | + schema, ok := tool.InputSchema.(*jsonschema.Schema) |
| 28 | + require.True(t, ok, "InputSchema should be *jsonschema.Schema") |
| 29 | + assert.Contains(t, schema.Properties, "org") |
| 30 | + assert.Contains(t, schema.Properties, "username") |
| 31 | + assert.ElementsMatch(t, schema.Required, []string{"org", "username"}) |
| 32 | + assert.True(t, serverTool.IsReadOnly()) |
| 33 | + assert.ElementsMatch(t, []string{"read:org"}, serverTool.RequiredScopes) |
| 34 | +} |
| 35 | + |
| 36 | +func Test_CheckOrgMembership_PublicMember(t *testing.T) { |
| 37 | + output := runCheckOrgMembership(t, MockHTTPClientWithHandlers(map[string]http.HandlerFunc{ |
| 38 | + GetOrgsMembersByOrgByUsername: expectPath(t, "/orgs/canonical/members/octocat").andThen(mockStatus(http.StatusNoContent)), |
| 39 | + GetOrgsPublicMembersByOrgByUsername: expectPath(t, "/orgs/canonical/public_members/octocat").andThen(mockStatus(http.StatusNoContent)), |
| 40 | + }), false) |
| 41 | + |
| 42 | + assert.Equal(t, CheckOrgMembershipOutput{ |
| 43 | + Org: "canonical", |
| 44 | + Username: "octocat", |
| 45 | + IsMember: true, |
| 46 | + Visibility: "public", |
| 47 | + }, output) |
| 48 | +} |
| 49 | + |
| 50 | +func Test_CheckOrgMembership_PrivateMember(t *testing.T) { |
| 51 | + output := runCheckOrgMembership(t, MockHTTPClientWithHandlers(map[string]http.HandlerFunc{ |
| 52 | + GetOrgsMembersByOrgByUsername: expectPath(t, "/orgs/canonical/members/octocat").andThen(mockStatus(http.StatusNoContent)), |
| 53 | + GetOrgsPublicMembersByOrgByUsername: expectPath(t, "/orgs/canonical/public_members/octocat").andThen(mockStatus(http.StatusNotFound)), |
| 54 | + }), false) |
| 55 | + |
| 56 | + assert.Equal(t, CheckOrgMembershipOutput{ |
| 57 | + Org: "canonical", |
| 58 | + Username: "octocat", |
| 59 | + IsMember: true, |
| 60 | + Visibility: "private", |
| 61 | + }, output) |
| 62 | +} |
| 63 | + |
| 64 | +func Test_CheckOrgMembership_NotAMember(t *testing.T) { |
| 65 | + output := runCheckOrgMembership(t, MockHTTPClientWithHandlers(map[string]http.HandlerFunc{ |
| 66 | + GetOrgsMembersByOrgByUsername: expectPath(t, "/orgs/canonical/members/octocat").andThen(mockStatus(http.StatusNotFound)), |
| 67 | + GetOrgsPublicMembersByOrgByUsername: expectPath(t, "/orgs/canonical/public_members/octocat").andThen(mockStatus(http.StatusNotFound)), |
| 68 | + GetOrgsByOrg: expectPath(t, "/orgs/canonical").andThen(mockResponse(t, http.StatusOK, &gogithub.Organization{Login: gogithub.Ptr("canonical")})), |
| 69 | + }), false) |
| 70 | + |
| 71 | + assert.Equal(t, "canonical", output.Org) |
| 72 | + assert.Equal(t, "octocat", output.Username) |
| 73 | + assert.False(t, output.IsMember) |
| 74 | + assert.Equal(t, "none", output.Visibility) |
| 75 | + assert.Equal(t, "result reflects caller visibility; private members of orgs you can't see appear as non-members.", output.Note) |
| 76 | +} |
| 77 | + |
| 78 | +func Test_CheckOrgMembership_OrgNotFound(t *testing.T) { |
| 79 | + text := runCheckOrgMembershipError(t, MockHTTPClientWithHandlers(map[string]http.HandlerFunc{ |
| 80 | + GetOrgsMembersByOrgByUsername: expectPath(t, "/orgs/missing-org/members/octocat").andThen(mockStatus(http.StatusNotFound)), |
| 81 | + GetOrgsPublicMembersByOrgByUsername: expectPath(t, "/orgs/missing-org/public_members/octocat").andThen(mockStatus(http.StatusNotFound)), |
| 82 | + GetOrgsByOrg: expectPath(t, "/orgs/missing-org").andThen(mockResponse(t, http.StatusNotFound, map[string]string{"message": "Not Found"})), |
| 83 | + }), map[string]any{"org": "missing-org", "username": "octocat"}) |
| 84 | + |
| 85 | + assert.Contains(t, text, "failed to get organization") |
| 86 | + assert.Contains(t, text, "404") |
| 87 | +} |
| 88 | + |
| 89 | +func Test_CheckOrgMembership_ScopeError(t *testing.T) { |
| 90 | + for _, status := range []int{http.StatusUnauthorized, http.StatusForbidden} { |
| 91 | + t.Run(http.StatusText(status), func(t *testing.T) { |
| 92 | + text := runCheckOrgMembershipError(t, MockHTTPClientWithHandlers(map[string]http.HandlerFunc{ |
| 93 | + GetOrgsMembersByOrgByUsername: expectPath(t, "/orgs/canonical/members/octocat").andThen( |
| 94 | + mockResponse(t, status, map[string]string{"message": http.StatusText(status)}), |
| 95 | + ), |
| 96 | + }), map[string]any{"org": "canonical", "username": "octocat"}) |
| 97 | + |
| 98 | + assert.Contains(t, text, "failed to check organization membership") |
| 99 | + assert.Contains(t, text, http.StatusText(status)) |
| 100 | + }) |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +func runCheckOrgMembership(t *testing.T, httpClient *http.Client, expectError bool) CheckOrgMembershipOutput { |
| 105 | + t.Helper() |
| 106 | + |
| 107 | + result := callCheckOrgMembership(t, httpClient, map[string]any{"org": "canonical", "username": "octocat"}) |
| 108 | + require.Equal(t, expectError, result.IsError) |
| 109 | + |
| 110 | + textContent := getTextResult(t, result) |
| 111 | + var output CheckOrgMembershipOutput |
| 112 | + require.NoError(t, json.Unmarshal([]byte(textContent.Text), &output)) |
| 113 | + return output |
| 114 | +} |
| 115 | + |
| 116 | +func runCheckOrgMembershipError(t *testing.T, httpClient *http.Client, args map[string]any) string { |
| 117 | + t.Helper() |
| 118 | + |
| 119 | + result := callCheckOrgMembership(t, httpClient, args) |
| 120 | + textContent := getErrorResult(t, result) |
| 121 | + return textContent.Text |
| 122 | +} |
| 123 | + |
| 124 | +func callCheckOrgMembership(t *testing.T, httpClient *http.Client, args map[string]any) *mcp.CallToolResult { |
| 125 | + t.Helper() |
| 126 | + |
| 127 | + client := gogithub.NewClient(httpClient) |
| 128 | + deps := BaseDeps{Client: client} |
| 129 | + serverTool := CheckOrgMembership(translations.NullTranslationHelper) |
| 130 | + handler := serverTool.Handler(deps) |
| 131 | + request := createMCPRequest(args) |
| 132 | + |
| 133 | + result, err := handler(ContextWithDeps(context.Background(), deps), &request) |
| 134 | + require.NoError(t, err) |
| 135 | + return result |
| 136 | +} |
| 137 | + |
| 138 | +func mockStatus(status int) http.HandlerFunc { |
| 139 | + return func(w http.ResponseWriter, _ *http.Request) { |
| 140 | + w.WriteHeader(status) |
| 141 | + } |
| 142 | +} |
0 commit comments