/api/v1 offers the MCP tools as plain HTTP for scripts: GET /api/v1/tools lists what a key may call, POST /api/v1/<tool> calls one with JSON. Each call goes through /mcp internally, so keys, scopes, limits, logging and approvals are the same. /api/v1/docs documents every tool. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
90 lines
3.3 KiB
Go
90 lines
3.3 KiB
Go
package web
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"net/http"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"supermarket/internal/users"
|
|
)
|
|
|
|
func apiDo(h http.Handler, method, path, key, body string) (int, map[string]any) {
|
|
var rd *bytes.Reader
|
|
if body != "" {
|
|
rd = bytes.NewReader([]byte(body))
|
|
} else {
|
|
rd = bytes.NewReader(nil)
|
|
}
|
|
hdr := map[string]string{"Content-Type": "application/json", csrfHeader: ""}
|
|
if key != "" {
|
|
hdr["Authorization"] = "Bearer " + key
|
|
}
|
|
rec := do(h, method, path, rd, hdr)
|
|
var out map[string]any
|
|
if err := json.Unmarshal(rec.Body.Bytes(), &out); err != nil {
|
|
var list []any
|
|
if json.Unmarshal(rec.Body.Bytes(), &list) == nil {
|
|
out = map[string]any{"list": list}
|
|
}
|
|
}
|
|
return rec.Code, out
|
|
}
|
|
|
|
func TestJSONAPI(t *testing.T) {
|
|
s, h := newTestServer(t, nil)
|
|
agent, key := newAgent(t, s, "Script", map[string]users.Access{"content": users.Edit, "market": users.View})
|
|
|
|
// Without a key, or a wrong one: 401. No form token needed.
|
|
if code, _ := apiDo(h, http.MethodGet, "/api/v1/tools", "", ""); code != http.StatusUnauthorized {
|
|
t.Errorf("no key: %d", code)
|
|
}
|
|
if code, _ := apiDo(h, http.MethodPost, "/api/v1/content_drafts", "sm_wrong", "{}"); code != http.StatusUnauthorized {
|
|
t.Errorf("wrong key: %d", code)
|
|
}
|
|
code, out := apiDo(h, http.MethodGet, "/api/v1/tools", key, "")
|
|
raw, _ := json.Marshal(out)
|
|
if code != http.StatusOK || !strings.Contains(string(raw), `"name":"content_create_draft"`) || strings.Contains(string(raw), "seo_next") ||
|
|
!strings.Contains(string(raw), `"input_schema"`) {
|
|
t.Fatalf("tools: %d %s", code, raw)
|
|
}
|
|
// A call, its JSON answer.
|
|
code, out = apiDo(h, http.MethodPost, "/api/v1/content_create_draft", key, `{"channel": "x", "text": "From a script."}`)
|
|
if code != http.StatusOK || out["status"] != "draft" || out["text"] != "From a script." {
|
|
t.Fatalf("create: %d %v", code, out)
|
|
}
|
|
code, out = apiDo(h, http.MethodPost, "/api/v1/content_drafts", key, "")
|
|
if code != http.StatusOK || len(out["list"].([]any)) != 1 {
|
|
t.Errorf("list: %d %v", code, out)
|
|
}
|
|
// Errors.
|
|
if code, out := apiDo(h, http.MethodPost, "/api/v1/content_create_draft", key, `{"channel": "myspace", "text": "x"}`); code != http.StatusUnprocessableEntity || out["error"] == nil {
|
|
t.Errorf("tool says no: %d %v", code, out)
|
|
}
|
|
if code, _ := apiDo(h, http.MethodPost, "/api/v1/seo_next", key, `{}`); code != http.StatusNotFound {
|
|
t.Errorf("not given: %d", code)
|
|
}
|
|
if code, _ := apiDo(h, http.MethodPost, "/api/v1/content_drafts", key, `[1, 2]`); code != http.StatusBadRequest {
|
|
t.Errorf("not an object: %d", code)
|
|
}
|
|
// The same limits and log as MCP.
|
|
if calls, _ := users.RecentCalls(s.DB, agent.ID, 10); len(calls) < 3 || calls[len(calls)-1].Tool != "content_create_draft" {
|
|
t.Errorf("calls %+v", calls)
|
|
}
|
|
users.Pause(s.DB, agent.ID, true, 0, time.Now())
|
|
if code, _ := apiDo(h, http.MethodPost, "/api/v1/content_drafts", key, ""); code != http.StatusForbidden {
|
|
t.Errorf("paused: %d", code)
|
|
}
|
|
|
|
// The docs: every tool, no key.
|
|
rec := do(h, http.MethodGet, "/api/v1/docs", nil, nil)
|
|
body := rec.Body.String()
|
|
for _, want := range []string{`id="content_publish"`, `id="seo_morning_check"`, `id="crm_pending_actions"`, `id="metrics_months"`, "Authorization: Bearer"} {
|
|
if !strings.Contains(body, want) {
|
|
t.Errorf("docs lack %s", want)
|
|
}
|
|
}
|
|
}
|