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)
}
}
}