8
8
"net/http/httptest"
9
9
"strings"
10
10
"testing"
11
- "time"
12
11
13
12
"github.com/honeybadger-io/honeybadger-mcp-server/internal/hbapi"
14
13
"github.com/mark3labs/mcp-go/mcp"
@@ -67,15 +66,23 @@ func TestHandleListProjects(t *testing.T) {
67
66
t .Fatal ("expected successful result, got error" )
68
67
}
69
68
70
- // Check that tokens are sanitized
69
+ // Verify the response can be unmarshaled as a projects list response
71
70
resultText := getResultText (result )
72
- if strings .Contains (resultText , "secret123" ) {
73
- t .Error ("Token should be sanitized from response" )
71
+ var response hbapi.ProjectsResponse
72
+ if err := json .Unmarshal ([]byte (resultText ), & response ); err != nil {
73
+ t .Errorf ("Response should be valid JSON projects list response: %v" , err )
74
+ }
75
+
76
+ if len (response .Results ) != 2 {
77
+ t .Errorf ("expected 2 projects, got %d" , len (response .Results ))
78
+ }
79
+
80
+ if response .Results [0 ].Name != "Project 1" {
81
+ t .Errorf ("expected first project name 'Project 1', got %s" , response .Results [0 ].Name )
74
82
}
75
83
76
- // Check that project data is still present
77
- if ! strings .Contains (resultText , "Project 1" ) {
78
- t .Error ("Project name should be present in response" )
84
+ if response .Results [1 ].Name != "Project 2" {
85
+ t .Errorf ("expected second project name 'Project 2', got %s" , response .Results [1 ].Name )
79
86
}
80
87
}
81
88
@@ -136,7 +143,6 @@ func TestHandleListProjects_WithAccountID(t *testing.T) {
136
143
WithBaseURL (server .URL ).
137
144
WithAuthToken ("test-token" )
138
145
139
- // Test with account_id parameter
140
146
req := mcp.CallToolRequest {
141
147
Params : mcp.CallToolParams {
142
148
Arguments : map [string ]interface {}{
@@ -154,15 +160,23 @@ func TestHandleListProjects_WithAccountID(t *testing.T) {
154
160
t .Fatal ("expected successful result, got error" )
155
161
}
156
162
157
- // Check that tokens are sanitized
163
+ // Verify the response can be unmarshaled as a projects list response
158
164
resultText := getResultText (result )
159
- if strings .Contains (resultText , "secret123" ) {
160
- t .Error ("Token should be sanitized from response" )
165
+ var response hbapi.ProjectsResponse
166
+ if err := json .Unmarshal ([]byte (resultText ), & response ); err != nil {
167
+ t .Errorf ("Response should be valid JSON projects list response: %v" , err )
168
+ }
169
+
170
+ if len (response .Results ) != 1 {
171
+ t .Errorf ("expected 1 project, got %d" , len (response .Results ))
172
+ }
173
+
174
+ if response .Results [0 ].Name != "Project 1" {
175
+ t .Errorf ("expected project name 'Project 1', got %s" , response .Results [0 ].Name )
161
176
}
162
177
163
- // Check that project data is still present
164
- if ! strings .Contains (resultText , "Project 1" ) {
165
- t .Error ("Project name should be present in response" )
178
+ if response .Results [0 ].ID != 1 {
179
+ t .Errorf ("expected project ID 1, got %d" , response .Results [0 ].ID )
166
180
}
167
181
}
168
182
@@ -203,14 +217,23 @@ func TestHandleGetProject(t *testing.T) {
203
217
t .Fatal ("expected successful result, got error" )
204
218
}
205
219
206
- // Check that token is sanitized
207
- if strings .Contains (getResultText (result ), "secret123" ) {
208
- t .Error ("Token should be sanitized from response" )
220
+ // Verify the response can be unmarshaled as a project
221
+ resultText := getResultText (result )
222
+ var project hbapi.Project
223
+ if err := json .Unmarshal ([]byte (resultText ), & project ); err != nil {
224
+ t .Errorf ("Response should be valid JSON project: %v" , err )
209
225
}
210
226
211
- // Check that project data is still present
212
- if ! strings .Contains (getResultText (result ), "Test Project" ) {
213
- t .Error ("Project name should be present in response" )
227
+ if project .ID != 123 {
228
+ t .Errorf ("expected project ID 123, got %d" , project .ID )
229
+ }
230
+
231
+ if project .Name != "Test Project" {
232
+ t .Errorf ("expected project name 'Test Project', got %s" , project .Name )
233
+ }
234
+
235
+ if ! project .Active {
236
+ t .Error ("expected project to be active" )
214
237
}
215
238
}
216
239
@@ -319,14 +342,27 @@ func TestHandleCreateProject(t *testing.T) {
319
342
t .Fatal ("expected successful result, got error" )
320
343
}
321
344
322
- // Check that API key is sanitized
323
- if strings .Contains (getResultText (result ), "secret789" ) {
324
- t .Error ("API key should be sanitized from response" )
345
+ // Verify the response can be unmarshaled as a project
346
+ resultText := getResultText (result )
347
+ var project hbapi.Project
348
+ if err := json .Unmarshal ([]byte (resultText ), & project ); err != nil {
349
+ t .Errorf ("Response should be valid JSON project: %v" , err )
350
+ }
351
+
352
+ if project .ID != 456 {
353
+ t .Errorf ("expected project ID 456, got %d" , project .ID )
354
+ }
355
+
356
+ if project .Name != "New Project" {
357
+ t .Errorf ("expected project name 'New Project', got %s" , project .Name )
358
+ }
359
+
360
+ if ! project .Active {
361
+ t .Error ("expected created project to be active" )
325
362
}
326
363
327
- // Check that project data is still present
328
- if ! strings .Contains (getResultText (result ), "New Project" ) {
329
- t .Error ("Project name should be present in response" )
364
+ if project .Owner .Email != "user@example.com" {
365
+ t .Errorf ("expected owner email 'user@example.com', got %s" , project .Owner .Email )
330
366
}
331
367
}
332
368
@@ -617,35 +653,6 @@ func TestHandleDeleteProject_Error(t *testing.T) {
617
653
}
618
654
}
619
655
620
- func TestSanitizeProject (t * testing.T ) {
621
- project := & hbapi.Project {
622
- ID : 123 ,
623
- Name : "Test Project" ,
624
- Active : true ,
625
- CreatedAt : time .Date (2024 , 1 , 1 , 0 , 0 , 0 , 0 , time .UTC ),
626
- Token : "secret123" ,
627
- Owner : hbapi.Account {ID : 1 , Email : "user@example.com" , Name : "User 1" },
628
- }
629
-
630
- sanitizeProject (project )
631
-
632
- // Check that token field is removed
633
- if project .Token != "" {
634
- t .Error ("token field should be cleared" )
635
- }
636
-
637
- // Check that non-sensitive fields remain
638
- if project .ID != 123 {
639
- t .Error ("project id should remain" )
640
- }
641
- if project .Name != "Test Project" {
642
- t .Error ("project name should remain" )
643
- }
644
- if project .Owner .Email != "user@example.com" {
645
- t .Error ("owner fields should remain" )
646
- }
647
- }
648
-
649
656
func TestHandleGetProjectReport (t * testing.T ) {
650
657
mockResponse := `[["RuntimeError", 8347], ["SocketError", 4651]]`
651
658
0 commit comments