~james/core

core/spindle/xrpc/trigger_sha_test.go -rw-r--r-- 1.3 KiB
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 17 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
package xrpc

import (
	"encoding/json"
	"net/http"
	"strings"
	"testing"

	"tangled.org/core/api/org_tangled"
	xrpcerr "tangled.org/core/xrpc/errors"
)

func TestManualTriggerShaRules(t *testing.T) {
	f := newRepoPrincipalFixture(t)
	trigger := func(sha string) (int, xrpcerr.XrpcError) {
		body, _ := json.Marshal(org_tangled.EventNotifyEvent_Input{
			Repo:  f.repoDid.String(),
			Event: &org_tangled.EventNotifyEvent_Input_Event{CiTriggerManual: &org_tangled.CiTriggerManual{Repo: f.repoDid.String(), Commit: sha}},
		})
		w := f.call(f.x.handleOrgTangledEventNotifyEvent, http.MethodPost, "/"+org_tangled.EventNotifyEventNSID, f.ownerDid, body)
		var out xrpcerr.XrpcError
		json.Unmarshal(w.Body.Bytes(), &out)
		return w.Code, out
	}

	if code, out := trigger(strings.Repeat("a", 64)); code != http.StatusOK {
		t.Errorf("sha256 commit hash answered %d (%s), want it accepted", code, out.Message)
	}
	for name, sha := range map[string]string{
		"too short":  strings.Repeat("a", 39),
		"in between": strings.Repeat("a", 50),
		"not hex":    strings.Repeat("z", 40),
		"uppercase":  strings.Repeat("A", 40),
	} {
		if code, out := trigger(sha); code != http.StatusBadRequest || out.Tag != "InvalidRequest" {
			t.Errorf("%s: answered %d %s, want 400 InvalidRequest", name, code, out.Tag)
		}
	}
}