55 lines
1.9 KiB
Go
55 lines
1.9 KiB
Go
package tools_test
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"gitea.d-ma.be/mathias/gitea-mcp/internal/allowlist"
|
|
"gitea.d-ma.be/mathias/gitea-mcp/internal/gitea"
|
|
"gitea.d-ma.be/mathias/gitea-mcp/internal/tools"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
const commentFixture = `{"id":7,"body":"hello","html_url":"http://example.com/issues/42#comment-7"}`
|
|
|
|
func TestIssueCommentAppliesFooter(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/issues/42/comments", r.URL.Path)
|
|
var err error
|
|
captured, err = io.ReadAll(r.Body)
|
|
require.NoError(t, err)
|
|
w.WriteHeader(http.StatusCreated)
|
|
_, _ = w.Write([]byte(commentFixture))
|
|
}))
|
|
defer srv.Close()
|
|
|
|
tool := tools.NewIssueComment(gitea.NewClient(srv.URL, "tok"), allowlist.New([]string{"o"}))
|
|
ctx := callerContext("mathiasbq")
|
|
_, err := tool.Call(ctx, json.RawMessage(`{"owner":"o","name":"r","number":42,"body":"hello"}`))
|
|
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 TestIssueCommentAllowlistRejects(t *testing.T) {
|
|
tool := tools.NewIssueComment(gitea.NewClient("http://unused", ""), allowlist.New([]string{"allowed"}))
|
|
_, err := tool.Call(context.Background(), json.RawMessage(`{"owner":"evil","name":"r","number":1,"body":"hi"}`))
|
|
require.Error(t, err)
|
|
}
|
|
|
|
func TestIssueCommentRequiresBody(t *testing.T) {
|
|
tool := tools.NewIssueComment(gitea.NewClient("http://unused", ""), allowlist.New([]string{"o"}))
|
|
_, err := tool.Call(context.Background(), json.RawMessage(`{"owner":"o","name":"r","number":1,"body":""}`))
|
|
require.Error(t, err)
|
|
assert.ErrorIs(t, err, gitea.ErrValidation)
|
|
}
|