package web
import (
"io"
"net/http"
"net/http/httptest"
"regexp"
"strings"
"testing"
)
// The per-line links of a file page are functional (ln= picks the window
// a huge file draws) but a crawler that follows each of them fetches the
// file once per line. robots.txt turns them away with rules longer than
// the default branch's Allow, which would otherwise win the longest match.
func TestRobotsTurnsCrawlersAwayFromPerLineFileURLs(t *testing.T) {
srv := httptest.NewServer(HandlerWith(viewRegistry(t, nil), nil, nil, nil))
defer srv.Close()
resp, err := http.Get(srv.URL + "/robots.txt")
if err != nil {
t.Fatal(err)
}
body, _ := io.ReadAll(resp.Body)
resp.Body.Close()
for path, want := range map[string]bool{
"/r/netbsd/src/file/trunk/sys/kern/init_main.c?ln=412": false,
"/r/netbsd/src/file/trunk/sys/kern/init_main.c?ln=412-420": false,
"/r/netbsd/src/file/trunk/sys/kern/init_main.c?from=400&ln=412": false,
"/r/netbsd/src/file/trunk/sys/kern/init_main.c?above=3&ln=412#L400": false,
"/r/netbsd/src/doc/abc/README.md?ln=3": false,
"/r/netbsd/src/file/trunk/sys/kern/init_main.c": true,
"/r/netbsd/src/tree/trunk/sys/kern": true,
} {
if got := robotsAllows(string(body), path); got != want {
t.Errorf("FAIL: %s allowed=%v, want %v", path, got, want)
}
}
if !t.Failed() {
t.Log("ok")
}
}
var anchorRe = regexp.MustCompile(`<a [^>]*>`)
// Every per-line link is nofollow, and a file page reached with a line
// range names the plain file as its canonical address and asks not to be
// indexed; the same page without a range does neither.
func TestFilePageKeepsLineViewsOutOfSearchIndexes(t *testing.T) {
srv, _ := lineServer(t)
_, plain := getPage(t, srv, "/r/alpha/file/trunk/big.go")
if strings.Contains(plain, `name="robots"`) || strings.Contains(plain, `rel="canonical"`) {
t.Error("FAIL: the plain file page carries robots or canonical")
}
for _, a := range anchorRe.FindAllString(plain, -1) {
if strings.Contains(a, `class="ln"`) && !strings.Contains(a, `rel="nofollow"`) {
t.Fatalf("FAIL: a line link follows: %s", a)
}
}
if !strings.Contains(plain, `class="ln"`) {
t.Error("FAIL: no line links drawn")
}
for _, q := range []string{"?ln=7", "?ln=600-620", "?from=400"} {
_, body := getPage(t, srv, "/r/alpha/file/trunk/big.go"+q)
if !strings.Contains(body, `<meta name="robots" content="noindex">`) {
t.Errorf("FAIL: %s lacks noindex", q)
}
canon := regexp.MustCompile(`<link rel="canonical" href="([^"]*)"`).FindStringSubmatch(body)
if canon == nil || !strings.HasSuffix(canon[1], "/r/alpha/file/trunk/big.go") {
t.Errorf("FAIL: %s canonical = %v", q, canon)
}
for _, a := range anchorRe.FindAllString(body, -1) {
if (strings.Contains(a, `class="ln"`) || strings.Contains(a, `data-hook="line-link"`)) && !strings.Contains(a, `rel="nofollow"`) {
t.Errorf("FAIL: %s: a line link follows: %s", q, a)
}
}
}
if !t.Failed() {
t.Log("ok")
}
}
// A diff's line numbers open the file at that line; they are nofollow too.
func TestDiffLineLinksDoNotFollow(t *testing.T) {
d := *noteDiff()
d.From, d.To = "parent0", "child1"
for _, split := range []bool{false, true} {
var buf strings.Builder
if err := noHooks.template(diffPage).ExecuteTemplate(&buf, "diff-more",
pageData{Repo: "r", Diff: d, DiffSplit: split}); err != nil {
t.Fatal(err)
}
n := 0
for _, a := range anchorRe.FindAllString(buf.String(), -1) {
if strings.Contains(a, `data-hook="diff-line-file"`) {
n++
if !strings.Contains(a, `rel="nofollow"`) {
t.Errorf("FAIL: split=%v: a diff line link follows: %s", split, a)
}
}
}
if n == 0 {
t.Errorf("FAIL: split=%v: no diff line links drawn", split)
}
}
if !t.Failed() {
t.Log("ok")
}
}
// merge.conflicts walks the whole store, and on a mirror the traffic that
// asks for it is scrapers: a signed-out reader's named pair gets the
// pickers, not the merge, and the answer asks not to be indexed. A
// signed-in reader keeps the preview.
func TestAMirrorAnswersASignedOutNamedPairWithThePickers(t *testing.T) {
resetMeldCaches()
mirror := HandlerWith(mpRegistry(t, 15, 40, true, nil), nil, nil, nil, authOwner)
mpConflictCalls.Store(0)
for _, target := range []string{
"/r/lib/merge?branch=feat-003&onto=trunk",
"/r/lib/merge?branch=feat-003",
"/r/lib/merge",
} {
body := mpGet(t, mirror, target)
if n := mpConflictCalls.Load(); n != 0 {
t.Errorf("FAIL: %s asked merge.conflicts %d times", target, n)
}
for _, hook := range []string{`data-hook="merge-pick"`, `data-hook="merge-mirror"`, `<meta name="robots" content="noindex">`} {
if !strings.Contains(body, hook) {
t.Errorf("FAIL: %s lacks %s", target, hook)
}
}
if strings.Contains(body, `data-hook="meld"`) {
t.Errorf("FAIL: %s drew the merge", target)
}
}
if body := mpGet(t, mirror, "/r/lib/merge?branch=feat-003&onto=trunk"); !strings.Contains(body, `value="feat-003"`) && !strings.Contains(body, `<option selected>feat-003</option>`) {
t.Error("FAIL: the named branch is not the picker's selection")
}
mpConflictCalls.Store(0)
owner := []string{"Authorization", "Basic b3duZXI6cHc="}
body := mpGet(t, mirror, "/r/lib/merge?branch=feat-000&onto=trunk", owner...)
if !strings.Contains(body, `data-hook="meld"`) || strings.Contains(body, `data-hook="merge-pick"`) || mpConflictCalls.Load() == 0 {
t.Error("FAIL: a signed-in reader's named pair on a mirror is not drawn")
}
if !t.Failed() {
t.Log("ok")
}
}