Files
gitea-mcp/internal/tools/pr_create_test.go
T
mathiasandClaude Opus 4.8 4c1f36f9eb
CD / Lint / Test / Vet (push) Successful in 7s
CD / Build & Import (push) Successful in 23s
CD / Deploy via GitOps (push) Has been skipped
chore(rename): module gitea.d-ma.be → git.d-ma.be/mathias/gitea-mcp (#39)
Gitea host renamed (infra ADR-0004); the mcp-chassis dep already migrated
(3329ff3), so the sequencing gate is clear. `go mod edit -module` + bulk import
rewrite across all .go files. gitea-mcp is a server binary (not an imported
library), so no downstream consumers break. build + task check green.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-03 22:48:26 +02:00

108 lines
3.3 KiB
Go

package tools_test
import (
"context"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"git.d-ma.be/mathias/gitea-mcp/internal/allowlist"
"git.d-ma.be/mathias/gitea-mcp/internal/auth"
"git.d-ma.be/mathias/gitea-mcp/internal/gitea"
"git.d-ma.be/mathias/gitea-mcp/internal/tools"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const prFixture = `{
"number": 3,
"title": "My PR",
"body": "description",
"html_url": "http://example.com/pulls/3",
"state": "open",
"draft": false,
"head": {"ref": "feat/new"},
"base": {"ref": "main"}
}`
func callerContext(user string) context.Context {
var capturedCtx context.Context
h := auth.CallerMiddleware(http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) {
capturedCtx = r.Context()
}))
req := httptest.NewRequest("POST", "/", nil)
if user != "" {
req.Header.Set("X-Auth-Request-User", user)
}
h.ServeHTTP(httptest.NewRecorder(), req)
return capturedCtx
}
func TestPRCreateAppliesIdentityFooter(t *testing.T) {
var captured []byte
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "/api/v1/repos/o/r/pulls", r.URL.Path)
var err error
captured, err = io.ReadAll(r.Body)
require.NoError(t, err)
w.WriteHeader(http.StatusCreated)
_, _ = w.Write([]byte(prFixture))
}))
defer srv.Close()
tool := tools.NewPRCreate(gitea.NewClient(srv.URL, "tok"), allowlist.New([]string{"o"}))
ctx := callerContext("mathiasbq")
_, err := tool.Call(ctx, json.RawMessage(`{
"owner":"o","name":"r","title":"My PR","body":"description","head":"feat/new","base":"main"
}`))
require.NoError(t, err)
var payload map[string]any
require.NoError(t, json.Unmarshal(captured, &payload))
body, _ := payload["body"].(string)
assert.Contains(t, body, "_Created via git-mcp on behalf of @mathiasbq_")
}
func TestPRCreateNoFooterWhenCallerEmpty(t *testing.T) {
var captured []byte
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var err error
captured, err = io.ReadAll(r.Body)
require.NoError(t, err)
w.WriteHeader(http.StatusCreated)
_, _ = w.Write([]byte(prFixture))
}))
defer srv.Close()
tool := tools.NewPRCreate(gitea.NewClient(srv.URL, "tok"), allowlist.New([]string{"o"}))
_, err := tool.Call(context.Background(), json.RawMessage(`{
"owner":"o","name":"r","title":"My PR","body":"description","head":"feat/new","base":"main"
}`))
require.NoError(t, err)
var payload map[string]any
require.NoError(t, json.Unmarshal(captured, &payload))
body, _ := payload["body"].(string)
assert.False(t, strings.Contains(body, "_Created via git-mcp on behalf of"), "footer should not be present when caller is empty")
}
func TestPRCreateAllowlistRejects(t *testing.T) {
tool := tools.NewPRCreate(gitea.NewClient("http://unused", ""), allowlist.New([]string{"allowed"}))
_, err := tool.Call(context.Background(), json.RawMessage(`{
"owner":"evil","name":"r","title":"T","head":"feat/x","base":"main"
}`))
require.Error(t, err)
}
func TestPRCreateRequiresTitle(t *testing.T) {
tool := tools.NewPRCreate(gitea.NewClient("http://unused", ""), allowlist.New([]string{"o"}))
_, err := tool.Call(context.Background(), json.RawMessage(`{
"owner":"o","name":"r","title":"","head":"feat/x","base":"main"
}`))
require.Error(t, err)
assert.ErrorIs(t, err, gitea.ErrValidation)
}