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