forked from Gitlink/gitlink-cli
180 lines
4.8 KiB
Go
180 lines
4.8 KiB
Go
package webhook
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/gitlink-org/gitlink-cli/internal/client"
|
|
"github.com/gitlink-org/gitlink-cli/shortcuts/common"
|
|
)
|
|
|
|
func TestWebhookList(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != "GET" || r.URL.Path != "/v1/owner/repo/webhooks.json" {
|
|
t.Fatalf("unexpected request: %s %s", r.Method, r.URL.Path)
|
|
}
|
|
writeJSON(t, w, map[string]interface{}{
|
|
"total_count": 1,
|
|
"webhooks": []map[string]interface{}{
|
|
{"id": 1, "url": "https://example.com/hook", "is_active": true},
|
|
},
|
|
})
|
|
}))
|
|
defer server.Close()
|
|
|
|
err := runWebhookShortcut(t, server, "list", map[string]string{})
|
|
if err != nil {
|
|
t.Fatalf("list shortcut failed: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestWebhookCreate(t *testing.T) {
|
|
var payload map[string]interface{}
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method == "POST" && r.URL.Path == "/v1/owner/repo/webhooks.json" {
|
|
payload = decodeJSON(t, r)
|
|
writeJSON(t, w, map[string]interface{}{
|
|
"id": 42,
|
|
"url": "https://example.com/hook",
|
|
"active": true,
|
|
})
|
|
return
|
|
}
|
|
t.Fatalf("unexpected request: %s %s", r.Method, r.URL.Path)
|
|
}))
|
|
defer server.Close()
|
|
|
|
err := runWebhookShortcut(t, server, "create", map[string]string{
|
|
"url": "https://example.com/hook",
|
|
"events": "push,create",
|
|
"secret": "mysecret",
|
|
"active": "true",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create shortcut failed: %v", err)
|
|
}
|
|
|
|
assertEqual(t, payload["url"], "https://example.com/hook")
|
|
assertEqual(t, payload["active"], true)
|
|
assertEqual(t, payload["type"], "gitea")
|
|
assertEqual(t, payload["secret"], "mysecret")
|
|
|
|
events, ok := payload["events"].([]interface{})
|
|
if !ok {
|
|
t.Fatalf("events should be a slice, got %T", payload["events"])
|
|
}
|
|
if len(events) != 2 {
|
|
t.Fatalf("expected 2 events, got %d", len(events))
|
|
}
|
|
}
|
|
|
|
func TestWebhookDelete(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method == "DELETE" && r.URL.Path == "/v1/owner/repo/webhooks/42.json" {
|
|
writeJSON(t, w, map[string]interface{}{
|
|
"status": 0,
|
|
"message": "success",
|
|
})
|
|
return
|
|
}
|
|
t.Fatalf("unexpected request: %s %s", r.Method, r.URL.Path)
|
|
}))
|
|
defer server.Close()
|
|
|
|
err := runWebhookShortcut(t, server, "delete", map[string]string{
|
|
"id": "42",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("delete shortcut failed: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestWebhookTest(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method == "POST" && r.URL.Path == "/v1/owner/repo/webhooks/42/tests.json" {
|
|
writeJSON(t, w, map[string]interface{}{
|
|
"status": 0,
|
|
"message": "success",
|
|
})
|
|
return
|
|
}
|
|
t.Fatalf("unexpected request: %s %s", r.Method, r.URL.Path)
|
|
}))
|
|
defer server.Close()
|
|
|
|
err := runWebhookShortcut(t, server, "test", map[string]string{
|
|
"id": "42",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("test shortcut failed: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestWebhookUpdateRequiresAtLeastOneField(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
t.Fatal("no request should be made when no fields are provided")
|
|
}))
|
|
defer server.Close()
|
|
|
|
err := runWebhookShortcut(t, server, "update", map[string]string{
|
|
"id": "42",
|
|
})
|
|
if err == nil {
|
|
t.Fatal("expected error when no update fields provided, got nil")
|
|
}
|
|
}
|
|
|
|
func runWebhookShortcut(t *testing.T, server *httptest.Server, name string, args map[string]string) error {
|
|
t.Helper()
|
|
shortcut := findWebhookShortcut(t, name)
|
|
ctx := &common.RuntimeContext{
|
|
Client: &client.Client{
|
|
HTTP: server.Client(),
|
|
BaseURL: server.URL,
|
|
},
|
|
Owner: "owner",
|
|
Repo: "repo",
|
|
Format: "json",
|
|
Args: args,
|
|
}
|
|
return shortcut.Run(ctx)
|
|
}
|
|
|
|
func findWebhookShortcut(t *testing.T, name string) *common.Shortcut {
|
|
t.Helper()
|
|
for _, shortcut := range Shortcuts() {
|
|
if shortcut.Name == name {
|
|
return shortcut
|
|
}
|
|
}
|
|
t.Fatalf("shortcut %q not found", name)
|
|
return nil
|
|
}
|
|
|
|
func decodeJSON(t *testing.T, r *http.Request) map[string]interface{} {
|
|
t.Helper()
|
|
var payload map[string]interface{}
|
|
if err := json.NewDecoder(r.Body).Decode(&payload); err != nil {
|
|
t.Fatalf("failed to decode request body: %v", err)
|
|
}
|
|
return payload
|
|
}
|
|
|
|
func writeJSON(t *testing.T, w http.ResponseWriter, payload interface{}) {
|
|
t.Helper()
|
|
w.Header().Set("Content-Type", "application/json")
|
|
if err := json.NewEncoder(w).Encode(payload); err != nil {
|
|
t.Fatalf("failed to write response: %v", err)
|
|
}
|
|
}
|
|
|
|
func assertEqual(t *testing.T, got interface{}, want interface{}) {
|
|
t.Helper()
|
|
if fmt.Sprintf("%v", got) != fmt.Sprintf("%v", want) {
|
|
t.Fatalf("got %v (%T), want %v (%T)", got, got, want, want)
|
|
}
|
|
}
|