gitlink-cli/shortcuts/webhook/webhook_test.go

156 lines
4.2 KiB
Go

package webhook
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
"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" || !strings.Contains(r.URL.Path, "/webhooks") {
t.Fatalf("unexpected request: %s %s", r.Method, r.URL.Path)
}
common.WriteJSON(t, w, map[string]interface{}{
"total_count": float64(1),
"webhooks": []interface{}{
map[string]interface{}{
"id": float64(1),
"url": "http://example.com/hook",
"active": true,
},
},
})
}))
defer server.Close()
ctx := common.NewTestContext(t, server, "owner", "repo", map[string]string{})
err := common.RunShortcut(t, Shortcuts(), "list", ctx)
if err != nil {
t.Fatalf("list failed: %v", err)
}
}
func TestWebhookCreate(t *testing.T) {
var requestMethod string
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
requestMethod = r.Method
common.WriteJSON(t, w, map[string]interface{}{
"id": float64(2),
"url": "http://example.com/hook",
"active": true,
})
}))
defer server.Close()
ctx := common.NewTestContext(t, server, "owner", "repo", map[string]string{
"url": "http://example.com/hook",
"events": "push,create",
})
err := common.RunShortcut(t, Shortcuts(), "create", ctx)
if err != nil {
t.Fatalf("create failed: %v", err)
}
if requestMethod != "POST" {
t.Errorf("expected POST, got %s", requestMethod)
}
}
func TestWebhookView(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if !strings.Contains(r.URL.Path, "/webhooks/1") {
t.Fatalf("unexpected path: %s", r.URL.Path)
}
common.WriteJSON(t, w, map[string]interface{}{
"id": float64(1),
"url": "http://example.com/hook",
"content_type": "json",
"http_method": "POST",
"active": true,
"branch_filter": "*",
"events": []interface{}{"push"},
})
}))
defer server.Close()
ctx := common.NewTestContext(t, server, "owner", "repo", map[string]string{
"id": "1",
})
err := common.RunShortcut(t, Shortcuts(), "view", ctx)
if err != nil {
t.Fatalf("view failed: %v", err)
}
}
func TestWebhookUpdate(t *testing.T) {
var methods []string
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
methods = append(methods, r.Method)
switch r.Method {
case "GET":
common.WriteJSON(t, w, map[string]interface{}{
"id": float64(1),
"url": "http://old.example.com",
"content_type": "json",
"http_method": "POST",
"secret": "oldsecret",
"branch_filter": "*",
"active": true,
"events": []interface{}{"push"},
})
case "PUT":
common.WriteJSON(t, w, map[string]interface{}{
"id": float64(1),
"url": "http://new.example.com",
})
}
}))
defer server.Close()
ctx := common.NewTestContext(t, server, "owner", "repo", map[string]string{
"id": "1",
"url": "http://new.example.com",
})
err := common.RunShortcut(t, Shortcuts(), "update", ctx)
if err != nil {
t.Fatalf("update failed: %v", err)
}
// Should have made two requests: GET (fetch current) then PUT (update)
if len(methods) != 2 {
t.Fatalf("expected 2 requests, got %d: %v", len(methods), methods)
}
if methods[0] != "GET" {
t.Errorf("first request should be GET, got %s", methods[0])
}
if methods[1] != "PUT" {
t.Errorf("second request should be PUT, got %s", methods[1])
}
}
func TestWebhookDelete(t *testing.T) {
var requestMethod string
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
requestMethod = r.Method
common.WriteJSON(t, w, map[string]interface{}{
"status": float64(0),
"message": "success",
})
}))
defer server.Close()
ctx := common.NewTestContext(t, server, "owner", "repo", map[string]string{
"id": "1",
})
err := common.RunShortcut(t, Shortcuts(), "delete", ctx)
if err != nil {
t.Fatalf("delete failed: %v", err)
}
if requestMethod != "DELETE" {
t.Errorf("expected DELETE, got %s", requestMethod)
}
}