internal/control/webhook_test.go

v1.31.0
gitbay/internal/control/webhook_test.go history · blame · raw

41 lines · 1401 bytes

 1package control
 2
 3import (
 4	"bytes"
 5	"strings"
 6	"testing"
 7
 8	"gitbay.org/gitbay/internal/protocol"
 9	"gitbay.org/gitbay/internal/store"
10)
11
12// TestWebhookAddRefusedURLIsAFailure: a URL the server refuses after
13// parsing it — here one that resolves to a loopback address — is the
14// caller's value being rejected, not a malformed command line. Exit 1
15// carries the sentence verbatim to every client; exit 2 reads as an
16// app bug and is shown as a generic error (#187).
17func TestWebhookAddRefusedURLIsAFailure(t *testing.T) {
18	st, repo, uid := newQueueTestRepo(t)
19	var out, errOut bytes.Buffer
20	c := &Ctx{
21		User:   store.User{ID: uid, Username: "alice"},
22		Store:  st,
23		Scope:  "full",
24		Stdin:  strings.NewReader(""),
25		Stdout: &out,
26		Stderr: &errOut,
27	}
28	code := Dispatch(c, []string{"webhook", "add", repo.Path(), "http://127.0.0.1/hook"})
29	if code != protocol.ExitFailure {
30		t.Fatalf("exit %d, want %d (%s)", code, protocol.ExitFailure, strings.TrimSpace(errOut.String()))
31	}
32	if !strings.Contains(errOut.String(), "private or local address") {
33		t.Errorf("message %q lacks the server's reason", strings.TrimSpace(errOut.String()))
34	}
35	// The shape of the command line is still a usage error.
36	out.Reset()
37	errOut.Reset()
38	if code := Dispatch(c, []string{"webhook", "add", repo.Path()}); code != protocol.ExitUsage {
39		t.Errorf("missing url: exit %d, want %d", code, protocol.ExitUsage)
40	}
41}