Skip to content
quarry Artifact [25fb2acc85]
Login

Artifact [25fb2acc85]

Artifact 25fb2acc85aa29ba3cbc42c18fd5ec32c3081a9d6f0650b9aad15f7bb26f4163:


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())
	}
}