87 lines
3.5 KiB
Go
87 lines
3.5 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 TestWorkflowRunTriggerSuccess(t *testing.T) {
|
|
// Fake server handles both the repo endpoint (default_branch) and the dispatch endpoint.
|
|
repoHit := false
|
|
dispatchHit := false
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
switch {
|
|
case r.URL.Path == "/api/v1/repos/mathias/myrepo" && r.Method == http.MethodGet:
|
|
repoHit = true
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_, _ = w.Write([]byte(`{"name":"myrepo","full_name":"mathias/myrepo","default_branch":"main"}`))
|
|
case r.URL.Path == "/api/v1/repos/mathias/myrepo/actions/workflows/ci.yml/dispatches" && r.Method == http.MethodPost:
|
|
dispatchHit = true
|
|
w.Header().Set("Location", "/api/v1/repos/mathias/myrepo/actions/runs/42")
|
|
w.WriteHeader(http.StatusNoContent)
|
|
default:
|
|
http.NotFound(w, r)
|
|
}
|
|
}))
|
|
defer srv.Close()
|
|
|
|
tool := tools.NewWorkflowRunTrigger(gitea.NewClient(srv.URL, "tok"), allowlist.New([]string{"mathias"}), srv.URL)
|
|
out, err := tool.Call(context.Background(), json.RawMessage(`{"owner":"mathias","name":"myrepo","workflow":"ci.yml"}`))
|
|
require.NoError(t, err)
|
|
assert.True(t, repoHit, "expected GET /repo for default branch")
|
|
assert.True(t, dispatchHit, "expected POST dispatch")
|
|
|
|
var result map[string]any
|
|
require.NoError(t, json.Unmarshal(out, &result))
|
|
assert.Equal(t, float64(42), result["run_id"])
|
|
assert.Contains(t, result["html_url"], "/mathias/myrepo/actions/runs/42")
|
|
}
|
|
|
|
func TestWorkflowRunTriggerExplicitRef(t *testing.T) {
|
|
repoHit := false
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path == "/api/v1/repos/mathias/myrepo" {
|
|
repoHit = true
|
|
}
|
|
if r.URL.Path == "/api/v1/repos/mathias/myrepo/actions/workflows/ci.yml/dispatches" {
|
|
w.Header().Set("Location", "/api/v1/repos/mathias/myrepo/actions/runs/99")
|
|
w.WriteHeader(http.StatusNoContent)
|
|
return
|
|
}
|
|
http.NotFound(w, r)
|
|
}))
|
|
defer srv.Close()
|
|
|
|
tool := tools.NewWorkflowRunTrigger(gitea.NewClient(srv.URL, "tok"), allowlist.New([]string{"mathias"}), srv.URL)
|
|
out, err := tool.Call(context.Background(), json.RawMessage(`{"owner":"mathias","name":"myrepo","workflow":"ci.yml","ref":"develop"}`))
|
|
require.NoError(t, err)
|
|
assert.False(t, repoHit, "should not call GET /repo when ref is provided")
|
|
|
|
var result map[string]any
|
|
require.NoError(t, json.Unmarshal(out, &result))
|
|
assert.Equal(t, float64(99), result["run_id"])
|
|
}
|
|
|
|
func TestWorkflowRunTriggerAllowlistRejects(t *testing.T) {
|
|
tool := tools.NewWorkflowRunTrigger(gitea.NewClient("http://unused", ""), allowlist.New([]string{"mathias"}), "http://unused")
|
|
_, err := tool.Call(context.Background(), json.RawMessage(`{"owner":"evil","name":"repo","workflow":"ci.yml"}`))
|
|
require.Error(t, err)
|
|
}
|
|
|
|
func TestWorkflowRunTriggerRequiresWorkflow(t *testing.T) {
|
|
// workflow field is present in required schema but let's test empty string fallback guard
|
|
tool := tools.NewWorkflowRunTrigger(gitea.NewClient("http://unused", ""), allowlist.New([]string{"mathias"}), "http://unused")
|
|
_, err := tool.Call(context.Background(), json.RawMessage(`{"owner":"mathias","name":"repo","workflow":""}`))
|
|
require.Error(t, err)
|
|
assert.Contains(t, err.Error(), "workflow")
|
|
}
|