Skip to content
quarry Artifact [95caad9d1d]
Login

Artifact [95caad9d1d]

Artifact 95caad9d1db1f3d35f36a884e5a3a2dd83ed03a7b109da0c3ed3222f13fd6c38:


package oltp

import (
	"testing"
	"time"
)

// A token that has reported and one that never has must be tellable
// apart, or "which of these can I revoke?" has no answer. The stamp is
// written by the check that admits a report, and only by a token that
// was actually accepted.
func TestReporterTokenRecordsItsLastUse(t *testing.T) {
	d := testDB(t)
	tok, err := d.MintReporterToken("alpha", "runner")
	if err != nil {
		t.Fatal(err)
	}
	ts, err := d.ReporterTokens("alpha")
	if err != nil || len(ts) != 1 {
		t.Fatalf("tokens %v err %v", ts, err)
	}
	if ts[0].Used != "" {
		t.Errorf("a freshly minted token claims a use: %q", ts[0].Used)
	}
	if !d.CheckReporterToken("alpha", tok) {
		t.Fatal("the minted token was refused")
	}
	ts, _ = d.ReporterTokens("alpha")
	if ts[0].Used == "" {
		t.Error("an accepted report left no last-used stamp")
	}
	// A wrong token is not this token being used.
	before := ts[0].Used
	if d.CheckReporterToken("alpha", "qcr_nonsense") {
		t.Fatal("a token nobody minted was accepted")
	}
	ts, _ = d.ReporterTokens("alpha")
	if ts[0].Used != before {
		t.Error("a refused token moved another token's last-used")
	}
}

// A required check is "seen" when either source reported it: an
// external status, or the forge's own CI run under that workflow name.
func TestCheckNamesSeenIncludesTheForgesOwnRuns(t *testing.T) {
	db := testDB(t)
	if _, err := db.db.Exec(`INSERT INTO ci_run (run_id, repo, checkin, workflow, pipeline, status, started)
		VALUES ('r1', 'alpha', 'abc', 'quarry', 'ci/quarry.json', 'completed', ?)`, time.Now().UTC().Format(time.RFC3339)); err != nil {
		t.Fatal(err)
	}
	if _, err := db.db.Exec(`INSERT INTO ci_run (run_id, repo, checkin, workflow, pipeline, status, started)
		VALUES ('r0', 'alpha', 'abc', 'ancient', 'ci/old.json', 'completed', '2020-01-01T00:00:00Z')`); err != nil {
		t.Fatal(err)
	}
	names, err := db.CheckNamesSeen("alpha", time.Now().Add(-90*24*time.Hour))
	if err != nil {
		t.Fatal(err)
	}
	if len(names) != 1 || names[0] != "quarry" {
		t.Fatalf("seen = %v, want [quarry]: recent forge runs count, ancient ones do not", names)
	}
}