registry/internal/api/handlers/v0/edit_test.go
Radoslav Dimitrov ddcc586842
Remove the in-memory database (#537)
<!-- Provide a brief summary of your changes -->

## Motivation and Context
<!-- Why is this change needed? What problem does it solve? -->
The follow PR removes the in-memory database option from the registry.

We had a chat about this with @domdomegg and although it is supposed to
be convenient, it isn't that useful in reality. We want to test and
develop the registry in a setup that is closer to the environment it is
supposed to be run. One of the main reasons are:
* We don't use it anywhere else besides unit testing and potentially
local development. Instead we should use postgresql for reliable results
(in-memory was actually giving false positive results)
* We have to keep maintaining it and make sure it mimics the behaviour
we have for postgresql which is not yielding a lot of value

## How Has This Been Tested?
<!-- Have you tested this in a real application? Which scenarios were
tested? -->
Yes, I've updated the test unit make target to run in the postgresql
environment and now we can see the race condition issue happening.

## Breaking Changes
<!-- Will users need to update their code or configurations? -->
No

## Types of changes
<!-- What types of changes does your code introduce? Put an `x` in all
the boxes that apply: -->
- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)
- [ ] Documentation update

## Checklist
<!-- Go over all the following points, and put an `x` in all the boxes
that apply. -->
- [ ] I have read the [MCP
Documentation](https://modelcontextprotocol.io)
- [ ] My code follows the repository's style guidelines
- [ ] New and existing tests pass locally
- [ ] I have added appropriate error handling
- [ ] I have added or updated documentation as needed

## Additional context
<!-- Add any other context, implementation notes, or design decisions
-->

---------

Signed-off-by: Radoslav Dimitrov <radoslav@stacklok.com>
2025-09-23 16:40:20 +01:00

333 lines
10 KiB
Go

package v0_test
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/danielgtaylor/huma/v2"
"github.com/danielgtaylor/huma/v2/adapters/humago"
"github.com/stretchr/testify/assert"
v0 "github.com/modelcontextprotocol/registry/internal/api/handlers/v0"
"github.com/modelcontextprotocol/registry/internal/auth"
"github.com/modelcontextprotocol/registry/internal/config"
"github.com/modelcontextprotocol/registry/internal/database"
"github.com/modelcontextprotocol/registry/internal/service"
apiv0 "github.com/modelcontextprotocol/registry/pkg/api/v0"
"github.com/modelcontextprotocol/registry/pkg/model"
)
func TestEditServerEndpoint(t *testing.T) {
// Create registry service and insert a common test server
registryService := service.NewRegistryService(database.NewTestDB(t), config.NewConfig())
// Publish a test server that will be used across test cases
testServer := apiv0.ServerJSON{
Name: "io.github.domdomegg/test-server",
Description: "Original test server",
Status: model.StatusActive,
Repository: model.Repository{
URL: "https://github.com/domdomegg/test-server",
Source: "github",
ID: "domdomegg/test-server",
},
Version: "1.0.0",
}
published, err := registryService.Publish(testServer)
assert.NoError(t, err)
assert.NotNil(t, published)
assert.NotNil(t, published.Meta)
assert.NotNil(t, published.Meta.Official)
testServerID := published.Meta.Official.ServerID
// Publish a second server for permission testing
otherServer := apiv0.ServerJSON{
Name: "io.github.other/test-server",
Description: "Other test server",
Status: model.StatusActive,
Repository: model.Repository{
URL: "https://github.com/other/test-server",
Source: "github",
ID: "other/test-server",
},
Version: "1.0.0",
}
otherPublished, err := registryService.Publish(otherServer)
assert.NoError(t, err)
assert.NotNil(t, otherPublished)
assert.NotNil(t, otherPublished.Meta)
assert.NotNil(t, otherPublished.Meta.Official)
otherServerID := otherPublished.Meta.Official.ServerID
// Publish a deleted server for undelete testing
deletedServer := apiv0.ServerJSON{
Name: "io.github.domdomegg/deleted-server",
Description: "Deleted test server",
Status: model.StatusDeleted,
Repository: model.Repository{
URL: "https://github.com/domdomegg/deleted-server",
Source: "github",
ID: "domdomegg/deleted-server",
},
Version: "1.0.0",
}
deletedPublished, err := registryService.Publish(deletedServer)
assert.NoError(t, err)
assert.NotNil(t, deletedPublished)
assert.NotNil(t, deletedPublished.Meta)
assert.NotNil(t, deletedPublished.Meta.Official)
deletedServerID := deletedPublished.Meta.Official.ServerID
testCases := []struct {
name string
authHeader string
requestBody interface{}
serverID string
version string
expectedStatus int
expectedError string
}{
{
name: "successful edit with valid token and permissions",
authHeader: func() string {
cfg := &config.Config{JWTPrivateKey: "bb2c6b424005acd5df47a9e2c87f446def86dd740c888ea3efb825b23f7ef47c"}
token, _ := generateTestJWTToken(cfg, auth.JWTClaims{
AuthMethod: auth.MethodGitHubAT,
AuthMethodSubject: "domdomegg",
Permissions: []auth.Permission{
{Action: auth.PermissionActionEdit, ResourcePattern: "io.github.domdomegg/*"},
},
})
return "Bearer " + token
}(),
requestBody: apiv0.ServerJSON{
Name: "io.github.domdomegg/test-server",
Description: "Updated test server",
Status: model.StatusDeprecated,
Repository: model.Repository{
URL: "https://github.com/domdomegg/test-server",
Source: "github",
ID: "domdomegg/test-server",
},
Version: "1.0.0",
},
serverID: testServerID,
version: "1.0.0",
expectedStatus: http.StatusOK,
},
{
name: "missing authorization header",
authHeader: "",
requestBody: apiv0.ServerJSON{},
serverID: testServerID,
version: "1.0.0",
expectedStatus: 422,
expectedError: "required header parameter is missing",
},
{
name: "invalid authorization header format",
authHeader: "InvalidFormat token123",
requestBody: apiv0.ServerJSON{
Name: "io.github.domdomegg/test-server",
Description: "Test server",
Version: "1.0.0",
},
serverID: testServerID,
version: "1.0.0",
expectedStatus: http.StatusUnauthorized,
expectedError: "Unauthorized",
},
{
name: "invalid token",
authHeader: "Bearer invalid-token",
requestBody: apiv0.ServerJSON{
Name: "io.github.domdomegg/test-server",
Description: "Test server",
Version: "1.0.0",
},
serverID: testServerID,
version: "1.0.0",
expectedStatus: http.StatusUnauthorized,
expectedError: "Unauthorized",
},
{
name: "permission denied - no edit permissions",
authHeader: func() string {
cfg := &config.Config{JWTPrivateKey: "bb2c6b424005acd5df47a9e2c87f446def86dd740c888ea3efb825b23f7ef47c"}
token, _ := generateTestJWTToken(cfg, auth.JWTClaims{
AuthMethod: auth.MethodGitHubAT,
AuthMethodSubject: "domdomegg",
Permissions: []auth.Permission{
{Action: auth.PermissionActionPublish, ResourcePattern: "io.github.domdomegg/*"},
},
})
return "Bearer " + token
}(),
requestBody: apiv0.ServerJSON{
Name: "io.github.domdomegg/test-server",
Description: "Updated test server",
Version: "1.0.0",
},
serverID: testServerID,
version: "1.0.0",
expectedStatus: http.StatusForbidden,
expectedError: "Forbidden",
},
{
name: "permission denied - wrong resource",
authHeader: func() string {
cfg := &config.Config{JWTPrivateKey: "bb2c6b424005acd5df47a9e2c87f446def86dd740c888ea3efb825b23f7ef47c"}
token, _ := generateTestJWTToken(cfg, auth.JWTClaims{
AuthMethod: auth.MethodGitHubAT,
AuthMethodSubject: "domdomegg",
Permissions: []auth.Permission{
{Action: auth.PermissionActionEdit, ResourcePattern: "io.github.domdomegg/*"},
},
})
return "Bearer " + token
}(),
requestBody: apiv0.ServerJSON{
Name: "io.github.other/test-server",
Description: "Updated test server",
Version: "1.0.0",
},
serverID: otherServerID,
version: "1.0.0",
expectedStatus: http.StatusForbidden,
expectedError: "Forbidden",
},
{
name: "server not found",
authHeader: func() string {
cfg := &config.Config{JWTPrivateKey: "bb2c6b424005acd5df47a9e2c87f446def86dd740c888ea3efb825b23f7ef47c"}
token, _ := generateTestJWTToken(cfg, auth.JWTClaims{
AuthMethod: auth.MethodGitHubAT,
AuthMethodSubject: "domdomegg",
Permissions: []auth.Permission{
{Action: auth.PermissionActionEdit, ResourcePattern: "io.github.domdomegg/*"},
},
})
return "Bearer " + token
}(),
requestBody: apiv0.ServerJSON{
Name: "io.github.domdomegg/nonexistent-server",
Description: "Updated test server",
Version: "1.0.0",
},
serverID: "550e8400-e29b-41d4-a716-446655440999", // Non-existent ID
version: "1.0.0",
expectedStatus: http.StatusNotFound,
expectedError: "Not Found",
},
{
name: "validation error - invalid server name",
authHeader: func() string {
cfg := &config.Config{JWTPrivateKey: "bb2c6b424005acd5df47a9e2c87f446def86dd740c888ea3efb825b23f7ef47c"}
token, _ := generateTestJWTToken(cfg, auth.JWTClaims{
AuthMethod: auth.MethodGitHubAT,
AuthMethodSubject: "domdomegg",
Permissions: []auth.Permission{
{Action: auth.PermissionActionEdit, ResourcePattern: "*"},
},
})
return "Bearer " + token
}(),
requestBody: apiv0.ServerJSON{
Name: "invalid-name-format", // Missing namespace/name format
Description: "Test server",
Version: "1.0.0",
},
serverID: testServerID,
version: "1.0.0",
expectedStatus: http.StatusBadRequest,
expectedError: "Bad Request",
},
{
name: "cannot undelete server",
authHeader: func() string {
cfg := &config.Config{JWTPrivateKey: "bb2c6b424005acd5df47a9e2c87f446def86dd740c888ea3efb825b23f7ef47c"}
token, _ := generateTestJWTToken(cfg, auth.JWTClaims{
AuthMethod: auth.MethodGitHubAT,
AuthMethodSubject: "domdomegg",
Permissions: []auth.Permission{
{Action: auth.PermissionActionEdit, ResourcePattern: "*"},
},
})
return "Bearer " + token
}(),
requestBody: apiv0.ServerJSON{
Name: "io.github.domdomegg/deleted-server",
Description: "Trying to undelete server",
Status: model.StatusActive,
Repository: model.Repository{
URL: "https://github.com/domdomegg/deleted-server",
Source: "github",
ID: "domdomegg/deleted-server",
},
Version: "1.0.1",
},
serverID: deletedServerID,
version: "1.0.0",
expectedStatus: http.StatusBadRequest,
expectedError: "Cannot change status of deleted server",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
// Create Huma API
mux := http.NewServeMux()
humaConfig := huma.DefaultConfig("Test API", "1.0.0")
api := humago.New(mux, humaConfig)
// Register edit endpoints
cfg := &config.Config{
JWTPrivateKey: "bb2c6b424005acd5df47a9e2c87f446def86dd740c888ea3efb825b23f7ef47c",
}
v0.RegisterEditEndpoints(api, registryService, cfg)
// Create request body
var requestBody []byte
var err error
if str, ok := tc.requestBody.(string); ok {
requestBody = []byte(str)
} else {
requestBody, err = json.Marshal(tc.requestBody)
assert.NoError(t, err)
}
// Create request
url := "/v0/servers/" + tc.serverID
if tc.version != "" {
url += "?version=" + tc.version
}
req := httptest.NewRequest(http.MethodPut, url, bytes.NewReader(requestBody))
req.Header.Set("Content-Type", "application/json")
if tc.authHeader != "" {
req.Header.Set("Authorization", tc.authHeader)
}
// Create response recorder
w := httptest.NewRecorder()
// Call the endpoint
mux.ServeHTTP(w, req)
// Check status code
assert.Equal(t, tc.expectedStatus, w.Code)
// Check error message if expected
if tc.expectedError != "" {
assert.Contains(t, w.Body.String(), tc.expectedError)
}
// No mock assertions needed with real service
})
}
}