internal/httpd/wiki_test.go

v1.39.0
gitbay/internal/httpd/wiki_test.go history · blame · raw

72 lines · 2692 bytes

 1package httpd
 2
 3import (
 4	"html/template"
 5	"reflect"
 6	"strings"
 7	"testing"
 8
 9	"gitbay.org/gitbay/internal/store"
10)
11
12func TestWikiResolve(t *testing.T) {
13	have := map[string]bool{"Admin": true, "Architecture/Identity": true, "Architecture/Deep/Page": true}
14	exists := func(s string) bool { return have[s] }
15	cases := []struct {
16		page, link, want string
17		ok               bool
18	}{
19		{"Home", "Admin", "Admin", true},
20		{"Home", "Missing", "Missing", true},
21		{"Home", "../etc/passwd", "", false},
22		{"Architecture/Trust", "Identity", "Architecture/Identity", true},
23		{"Architecture/Trust", "Admin", "Admin", true},
24		{"Architecture/Trust", "Nowhere", "Architecture/Nowhere", true},
25		{"Architecture/Trust", "../Admin", "Admin", true},
26		{"Architecture/Trust", "Deep/Page", "Architecture/Deep/Page", true},
27		{"Architecture/Deep/Page", "../Identity", "Architecture/Identity", true},
28		{"Architecture/Trust", "../../x", "", false},
29		{"", "Admin", "Admin", true},
30	}
31	for _, c := range cases {
32		got, ok := wikiResolve(c.page, c.link, exists)
33		if got != c.want || ok != c.ok {
34			t.Errorf("wikiResolve(%q, %q) = %q, %v; want %q, %v", c.page, c.link, got, ok, c.want, c.ok)
35		}
36	}
37}
38
39func TestRewriteWikiLinksInSubfolder(t *testing.T) {
40	p := repoPage{Repo: store.Repo{OwnerName: "krz", Name: "gitbay"}}
41	pages := map[string]bool{"Admin": true, "Architecture/Identity": true}
42	files := map[string]bool{"diagrams/a.svg": true, "Architecture/b.svg": true}
43	in := template.HTML(`<a href="Identity.org">i</a><a href="Admin.org">a</a>` +
44		`<a href="b.svg">diagram</a>` +
45		`<img src="b.svg"><img src="diagrams/a.svg"><a href="https://x.test/">x</a>`)
46	out := string(rewriteWikiLinks(in, p, "Architecture/Trust",
47		func(s string) bool { return pages[s] }, func(s string) bool { return files[s] }))
48	for _, want := range []string{
49		`href="/krz/gitbay/wiki/Architecture/Identity"`,
50		`href="/krz/gitbay/wiki/Admin"`,
51		`href="/krz/gitbay/wiki/_raw/Architecture/b.svg"`,
52		`src="/krz/gitbay/wiki/_raw/Architecture/b.svg"`,
53		`src="/krz/gitbay/wiki/_raw/diagrams/a.svg"`,
54		`href="https://x.test/"`,
55	} {
56		if !strings.Contains(out, want) {
57			t.Errorf("missing %s in\n%s", want, out)
58		}
59	}
60}
61
62func TestWikiNav(t *testing.T) {
63	got := wikiNav([]string{"API", "Architecture/Identity", "Architecture/Trust", "Home", "Ops/Backups"})
64	want := []wikiNavGroup{
65		{Dir: "", Pages: []wikiNavPage{{"API", "API"}, {"Home", "Home"}}},
66		{Dir: "Architecture", Pages: []wikiNavPage{{"Architecture/Identity", "Identity"}, {"Architecture/Trust", "Trust"}}},
67		{Dir: "Ops", Pages: []wikiNavPage{{"Ops/Backups", "Backups"}}},
68	}
69	if !reflect.DeepEqual(got, want) {
70		t.Errorf("wikiNav = %+v\nwant %+v", got, want)
71	}
72}