|
| 1 | +package v0_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "crypto/ed25519" |
| 7 | + "crypto/rand" |
| 8 | + "encoding/hex" |
| 9 | + "encoding/json" |
| 10 | + "fmt" |
| 11 | + "net/http" |
| 12 | + "net/http/httptest" |
| 13 | + "testing" |
| 14 | + |
| 15 | + "github.com/danielgtaylor/huma/v2" |
| 16 | + "github.com/danielgtaylor/huma/v2/adapters/humago" |
| 17 | + v0 "github.com/modelcontextprotocol/registry/internal/api/handlers/v0" |
| 18 | + "github.com/modelcontextprotocol/registry/internal/auth" |
| 19 | + "github.com/modelcontextprotocol/registry/internal/config" |
| 20 | + "github.com/modelcontextprotocol/registry/internal/database" |
| 21 | + "github.com/modelcontextprotocol/registry/internal/service" |
| 22 | + "github.com/modelcontextprotocol/registry/pkg/model" |
| 23 | + "github.com/stretchr/testify/require" |
| 24 | +) |
| 25 | + |
| 26 | +// Use raw JSON: marshaling a Go string with omitempty would hide the difference |
| 27 | +// between a missing value and an explicitly empty CLI argument. |
| 28 | +func TestPublishEndpoint_ArgumentPresence(t *testing.T) { |
| 29 | + seed := make([]byte, ed25519.SeedSize) |
| 30 | + _, err := rand.Read(seed) |
| 31 | + require.NoError(t, err) |
| 32 | + cfg := &config.Config{JWTPrivateKey: hex.EncodeToString(seed)} |
| 33 | + token, err := generateTestJWTToken(cfg, auth.JWTClaims{ |
| 34 | + AuthMethod: auth.MethodNone, |
| 35 | + Permissions: []auth.Permission{ |
| 36 | + {Action: auth.PermissionActionPublish, ResourcePattern: "example/*"}, |
| 37 | + }, |
| 38 | + }) |
| 39 | + require.NoError(t, err) |
| 40 | + |
| 41 | + for _, prefix := range []string{"/v0", "/v0.1"} { |
| 42 | + for _, field := range []string{"runtimeArguments", "packageArguments"} { |
| 43 | + t.Run(prefix+"/"+field, func(t *testing.T) { |
| 44 | + registry := service.NewRegistryService(database.NewTestDB(t), cfg) |
| 45 | + mux := http.NewServeMux() |
| 46 | + api := humago.New(mux, huma.DefaultConfig("Test API", "1.0.0")) |
| 47 | + v0.RegisterPublishEndpoint(api, prefix, registry, cfg) |
| 48 | + |
| 49 | + for i, tc := range []struct { |
| 50 | + name string |
| 51 | + arg string |
| 52 | + status int |
| 53 | + }{ |
| 54 | + {"empty value", `{"type":"positional","value":""}`, http.StatusOK}, |
| 55 | + {"empty hint", `{"type":"positional","valueHint":""}`, http.StatusOK}, |
| 56 | + {"both empty", `{"type":"positional","value":"","valueHint":""}`, http.StatusOK}, |
| 57 | + {"value", `{"type":"positional","value":"file.txt"}`, http.StatusOK}, |
| 58 | + {"hint", `{"type":"positional","valueHint":"file"}`, http.StatusOK}, |
| 59 | + {"named", `{"type":"named","name":"--file","value":""}`, http.StatusOK}, |
| 60 | + {"missing", `{"type":"positional"}`, http.StatusUnprocessableEntity}, |
| 61 | + {"null", `{"type":"positional","value":null}`, http.StatusUnprocessableEntity}, |
| 62 | + {"invalid type", `{"type":"literal","value":"file.txt"}`, http.StatusUnprocessableEntity}, |
| 63 | + } { |
| 64 | + t.Run(tc.name, func(t *testing.T) { |
| 65 | + name := fmt.Sprintf("example/argument-%d", i) |
| 66 | + body := fmt.Sprintf(`{"$schema":%q,"name":%q,"description":"Argument presence regression test","version":"1.0.0","packages":[{"registryType":"npm","identifier":"test-package","version":"1.0.0","transport":{"type":"stdio"},%q:[%s]}]}`, model.CurrentSchemaURL, name, field, tc.arg) |
| 67 | + req := httptest.NewRequest(http.MethodPost, prefix+"/publish", bytes.NewBufferString(body)) |
| 68 | + req.Header.Set("Content-Type", "application/json") |
| 69 | + req.Header.Set("Authorization", "Bearer "+token) |
| 70 | + rr := httptest.NewRecorder() |
| 71 | + mux.ServeHTTP(rr, req) |
| 72 | + require.Equal(t, tc.status, rr.Code, rr.Body.String()) |
| 73 | + if tc.status != http.StatusOK { |
| 74 | + return |
| 75 | + } |
| 76 | + |
| 77 | + // Read through the service again to exercise JSONB storage decoding, |
| 78 | + // then check that the value survives response serialization too. |
| 79 | + stored, err := registry.GetServerByNameAndVersion(context.Background(), name, "1.0.0", false) |
| 80 | + require.NoError(t, err) |
| 81 | + args := stored.Server.Packages[0].RuntimeArguments |
| 82 | + if field == "packageArguments" { |
| 83 | + args = stored.Server.Packages[0].PackageArguments |
| 84 | + } |
| 85 | + require.Len(t, args, 1) |
| 86 | + encoded, err := json.Marshal(args[0]) |
| 87 | + require.NoError(t, err) |
| 88 | + require.JSONEq(t, tc.arg, string(encoded)) |
| 89 | + require.Contains(t, rr.Body.String(), string(encoded)) |
| 90 | + }) |
| 91 | + } |
| 92 | + }) |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments