|
| 1 | +package github |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "net/http" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/google/go-github/v82/github" |
| 11 | + "github.com/google/jsonschema-go/jsonschema" |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | + "github.com/stretchr/testify/require" |
| 14 | + |
| 15 | + "github.com/github/github-mcp-server/internal/toolsnaps" |
| 16 | + "github.com/github/github-mcp-server/pkg/translations" |
| 17 | +) |
| 18 | + |
| 19 | +func Test_GetUser(t *testing.T) { |
| 20 | + // Verify tool definition once |
| 21 | + serverTool := GetUser(translations.NullTranslationHelper) |
| 22 | + tool := serverTool.Tool |
| 23 | + require.NoError(t, toolsnaps.Test(tool.Name, tool)) |
| 24 | + |
| 25 | + schema, ok := tool.InputSchema.(*jsonschema.Schema) |
| 26 | + require.True(t, ok, "InputSchema should be *jsonschema.Schema") |
| 27 | + |
| 28 | + assert.Equal(t, "get_user", tool.Name) |
| 29 | + assert.NotEmpty(t, tool.Description) |
| 30 | + assert.Contains(t, schema.Properties, "username") |
| 31 | + assert.ElementsMatch(t, schema.Required, []string{"username"}) |
| 32 | + |
| 33 | + mockUser := &github.User{ |
| 34 | + Login: github.Ptr("google?"), |
| 35 | + ID: github.Ptr(int64(1234)), |
| 36 | + HTMLURL: github.Ptr("https://github.com/non-existent-john-doe"), |
| 37 | + AvatarURL: github.Ptr("https://github.com/avatar-url/avatar.png"), |
| 38 | + Name: github.Ptr("John Doe"), |
| 39 | + Company: github.Ptr("Gophers"), |
| 40 | + Blog: github.Ptr("https://blog.golang.org"), |
| 41 | + Location: github.Ptr("Europe/Berlin"), |
| 42 | + Email: github.Ptr("non-existent-john-doe@gmail.com"), |
| 43 | + Hireable: github.Ptr(false), |
| 44 | + Bio: github.Ptr("Just a test user"), |
| 45 | + TwitterUsername: github.Ptr("non_existent_john_doe"), |
| 46 | + PublicRepos: github.Ptr(42), |
| 47 | + PublicGists: github.Ptr(11), |
| 48 | + Followers: github.Ptr(10), |
| 49 | + Following: github.Ptr(50), |
| 50 | + CreatedAt: &github.Timestamp{Time: time.Now().Add(-365 * 24 * time.Hour)}, |
| 51 | + UpdatedAt: &github.Timestamp{Time: time.Now()}, |
| 52 | + PrivateGists: github.Ptr(11), |
| 53 | + TotalPrivateRepos: github.Ptr(int64(5)), |
| 54 | + OwnedPrivateRepos: github.Ptr(int64(3)), |
| 55 | + } |
| 56 | + |
| 57 | + tests := []struct { |
| 58 | + name string |
| 59 | + mockedClient *http.Client |
| 60 | + requestArgs map[string]any |
| 61 | + expectError bool |
| 62 | + expectedUser *github.User |
| 63 | + expectedErrMsg string |
| 64 | + }{ |
| 65 | + { |
| 66 | + name: "successful user retrieval by username", |
| 67 | + mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{ |
| 68 | + GetUserByUsername: mockResponse(t, http.StatusOK, mockUser), |
| 69 | + }), |
| 70 | + requestArgs: map[string]any{ |
| 71 | + "username": "non-existent-john-doe", |
| 72 | + }, |
| 73 | + expectError: false, |
| 74 | + expectedUser: mockUser, |
| 75 | + }, |
| 76 | + { |
| 77 | + name: "user not found", |
| 78 | + mockedClient: MockHTTPClientWithHandler(mockResponse(t, http.StatusNotFound, `{"message":"user not found"}`)), |
| 79 | + requestArgs: map[string]any{ |
| 80 | + "username": "other-non-existent-john-doe", |
| 81 | + }, |
| 82 | + expectError: true, |
| 83 | + expectedErrMsg: "failed to get user", |
| 84 | + }, |
| 85 | + { |
| 86 | + name: "error getting user", |
| 87 | + mockedClient: MockHTTPClientWithHandler(badRequestHandler("some other error")), |
| 88 | + requestArgs: map[string]any{ |
| 89 | + "username": "non-existent-john-doe", |
| 90 | + }, |
| 91 | + expectError: true, |
| 92 | + expectedErrMsg: "failed to get user", |
| 93 | + }, |
| 94 | + { |
| 95 | + name: "missing username parameter", |
| 96 | + mockedClient: MockHTTPClientWithHandler(badRequestHandler("missing username parameter")), |
| 97 | + requestArgs: map[string]any{}, |
| 98 | + expectError: true, |
| 99 | + expectedErrMsg: "missing required parameter", |
| 100 | + }, |
| 101 | + } |
| 102 | + |
| 103 | + for _, tc := range tests { |
| 104 | + t.Run(tc.name, func(t *testing.T) { |
| 105 | + // Setup client with mock |
| 106 | + client := github.NewClient(tc.mockedClient) |
| 107 | + deps := BaseDeps{ |
| 108 | + Client: client, |
| 109 | + } |
| 110 | + handler := serverTool.Handler(deps) |
| 111 | + |
| 112 | + // Create call request |
| 113 | + request := createMCPRequest(tc.requestArgs) |
| 114 | + |
| 115 | + // Call handler |
| 116 | + result, err := handler(ContextWithDeps(context.Background(), deps), &request) |
| 117 | + |
| 118 | + // Verify results |
| 119 | + if tc.expectError { |
| 120 | + require.NoError(t, err) |
| 121 | + require.True(t, result.IsError) |
| 122 | + errorContent := getErrorResult(t, result) |
| 123 | + assert.Contains(t, errorContent.Text, tc.expectedErrMsg) |
| 124 | + return |
| 125 | + } |
| 126 | + |
| 127 | + require.NoError(t, err) |
| 128 | + require.False(t, result.IsError) |
| 129 | + |
| 130 | + // Parse the result and get the text content if no error |
| 131 | + textContent := getTextResult(t, result) |
| 132 | + |
| 133 | + // Parse and verify the result |
| 134 | + var returnedUser MinimalUser |
| 135 | + err = json.Unmarshal([]byte(textContent.Text), &returnedUser) |
| 136 | + require.NoError(t, err) |
| 137 | + |
| 138 | + assert.Equal(t, *tc.expectedUser.Login, returnedUser.Login) |
| 139 | + assert.Equal(t, *tc.expectedUser.ID, returnedUser.ID) |
| 140 | + assert.Equal(t, *tc.expectedUser.HTMLURL, returnedUser.ProfileURL) |
| 141 | + assert.Equal(t, *tc.expectedUser.AvatarURL, returnedUser.AvatarURL) |
| 142 | + // Details |
| 143 | + assert.Equal(t, *tc.expectedUser.Name, returnedUser.Details.Name) |
| 144 | + assert.Equal(t, *tc.expectedUser.Company, returnedUser.Details.Company) |
| 145 | + assert.Equal(t, *tc.expectedUser.Blog, returnedUser.Details.Blog) |
| 146 | + assert.Equal(t, *tc.expectedUser.Location, returnedUser.Details.Location) |
| 147 | + assert.Equal(t, *tc.expectedUser.Email, returnedUser.Details.Email) |
| 148 | + assert.Equal(t, *tc.expectedUser.Hireable, returnedUser.Details.Hireable) |
| 149 | + assert.Equal(t, *tc.expectedUser.Bio, returnedUser.Details.Bio) |
| 150 | + assert.Equal(t, *tc.expectedUser.TwitterUsername, returnedUser.Details.TwitterUsername) |
| 151 | + assert.Equal(t, *tc.expectedUser.PublicRepos, returnedUser.Details.PublicRepos) |
| 152 | + assert.Equal(t, *tc.expectedUser.PublicGists, returnedUser.Details.PublicGists) |
| 153 | + assert.Equal(t, *tc.expectedUser.Followers, returnedUser.Details.Followers) |
| 154 | + assert.Equal(t, *tc.expectedUser.Following, returnedUser.Details.Following) |
| 155 | + assert.Equal(t, *tc.expectedUser.PrivateGists, returnedUser.Details.PrivateGists) |
| 156 | + assert.Equal(t, *tc.expectedUser.TotalPrivateRepos, returnedUser.Details.TotalPrivateRepos) |
| 157 | + assert.Equal(t, *tc.expectedUser.OwnedPrivateRepos, returnedUser.Details.OwnedPrivateRepos) |
| 158 | + }) |
| 159 | + } |
| 160 | +} |
0 commit comments