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 TestRepoMirrorPushTool_Add(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { assert.Equal(t, http.MethodPost, r.Method) assert.Equal(t, "/api/v1/repos/mathias/infra/push_mirrors", r.URL.Path) w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusCreated) _, _ = w.Write([]byte(`{"id":1,"remote_name":"mirror-github","remote_address":"https://github.com/mathias/infra.git","interval":"8h0m0s","sync_on_commit":true}`)) })) defer srv.Close() tool := tools.NewRepoMirrorPush(gitea.NewClient(srv.URL, "tok"), allowlist.New([]string{"mathias"})) out, err := tool.Call(context.Background(), json.RawMessage(`{ "owner":"mathias","name":"infra","action":"add", "remote_address":"https://github.com/mathias/infra.git", "remote_username":"mathias","remote_password":"secret", "interval":"8h0m0s","sync_on_commit":true }`)) require.NoError(t, err) // password must never appear in output assert.NotContains(t, string(out), "secret") assert.Contains(t, string(out), `"remote_name":"mirror-github"`) } func TestRepoMirrorPushTool_List(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { assert.Equal(t, http.MethodGet, r.Method) assert.Equal(t, "/api/v1/repos/mathias/infra/push_mirrors", r.URL.Path) w.Header().Set("Content-Type", "application/json") _, _ = w.Write([]byte(`[{"id":1,"remote_name":"mirror-github","remote_address":"https://github.com/mathias/infra.git","interval":"8h0m0s","sync_on_commit":true}]`)) })) defer srv.Close() tool := tools.NewRepoMirrorPush(gitea.NewClient(srv.URL, "tok"), allowlist.New([]string{"mathias"})) out, err := tool.Call(context.Background(), json.RawMessage(`{"owner":"mathias","name":"infra","action":"list"}`)) require.NoError(t, err) assert.Contains(t, string(out), `"remote_name":"mirror-github"`) } func TestRepoMirrorPushTool_Delete(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { assert.Equal(t, http.MethodDelete, r.Method) assert.Equal(t, "/api/v1/repos/mathias/infra/push_mirrors/mirror-github", r.URL.Path) w.WriteHeader(http.StatusNoContent) })) defer srv.Close() tool := tools.NewRepoMirrorPush(gitea.NewClient(srv.URL, "tok"), allowlist.New([]string{"mathias"})) out, err := tool.Call(context.Background(), json.RawMessage(`{"owner":"mathias","name":"infra","action":"delete","mirror_name":"mirror-github"}`)) require.NoError(t, err) assert.Contains(t, string(out), "deleted") } func TestRepoMirrorPushTool_DeleteRequiresMirrorName(t *testing.T) { tool := tools.NewRepoMirrorPush(gitea.NewClient("http://unused", ""), allowlist.New([]string{"mathias"})) _, err := tool.Call(context.Background(), json.RawMessage(`{"owner":"mathias","name":"infra","action":"delete"}`)) require.Error(t, err) assert.Contains(t, err.Error(), "mirror_name") } func TestRepoMirrorPushTool_AllowlistRejects(t *testing.T) { tool := tools.NewRepoMirrorPush(gitea.NewClient("http://unused", ""), allowlist.New([]string{"mathias"})) _, err := tool.Call(context.Background(), json.RawMessage(`{"owner":"evil","name":"x","action":"list"}`)) require.Error(t, err) }