verae-mcp-timestamping-serv.../internal/tools/bearer_test.go
George Lambert 369f9210bd
Some checks failed
test-and-deploy / test (push) Failing after 12m11s
test-and-deploy / deploy-138 (push) Has been cancelled
fix(auth): clear public write errors and Grok client path
Public DEC-654 still requires a caller bearer; anonymous session now
returns that guidance as a tool error instead of silent valid:false.
Error text names JWT and API key, Grok bearer_token_file, and local
stdio. BearerFromRequest also accepts X-Api-Key / X-Verae-Key.
Document Grok stdio vs public URL; add scripts/sync-mcp-bearer.sh.
2026-10-12 14:09:22 -04:00

60 lines
1.6 KiB
Go

package tools
import (
"net/http"
"strings"
"testing"
"github.com/modelcontextprotocol/go-sdk/mcp"
)
func TestLooksLikeCID(t *testing.T) {
t.Parallel()
cases := []struct {
id string
want bool
}{
{"", false},
{"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", false},
{"bafyreigdyrzt5sfp7udm7hu76uh7b314167c576e3mgkudqjceaaeeooma", true},
{"QmYwAPJzv5CZsnA625s3Xf2nemtYkPpHdWQz79JREaLEkF", true},
{"not-a-local-receipt", true},
}
for _, tc := range cases {
if got := looksLikeCID(tc.id); got != tc.want {
t.Fatalf("looksLikeCID(%q)=%v want %v", tc.id, got, tc.want)
}
}
}
func TestBearerFromRequest(t *testing.T) {
t.Parallel()
req := &mcp.CallToolRequest{Extra: &mcp.RequestExtra{Header: http.Header{}}}
if got := BearerFromRequest(req); got != "" {
t.Fatalf("empty headers: %q", got)
}
req.Extra.Header.Set("Authorization", "Bearer jwt-one")
if got := BearerFromRequest(req); got != "jwt-one" {
t.Fatalf("Authorization: %q", got)
}
req.Extra.Header = http.Header{}
req.Extra.Header.Set("X-Api-Key", "key-two")
if got := BearerFromRequest(req); got != "key-two" {
t.Fatalf("X-Api-Key: %q", got)
}
req.Extra.Header = http.Header{}
req.Extra.Header.Set("Authorization", "bearer spaced ")
if got := BearerFromRequest(req); got != "spaced" {
t.Fatalf("case/space: %q", got)
}
}
func TestErrWriteNeedsKeyMentionsJWTAndGrok(t *testing.T) {
t.Parallel()
msg := ErrWriteNeedsKey.Error()
for _, want := range []string{"Authorization: Bearer", "JWT", "bearer_token_file", "stdio"} {
if !strings.Contains(msg, want) {
t.Fatalf("ErrWriteNeedsKey missing %q: %s", want, msg)
}
}
}