53 lines
1.8 KiB
Go
53 lines
1.8 KiB
Go
package tools_test
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"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"
|
|
)
|
|
|
|
func TestFileDeleteSuccess(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
assert.Equal(t, http.MethodDelete, r.Method)
|
|
w.WriteHeader(http.StatusOK)
|
|
_, _ = w.Write([]byte(`{"content":null,"commit":{"sha":"cmt1","html_url":"http://example.com/commit/cmt1"}}`))
|
|
}))
|
|
defer srv.Close()
|
|
|
|
tool := tools.NewFileDelete(gitea.NewClient(srv.URL, "tok"), allowlist.New([]string{"owner"}))
|
|
out, err := tool.Call(context.Background(), json.RawMessage(`{
|
|
"owner":"owner","name":"repo","path":"src/old.go",
|
|
"branch":"main","message":"remove old.go","sha":"blobsha"
|
|
}`))
|
|
require.NoError(t, err)
|
|
|
|
var result map[string]any
|
|
require.NoError(t, json.Unmarshal(out, &result))
|
|
assert.Equal(t, "cmt1", result["commit_sha"])
|
|
}
|
|
|
|
func TestFileDeleteRequiresSha(t *testing.T) {
|
|
tool := tools.NewFileDelete(gitea.NewClient("http://unused", ""), allowlist.New([]string{"owner"}))
|
|
_, err := tool.Call(context.Background(), json.RawMessage(`{
|
|
"owner":"owner","name":"repo","path":"f.go","branch":"main","message":"rm"
|
|
}`))
|
|
require.Error(t, err)
|
|
assert.ErrorIs(t, err, gitea.ErrValidation)
|
|
}
|
|
|
|
func TestFileDeleteAllowlistRejects(t *testing.T) {
|
|
tool := tools.NewFileDelete(gitea.NewClient("http://unused", ""), allowlist.New([]string{"allowed"}))
|
|
_, err := tool.Call(context.Background(), json.RawMessage(`{
|
|
"owner":"evil","name":"repo","path":"f.go","branch":"main","message":"rm","sha":"abc"
|
|
}`))
|
|
require.Error(t, err)
|
|
}
|