Skip to content
quarry Artifact [acfbf062bc]
Login

Artifact [acfbf062bc]

Artifact acfbf062bc47bcb533f816e2fb4991d9ddbb8f425e017efeb5a7766cae10dd52:


package runner

import (
	"context"
	"os"
	"path/filepath"
	"strings"
	"testing"
)

// The runner is given a private repository's sync token as
// QUARRY_RUNNER_PASSWORD, and zit reads ZIT_PASSWORD: the clone ran with
// --user and no password, so a runner for a private repository never
// started (ticket 430). zit is now handed the token.
func TestZitIsHandedTheFetchPassword(t *testing.T) {
	seen := filepath.Join(t.TempDir(), "seen")
	zit := filepath.Join(t.TempDir(), "zit")
	script := "#!/bin/sh\ncase \"$1\" in\nclone) printf '%s %s\\n' \"$ZIT_PASSWORD\" \"$*\" >> " + seen + "; : > .zit ;;\nupdate) [ -f .zit ] || exit 1 ;;\nesac\n"
	if err := os.WriteFile(zit, []byte(script), 0o755); err != nil {
		t.Fatal(err)
	}
	t.Setenv("ZIT_PASSWORD", "")
	dir := filepath.Join(t.TempDir(), "ws")
	co := ZitCheckout{Zit: zit, URL: "https://forge.example/r/demo", User: "fetcher", Password: "the-sync-token"}
	if err := co.Checkout(context.Background(), dir, strings.Repeat("a", 64)); err != nil {
		t.Fatalf("FAIL: checkout: %v", err)
	}
	got, _ := os.ReadFile(seen)
	if !strings.HasPrefix(string(got), "the-sync-token ") {
		t.Errorf("FAIL: zit clone did not receive the password in ZIT_PASSWORD")
	}
	if !strings.Contains(string(got), "--user fetcher") {
		t.Errorf("FAIL: zit clone was not told the account: %q", got)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// With no account named, a private repository's refusal says what the
// runner needs, where it used to repeat zit's line and restart forever.
//
// zit printed the 401 challenge's line until the forge answered as
// Fossil's server does; it now prints the protocol's own refusal.
func TestAPrivateRepositoryWithNoAccountNamesTheFlag(t *testing.T) {
	for _, line := range []string{
		"zit clone: the peer wants a login (HTTP 401)",
		"zit clone: the peer refused: not authorized to clone",
		"zit pull: the peer refused: not authorized to read",
	} {
		zit := filepath.Join(t.TempDir(), "zit")
		script := "#!/bin/sh\necho '" + line + "'; exit 1\n"
		if err := os.WriteFile(zit, []byte(script), 0o755); err != nil {
			t.Fatal(err)
		}
		err := ZitCheckout{Zit: zit, URL: "https://forge.example/r/demo"}.Checkout(context.Background(), filepath.Join(t.TempDir(), "ws"), "trunk")
		if err == nil {
			t.Fatal("FAIL: a refused clone was reported as a success")
		}
		for _, hook := range []string{"--user", "QUARRY_RUNNER_PASSWORD"} {
			if !strings.Contains(err.Error(), hook) {
				t.Errorf("FAIL: %q: the refusal does not name %s", line, hook)
			}
		}
	}
	if !t.Failed() {
		t.Log("ok")
	}
}