Skip to content
quarry Artifact [2d96aee247]
Login

Artifact [2d96aee247]

Artifact 2d96aee24782386acce54963a9466dbf2a994a17ae6c76d9635a51063e854427:


package web

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

// A private repository the viewer cannot see and a name that was never
// registered answer every path the same way, signed in or out, so
// neither a page, a fragment, the card nor a stream can tell them apart
// (jva, ^3800). A public repository passes through.
func TestAPrivateRepoAnswersLikeANameThatIsNotThere(t *testing.T) {
	r := viewRegistry(t, nil)
	reached := ""
	next := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
		reached = req.URL.Path
		w.WriteHeader(http.StatusTeapot)
	})
	visible := func(repo string, _ *http.Request) bool { return repo != "orchard" }
	known := func(repo string) bool { return repo == "orchard" || repo == "open" }
	h := unreadableNotFound(r, visible, known, next)
	answer := func(path, cookie string) (int, string) {
		req := httptest.NewRequest("GET", path, nil)
		if cookie != "" {
			req.Header.Set("Cookie", cookie)
		}
		rec := httptest.NewRecorder()
		h.ServeHTTP(rec, req)
		return rec.Code, rec.Body.String()
	}
	for _, sub := range []string{"", "/nav.frag", "/og.png", "/ci.sse", "/people.frag", "/merge"} {
		for _, cookie := range []string{"", "session=x"} {
			pc, pb := answer("/r/orchard"+sub, cookie)
			uc, ub := answer("/r/zzqqnon"+sub, cookie)
			// Same-length names: the page echoes the name it was asked for.
			if pc != http.StatusNotFound || pc != uc || pb != strings.ReplaceAll(ub, "zzqqnon", "orchard") {
				t.Errorf("FAIL: %s (cookie %q): private %d/%dB, unknown %d/%dB", sub, cookie, pc, len(pb), uc, len(ub))
			}
		}
	}
	if reached != "" {
		t.Errorf("FAIL: a hidden path reached its handler: %s", reached)
	}
	if code, _ := answer("/r/open/nav.frag", ""); code != http.StatusTeapot {
		t.Errorf("FAIL: a public repository did not pass: %d", code)
	}
	t.Log("ok")
}

// A link to a private repository unfurls as the sign-in card, which
// names nothing from the link: no repository in its image or title.
func TestAPrivateRepoUnfurlsAsTheSignInCard(t *testing.T) {
	r := viewRegistry(t, nil)
	visible := func(repo string, _ *http.Request) bool { return repo != "orchard" }
	known := func(repo string) bool { return repo == "orchard" }
	h := unreadableNotFound(r, visible, known, http.NotFoundHandler())
	rec := httptest.NewRecorder()
	h.ServeHTTP(rec, httptest.NewRequest("GET", "/r/orchard", nil))
	body := rec.Body.String()
	for _, tag := range []string{`property="og:image" content="`, `property="og:title" content="`, `property="og:image:alt" content="`} {
		at := strings.Index(body, tag)
		if at < 0 {
			t.Fatalf("FAIL: no %s on the page", tag)
		}
		v := body[at+len(tag):]
		v = v[:strings.IndexByte(v, '"')]
		if strings.Contains(v, "orchard") {
			t.Errorf("FAIL: %s names the repository: %q", tag, v)
		}
		if strings.HasPrefix(tag, `property="og:image" `) && !strings.HasSuffix(v, "/assets/og-signin.png") {
			t.Errorf("FAIL: the preview image is %q, not the sign-in card", v)
		}
	}
	if !strings.Contains(body, `<meta name="twitter:card" content="summary_large_image">`) {
		t.Error("FAIL: the sign-in card is not offered wide")
	}
	card := httptest.NewRecorder()
	handler := HandlerWith(r, nil, nil, nil)
	handler.ServeHTTP(card, httptest.NewRequest("GET", "/assets/og-signin.png", nil))
	if card.Code != 200 || card.Header().Get("Content-Type") != "image/png" || card.Body.Len() < 1000 {
		t.Errorf("FAIL: /assets/og-signin.png answered %d %q, %dB", card.Code, card.Header().Get("Content-Type"), card.Body.Len())
	}
}

// Messages on a signed-in Mac fetches a shared link with the sharer's
// own session, so it sees the real page. A private repository's own
// pages must still name the sign-in card, never the repository's card.
func TestAPrivateRepoPageNamesTheSignInCard(t *testing.T) {
	ogImage := func(body string) string {
		tag := `property="og:image" content="`
		at := strings.Index(body, tag)
		if at < 0 {
			t.Fatal("FAIL: no og:image on the page")
		}
		v := body[at+len(tag):]
		return v[:strings.IndexByte(v, '"')]
	}
	d := pageData{Repo: "orchard"}
	d.Posture.Private = true
	if v := ogImage(renderSettings(t, d)); !strings.HasSuffix(v, "/assets/og-signin.png") {
		t.Errorf("FAIL: a private repository's page previews as %q", v)
	}
	if v := ogImage(renderSettings(t, pageData{Repo: "orchard"})); !strings.HasSuffix(v, "/og.png") {
		t.Errorf("FAIL: a public repository's page previews as %q", v)
	}
	t.Log("ok")
}