refactor(tests): remove redundant TestServersDetailHandler integration tests

This commit is contained in:
Avinash Sridhar 2025-05-27 13:24:57 -04:00 • committed by Toby Padilla
commit 5736edd07a

View file

@ -3,7 +3,6 @@ package v0
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"net/http/httptest"
"testing"
@ -263,179 +262,6 @@ func TestServersHandler(t *testing.T) {
}
}
func TestServersDetailHandler(t *testing.T) {
validServerID := uuid.New().String()
testCases := []struct {
name string
method string
serverID string
setupMocks func(*MockRegistryService)
expectedStatus int
expectedServerDetail *model.ServerDetail
expectedError string
}{
{
name: "successful get server detail",
method: http.MethodGet,
serverID: validServerID,
setupMocks: func(registry *MockRegistryService) {
serverDetail := &model.ServerDetail{
Server: model.Server{
ID: validServerID,
Name: "test-server",
Description: "A test server",
Repository: model.Repository{
URL: "https://github.com/example/test-server",
Source: "github",
ID: "example/test-server",
},
VersionDetail: model.VersionDetail{
Version: "1.0.0",
ReleaseDate: "2025-05-25T00:00:00Z",
IsLatest: true,
},
},
Packages: []model.Package{
{
RegistryName: "test-package",
Name: "example-package",
Version: "1.0.0",
},
},
Remotes: []model.Remote{
{
TransportType: "http",
URL: "https://example.com/mcp",
},
},
}
registry.On("GetByID", validServerID).Return(serverDetail, nil)
},
expectedStatus: http.StatusOK,
expectedServerDetail: &model.ServerDetail{
Server: model.Server{
ID: validServerID,
Name: "test-server",
Description: "A test server",
Repository: model.Repository{
URL: "https://github.com/example/test-server",
Source: "github",
ID: "example/test-server",
},
VersionDetail: model.VersionDetail{
Version: "1.0.0",
ReleaseDate: "2025-05-25T00:00:00Z",
IsLatest: true,
},
},
Packages: []model.Package{
{
RegistryName: "test-package",
Name: "example-package",
Version: "1.0.0",
},
},
Remotes: []model.Remote{
{
TransportType: "http",
URL: "https://example.com/mcp",
},
},
},
},
{
name: "server not found",
method: http.MethodGet,
serverID: uuid.New().String(),
setupMocks: func(registry *MockRegistryService) {
registry.On("GetByID", mock.AnythingOfType("string")).Return((*model.ServerDetail)(nil), errors.New("record not found"))
},
expectedStatus: http.StatusNotFound,
expectedError: "Server not found",
},
{
name: "invalid server ID format",
method: http.MethodGet,
serverID: "invalid-uuid",
setupMocks: func(registry *MockRegistryService) {
// Mock won't be called due to early validation
},
expectedStatus: http.StatusBadRequest,
expectedError: "Invalid server ID format",
},
{
name: "registry service error",
method: http.MethodGet,
serverID: validServerID,
setupMocks: func(registry *MockRegistryService) {
registry.On("GetByID", validServerID).Return((*model.ServerDetail)(nil), errors.New("database connection error"))
},
expectedStatus: http.StatusInternalServerError,
expectedError: "Error retrieving server details",
},
{
name: "method not allowed",
method: http.MethodPost,
serverID: validServerID,
setupMocks: func(registry *MockRegistryService) {},
expectedStatus: http.StatusMethodNotAllowed,
expectedError: "Method not allowed",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
// Create mock registry service
mockRegistry := new(MockRegistryService)
tc.setupMocks(mockRegistry)
// Create handler
handler := ServersDetailHandler(mockRegistry)
// Create request with path value
url := fmt.Sprintf("/v0/servers/%s", tc.serverID)
req, err := http.NewRequest(tc.method, url, nil)
if err != nil {
t.Fatal(err)
}
// Set the path value for the server ID (simulating mux behavior)
req.SetPathValue("id", tc.serverID)
// Create response recorder
rr := httptest.NewRecorder()
// Call the handler
handler.ServeHTTP(rr, req)
// Check status code
assert.Equal(t, tc.expectedStatus, rr.Code)
if tc.expectedStatus == http.StatusOK {
// Check content type
assert.Equal(t, "application/json", rr.Header().Get("Content-Type"))
// Parse response body
var resp model.ServerDetail
err = json.NewDecoder(rr.Body).Decode(&resp)
assert.NoError(t, err)
// Check the response data
assert.Equal(t, *tc.expectedServerDetail, resp)
} else {
// Check error message for non-200 responses
if tc.expectedError != "" {
assert.Contains(t, rr.Body.String(), tc.expectedError)
}
}
// Verify mock expectations
mockRegistry.AssertExpectations(t)
})
}
}
// TestServersHandlerIntegration tests the servers list handler with actual HTTP requests
func TestServersHandlerIntegration(t *testing.T) {
// Create mock registry service