94 lines
2.9 KiB
Go
94 lines
2.9 KiB
Go
package gitea_test
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"gitea.d-ma.be/mathias/gitea-mcp/internal/gitea"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestDispatchWorkflow(t *testing.T) {
|
|
var gotBody []byte
|
|
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/o/r/actions/workflows/ci.yml/dispatches", r.URL.Path)
|
|
var err error
|
|
gotBody, err = io.ReadAll(r.Body)
|
|
assert.NoError(t, err)
|
|
w.Header().Set("Location", "/api/v1/repos/o/r/actions/runs/789")
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}))
|
|
defer srv.Close()
|
|
|
|
c := gitea.NewClient(srv.URL, "tok")
|
|
result, err := c.DispatchWorkflow(context.Background(), "o", "r", "ci.yml", gitea.DispatchWorkflowArgs{
|
|
Ref: "main",
|
|
Inputs: map[string]any{"env": "prod"},
|
|
})
|
|
require.NoError(t, err)
|
|
assert.Equal(t, int64(789), result.RunID)
|
|
|
|
var body map[string]any
|
|
require.NoError(t, json.Unmarshal(gotBody, &body))
|
|
assert.Equal(t, "main", body["ref"])
|
|
inputs, ok := body["inputs"].(map[string]any)
|
|
require.True(t, ok)
|
|
assert.Equal(t, "prod", inputs["env"])
|
|
}
|
|
|
|
func TestDispatchWorkflowMissingLocation(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
// 204 but no Location header
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}))
|
|
defer srv.Close()
|
|
|
|
c := gitea.NewClient(srv.URL, "tok")
|
|
_, err := c.DispatchWorkflow(context.Background(), "o", "r", "ci.yml", gitea.DispatchWorkflowArgs{Ref: "main"})
|
|
require.Error(t, err)
|
|
assert.Contains(t, err.Error(), "Location")
|
|
}
|
|
|
|
func TestDispatchWorkflowError404(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusNotFound)
|
|
}))
|
|
defer srv.Close()
|
|
|
|
c := gitea.NewClient(srv.URL, "tok")
|
|
_, err := c.DispatchWorkflow(context.Background(), "o", "r", "ci.yml", gitea.DispatchWorkflowArgs{Ref: "main"})
|
|
require.Error(t, err)
|
|
assert.True(t, errors.Is(err, gitea.ErrNotFound))
|
|
}
|
|
|
|
func TestGetWorkflowRun(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
assert.Equal(t, "/api/v1/repos/o/r/actions/runs/789", r.URL.Path)
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_, _ = w.Write([]byte(`{
|
|
"id":789,
|
|
"status":"completed",
|
|
"conclusion":"success",
|
|
"started_at":"2026-05-04T10:00:00Z",
|
|
"html_url":"http://gitea.example/o/r/actions/runs/789"
|
|
}`))
|
|
}))
|
|
defer srv.Close()
|
|
|
|
c := gitea.NewClient(srv.URL, "tok")
|
|
run, err := c.GetWorkflowRun(context.Background(), "o", "r", 789)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, int64(789), run.ID)
|
|
assert.Equal(t, "completed", run.Status)
|
|
assert.Equal(t, "success", run.Conclusion)
|
|
assert.Equal(t, "2026-05-04T10:00:00Z", run.StartedAt)
|
|
assert.Equal(t, "http://gitea.example/o/r/actions/runs/789", run.HTMLURL)
|
|
}
|