package web import ( "net/http" "net/http/httptest" "net/url" "strings" "testing" "github.com/google/uuid" "github.com/gramanas/blogspace/internal/config" "github.com/gramanas/blogspace/internal/store" ) func TestHostname(t *testing.T) { cases := map[string]string{"Example.com:8080": "example.com", "a.example.com": "a.example.com", "example.com.": "example.com", "[::1]:80": "::1"} for in, want := range cases { if got := hostname(in); got != want { t.Errorf("hostname(%q) = %q, want %q", in, got, want) } } } // Host routing that does not need the database: unknown hosts and reserved/nested subdomains 404 before any lookup. func TestHostRoutingWithoutDB(t *testing.T) { cfg := &config.Config{BaseDomain: "example.com", JWTSecret: []byte("x"), MaxUploadBytes: 1 << 20} s := NewServer(cfg, nil) for _, host := range []string{"evil.com", "a.b.example.com", "www.blog.example.com", "static.example.com", "notexample.com"} { rec := httptest.NewRecorder() req := httptest.NewRequest("GET", "/", nil) req.Host = host s.ServeHTTP(rec, req) if rec.Code != http.StatusNotFound { t.Errorf("host %q: got %d, want 404", host, rec.Code) } } // root domain (and www) reach the management mux: /webadmin renders without DB access for _, host := range []string{"example.com", "www.example.com", "example.com:8080"} { rec := httptest.NewRecorder() req := httptest.NewRequest("GET", "/webadmin", nil) req.Host = host s.ServeHTTP(rec, req) if rec.Code != http.StatusOK || !strings.Contains(rec.Body.String(), "Log in") { t.Errorf("host %q /webadmin: got %d", host, rec.Code) } } } // On the root domain the literal management routes must win over the blog's /{page} wildcards. func TestRootRoutePrecedence(t *testing.T) { cfg := &config.Config{BaseDomain: "example.com", JWTSecret: []byte("x"), MaxUploadBytes: 1 << 20} s := NewServer(cfg, nil) for path, want := range map[string]int{"/webadmin": 200, "/healthz": 200, "/dashboard": 303, "/admin/": 303, "/b/alice/posts": 303, "/b/alice/announcements": 303, "/account/password": 303} { rec := httptest.NewRecorder() req := httptest.NewRequest("GET", path, nil) req.Host = "example.com" s.ServeHTTP(rec, req) if rec.Code != want { t.Errorf("%s: got %d, want %d", path, rec.Code, want) } } for _, slug := range []string{"webadmin", "admin", "b", "media", "feed.xml"} { if !reservedPageSlugs[slug] { t.Errorf("page slug %q should be reserved", slug) } } } func TestThemeNormalizeAndCSS(t *testing.T) { th := ParseTheme([]byte(`{"bg_color":"red;}body{display:none","font":"comic","bg_image":"../etc","nav_position":"left-sidebar"}`)) if th.BgColor != DefaultTheme().BgColor || th.Font != "sans" || th.BgImage != "" || th.NavPosition != "left-sidebar" { t.Errorf("normalize: %+v", th) } f := url.Values{"bg_color": {"#ABCDEF"}, "content_width": {"wide"}, "header_show_title": {"on"}, "bg_image": {"not-a-uuid"}} th = ThemeFromForm(DefaultTheme(), f) if th.BgColor != "#abcdef" || th.ContentWidth != "wide" || !th.HeaderShowTitle || th.BgImage != "" { t.Errorf("from form: %+v", th) } css := th.CSS() if !strings.Contains(css, "background-color:#abcdef") || !strings.Contains(css, "max-width:1100px") || strings.Contains(css, "display:none") { t.Errorf("css: %s", css) } } func TestAllTemplatesParse(t *testing.T) { tpl := newTemplates(false, funcs) for _, name := range []string{"auth/login.html", "dashboard/overview.html", "dashboard/pages.html", "dashboard/page_form.html", "dashboard/posts.html", "dashboard/post_form.html", "dashboard/design.html", "dashboard/images.html", "dashboard/settings.html", "dashboard/password.html", "dashboard/confirm.html", "dashboard/sections.html", "dashboard/section_form.html", "admin/index.html", "admin/new_user.html", "admin/delete_user.html", "blog/page.html", "blog/post.html", "blog/404.html"} { if _, err := tpl.get(name); err != nil { t.Errorf("%s: %v", name, err) } } } // /webadmin on a blog's own host bounces to the root login page and back to that blog's dashboard. func TestSubdomainWebadminRedirect(t *testing.T) { cfg := &config.Config{BaseDomain: "example.com", JWTSecret: []byte("x"), MaxUploadBytes: 1 << 20} s := NewServer(cfg, nil) rec := httptest.NewRecorder() req := httptest.NewRequest("GET", "/webadmin", nil) req.Host = "alice.example.com" s.ServeHTTP(rec, req) if want := "http://example.com/webadmin?next=%2Fb%2Falice%2F"; rec.Code != http.StatusSeeOther || rec.Header().Get("Location") != want { t.Errorf("got %d %q, want 303 %q", rec.Code, rec.Header().Get("Location"), want) } } func TestAppendImageMD(t *testing.T) { img := &store.Image{ID: uuid.MustParse("6ba7b810-9dad-11d1-80b4-00c04fd430c8"), Filename: "cat].png"} line := "![cat.png](/media/6ba7b810-9dad-11d1-80b4-00c04fd430c8)" if got := appendImageMD("", img); got != line+"\n" { t.Errorf("empty body: %q", got) } if got := appendImageMD("hello\n", img); got != "hello\n\n"+line+"\n" { t.Errorf("with body: %q", got) } if got := appendImageMD("hello", nil); got != "hello" { t.Errorf("nil image should not change the body: %q", got) } } func TestSplitSections(t *testing.T) { secs := []store.Section{{ID: 1, Placement: "above"}, {ID: 2, Placement: "below"}, {ID: 3, Placement: "sidebar"}} above, below, sidebar := splitSections(secs, true) if len(above) != 1 || len(below) != 1 || len(sidebar) != 1 || sidebar[0].ID != 3 { t.Errorf("with sidebar: %v %v %v", above, below, sidebar) } above, _, sidebar = splitSections(secs, false) if len(above) != 2 || len(sidebar) != 0 || above[1].ID != 3 { // sidebar falls back to above, keeping order t.Errorf("without sidebar: %v %v", above, sidebar) } sec := &store.Section{Placement: "footer", Style: "