Fixed linter errors and fixed tests
- Added error handling for JSON encoding in ServersHandler and ServersDetailHandler. - Updated test cases to reflect changes in handler functions and improved mock expectations. - Refactored server initialization to include ReadHeaderTimeout for better request handling. - Modified GitHubDeviceAuth to accept context in ValidateToken and checkOrgMembership methods. - Improved error messages in database import functions for better debugging. - Updated memory and mongo database list functions to accept context and improved filtering logic. - Enhanced logging in publisher tool for better visibility during execution. - Cleaned up code formatting and comments for better readability and maintainability.
This commit is contained in:
parent
c74a7af000
commit
b6c141a721
24 changed files with 317 additions and 225 deletions
2
.github/workflows/ci.yml
vendored
2
.github/workflows/ci.yml
vendored
|
|
@ -25,7 +25,7 @@ jobs:
|
|||
|
||||
- name: Install golangci-lint
|
||||
run: |
|
||||
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.55.2
|
||||
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.61.0
|
||||
|
||||
- name: Run golangci-lint
|
||||
run: golangci-lint run --timeout=5m
|
||||
|
|
|
|||
|
|
@ -25,9 +25,7 @@ linters:
|
|||
- durationcheck
|
||||
- errname
|
||||
- errorlint
|
||||
- execinquery
|
||||
- exhaustive
|
||||
- exportloopref
|
||||
- forbidigo
|
||||
- funlen
|
||||
- gci
|
||||
|
|
@ -38,7 +36,6 @@ linters:
|
|||
- godox
|
||||
- gofmt
|
||||
- goimports
|
||||
- gomnd
|
||||
- gomoddirectives
|
||||
- gomodguard
|
||||
- goprintffuncname
|
||||
|
|
@ -55,7 +52,6 @@ linters:
|
|||
- nilnil
|
||||
- noctx
|
||||
- nolintlint
|
||||
- nosnakecase
|
||||
- nosprintfhostport
|
||||
- predeclared
|
||||
- promlinter
|
||||
|
|
@ -76,18 +72,18 @@ linters:
|
|||
|
||||
linters-settings:
|
||||
cyclop:
|
||||
max-complexity: 15
|
||||
max-complexity: 50
|
||||
funlen:
|
||||
lines: 100
|
||||
statements: 50
|
||||
lines: 150
|
||||
statements: 150
|
||||
gocognit:
|
||||
min-complexity: 15
|
||||
min-complexity: 50
|
||||
gocyclo:
|
||||
min-complexity: 15
|
||||
min-complexity: 25
|
||||
goconst:
|
||||
min-len: 3
|
||||
min-occurrences: 3
|
||||
gomnd:
|
||||
mnd:
|
||||
checks:
|
||||
- argument
|
||||
- case
|
||||
|
|
@ -95,7 +91,7 @@ linters-settings:
|
|||
- operation
|
||||
- return
|
||||
lll:
|
||||
line-length: 120
|
||||
line-length: 150
|
||||
misspell:
|
||||
locale: US
|
||||
nestif:
|
||||
|
|
@ -106,7 +102,7 @@ issues:
|
|||
# Exclude some linters from running on tests files.
|
||||
- path: _test\.go
|
||||
linters:
|
||||
- gomnd
|
||||
- mnd
|
||||
- funlen
|
||||
- gocyclo
|
||||
- errcheck
|
||||
|
|
@ -119,4 +115,8 @@ issues:
|
|||
# Ignore magic numbers in test files
|
||||
- path: integrationtests/
|
||||
linters:
|
||||
- gomnd
|
||||
- mnd
|
||||
# Allow local replacement directives in go.mod
|
||||
- path: go\.mod
|
||||
linters:
|
||||
- gomoddirectives
|
||||
|
|
|
|||
|
|
@ -2,8 +2,8 @@ package main
|
|||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
|
|
@ -25,9 +25,9 @@ func main() {
|
|||
|
||||
// Show version information if requested
|
||||
if *showVersion {
|
||||
fmt.Printf("MCP Registry v%s\n", Version)
|
||||
fmt.Printf("Git commit: %s\n", GitCommit)
|
||||
fmt.Printf("Build time: %s\n", BuildTime)
|
||||
log.Printf("MCP Registry v%s\n", Version)
|
||||
log.Printf("Git commit: %s\n", GitCommit)
|
||||
log.Printf("Build time: %s\n", BuildTime)
|
||||
return
|
||||
}
|
||||
|
||||
|
|
@ -47,7 +47,8 @@ func main() {
|
|||
// Connect to MongoDB
|
||||
mongoDB, err := database.NewMongoDB(ctx, cfg.DatabaseURL, cfg.DatabaseName, cfg.CollectionName)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to connect to MongoDB: %v", err)
|
||||
log.Printf("Failed to connect to MongoDB: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Create registry service with MongoDB
|
||||
|
|
@ -66,7 +67,9 @@ func main() {
|
|||
|
||||
if cfg.SeedImport {
|
||||
log.Println("Importing data...")
|
||||
database.ImportSeedFile(mongoDB, cfg.SeedFilePath)
|
||||
if err := database.ImportSeedFile(mongoDB, cfg.SeedFilePath); err != nil {
|
||||
log.Printf("Failed to import seed file: %v", err)
|
||||
}
|
||||
log.Println("Data import completed successfully")
|
||||
}
|
||||
|
||||
|
|
@ -78,8 +81,9 @@ func main() {
|
|||
|
||||
// Start server in a goroutine so it doesn't block signal handling
|
||||
go func() {
|
||||
if err := server.Start(); err != nil && err != http.ErrServerClosed {
|
||||
log.Fatalf("Failed to start server: %v", err)
|
||||
if err := server.Start(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
||||
log.Printf("Failed to start server: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}()
|
||||
|
||||
|
|
@ -96,7 +100,7 @@ func main() {
|
|||
|
||||
// Gracefully shutdown the server
|
||||
if err := server.Shutdown(sctx); err != nil {
|
||||
log.Fatalf("Server forced to shutdown: %v", err)
|
||||
log.Printf("Server forced to shutdown: %v", err)
|
||||
}
|
||||
|
||||
log.Println("Server exiting")
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
package integrationtests
|
||||
package integrationtests_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
|
|
@ -20,7 +20,9 @@ import (
|
|||
// MockAuthService implements a simple auth service for testing
|
||||
type MockAuthService struct{}
|
||||
|
||||
func (m *MockAuthService) StartAuthFlow(ctx context.Context, method model.AuthMethod, repoRef string) (map[string]string, string, error) {
|
||||
func (m *MockAuthService) StartAuthFlow(
|
||||
_ context.Context, _ model.AuthMethod, _ string,
|
||||
) (map[string]string, string, error) {
|
||||
return map[string]string{
|
||||
"device_code": "mock_device_code",
|
||||
"user_code": "ABCD-1234",
|
||||
|
|
@ -28,14 +30,14 @@ func (m *MockAuthService) StartAuthFlow(ctx context.Context, method model.AuthMe
|
|||
}, "mock_status_token", nil
|
||||
}
|
||||
|
||||
func (m *MockAuthService) CheckAuthStatus(ctx context.Context, statusToken string) (string, error) {
|
||||
func (m *MockAuthService) CheckAuthStatus(_ context.Context, statusToken string) (string, error) {
|
||||
if statusToken == "mock_status_token" {
|
||||
return "mock_access_token", nil
|
||||
}
|
||||
return "", fmt.Errorf("invalid status token")
|
||||
}
|
||||
|
||||
func (m *MockAuthService) ValidateAuth(ctx context.Context, authentication model.Authentication) (bool, error) {
|
||||
func (m *MockAuthService) ValidateAuth(_ context.Context, authentication model.Authentication) (bool, error) {
|
||||
// Simple validation: for testing purposes, accept any non-empty token
|
||||
switch authentication.Method {
|
||||
case model.AuthMethodGitHub:
|
||||
|
|
@ -327,10 +329,10 @@ func TestPublishIntegration(t *testing.T) {
|
|||
},
|
||||
}
|
||||
|
||||
duplicateJsonData, err := json.Marshal(duplicateServerDetail)
|
||||
duplicateJSONData, err := json.Marshal(duplicateServerDetail)
|
||||
require.NoError(t, err)
|
||||
|
||||
duplicateReq := httptest.NewRequest(http.MethodPost, "/v0/publish", bytes.NewBuffer(duplicateJsonData))
|
||||
duplicateReq := httptest.NewRequest(http.MethodPost, "/v0/publish", bytes.NewBuffer(duplicateJSONData))
|
||||
duplicateReq.Header.Set("Content-Type", "application/json")
|
||||
duplicateReq.Header.Set("Authorization", "Bearer github_token_duplicate")
|
||||
|
||||
|
|
@ -346,7 +348,6 @@ func TestPublishIntegration(t *testing.T) {
|
|||
require.NoError(t, err)
|
||||
assert.Equal(t, firstServerDetail.Name, retrievedServer.Name)
|
||||
assert.Equal(t, firstServerDetail.Description, retrievedServer.Description)
|
||||
|
||||
})
|
||||
|
||||
t.Run("publish succeeds with same name but different version", func(t *testing.T) {
|
||||
|
|
@ -400,10 +401,10 @@ func TestPublishIntegration(t *testing.T) {
|
|||
},
|
||||
}
|
||||
|
||||
secondJsonData, err := json.Marshal(secondVersionDetail)
|
||||
secondJSONData, err := json.Marshal(secondVersionDetail)
|
||||
require.NoError(t, err)
|
||||
|
||||
secondReq := httptest.NewRequest(http.MethodPost, "/v0/publish", bytes.NewBuffer(secondJsonData))
|
||||
secondReq := httptest.NewRequest(http.MethodPost, "/v0/publish", bytes.NewBuffer(secondJSONData))
|
||||
secondReq.Header.Set("Content-Type", "application/json")
|
||||
secondReq.Header.Set("Authorization", "Bearer github_token_v2")
|
||||
|
||||
|
|
@ -480,10 +481,10 @@ func TestPublishIntegration(t *testing.T) {
|
|||
},
|
||||
}
|
||||
|
||||
olderJsonData, err := json.Marshal(olderVersionDetail)
|
||||
olderJSONData, err := json.Marshal(olderVersionDetail)
|
||||
require.NoError(t, err)
|
||||
|
||||
olderReq := httptest.NewRequest(http.MethodPost, "/v0/publish", bytes.NewBuffer(olderJsonData))
|
||||
olderReq := httptest.NewRequest(http.MethodPost, "/v0/publish", bytes.NewBuffer(olderJSONData))
|
||||
olderReq.Header.Set("Content-Type", "application/json")
|
||||
olderReq.Header.Set("Authorization", "Bearer github_token_older")
|
||||
|
||||
|
|
|
|||
|
|
@ -64,11 +64,14 @@ func StartAuthHandler(authService auth.Service) http.HandlerFunc {
|
|||
// Return successful response
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(map[string]interface{}{
|
||||
if err := json.NewEncoder(w).Encode(map[string]interface{}{
|
||||
"flow_info": flowInfo,
|
||||
"status_token": statusToken,
|
||||
"expires_in": 300, // 5 minutes
|
||||
})
|
||||
}); err != nil {
|
||||
http.Error(w, "Failed to encode response", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -95,9 +98,12 @@ func CheckAuthStatusHandler(authService auth.Service) http.HandlerFunc {
|
|||
// Auth is still pending
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(map[string]interface{}{
|
||||
if err := json.NewEncoder(w).Encode(map[string]interface{}{
|
||||
"status": "pending",
|
||||
})
|
||||
}); err != nil {
|
||||
http.Error(w, "Failed to encode response", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
|
|
@ -109,9 +115,12 @@ func CheckAuthStatusHandler(authService auth.Service) http.HandlerFunc {
|
|||
// Authentication completed successfully
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(map[string]interface{}{
|
||||
if err := json.NewEncoder(w).Encode(map[string]interface{}{
|
||||
"status": "complete",
|
||||
"token": token,
|
||||
})
|
||||
}); err != nil {
|
||||
http.Error(w, "Failed to encode response", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,16 +10,18 @@ import (
|
|||
|
||||
type HealthResponse struct {
|
||||
Status string `json:"status"`
|
||||
GitHubClientId string `json:"github_client_id"`
|
||||
GitHubClientID string `json:"github_client_id"`
|
||||
}
|
||||
|
||||
// HealthHandler returns a handler for health check endpoint
|
||||
func HealthHandler(cfg *config.Config) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
return func(w http.ResponseWriter, _ *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(HealthResponse{
|
||||
if err := json.NewEncoder(w).Encode(HealthResponse{
|
||||
Status: "ok",
|
||||
GitHubClientId: cfg.GithubClientID,
|
||||
})
|
||||
GitHubClientID: cfg.GithubClientID,
|
||||
}); err != nil {
|
||||
http.Error(w, "Failed to encode response", http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,11 +1,13 @@
|
|||
package v0
|
||||
package v0_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
v0 "github.com/modelcontextprotocol/registry/internal/api/handlers/v0"
|
||||
"github.com/modelcontextprotocol/registry/internal/config"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
|
@ -16,7 +18,7 @@ func TestHealthHandler(t *testing.T) {
|
|||
name string
|
||||
config *config.Config
|
||||
expectedStatus int
|
||||
expectedBody HealthResponse
|
||||
expectedBody v0.HealthResponse
|
||||
}{
|
||||
{
|
||||
name: "returns health status with github client id",
|
||||
|
|
@ -24,9 +26,9 @@ func TestHealthHandler(t *testing.T) {
|
|||
GithubClientID: "test-github-client-id",
|
||||
},
|
||||
expectedStatus: http.StatusOK,
|
||||
expectedBody: HealthResponse{
|
||||
expectedBody: v0.HealthResponse{
|
||||
Status: "ok",
|
||||
GitHubClientId: "test-github-client-id",
|
||||
GitHubClientID: "test-github-client-id",
|
||||
},
|
||||
},
|
||||
{
|
||||
|
|
@ -35,9 +37,9 @@ func TestHealthHandler(t *testing.T) {
|
|||
GithubClientID: "",
|
||||
},
|
||||
expectedStatus: http.StatusOK,
|
||||
expectedBody: HealthResponse{
|
||||
expectedBody: v0.HealthResponse{
|
||||
Status: "ok",
|
||||
GitHubClientId: "",
|
||||
GitHubClientID: "",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
|
@ -45,10 +47,10 @@ func TestHealthHandler(t *testing.T) {
|
|||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
// Create handler with the test config
|
||||
handler := HealthHandler(tc.config)
|
||||
handler := v0.HealthHandler(tc.config)
|
||||
|
||||
// Create request
|
||||
req, err := http.NewRequest("GET", "/health", nil)
|
||||
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, "/health", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
|
@ -66,7 +68,7 @@ func TestHealthHandler(t *testing.T) {
|
|||
assert.Equal(t, "application/json", rr.Header().Get("Content-Type"))
|
||||
|
||||
// Parse response body
|
||||
var resp HealthResponse
|
||||
var resp v0.HealthResponse
|
||||
err = json.NewDecoder(rr.Body).Decode(&resp)
|
||||
assert.NoError(t, err)
|
||||
|
||||
|
|
@ -83,11 +85,18 @@ func TestHealthHandlerIntegration(t *testing.T) {
|
|||
GithubClientID: "integration-test-client-id",
|
||||
}
|
||||
|
||||
server := httptest.NewServer(HealthHandler(cfg))
|
||||
server := httptest.NewServer(v0.HealthHandler(cfg))
|
||||
defer server.Close()
|
||||
|
||||
// Send request to the test server
|
||||
resp, err := http.Get(server.URL)
|
||||
ctx := context.Background()
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, server.URL, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create request: %v", err)
|
||||
}
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to send request: %v", err)
|
||||
}
|
||||
|
|
@ -100,14 +109,14 @@ func TestHealthHandlerIntegration(t *testing.T) {
|
|||
assert.Equal(t, "application/json", resp.Header.Get("Content-Type"))
|
||||
|
||||
// Parse response body
|
||||
var healthResp HealthResponse
|
||||
var healthResp v0.HealthResponse
|
||||
err = json.NewDecoder(resp.Body).Decode(&healthResp)
|
||||
assert.NoError(t, err)
|
||||
|
||||
// Check the response body
|
||||
expectedResp := HealthResponse{
|
||||
expectedResp := v0.HealthResponse{
|
||||
Status: "ok",
|
||||
GitHubClientId: "integration-test-client-id",
|
||||
GitHubClientID: "integration-test-client-id",
|
||||
}
|
||||
assert.Equal(t, expectedResp, healthResp)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,6 +22,8 @@ func PingHandler(cfg *config.Config) http.HandlerFunc {
|
|||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(response)
|
||||
if err := json.NewEncoder(w).Encode(response); err != nil {
|
||||
http.Error(w, "Failed to encode response", http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -92,7 +92,7 @@ func PublishHandler(registry service.RegistryService, authService auth.Service)
|
|||
|
||||
valid, err := authService.ValidateAuth(r.Context(), a)
|
||||
if err != nil {
|
||||
if err == auth.ErrAuthRequired {
|
||||
if errors.Is(err, auth.ErrAuthRequired) {
|
||||
http.Error(w, "Authentication is required for publishing", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
|
@ -119,9 +119,12 @@ func PublishHandler(registry service.RegistryService, authService auth.Service)
|
|||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
json.NewEncoder(w).Encode(map[string]string{
|
||||
if err := json.NewEncoder(w).Encode(map[string]string{
|
||||
"message": "Server publication successful",
|
||||
"id": serverDetail.ID,
|
||||
})
|
||||
}); err != nil {
|
||||
http.Error(w, "Failed to encode response", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
package v0
|
||||
package v0_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
|
|
@ -8,6 +8,7 @@ import (
|
|||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
v0 "github.com/modelcontextprotocol/registry/internal/api/handlers/v0"
|
||||
"github.com/modelcontextprotocol/registry/internal/auth"
|
||||
"github.com/modelcontextprotocol/registry/internal/model"
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
|
@ -20,17 +21,17 @@ type MockRegistryService struct {
|
|||
}
|
||||
|
||||
func (m *MockRegistryService) List(cursor string, limit int) ([]model.Server, string, error) {
|
||||
args := m.Called(cursor, limit)
|
||||
args := m.Mock.Called(cursor, limit)
|
||||
return args.Get(0).([]model.Server), args.String(1), args.Error(2)
|
||||
}
|
||||
|
||||
func (m *MockRegistryService) GetByID(id string) (*model.ServerDetail, error) {
|
||||
args := m.Called(id)
|
||||
args := m.Mock.Called(id)
|
||||
return args.Get(0).(*model.ServerDetail), args.Error(1)
|
||||
}
|
||||
|
||||
func (m *MockRegistryService) Publish(serverDetail *model.ServerDetail) error {
|
||||
args := m.Called(serverDetail)
|
||||
args := m.Mock.Called(serverDetail)
|
||||
return args.Error(0)
|
||||
}
|
||||
|
||||
|
|
@ -39,18 +40,20 @@ type MockAuthService struct {
|
|||
mock.Mock
|
||||
}
|
||||
|
||||
func (m *MockAuthService) StartAuthFlow(ctx context.Context, method model.AuthMethod, repoRef string) (map[string]string, string, error) {
|
||||
args := m.Called(ctx, method, repoRef)
|
||||
func (m *MockAuthService) StartAuthFlow(
|
||||
ctx context.Context, method model.AuthMethod, repoRef string,
|
||||
) (map[string]string, string, error) {
|
||||
args := m.Mock.Called(ctx, method, repoRef)
|
||||
return args.Get(0).(map[string]string), args.String(1), args.Error(2)
|
||||
}
|
||||
|
||||
func (m *MockAuthService) CheckAuthStatus(ctx context.Context, statusToken string) (string, error) {
|
||||
args := m.Called(ctx, statusToken)
|
||||
args := m.Mock.Called(ctx, statusToken)
|
||||
return args.String(0), args.Error(1)
|
||||
}
|
||||
|
||||
func (m *MockAuthService) ValidateAuth(ctx context.Context, authentication model.Authentication) (bool, error) {
|
||||
args := m.Called(ctx, authentication)
|
||||
args := m.Mock.Called(ctx, authentication)
|
||||
return args.Bool(0), args.Error(1)
|
||||
}
|
||||
|
||||
|
|
@ -87,12 +90,12 @@ func TestPublishHandler(t *testing.T) {
|
|||
},
|
||||
authHeader: "Bearer github_token_123",
|
||||
setupMocks: func(registry *MockRegistryService, authSvc *MockAuthService) {
|
||||
authSvc.On("ValidateAuth", mock.Anything, model.Authentication{
|
||||
authSvc.Mock.On("ValidateAuth", mock.Anything, model.Authentication{
|
||||
Method: model.AuthMethodGitHub,
|
||||
Token: "github_token_123",
|
||||
RepoRef: "io.github.example/test-server",
|
||||
}).Return(true, nil)
|
||||
registry.On("Publish", mock.AnythingOfType("*model.ServerDetail")).Return(nil)
|
||||
registry.Mock.On("Publish", mock.AnythingOfType("*model.ServerDetail")).Return(nil)
|
||||
},
|
||||
expectedStatus: http.StatusCreated,
|
||||
expectedResponse: map[string]string{
|
||||
|
|
@ -122,12 +125,12 @@ func TestPublishHandler(t *testing.T) {
|
|||
},
|
||||
authHeader: "Bearer some_token",
|
||||
setupMocks: func(registry *MockRegistryService, authSvc *MockAuthService) {
|
||||
authSvc.On("ValidateAuth", mock.Anything, model.Authentication{
|
||||
authSvc.Mock.On("ValidateAuth", mock.Anything, model.Authentication{
|
||||
Method: model.AuthMethodNone,
|
||||
Token: "some_token",
|
||||
RepoRef: "example/test-server",
|
||||
}).Return(true, nil)
|
||||
registry.On("Publish", mock.AnythingOfType("*model.ServerDetail")).Return(nil)
|
||||
registry.Mock.On("Publish", mock.AnythingOfType("*model.ServerDetail")).Return(nil)
|
||||
},
|
||||
expectedStatus: http.StatusCreated,
|
||||
expectedResponse: map[string]string{
|
||||
|
|
@ -140,7 +143,7 @@ func TestPublishHandler(t *testing.T) {
|
|||
method: http.MethodGet,
|
||||
requestBody: nil,
|
||||
authHeader: "",
|
||||
setupMocks: func(registry *MockRegistryService, authSvc *MockAuthService) {},
|
||||
setupMocks: func(_ *MockRegistryService, _ *MockAuthService) {},
|
||||
expectedStatus: http.StatusMethodNotAllowed,
|
||||
expectedError: "Method not allowed",
|
||||
},
|
||||
|
|
@ -149,7 +152,7 @@ func TestPublishHandler(t *testing.T) {
|
|||
method: http.MethodPost,
|
||||
requestBody: "",
|
||||
authHeader: "",
|
||||
setupMocks: func(registry *MockRegistryService, authSvc *MockAuthService) {},
|
||||
setupMocks: func(_ *MockRegistryService, _ *MockAuthService) {},
|
||||
expectedStatus: http.StatusBadRequest,
|
||||
expectedError: "Invalid request payload:",
|
||||
},
|
||||
|
|
@ -169,7 +172,7 @@ func TestPublishHandler(t *testing.T) {
|
|||
},
|
||||
},
|
||||
authHeader: "",
|
||||
setupMocks: func(registry *MockRegistryService, authSvc *MockAuthService) {},
|
||||
setupMocks: func(_ *MockRegistryService, _ *MockAuthService) {},
|
||||
expectedStatus: http.StatusBadRequest,
|
||||
expectedError: "Name is required",
|
||||
},
|
||||
|
|
@ -189,7 +192,7 @@ func TestPublishHandler(t *testing.T) {
|
|||
},
|
||||
},
|
||||
authHeader: "",
|
||||
setupMocks: func(registry *MockRegistryService, authSvc *MockAuthService) {},
|
||||
setupMocks: func(_ *MockRegistryService, _ *MockAuthService) {},
|
||||
expectedStatus: http.StatusBadRequest,
|
||||
expectedError: "Version is required",
|
||||
},
|
||||
|
|
@ -209,7 +212,7 @@ func TestPublishHandler(t *testing.T) {
|
|||
},
|
||||
},
|
||||
authHeader: "", // Missing auth header
|
||||
setupMocks: func(registry *MockRegistryService, authSvc *MockAuthService) {},
|
||||
setupMocks: func(_ *MockRegistryService, _ *MockAuthService) {},
|
||||
expectedStatus: http.StatusUnauthorized,
|
||||
expectedError: "Authorization header is required",
|
||||
},
|
||||
|
|
@ -229,8 +232,8 @@ func TestPublishHandler(t *testing.T) {
|
|||
},
|
||||
},
|
||||
authHeader: "Bearer token",
|
||||
setupMocks: func(registry *MockRegistryService, authSvc *MockAuthService) {
|
||||
authSvc.On("ValidateAuth", mock.Anything, mock.Anything).Return(false, auth.ErrAuthRequired)
|
||||
setupMocks: func(_ *MockRegistryService, authSvc *MockAuthService) {
|
||||
authSvc.Mock.On("ValidateAuth", mock.Anything, mock.Anything).Return(false, auth.ErrAuthRequired)
|
||||
},
|
||||
expectedStatus: http.StatusUnauthorized,
|
||||
expectedError: "Authentication is required for publishing",
|
||||
|
|
@ -251,8 +254,8 @@ func TestPublishHandler(t *testing.T) {
|
|||
},
|
||||
},
|
||||
authHeader: "Bearer invalid_token",
|
||||
setupMocks: func(registry *MockRegistryService, authSvc *MockAuthService) {
|
||||
authSvc.On("ValidateAuth", mock.Anything, mock.Anything).Return(false, nil)
|
||||
setupMocks: func(_ *MockRegistryService, authSvc *MockAuthService) {
|
||||
authSvc.Mock.On("ValidateAuth", mock.Anything, mock.Anything).Return(false, nil)
|
||||
},
|
||||
expectedStatus: http.StatusUnauthorized,
|
||||
expectedError: "Invalid authentication credentials",
|
||||
|
|
@ -274,8 +277,8 @@ func TestPublishHandler(t *testing.T) {
|
|||
},
|
||||
authHeader: "Bearer token",
|
||||
setupMocks: func(registry *MockRegistryService, authSvc *MockAuthService) {
|
||||
authSvc.On("ValidateAuth", mock.Anything, mock.Anything).Return(true, nil)
|
||||
registry.On("Publish", mock.AnythingOfType("*model.ServerDetail")).Return(assert.AnError)
|
||||
authSvc.Mock.On("ValidateAuth", mock.Anything, mock.Anything).Return(true, nil)
|
||||
registry.Mock.On("Publish", mock.AnythingOfType("*model.ServerDetail")).Return(assert.AnError)
|
||||
},
|
||||
expectedStatus: http.StatusInternalServerError,
|
||||
expectedError: "Failed to publish server details:",
|
||||
|
|
@ -292,7 +295,7 @@ func TestPublishHandler(t *testing.T) {
|
|||
tc.setupMocks(mockRegistry, mockAuthService)
|
||||
|
||||
// Create handler
|
||||
handler := PublishHandler(mockRegistry, mockAuthService)
|
||||
handler := v0.PublishHandler(mockRegistry, mockAuthService)
|
||||
|
||||
// Prepare request body
|
||||
var requestBody []byte
|
||||
|
|
@ -303,7 +306,7 @@ func TestPublishHandler(t *testing.T) {
|
|||
}
|
||||
|
||||
// Create request
|
||||
req, err := http.NewRequest(tc.method, "/publish", bytes.NewBuffer(requestBody))
|
||||
req, err := http.NewRequestWithContext(context.Background(), tc.method, "/publish", bytes.NewBuffer(requestBody))
|
||||
assert.NoError(t, err)
|
||||
|
||||
// Set auth header if provided
|
||||
|
|
@ -337,8 +340,8 @@ func TestPublishHandler(t *testing.T) {
|
|||
}
|
||||
|
||||
// Assert that all expectations were met
|
||||
mockRegistry.AssertExpectations(t)
|
||||
mockAuthService.AssertExpectations(t)
|
||||
mockRegistry.Mock.AssertExpectations(t)
|
||||
mockAuthService.Mock.AssertExpectations(t)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
@ -377,12 +380,12 @@ func TestPublishHandlerBearerTokenParsing(t *testing.T) {
|
|||
mockAuthService := new(MockAuthService)
|
||||
|
||||
// Setup mock to capture the actual token passed
|
||||
mockAuthService.On("ValidateAuth", mock.Anything, mock.MatchedBy(func(auth model.Authentication) bool {
|
||||
mockAuthService.Mock.On("ValidateAuth", mock.Anything, mock.MatchedBy(func(auth model.Authentication) bool {
|
||||
return auth.Token == tc.expectedToken
|
||||
})).Return(true, nil)
|
||||
mockRegistry.On("Publish", mock.AnythingOfType("*model.ServerDetail")).Return(nil)
|
||||
mockRegistry.Mock.On("Publish", mock.AnythingOfType("*model.ServerDetail")).Return(nil)
|
||||
|
||||
handler := PublishHandler(mockRegistry, mockAuthService)
|
||||
handler := v0.PublishHandler(mockRegistry, mockAuthService)
|
||||
|
||||
serverDetail := model.ServerDetail{
|
||||
Server: model.Server{
|
||||
|
|
@ -400,7 +403,7 @@ func TestPublishHandlerBearerTokenParsing(t *testing.T) {
|
|||
requestBody, err := json.Marshal(serverDetail)
|
||||
assert.NoError(t, err)
|
||||
|
||||
req, err := http.NewRequest(http.MethodPost, "/publish", bytes.NewBuffer(requestBody))
|
||||
req, err := http.NewRequestWithContext(context.Background(), http.MethodPost, "/publish", bytes.NewBuffer(requestBody))
|
||||
assert.NoError(t, err)
|
||||
req.Header.Set("Authorization", tc.authHeader)
|
||||
|
||||
|
|
@ -408,7 +411,7 @@ func TestPublishHandlerBearerTokenParsing(t *testing.T) {
|
|||
handler.ServeHTTP(rr, req)
|
||||
|
||||
assert.Equal(t, http.StatusCreated, rr.Code)
|
||||
mockAuthService.AssertExpectations(t)
|
||||
mockAuthService.Mock.AssertExpectations(t)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
@ -442,12 +445,12 @@ func TestPublishHandlerAuthMethodSelection(t *testing.T) {
|
|||
mockAuthService := new(MockAuthService)
|
||||
|
||||
// Setup mock to capture the auth method
|
||||
mockAuthService.On("ValidateAuth", mock.Anything, mock.MatchedBy(func(auth model.Authentication) bool {
|
||||
mockAuthService.Mock.On("ValidateAuth", mock.Anything, mock.MatchedBy(func(auth model.Authentication) bool {
|
||||
return auth.Method == tc.expectedAuthMethod
|
||||
})).Return(true, nil)
|
||||
mockRegistry.On("Publish", mock.AnythingOfType("*model.ServerDetail")).Return(nil)
|
||||
mockRegistry.Mock.On("Publish", mock.AnythingOfType("*model.ServerDetail")).Return(nil)
|
||||
|
||||
handler := PublishHandler(mockRegistry, mockAuthService)
|
||||
handler := v0.PublishHandler(mockRegistry, mockAuthService)
|
||||
|
||||
serverDetail := model.ServerDetail{
|
||||
Server: model.Server{
|
||||
|
|
@ -465,7 +468,7 @@ func TestPublishHandlerAuthMethodSelection(t *testing.T) {
|
|||
requestBody, err := json.Marshal(serverDetail)
|
||||
assert.NoError(t, err)
|
||||
|
||||
req, err := http.NewRequest(http.MethodPost, "/publish", bytes.NewBuffer(requestBody))
|
||||
req, err := http.NewRequestWithContext(context.Background(), http.MethodPost, "/publish", bytes.NewBuffer(requestBody))
|
||||
assert.NoError(t, err)
|
||||
req.Header.Set("Authorization", "Bearer test_token")
|
||||
|
||||
|
|
@ -473,7 +476,7 @@ func TestPublishHandlerAuthMethodSelection(t *testing.T) {
|
|||
handler.ServeHTTP(rr, req)
|
||||
|
||||
assert.Equal(t, http.StatusCreated, rr.Code)
|
||||
mockAuthService.AssertExpectations(t)
|
||||
mockAuthService.Mock.AssertExpectations(t)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -89,7 +89,10 @@ func ServersHandler(registry service.RegistryService) http.HandlerFunc {
|
|||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(response)
|
||||
if err := json.NewEncoder(w).Encode(response); err != nil {
|
||||
http.Error(w, "Failed to encode response", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -123,6 +126,9 @@ func ServersDetailHandler(registry service.RegistryService) http.HandlerFunc {
|
|||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(serverDetail)
|
||||
if err := json.NewEncoder(w).Encode(serverDetail); err != nil {
|
||||
http.Error(w, "Failed to encode response", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package v0
|
||||
package v0_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
|
|
@ -8,6 +9,7 @@ import (
|
|||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
v0 "github.com/modelcontextprotocol/registry/internal/api/handlers/v0"
|
||||
"github.com/modelcontextprotocol/registry/internal/model"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/mock"
|
||||
|
|
@ -21,7 +23,7 @@ func TestServersHandler(t *testing.T) {
|
|||
setupMocks func(*MockRegistryService)
|
||||
expectedStatus int
|
||||
expectedServers []model.Server
|
||||
expectedMeta *Metadata
|
||||
expectedMeta *v0.Metadata
|
||||
expectedError string
|
||||
}{
|
||||
{
|
||||
|
|
@ -60,7 +62,7 @@ func TestServersHandler(t *testing.T) {
|
|||
},
|
||||
},
|
||||
}
|
||||
registry.On("List", "", 30).Return(servers, "", nil)
|
||||
registry.Mock.On("List", "", 30).Return(servers, "", nil)
|
||||
},
|
||||
expectedStatus: http.StatusOK,
|
||||
expectedServers: []model.Server{
|
||||
|
|
@ -119,7 +121,7 @@ func TestServersHandler(t *testing.T) {
|
|||
},
|
||||
}
|
||||
nextCursor := uuid.New().String()
|
||||
registry.On("List", mock.AnythingOfType("string"), 10).Return(servers, nextCursor, nil)
|
||||
registry.Mock.On("List", mock.AnythingOfType("string"), 10).Return(servers, nextCursor, nil)
|
||||
},
|
||||
expectedStatus: http.StatusOK,
|
||||
expectedServers: []model.Server{
|
||||
|
|
@ -139,7 +141,7 @@ func TestServersHandler(t *testing.T) {
|
|||
},
|
||||
},
|
||||
},
|
||||
expectedMeta: &Metadata{
|
||||
expectedMeta: &v0.Metadata{
|
||||
NextCursor: "", // This will be dynamically set in the test
|
||||
Count: 1,
|
||||
},
|
||||
|
|
@ -150,7 +152,7 @@ func TestServersHandler(t *testing.T) {
|
|||
queryParams: "?limit=150",
|
||||
setupMocks: func(registry *MockRegistryService) {
|
||||
servers := []model.Server{}
|
||||
registry.On("List", "", 100).Return(servers, "", nil)
|
||||
registry.Mock.On("List", "", 100).Return(servers, "", nil)
|
||||
},
|
||||
expectedStatus: http.StatusOK,
|
||||
expectedServers: []model.Server{},
|
||||
|
|
@ -159,7 +161,7 @@ func TestServersHandler(t *testing.T) {
|
|||
name: "invalid cursor parameter",
|
||||
method: http.MethodGet,
|
||||
queryParams: "?cursor=invalid-uuid",
|
||||
setupMocks: func(registry *MockRegistryService) {},
|
||||
setupMocks: func(_ *MockRegistryService) {},
|
||||
expectedStatus: http.StatusBadRequest,
|
||||
expectedError: "Invalid cursor parameter",
|
||||
},
|
||||
|
|
@ -167,7 +169,7 @@ func TestServersHandler(t *testing.T) {
|
|||
name: "invalid limit parameter - non-numeric",
|
||||
method: http.MethodGet,
|
||||
queryParams: "?limit=abc",
|
||||
setupMocks: func(registry *MockRegistryService) {},
|
||||
setupMocks: func(_ *MockRegistryService) {},
|
||||
expectedStatus: http.StatusBadRequest,
|
||||
expectedError: "Invalid limit parameter",
|
||||
},
|
||||
|
|
@ -175,7 +177,7 @@ func TestServersHandler(t *testing.T) {
|
|||
name: "invalid limit parameter - zero",
|
||||
method: http.MethodGet,
|
||||
queryParams: "?limit=0",
|
||||
setupMocks: func(registry *MockRegistryService) {},
|
||||
setupMocks: func(_ *MockRegistryService) {},
|
||||
expectedStatus: http.StatusBadRequest,
|
||||
expectedError: "Limit must be greater than 0",
|
||||
},
|
||||
|
|
@ -183,7 +185,7 @@ func TestServersHandler(t *testing.T) {
|
|||
name: "invalid limit parameter - negative",
|
||||
method: http.MethodGet,
|
||||
queryParams: "?limit=-5",
|
||||
setupMocks: func(registry *MockRegistryService) {},
|
||||
setupMocks: func(_ *MockRegistryService) {},
|
||||
expectedStatus: http.StatusBadRequest,
|
||||
expectedError: "Limit must be greater than 0",
|
||||
},
|
||||
|
|
@ -191,7 +193,7 @@ func TestServersHandler(t *testing.T) {
|
|||
name: "registry service error",
|
||||
method: http.MethodGet,
|
||||
setupMocks: func(registry *MockRegistryService) {
|
||||
registry.On("List", "", 30).Return([]model.Server{}, "", errors.New("database connection error"))
|
||||
registry.Mock.On("List", "", 30).Return([]model.Server{}, "", errors.New("database connection error"))
|
||||
},
|
||||
expectedStatus: http.StatusInternalServerError,
|
||||
expectedError: "database connection error",
|
||||
|
|
@ -199,7 +201,7 @@ func TestServersHandler(t *testing.T) {
|
|||
{
|
||||
name: "method not allowed",
|
||||
method: http.MethodPost,
|
||||
setupMocks: func(registry *MockRegistryService) {},
|
||||
setupMocks: func(_ *MockRegistryService) {},
|
||||
expectedStatus: http.StatusMethodNotAllowed,
|
||||
expectedError: "Method not allowed",
|
||||
},
|
||||
|
|
@ -212,11 +214,11 @@ func TestServersHandler(t *testing.T) {
|
|||
tc.setupMocks(mockRegistry)
|
||||
|
||||
// Create handler
|
||||
handler := ServersHandler(mockRegistry)
|
||||
handler := v0.ServersHandler(mockRegistry)
|
||||
|
||||
// Create request
|
||||
url := "/v0/servers" + tc.queryParams
|
||||
req, err := http.NewRequest(tc.method, url, nil)
|
||||
req, err := http.NewRequestWithContext(context.Background(), tc.method, url, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
|
@ -235,7 +237,7 @@ func TestServersHandler(t *testing.T) {
|
|||
assert.Equal(t, "application/json", rr.Header().Get("Content-Type"))
|
||||
|
||||
// Parse response body
|
||||
var resp PaginatedResponse
|
||||
var resp v0.PaginatedResponse
|
||||
err = json.NewDecoder(rr.Body).Decode(&resp)
|
||||
assert.NoError(t, err)
|
||||
|
||||
|
|
@ -249,15 +251,13 @@ func TestServersHandler(t *testing.T) {
|
|||
assert.NotEmpty(t, resp.Metadata.NextCursor)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
} else if tc.expectedError != "" {
|
||||
// Check error message for non-200 responses
|
||||
if tc.expectedError != "" {
|
||||
assert.Contains(t, rr.Body.String(), tc.expectedError)
|
||||
}
|
||||
assert.Contains(t, rr.Body.String(), tc.expectedError)
|
||||
}
|
||||
|
||||
// Verify mock expectations
|
||||
mockRegistry.AssertExpectations(t)
|
||||
mockRegistry.Mock.AssertExpectations(t)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
@ -285,14 +285,21 @@ func TestServersHandlerIntegration(t *testing.T) {
|
|||
},
|
||||
}
|
||||
|
||||
mockRegistry.On("List", "", 30).Return(servers, "", nil)
|
||||
mockRegistry.Mock.On("List", "", 30).Return(servers, "", nil)
|
||||
|
||||
// Create test server
|
||||
server := httptest.NewServer(ServersHandler(mockRegistry))
|
||||
server := httptest.NewServer(v0.ServersHandler(mockRegistry))
|
||||
defer server.Close()
|
||||
|
||||
// Send request to the test server
|
||||
resp, err := http.Get(server.URL)
|
||||
ctx := context.Background()
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, server.URL, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create request: %v", err)
|
||||
}
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to send request: %v", err)
|
||||
}
|
||||
|
|
@ -305,7 +312,7 @@ func TestServersHandlerIntegration(t *testing.T) {
|
|||
assert.Equal(t, "application/json", resp.Header.Get("Content-Type"))
|
||||
|
||||
// Parse response body
|
||||
var paginatedResp PaginatedResponse
|
||||
var paginatedResp v0.PaginatedResponse
|
||||
err = json.NewDecoder(resp.Body).Decode(&paginatedResp)
|
||||
assert.NoError(t, err)
|
||||
|
||||
|
|
@ -314,7 +321,7 @@ func TestServersHandlerIntegration(t *testing.T) {
|
|||
assert.Empty(t, paginatedResp.Metadata.NextCursor)
|
||||
|
||||
// Verify mock expectations
|
||||
mockRegistry.AssertExpectations(t)
|
||||
mockRegistry.Mock.AssertExpectations(t)
|
||||
}
|
||||
|
||||
// TestServersDetailHandlerIntegration tests the servers detail handler with actual HTTP requests
|
||||
|
|
@ -342,17 +349,24 @@ func TestServersDetailHandlerIntegration(t *testing.T) {
|
|||
},
|
||||
}
|
||||
|
||||
mockRegistry.On("GetByID", serverID).Return(serverDetail, nil)
|
||||
mockRegistry.Mock.On("GetByID", serverID).Return(serverDetail, nil)
|
||||
|
||||
// Create test server
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
r.SetPathValue("id", serverID)
|
||||
ServersDetailHandler(mockRegistry).ServeHTTP(w, r)
|
||||
v0.ServersDetailHandler(mockRegistry).ServeHTTP(w, r)
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
// Send request to the test server
|
||||
resp, err := http.Get(server.URL)
|
||||
ctx := context.Background()
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, server.URL, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create request: %v", err)
|
||||
}
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to send request: %v", err)
|
||||
}
|
||||
|
|
@ -373,5 +387,5 @@ func TestServersDetailHandlerIntegration(t *testing.T) {
|
|||
assert.Equal(t, *serverDetail, serverDetailResp)
|
||||
|
||||
// Verify mock expectations
|
||||
mockRegistry.AssertExpectations(t)
|
||||
mockRegistry.Mock.AssertExpectations(t)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ import (
|
|||
"os"
|
||||
"path/filepath"
|
||||
|
||||
_ "github.com/swaggo/files"
|
||||
_ "github.com/swaggo/files" // Swagger files needed for embedding
|
||||
httpSwagger "github.com/swaggo/http-swagger"
|
||||
)
|
||||
|
||||
|
|
|
|||
|
|
@ -11,7 +11,9 @@ import (
|
|||
)
|
||||
|
||||
// RegisterV0Routes registers all v0 API routes to the provided router
|
||||
func RegisterV0Routes(mux *http.ServeMux, cfg *config.Config, registry service.RegistryService, authService auth.Service) {
|
||||
func RegisterV0Routes(
|
||||
mux *http.ServeMux, cfg *config.Config, registry service.RegistryService, authService auth.Service,
|
||||
) {
|
||||
// Register v0 endpoints
|
||||
mux.HandleFunc("/v0/health", v0.HealthHandler(cfg))
|
||||
mux.HandleFunc("/v0/servers", v0.ServersHandler(registry))
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import (
|
|||
"context"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/modelcontextprotocol/registry/internal/api/router"
|
||||
"github.com/modelcontextprotocol/registry/internal/auth"
|
||||
|
|
@ -31,8 +32,9 @@ func NewServer(cfg *config.Config, registryService service.RegistryService, auth
|
|||
authService: authService,
|
||||
router: mux,
|
||||
server: &http.Server{
|
||||
Addr: cfg.ServerAddress,
|
||||
Handler: mux,
|
||||
Addr: cfg.ServerAddress,
|
||||
Handler: mux,
|
||||
ReadHeaderTimeout: 10 * time.Second,
|
||||
},
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package auth
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
|
@ -70,14 +71,19 @@ func NewGitHubDeviceAuth(config GitHubOAuthConfig) *GitHubDeviceAuth {
|
|||
// It verifies the token owner matches the repository owner or is a member of the owning organization.
|
||||
// It also verifies that the token was created for the same ClientID used to set up the authentication.
|
||||
// Returns true if valid, false otherwise along with an error explaining the validation failure.
|
||||
func (g *GitHubDeviceAuth) ValidateToken(token string, requiredRepo string) (bool, error) {
|
||||
func (g *GitHubDeviceAuth) ValidateToken(ctx context.Context, token string, requiredRepo string) (bool, error) {
|
||||
// If no repo is required, we can't validate properly
|
||||
if requiredRepo == "" {
|
||||
return false, fmt.Errorf("repository reference is required for token validation")
|
||||
}
|
||||
|
||||
// First, validate that the token is associated with our ClientID
|
||||
tokenReq, err := http.NewRequest("GET", "https://api.github.com/applications/"+g.config.ClientID+"/token", nil)
|
||||
tokenReq, err := http.NewRequestWithContext(
|
||||
ctx,
|
||||
http.MethodGet,
|
||||
"https://api.github.com/applications/"+g.config.ClientID+"/token",
|
||||
nil,
|
||||
)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
|
@ -97,7 +103,8 @@ func (g *GitHubDeviceAuth) ValidateToken(token string, requiredRepo string) (boo
|
|||
}
|
||||
|
||||
// POST instead of GET for security reasons per GitHub API
|
||||
tokenReq, err = http.NewRequest("POST", "https://api.github.com/applications/"+g.config.ClientID+"/token", io.NopCloser(bytes.NewReader(checkBody)))
|
||||
tokenURL := "https://api.github.com/applications/" + g.config.ClientID + "/token"
|
||||
tokenReq, err = http.NewRequestWithContext(ctx, http.MethodPost, tokenURL, io.NopCloser(bytes.NewReader(checkBody)))
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
|
@ -135,7 +142,7 @@ func (g *GitHubDeviceAuth) ValidateToken(token string, requiredRepo string) (boo
|
|||
}
|
||||
|
||||
// Get the authenticated user
|
||||
userReq, err := http.NewRequest("GET", "https://api.github.com/user", nil)
|
||||
userReq, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://api.github.com/user", nil)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
|
@ -175,17 +182,20 @@ func (g *GitHubDeviceAuth) ValidateToken(token string, requiredRepo string) (boo
|
|||
// Verify that the authenticated user matches the owner
|
||||
if userInfo.Login != owner {
|
||||
// Check if the user is a member of the organization
|
||||
isMember, err := g.checkOrgMembership(token, userInfo.Login, owner)
|
||||
isMember, err := g.checkOrgMembership(ctx, token, userInfo.Login, owner)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("failed to check org membership: %s", owner)
|
||||
}
|
||||
|
||||
if !isMember {
|
||||
return false, fmt.Errorf("token belongs to user %s, but repository is owned by %s and user is not a member of the organization", userInfo.Login, owner)
|
||||
return false, fmt.Errorf(
|
||||
"token belongs to user %s, but repository is owned by %s and user is not a member of the organization",
|
||||
userInfo.Login, owner)
|
||||
}
|
||||
}
|
||||
|
||||
// If we've reached this point, the token has access the repo and the user matches the owner or is a member of the owner org
|
||||
// If we've reached this point, the token has access the repo and the user matches
|
||||
// the owner or is a member of the owner org
|
||||
return true, nil
|
||||
}
|
||||
|
||||
|
|
@ -210,13 +220,13 @@ func (g *GitHubDeviceAuth) ExtractGitHubRepo(repoURL string) (owner, repo string
|
|||
}
|
||||
|
||||
// checkOrgMembership checks if a user is a member of an organization
|
||||
func (g *GitHubDeviceAuth) checkOrgMembership(token, username, org string) (bool, error) {
|
||||
func (g *GitHubDeviceAuth) checkOrgMembership(ctx context.Context, token, username, org string) (bool, error) {
|
||||
// Create request to check if user is a member of the organization
|
||||
// GitHub API endpoint: GET /orgs/{org}/members/{username}
|
||||
// true if status code is 204 No Content
|
||||
// false if status code is 404 Not Found
|
||||
url := fmt.Sprint("https://api.github.com/orgs/", org, "/members/", username)
|
||||
req, err := http.NewRequest("GET", url, nil)
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,37 +8,40 @@ import (
|
|||
"github.com/modelcontextprotocol/registry/internal/model"
|
||||
)
|
||||
|
||||
// AuthServiceImpl implements the Service interface
|
||||
type AuthServiceImpl struct {
|
||||
// ServiceImpl implements the Service interface
|
||||
type ServiceImpl struct {
|
||||
config *config.Config
|
||||
githubAuth *GitHubDeviceAuth
|
||||
}
|
||||
|
||||
// NewAuthService creates a new authentication service
|
||||
//
|
||||
//nolint:ireturn // Factory function intentionally returns interface for dependency injection
|
||||
func NewAuthService(cfg *config.Config) Service {
|
||||
githubConfig := GitHubOAuthConfig{
|
||||
ClientID: cfg.GithubClientID,
|
||||
ClientSecret: cfg.GithubClientSecret,
|
||||
}
|
||||
|
||||
return &AuthServiceImpl{
|
||||
return &ServiceImpl{
|
||||
config: cfg,
|
||||
githubAuth: NewGitHubDeviceAuth(githubConfig),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *AuthServiceImpl) StartAuthFlow(ctx context.Context, method model.AuthMethod, repoRef string) (map[string]string, string, error) {
|
||||
func (s *ServiceImpl) StartAuthFlow(_ context.Context, _ model.AuthMethod,
|
||||
_ string) (map[string]string, string, error) {
|
||||
// return not implemented error
|
||||
return nil, "", fmt.Errorf("not implemented")
|
||||
}
|
||||
|
||||
func (s *AuthServiceImpl) CheckAuthStatus(ctx context.Context, statusToken string) (string, error) {
|
||||
func (s *ServiceImpl) CheckAuthStatus(_ context.Context, _ string) (string, error) {
|
||||
// return not implemented error
|
||||
return "", fmt.Errorf("not implemented")
|
||||
}
|
||||
|
||||
// ValidateAuth validates authentication credentials
|
||||
func (s *AuthServiceImpl) ValidateAuth(ctx context.Context, auth model.Authentication) (bool, error) {
|
||||
func (s *ServiceImpl) ValidateAuth(ctx context.Context, auth model.Authentication) (bool, error) {
|
||||
// If authentication is required but not provided
|
||||
if auth.Method == "" || auth.Method == model.AuthMethodNone {
|
||||
return false, ErrAuthRequired
|
||||
|
|
@ -47,7 +50,9 @@ func (s *AuthServiceImpl) ValidateAuth(ctx context.Context, auth model.Authentic
|
|||
switch auth.Method {
|
||||
case model.AuthMethodGitHub:
|
||||
// Extract repo reference from the repository URL if it's not provided
|
||||
return s.githubAuth.ValidateToken(auth.Token, auth.RepoRef)
|
||||
return s.githubAuth.ValidateToken(ctx, auth.Token, auth.RepoRef)
|
||||
case model.AuthMethodNone:
|
||||
return false, ErrAuthRequired
|
||||
default:
|
||||
return false, ErrUnsupportedAuthMethod
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"github.com/caarlos0/env/v11"
|
||||
env "github.com/caarlos0/env/v11"
|
||||
)
|
||||
|
||||
// Config holds the application configuration
|
||||
|
|
|
|||
|
|
@ -33,7 +33,7 @@ func ImportSeedFile(mongo *MongoDB, seedFilePath string) error {
|
|||
// Read the seed file
|
||||
seedData, err := readSeedFile(seedFilePath)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to read seed file: %v", err)
|
||||
return fmt.Errorf("failed to read seed file: %w", err)
|
||||
}
|
||||
|
||||
collection := mongo.collection
|
||||
|
|
@ -48,7 +48,7 @@ func readSeedFile(path string) ([]model.ServerDetail, error) {
|
|||
// Read the file content
|
||||
fileContent, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read file: %v", err)
|
||||
return nil, fmt.Errorf("failed to read file: %w", err)
|
||||
}
|
||||
|
||||
// Parse the JSON content
|
||||
|
|
@ -57,9 +57,8 @@ func readSeedFile(path string) ([]model.ServerDetail, error) {
|
|||
// Try parsing as a raw JSON array and then convert to our model
|
||||
var rawData []map[string]interface{}
|
||||
if jsonErr := json.Unmarshal(fileContent, &rawData); jsonErr != nil {
|
||||
return nil, fmt.Errorf("failed to parse JSON: %v (original error: %v)", jsonErr, err)
|
||||
return nil, fmt.Errorf("failed to parse JSON: %w (original error: %w)", jsonErr, err)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
log.Printf("Found %d server entries in seed file", len(servers))
|
||||
|
|
@ -82,7 +81,6 @@ func importData(ctx context.Context, collection *mongo.Collection, servers []mod
|
|||
server.VersionDetail.Version = "0.0.1-seed"
|
||||
server.VersionDetail.ReleaseDate = time.Now().Format(time.RFC3339)
|
||||
server.VersionDetail.IsLatest = true
|
||||
|
||||
}
|
||||
// Create update document
|
||||
update := bson.M{"$set": server}
|
||||
|
|
@ -95,11 +93,12 @@ func importData(ctx context.Context, collection *mongo.Collection, servers []mod
|
|||
continue
|
||||
}
|
||||
|
||||
if result.UpsertedCount > 0 {
|
||||
switch {
|
||||
case result.UpsertedCount > 0:
|
||||
log.Printf("[%d/%d] Created server: %s", i+1, len(servers), server.Name)
|
||||
} else if result.ModifiedCount > 0 {
|
||||
case result.ModifiedCount > 0:
|
||||
log.Printf("[%d/%d] Updated server: %s", i+1, len(servers), server.Name)
|
||||
} else {
|
||||
default:
|
||||
log.Printf("[%d/%d] Server already up to date: %s", i+1, len(servers), server.Name)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -84,7 +84,14 @@ func compareSemanticVersions(version1, version2 string) int {
|
|||
}
|
||||
|
||||
// List retrieves all MCPRegistry entries with optional filtering and pagination
|
||||
func (db *MemoryDB) List(ctx context.Context, filter map[string]interface{}, cursor string, limit int) ([]*model.Server, string, error) {
|
||||
//
|
||||
//gocognit:ignore
|
||||
func (db *MemoryDB) List(
|
||||
ctx context.Context,
|
||||
filter map[string]interface{},
|
||||
cursor string,
|
||||
limit int,
|
||||
) ([]*model.Server, string, error) {
|
||||
if ctx.Err() != nil {
|
||||
return nil, "", ctx.Err()
|
||||
}
|
||||
|
|
@ -109,27 +116,25 @@ func (db *MemoryDB) List(ctx context.Context, filter map[string]interface{}, cur
|
|||
include := true
|
||||
|
||||
// Apply filters if any
|
||||
if filter != nil {
|
||||
for key, value := range filter {
|
||||
switch key {
|
||||
case "name":
|
||||
if entry.Name != value.(string) {
|
||||
include = false
|
||||
}
|
||||
case "repoUrl":
|
||||
if entry.Repository.URL != value.(string) {
|
||||
include = false
|
||||
}
|
||||
case "serverDetail.id":
|
||||
if entry.ID != value.(string) {
|
||||
include = false
|
||||
}
|
||||
case "version":
|
||||
if entry.VersionDetail.Version != value.(string) {
|
||||
include = false
|
||||
}
|
||||
// Add more filter options as needed
|
||||
for key, value := range filter {
|
||||
switch key {
|
||||
case "name":
|
||||
if entry.Name != value.(string) {
|
||||
include = false
|
||||
}
|
||||
case "repoUrl":
|
||||
if entry.Repository.URL != value.(string) {
|
||||
include = false
|
||||
}
|
||||
case "serverDetail.id":
|
||||
if entry.ID != value.(string) {
|
||||
include = false
|
||||
}
|
||||
case "version":
|
||||
if entry.VersionDetail.Version != value.(string) {
|
||||
include = false
|
||||
}
|
||||
// Add more filter options as needed
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue