test: expect the MCP view of BM answers
- viaMCP, includeBody and the material views (no body unless asked) - jz.bm.mcp-links: absolute url / *_url under MCP only - jz.bm.manage keyed per user for dispatched calls - bm_material and bm_materials in the pinned tool list Compile-only stubs; the tests fail on their assertions.
This commit is contained in:
144
mcp_test.go
144
mcp_test.go
@@ -1,13 +1,19 @@
|
||||
package bm_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"reflect"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.golem15.com/golem15/summercms/modules/backpack"
|
||||
"git.golem15.com/golem15/summercms/modules/bouncer"
|
||||
"git.golem15.com/golem15/summercms/modules/pact"
|
||||
"git.golem15.com/golem15/summercms/modules/pier"
|
||||
"git.golem15.com/golem15/summercms/modules/surf"
|
||||
@@ -47,6 +53,8 @@ var pinnedTools = []string{
|
||||
"bm_deadlines",
|
||||
"bm_mark_reading_owned",
|
||||
"bm_mark_reading_read",
|
||||
"bm_material",
|
||||
"bm_materials",
|
||||
"bm_meetings",
|
||||
"bm_readings",
|
||||
"bm_schedule",
|
||||
@@ -63,6 +71,8 @@ var studentQueryKeys = map[string][]string{
|
||||
"schedule": {"from", "to"},
|
||||
"deadlines": {"from", "to"},
|
||||
"readings": {"course"},
|
||||
"materials": {"course", "meeting", "tag", "type", "page", "per_page"},
|
||||
"material": {"include_body"},
|
||||
}
|
||||
|
||||
// bmTools returns jz.bm's declared tools.
|
||||
@@ -240,3 +250,137 @@ func TestTokenMirrorRoutes(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// dispatched returns r as pier dispatches it in-process: the api_token
|
||||
// marker with principal id on its context.
|
||||
func dispatched(r *http.Request, id uint) *http.Request {
|
||||
return r.WithContext(bouncer.WithDispatch(r.Context(), &bouncer.Dispatch{
|
||||
Guards: []string{"api_token"},
|
||||
Principal: &bouncer.Principal{ID: id},
|
||||
}))
|
||||
}
|
||||
|
||||
// TestMCPLinks (D-16): under MCP every string under url or *_url that starts
|
||||
// with a single / becomes absolute; other keys, protocol-relative and
|
||||
// absolute URLs, error answers and plain HTTP answers are left alone.
|
||||
func TestMCPLinks(t *testing.T) {
|
||||
const answer = `{"data":{"url":"/storage/x","files":[{"url":"/storage/y","name":"/n"}],"syllabus_url":"/sylabusy/z.pdf","link":"https://x.test/a","other":"/keep","cdn_url":"//cdn.test/a","abs_url":"https://y.test/b","count":12345678901234567890,"nested":{"items":[{"thumb_url":"/t.png"}]}}}`
|
||||
mw := (&bm.Plugin{}).Middlewares()[bm.MiddlewareMCPLinks]
|
||||
if mw == nil {
|
||||
t.Fatalf("%s is not exported", bm.MiddlewareMCPLinks)
|
||||
}
|
||||
handler := func(status int, body string) http.Handler {
|
||||
return mw(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Header().Set("Content-Length", strconv.Itoa(len(body)))
|
||||
w.WriteHeader(status)
|
||||
_, _ = w.Write([]byte(body))
|
||||
}))
|
||||
}
|
||||
|
||||
t.Run("plain HTTP untouched", func(t *testing.T) {
|
||||
rec := httptest.NewRecorder()
|
||||
handler(http.StatusOK, answer).ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/", nil))
|
||||
if rec.Body.String() != answer {
|
||||
t.Errorf("plain answer = %s, want it unchanged", rec.Body.String())
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("MCP answer absolute", func(t *testing.T) {
|
||||
rec := httptest.NewRecorder()
|
||||
handler(http.StatusOK, answer).ServeHTTP(rec, dispatched(httptest.NewRequest(http.MethodGet, "/", nil), 7))
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d", rec.Code)
|
||||
}
|
||||
if cl := rec.Header().Get("Content-Length"); cl != strconv.Itoa(rec.Body.Len()) {
|
||||
t.Errorf("Content-Length = %s, body is %d bytes", cl, rec.Body.Len())
|
||||
}
|
||||
dec := json.NewDecoder(bytes.NewReader(rec.Body.Bytes()))
|
||||
dec.UseNumber()
|
||||
var got struct {
|
||||
Data map[string]any `json:"data"`
|
||||
}
|
||||
if err := dec.Decode(&got); err != nil {
|
||||
t.Fatalf("decode %s: %v", rec.Body.String(), err)
|
||||
}
|
||||
d := got.Data
|
||||
want := map[string]string{
|
||||
"url": "http://example.com/storage/x",
|
||||
"syllabus_url": "http://example.com/sylabusy/z.pdf",
|
||||
"link": "https://x.test/a",
|
||||
"other": "/keep",
|
||||
"cdn_url": "//cdn.test/a",
|
||||
"abs_url": "https://y.test/b",
|
||||
}
|
||||
for k, v := range want {
|
||||
if d[k] != v {
|
||||
t.Errorf("%s = %v, want %s", k, d[k], v)
|
||||
}
|
||||
}
|
||||
file := d["files"].([]any)[0].(map[string]any)
|
||||
if file["url"] != "http://example.com/storage/y" || file["name"] != "/n" {
|
||||
t.Errorf("files[0] = %v", file)
|
||||
}
|
||||
thumb := d["nested"].(map[string]any)["items"].([]any)[0].(map[string]any)
|
||||
if thumb["thumb_url"] != "http://example.com/t.png" {
|
||||
t.Errorf("nested thumb_url = %v", thumb["thumb_url"])
|
||||
}
|
||||
if n, _ := d["count"].(json.Number); n.String() != "12345678901234567890" {
|
||||
t.Errorf("count = %v, want the number kept exactly", d["count"])
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("MCP forwarded scheme", func(t *testing.T) {
|
||||
rec := httptest.NewRecorder()
|
||||
r := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||
r.Host = "bm.example.test"
|
||||
r.Header.Set("X-Forwarded-Proto", "https")
|
||||
handler(http.StatusOK, `{"url":"/a"}`).ServeHTTP(rec, dispatched(r, 7))
|
||||
if !strings.Contains(rec.Body.String(), `"https://bm.example.test/a"`) {
|
||||
t.Errorf("answer = %s", rec.Body.String())
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("MCP error untouched", func(t *testing.T) {
|
||||
const notFound = `{"error":"not_found","url":"/x"}`
|
||||
rec := httptest.NewRecorder()
|
||||
handler(http.StatusNotFound, notFound).ServeHTTP(rec, dispatched(httptest.NewRequest(http.MethodGet, "/", nil), 7))
|
||||
if rec.Code != http.StatusNotFound || rec.Body.String() != notFound {
|
||||
t.Errorf("error answer = %d %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// TestManageBucketKey (research Pitfall 6): a dispatched management call is
|
||||
// throttled per user, a bearer request per token hash, a bare one per IP;
|
||||
// the token twins are throttled per user.
|
||||
func TestManageBucketKey(t *testing.T) {
|
||||
buckets := (&bm.Plugin{}).Buckets()
|
||||
manage, ok := buckets["jz.bm.manage"]
|
||||
if !ok {
|
||||
t.Fatal("jz.bm.manage is not declared")
|
||||
}
|
||||
if manage.Max != 120 {
|
||||
t.Errorf("jz.bm.manage Max = %d, want 120", manage.Max)
|
||||
}
|
||||
if got := manage.Key(dispatched(httptest.NewRequest(http.MethodGet, "/", nil), 7)); got != "jz.bm.manage.user|7" {
|
||||
t.Errorf("dispatched key = %q, want jz.bm.manage.user|7", got)
|
||||
}
|
||||
bearer := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||
bearer.Header.Set("Authorization", "Bearer usr_secret")
|
||||
if got := manage.Key(bearer); !strings.HasPrefix(got, "jz.bm.manage.token|") || strings.Contains(got, "usr_secret") {
|
||||
t.Errorf("bearer key = %q, want the token hash", got)
|
||||
}
|
||||
if got := manage.Key(httptest.NewRequest(http.MethodGet, "/", nil)); !strings.HasPrefix(got, "jz.bm.manage.ip|") {
|
||||
t.Errorf("bare key = %q, want the IP", got)
|
||||
}
|
||||
token, ok := buckets["jz.bm.token"]
|
||||
if !ok {
|
||||
t.Fatal("jz.bm.token is not declared")
|
||||
}
|
||||
r := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||
r = r.WithContext(bouncer.WithUser(context.Background(), &bouncer.Principal{ID: 7}))
|
||||
if got := token.Key(r); got != "jz.bm.token|u:7" {
|
||||
t.Errorf("token twin key = %q, want jz.bm.token|u:7", got)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user