package server import ( "net/http" "net/http/httptest" "strings" "testing" ) func TestCityNamedLinksAndLightweightDetails(t *testing.T) { s, token, domain := configFixture(t) s.mux.HandleFunc("/v/city/", s.handleCityMap) s.mux.HandleFunc("/v/search", s.handleSearch) for _, path := range []string{"/life/home/alpha/page?#%", "/work/home/beta/page"} { if _, err := s.store.CreatePageAtPathInDomain(domain, path); err != nil { t.Fatal(err) } } if _, err := s.applyCityAutoPlan(domain, "test"); err != nil { t.Fatal(err) } call := func(route string) *httptest.ResponseRecorder { r := httptest.NewRequest(http.MethodGet, "http://localhost"+route, nil) r.Header.Set("Authorization", "Bearer "+token) w := httptest.NewRecorder() s.withAuthorization(s.mux).ServeHTTP(w, r) return w } street, err := s.store.CityNode(domain, "street", "life/home") if err != nil { t.Fatal(err) } for _, route := range []string{"/v/city/s/life/home", "/v/city/s/work/home", "/v/city/s/uuid:" + street.UUID} { if w := call(route); w.Code != 200 { t.Fatalf("%s=%d %s", route, w.Code, w.Body.String()) } } for _, route := range []string{"/v/city/s/home", "/v/city/s/missing/home", "/v/city/q/life/extra"} { if w := call(route); w.Code != 404 { t.Fatalf("%s=%d", route, w.Code) } } w := call("/v/city/p/alpha") for _, wanted := range []string{`href="/v/city/q/life"`, `href="/v/city/s/life/home"`, `href="/life/home/alpha/page%3F%23%25"`, `href="/v/city?spot=`} { if w.Code != 200 || !strings.Contains(w.Body.String(), wanted) { t.Fatalf("detail missing %s (%d): %s", wanted, w.Code, w.Body.String()) } } for _, selector := range []string{"life/home", "uuid:" + street.UUID} { if w := call("/v/search?q=page&locality=street&locality_id=" + selector); w.Code != 200 { t.Fatalf("locality search %s=%d: %s", selector, w.Code, w.Body.String()) } } r := httptest.NewRequest(http.MethodGet, "http://localhost/v/city", nil) r.Header.Set("Authorization", "Bearer "+token) city, err := s.cityProjectionForRequest(cacheRequestDomain(withReadPolicyContext(r))) if err != nil { t.Fatal(err) } for _, q := range city.Quartiers { for _, st := range q.Streets { if st.DetailHref != "/v/city/s/"+q.Name+"/"+st.Name { t.Fatalf("projection link=%s", st.DetailHref) } } } macro := s.expandCityMacros("{{city-breadcrumbs}} {{spot-pages}}", "/life/home/alpha/page?#%", domain, func(string) bool { return true }) if !strings.Contains(macro, `href="/v/city/s/life/home"`) || !strings.Contains(macro, `href="/life/home/alpha/page%3F%23%25"`) { t.Fatalf("macro=%s", macro) } macro = s.expandCityMacros("{{city-breadcrumbs}} {{spot-pages}}", "/life/home/alpha/page?#%", domain, func(p string) bool { return p != "/life" }) if strings.Contains(macro, "alpha") || strings.Contains(macro, "home") { t.Fatalf("hidden ancestry leaked=%s", macro) } // Detail and breadcrumbs must not enumerate the entire City/Page graph. if _, err := s.store.DB.Exec(`DROP TABLE spot_pages`); err != nil { t.Fatal(err) } w = call("/v/city/s/life/home") if w.Code != 200 || !strings.Contains(w.Body.String(), `href="/v/city?street=`+street.UUID+`"`) { t.Fatalf("lightweight detail=%d %s", w.Code, w.Body.String()) } macro = s.expandCityMacros("{{street}}", "/life/home/page", domain, func(string) bool { return true }) if !strings.Contains(macro, `href="/v/city/s/life/home"`) { t.Fatalf("lightweight macro=%s", macro) } }