~james/core

core/spindle/notify_status_test.go -rw-r--r-- 2.3 KiB
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 16 hours ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package spindle

import (
	"errors"
	"fmt"
	"net/http"
	"testing"

	"tangled.org/core/spindle/db"
)

func TestNotifyEventRefusedCommitCarriesAStatusTheKnotDrops(t *testing.T) {
	e := newPushEnv(t)
	push := e.firstPush(t)
	e.w.mu.Lock()
	e.w.cgi = nil
	e.w.mu.Unlock()

	_, err := e.notify(e.n.s, push)
	var coded interface{ HTTPStatus() int }
	if !errors.As(err, &coded) {
		t.Fatalf("err = %v, want one that carries a status", err)
	}
	status := coded.HTTPStatus()
	if status < 400 || status >= 500 || status == http.StatusRequestTimeout || status == http.StatusTooManyRequests {
		t.Fatalf("status = %d, want a 4xx the knot drops the event on", status)
	}
}

func TestNotifyEventBannedRepoIsAnsweredWithNoPipeline(t *testing.T) {
	e := newPushEnv(t)
	if err := e.n.s.db.PutBan(db.BanEntry{SubjectDid: testRepoDid}); err != nil {
		t.Fatal(err)
	}

	uri, err := e.notify(e.n.s, e.firstPush(t))
	if err != nil || uri != "" {
		t.Fatalf("answered %q, %v, want an empty answer without an error", uri, err)
	}
	e.wantPipelines(t, 0, "for a banned repo")
}

func TestNotifyEventFailedPushFiresItsWebhookOnceAcrossRetries(t *testing.T) {
	h := newHookEnv(t)
	for range 3 {
		if _, err := h.notifyErr(pushEvent("refs/heads/main")); err == nil {
			t.Fatal("setup: the push was handled although the knot has no git")
		}
	}
	h.expectPush(t)
	h.expectNoPush(t, "a retry of a failed push fired the webhook again")
}

func TestNotifyEventMalformedShasCarryAStatusTheKnotDrops(t *testing.T) {
	e := newPushEnv(t)
	push := e.firstPush(t)
	push.EventPush.NewSha = "not-a-sha"

	_, err := e.notify(e.n.s, push)
	var coded interface{ HTTPStatus() int }
	if !errors.As(err, &coded) || coded.HTTPStatus() != http.StatusUnprocessableEntity {
		t.Fatalf("err = %v, want a 422", err)
	}
}

func TestARefusalFromAnyFetchCarriesAStatusTheKnotDrops(t *testing.T) {
	// the pipeline fetch comes back wrapped, refusedAsStatus has to unwrap it
	err := refusedAsStatus(fmt.Errorf("loading pipeline: %w", errRevRefused))

	var coded interface{ HTTPStatus() int }
	if !errors.As(err, &coded) || coded.HTTPStatus() != http.StatusUnprocessableEntity {
		t.Fatalf("err = %v, want a 422", err)
	}
	if plain := errors.New("couldn't fetch the push from the knot"); refusedAsStatus(plain) != plain {
		t.Fatal("an error the knot may retry was given a status")
	}
}