Skip to content
quarry Artifact [c5a2124984]
Login

Artifact [c5a2124984]

Artifact c5a21249842888464cb91ec001ff71d75f682dcd07f0d2e6c705b7d8059a0a30:


package oltp

import (
	"reflect"
	"strings"
	"testing"
)

// RFC 0009 §verdicts: required checks match branches the way protection
// does — names or globs, every matching rule applies — and a rule with
// no checks is a removed rule.
func TestRequiredChecksMatchLikeProtection(t *testing.T) {
	db := testDB(t)
	must := func(err error) {
		t.Helper()
		if err != nil {
			t.Fatal(err)
		}
	}
	must(db.SetCheckRule("r", "trunk", []string{"test", "build"}))
	must(db.SetCheckRule("r", "release-*", []string{"test", "sign"}))
	if got := db.RequiredChecks("r", "trunk"); !reflect.DeepEqual(got, []string{"build", "test"}) {
		t.Fatalf("trunk: %v", got)
	}
	if got := db.RequiredChecks("r", "release-1.2"); !reflect.DeepEqual(got, []string{"sign", "test"}) {
		t.Fatalf("glob: %v", got)
	}
	if got := db.RequiredChecks("r", "feature"); got != nil {
		t.Fatalf("unmatched branch requires %v", got)
	}
	// Overlapping rules union without repeating.
	must(db.SetCheckRule("r", "*", []string{"lint", "test"}))
	if got := db.RequiredChecks("r", "trunk"); !reflect.DeepEqual(got, []string{"lint", "test", "build"}) {
		t.Fatalf("union: %v", got)
	}
	// Blanking removes.
	must(db.SetCheckRule("r", "*", nil))
	must(db.SetCheckRule("r", "trunk", nil))
	if got := db.RequiredChecks("r", "trunk"); got != nil {
		t.Fatalf("removed but requires %v", got)
	}
}

// RFC 0009 §external-ci: tokens authenticate reports; states are the
// closed commit-status vocabulary; newest report per context wins.
func TestReporterTokensAndExternalChecks(t *testing.T) {
	db := testDB(t)
	tok, err := db.MintReporterToken("r", "jenkins")
	if err != nil || !strings.HasPrefix(tok, "qcr_") {
		t.Fatalf("mint: %q %v", tok, err)
	}
	if !db.CheckReporterToken("r", tok) || db.CheckReporterToken("other", tok) || db.CheckReporterToken("r", "qcr_wrong") {
		t.Fatal("token scope wrong")
	}
	list, err := db.ReporterTokens("r")
	if err != nil || len(list) != 1 || list[0].Label != "jenkins" || strings.Contains(list[0].Hash, tok) {
		t.Fatalf("list: %v %v", list, err)
	}
	if err := db.RevokeReporterToken("r", list[0].Hash); err != nil || db.CheckReporterToken("r", tok) {
		t.Fatal("revoke did not")
	}
	if err := db.UpsertExternalCheck("r", ExternalCheck{Subject: "abc", Context: "jenkins/test", State: "sparkling"}); err == nil {
		t.Fatal("open state vocabulary")
	}
	must := func(err error) {
		t.Helper()
		if err != nil {
			t.Fatal(err)
		}
	}
	must(db.UpsertExternalCheck("r", ExternalCheck{Subject: "abc", Context: "jenkins/test", State: "pending"}))
	must(db.UpsertExternalCheck("r", ExternalCheck{Subject: "abc", Context: "jenkins/test", State: "success", URL: "https://ci.example/1"}))
	cs, err := db.ExternalChecks("r", "abc")
	if err != nil || len(cs) != 1 || cs[0].State != "success" || cs[0].URL != "https://ci.example/1" {
		t.Fatalf("upsert: %v %v", cs, err)
	}
}