Skip to content
quarry Artifact [a1c8ec6779]
Login

Artifact [a1c8ec6779]

Artifact a1c8ec6779c78d5010a755dd6387e426d79c720b49e4c6e224b176ac5fd71556:


package oltp

import (
	"errors"
	"strings"
	"testing"
)

// Changing an endpoint's URL keeps its identity: the id, the secret,
// the events and the delivery history all stay, and the failure count
// and an automatic pause start over because the owner has pointed the
// endpoint somewhere new.
func TestSetWebhookURLKeepsSecretEventsAndHistory(t *testing.T) {
	d := excDB(t)
	w, err := d.AddWebhook("alpha", "https://old.example.test/h", "ticket")
	if err != nil {
		t.Fatal(err)
	}
	d.RecordDelivery(Delivery{Sub: w.ID, Repo: "alpha", Kind: "ticket.new", Status: 200})
	for i := 0; i < PauseAfter; i++ {
		d.SettleDelivery(w.ID, false, "gone")
	}
	was, err := d.SetWebhookURL("alpha", w.ID, "https://new.example.test/h")
	if err != nil {
		t.Fatalf("FAIL: %v", err)
	}
	if was != "https://old.example.test/h" {
		t.Errorf("FAIL: was = %q", was)
	}
	got, ok := d.WebhookByID(w.ID)
	if !ok {
		t.Fatal("FAIL: the endpoint is gone")
	}
	if got.URL != "https://new.example.test/h" {
		t.Errorf("FAIL: url = %q", got.URL)
	}
	if got.Secret != w.Secret || got.Events != "ticket" || got.Created != w.Created {
		t.Errorf("FAIL: identity changed: %+v", got)
	}
	if !got.Active || got.FailStreak != 0 || got.PausedAt != "" || got.PausedWhy != "" {
		t.Errorf("FAIL: the automatic pause survived: %+v", got)
	}
	if ds, _ := d.Deliveries(w.ID, 10); len(ds) != 1 {
		t.Errorf("FAIL: history = %d rows, want 1", len(ds))
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// A person's own pause is a decision, not a fault; a new URL does not
// undo it.
func TestSetWebhookURLLeavesAManualPause(t *testing.T) {
	d := excDB(t)
	w := addHook(t, d, "alpha")
	d.SetWebhookActive("alpha", w.ID, false)
	if _, err := d.SetWebhookURL("alpha", w.ID, "https://new.example.test/h"); err != nil {
		t.Fatal(err)
	}
	if got, _ := d.WebhookByID(w.ID); got.Active {
		t.Errorf("FAIL: a manual pause was lifted: %+v", got)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// The refusals are the creation's, word for word: one table of bad
// URLs run through both doors.
func TestSetWebhookURLRefusesWhatAddRefuses(t *testing.T) {
	d := excDB(t)
	w := addHook(t, d, "alpha")
	bad := []string{
		"", "not a url", "ftp://example.test/h", "http://example.test/h",
		"http://127.evil.example/h", "javascript:alert(1)", "https://",
		"https://example.test/" + strings.Repeat("a", 400),
	}
	for _, u := range bad {
		_, addErr := d.AddWebhook("alpha", u, "*")
		_, setErr := d.SetWebhookURL("alpha", w.ID, u)
		if addErr == nil || setErr == nil {
			t.Errorf("FAIL: %.40q: add err=%v set err=%v, both must refuse", u, addErr, setErr)
			continue
		}
		if addErr.Error() != setErr.Error() {
			t.Errorf("FAIL: %.40q refused differently: add %q, set %q", u, addErr, setErr)
		}
	}
	if got, _ := d.WebhookByID(w.ID); got.URL != "https://example.test/h" {
		t.Errorf("FAIL: a refused change moved the url: %q", got.URL)
	}
	// What creation accepts, a change accepts.
	for _, u := range []string{"https://hooks.example.test/x?k=1", "http://localhost:9000/h", "http://127.0.0.1/h"} {
		if _, err := d.SetWebhookURL("alpha", w.ID, u); err != nil {
			t.Errorf("FAIL: %q refused on change: %v", u, err)
		}
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// An id names a row the whole forge shares: another repository's
// endpoint, or none, is not found.
func TestSetWebhookURLIsRepositoryScoped(t *testing.T) {
	d := excDB(t)
	w := addHook(t, d, "alpha")
	for _, c := range []struct {
		repo string
		id   int64
	}{{"beta", w.ID}, {"alpha", w.ID + 99}} {
		if _, err := d.SetWebhookURL(c.repo, c.id, "https://new.example.test/h"); err == nil {
			t.Errorf("FAIL: %s/%d changed", c.repo, c.id)
		}
	}
	if got, _ := d.WebhookByID(w.ID); got.URL != "https://example.test/h" {
		t.Errorf("FAIL: url = %q", got.URL)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// A refusal names the scheme and host, never a path, query or
// credentials: it ends up in API errors, redirects and access logs.
func TestAWebhookRefusalNeverEchoesTheQuery(t *testing.T) {
	d := excDB(t)
	w := addHook(t, d, "alpha")
	for _, u := range []string{
		"http://user:[email protected]/in?token=s3cret",
		"ftp://files.example.test/in?token=s3cret",
		"https://" + strings.Repeat("a", 300) + ".test/in?token=s3cret",
	} {
		_, addErr := d.AddWebhook("alpha", u, "*")
		_, setErr := d.SetWebhookURL("alpha", w.ID, u)
		for _, err := range []error{addErr, setErr} {
			if err == nil {
				t.Errorf("FAIL: %.30q accepted", u)
				continue
			}
			for _, leak := range []string{"s3cret", "hunter2", "token=", "/in"} {
				if strings.Contains(err.Error(), leak) {
					t.Errorf("FAIL: the refusal carries %q: %.200s", leak, err)
				}
			}
		}
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// Every change to an endpoint names its repository: an id is a row the
// whole forge shares, and an admin of one repository must not move,
// pause or remove another's. A foreign id is not found and changes
// nothing.
func TestWebhookChangesAreRepositoryScoped(t *testing.T) {
	d := excDB(t)
	w := addHook(t, d, "alpha")
	ops := map[string]func() error{
		"events": func() error { return d.SetWebhookEvents("beta", w.ID, "ticket") },
		"pause":  func() error { return d.SetWebhookActive("beta", w.ID, false) },
		"resume": func() error { return d.SetWebhookActive("beta", w.ID, true) },
		"delete": func() error { return d.DeleteWebhook("beta", w.ID) },
	}
	for name, run := range ops {
		if err := run(); !errors.Is(err, ErrNoWebhook) {
			t.Errorf("FAIL: %s with a foreign repository: %v, want not found", name, err)
		}
	}
	got, ok := d.WebhookByID(w.ID)
	if !ok || got.Events != "*" || !got.Active {
		t.Errorf("FAIL: a foreign change landed: %+v ok=%v", got, ok)
	}
	// The owner's own calls still work.
	if err := d.SetWebhookEvents("alpha", w.ID, "ticket"); err != nil {
		t.Errorf("FAIL: events: %v", err)
	}
	if err := d.SetWebhookActive("alpha", w.ID, false); err != nil {
		t.Errorf("FAIL: pause: %v", err)
	}
	if err := d.DeleteWebhook("alpha", w.ID); err != nil {
		t.Errorf("FAIL: delete: %v", err)
	}
	if _, ok := d.WebhookByID(w.ID); ok {
		t.Error("FAIL: the owner's delete left it")
	}
	if !t.Failed() {
		t.Log("ok")
	}
}