Skip to content
quarry Artifact [8d13ecc446]
Login

Artifact [8d13ecc446]

Artifact 8d13ecc4469c20186942ffdc64140c954af84ae18b42d7c34a48509edf725139:


package web

import (
	"net/http/httptest"
	"net/url"
	"strings"
	"testing"
)

// asked pulls the ?clone= parameter out of a query string the way the
// handler does, so these cases are written the way a URL is.
func asked(query string) string {
	v, err := url.ParseQuery(query)
	if err != nil {
		panic(err)
	}
	return v.Get("clone")
}

func TestFossilIsOfferedFirstWhereItWorks(t *testing.T) {
	for _, tc := range []struct {
		name        string
		fossilClone bool
		query       string
		want        string
	}{
		{"a fossil-compatible store defaults to fossil", true, "", "fossil"},
		{"and zit is one click away", true, "clone=zit", "zit"},
		{"asking for fossil explicitly still gets fossil", true, "clone=fossil", "fossil"},
		// A store that emitted a sub-manifest cannot be cloned by
		// fossil at all, so zit is the only offer — and asking for
		// fossil must not produce a command that fails.
		{"a sub-manifest store is zit only", false, "", "zit"},
		{"even when fossil is asked for by name", false, "clone=fossil", "zit"},
		// The submanifest probe fails closed: an unanswered question
		// leaves fossilClone false, which this same branch handles.
		{"an unprobed store is zit only", false, "clone=zit", "zit"},
	} {
		if got := cloneTool(tc.fossilClone, asked(tc.query)); got != tc.want {
			t.Errorf("FAIL: %s: got %q, want %q", tc.name, got, tc.want)
		}
	}
}

// The picker is drawn only where there is a choice to make. On a
// sub-manifest store it must not appear at all.
func TestThePickerIsDrawnOnlyWhereThereIsAChoice(t *testing.T) {
	for _, tc := range []struct {
		fossilClone bool
		wantPicker  bool
	}{{true, true}, {false, false}} {
		var buf strings.Builder
		err := noHooks.template(treePage).ExecuteTemplate(&buf, "content", pageData{
			Repo: "alpha", CloneURL: "https://example.test/r/alpha",
			FossilClone: tc.fossilClone,
			CloneTool:   cloneTool(tc.fossilClone, ""),
		})
		if err != nil {
			t.Skipf("tree template needs more page data than this test builds: %v", err)
		}
		if got := strings.Contains(buf.String(), "clonepick"); got != tc.wantPicker {
			t.Errorf("FAIL: fossilClone=%v drew a picker=%v, want %v", tc.fossilClone, got, tc.wantPicker)
		}
	}
}

// A private repository refuses an anonymous clone (#509): its command
// names the reader's account, and fossil, which refuses an account with
// no password in the URL, is not offered.
func TestAPrivateRepoClonesAsTheReader(t *testing.T) {
	req := httptest.NewRequest("GET", "https://forge.test/r/flak", nil)
	if got := cloneURLAs(req, "flak", "lou"); got != "https://[email protected]/r/flak" {
		t.Errorf("FAIL: clone address %q", got)
	}
	render := func(d pageData) string {
		var sb strings.Builder
		if err := noHooks.template(treePage).Execute(&sb, d); err != nil {
			t.Fatalf("render: %v", err)
		}
		return sb.String()
	}
	priv := render(pageData{Repo: "flak", CloneAsks: true, CloneTool: "zit", CloneURL: "https://[email protected]/r/flak"})
	if !strings.Contains(priv, `data-hook="clone-asks"`) || !strings.Contains(priv, `href="/me#push"`) {
		t.Error("FAIL: a private repository's clone box does not say what password it asks for")
	}
	if strings.Contains(priv, `?clone=fossil`) || !strings.Contains(priv, `value="zit clone https://[email protected]/r/flak flak"`) {
		t.Error("FAIL: a private repository's clone box offers a command that cannot work")
	}
	pub := render(pageData{Repo: "open", FossilClone: true, CloneTool: "fossil", CloneURL: "https://forge.test/r/open"})
	if strings.Contains(pub, `data-hook="clone-asks"`) || !strings.Contains(pub, `?clone=fossil`) {
		t.Error("FAIL: a public repository's clone box changed")
	}
	t.Log("ok")
}