internal/httpd/wiki_test.go
70 lines · 2605 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 `<img src="b.svg"><img src="diagrams/a.svg"><a href="https://x.test/">x</a>`)
45 out := string(rewriteWikiLinks(in, p, "Architecture/Trust",
46 func(s string) bool { return pages[s] }, func(s string) bool { return files[s] }))
47 for _, want := range []string{
48 `href="/krz/gitbay/wiki/Architecture/Identity"`,
49 `href="/krz/gitbay/wiki/Admin"`,
50 `src="/krz/gitbay/wiki/_raw/Architecture/b.svg"`,
51 `src="/krz/gitbay/wiki/_raw/diagrams/a.svg"`,
52 `href="https://x.test/"`,
53 } {
54 if !strings.Contains(out, want) {
55 t.Errorf("missing %s in\n%s", want, out)
56 }
57 }
58}
59
60func TestWikiNav(t *testing.T) {
61 got := wikiNav([]string{"API", "Architecture/Identity", "Architecture/Trust", "Home", "Ops/Backups"})
62 want := []wikiNavGroup{
63 {Dir: "", Pages: []wikiNavPage{{"API", "API"}, {"Home", "Home"}}},
64 {Dir: "Architecture", Pages: []wikiNavPage{{"Architecture/Identity", "Identity"}, {"Architecture/Trust", "Trust"}}},
65 {Dir: "Ops", Pages: []wikiNavPage{{"Ops/Backups", "Backups"}}},
66 }
67 if !reflect.DeepEqual(got, want) {
68 t.Errorf("wikiNav = %+v\nwant %+v", got, want)
69 }
70}