Skip to content
quarry Artifact [e054e8b0cd]
Login

Artifact [e054e8b0cd]

Artifact e054e8b0cd9427b0db7858566a3b5a474706c9fa3759922812a318cc23e69f0a:


package main

import (
	"os"
	"path/filepath"
	"testing"

	"craftdesign.group/quarry/internal/oltp"
)

// A read-only sync token (no i, no y) clones the private repository it
// was minted for, through the sync lane wired as serve wires it: Basic
// counted by syncSignIn, resolved by basicSyncLogin, the read gate
// behind. This is a CI runner's account reading its own repository's
// task names; it must not need push letters to read, its clones must
// never count as failed sign-ins, and it still may not push.
func TestAReadOnlySyncTokenClonesItsPrivateRepository(t *testing.T) {
	r := newFirstPushRig(t, "website")
	r.commit(t, r.dirs["website"])
	db := r.rg.odb
	if err := db.AddAccount("website-read", "pw-read-12345", false); err != nil {
		t.Fatal(err)
	}
	if err := db.SetCaps("website-read", "website", "gjorz"); err != nil {
		t.Fatal(err)
	}
	tok, _, err := db.MintToken("website-read", oltp.TokenSpec{
		Name: "runner", Scopes: []string{oltp.ScopeSync},
		Repos: []string{"website"}, Ceiling: "gjorzh28",
	})
	if err != nil {
		t.Fatal(err)
	}
	r.front.SetBasicLogin(r.rg.doors.basicSyncLogin)
	r.front.SetBasicSignIn(r.rg.doors.syncSignIn)

	// More clones than the limiter's free misses: a runner restarting in
	// a loop must never hold its own account.
	var dir string
	for i := 0; i < 8; i++ {
		dir = t.TempDir()
		out, err := r.zit(t, dir, "clone", "--user", "website-read", "--password", tok, r.base+"website", ".")
		if err != nil {
			t.Fatalf("FAIL: clone %d with a read-only sync token was refused: %v\n%s", i+1, err, out)
		}
	}

	// The same token may not push.
	if err := os.WriteFile(filepath.Join(dir, "readonly.txt"), []byte("x\n"), 0o644); err != nil {
		t.Fatal(err)
	}
	if out, err := r.zit(t, dir, "add", "readonly.txt"); err != nil {
		t.Fatalf("add: %v\n%s", err, out)
	}
	if out, err := r.zit(t, dir, "commit", "-m", "should not land"); err != nil {
		t.Fatalf("commit: %v\n%s", err, out)
	}
	out, err := r.zit(t, dir, "push", "--user", "website-read", "--password", tok, r.base+"website")
	if err == nil {
		t.Errorf("FAIL: a read-only sync token pushed a check-in\n%s", out)
	}
	fresh := t.TempDir()
	if out, err := r.zit(t, fresh, "clone", "--user", "website-read", "--password", tok, r.base+"website", "."); err != nil {
		t.Fatalf("FAIL: clone after the refused push: %v\n%s", err, out)
	}
	if _, err := os.Stat(filepath.Join(fresh, "readonly.txt")); err == nil {
		t.Errorf("FAIL: the refused push's file is in the repository")
	}
	if !t.Failed() {
		t.Log("ok")
	}
}