Capture accumulated application, UI, CLI, regression tests and design documentation changes, including task project mapping and default CalDAV list naming. Keep deployment data, backups, local credentials and build artifacts out of Git. Move the Compose enrollment token to an ignored local environment file.
157 lines
6.0 KiB
Go
157 lines
6.0 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"pageswiki/internal/store"
|
|
)
|
|
|
|
func TestCityReadErrorsDistinguishMissingFromResourceLimits(t *testing.T) {
|
|
for _, test := range []struct {
|
|
err error
|
|
want int
|
|
}{
|
|
{store.ErrAccessDenied, http.StatusNotFound}, {sql.ErrNoRows, http.StatusNotFound},
|
|
{store.ErrCityMapBudget, http.StatusServiceUnavailable},
|
|
{context.DeadlineExceeded, http.StatusServiceUnavailable}, {context.Canceled, http.StatusServiceUnavailable},
|
|
} {
|
|
if got := cityReadErrorStatus(test.err); got != test.want {
|
|
t.Fatalf("status(%v)=%d want=%d", test.err, got, test.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestCityReadPortalPriorityAfterFiltering(t *testing.T) {
|
|
spot := store.CitySpot{UUID: "spot", PathPrefix: "/q/s/p", Pages: []store.CityPagePlacement{
|
|
{PageUUID: "guide", Path: "/q/s/p/guide", EdgeType: "guide"},
|
|
{PageUUID: "primary", Path: "/q/s/p/primary", EdgeType: "primary"},
|
|
{PageUUID: "boundary", Path: "/q/s/p", EdgeType: "path"},
|
|
}}
|
|
street := store.CityStreet{PathPrefix: "/q/s", Spots: []store.CitySpot{spot}}
|
|
quartier := store.CityQuartier{PathPrefix: "/q", Streets: []store.CityStreet{street}}
|
|
city := store.CityMap{Quartiers: []store.CityQuartier{quartier}}
|
|
for _, test := range []struct{ excluded, want string }{{"", "/q/s/p"}, {"/q/s/p/primary", "/q/s/p"}} {
|
|
projected := filterCityProjection(city, func(path string) bool { return path != test.excluded })
|
|
if got := projected.Quartiers[0].Streets[0].Spots[0].PortalPath; got != test.want {
|
|
t.Fatalf("portal=%s want=%s", got, test.want)
|
|
}
|
|
}
|
|
city.Quartiers[0].Streets[0].Spots[0].Pages = city.Quartiers[0].Streets[0].Spots[0].Pages[:2]
|
|
projected := filterCityProjection(city, func(string) bool { return true })
|
|
if got := projected.Quartiers[0].Streets[0].Spots[0].PortalPath; got != "/q/s/p/primary" {
|
|
t.Fatalf("primary Page lost to first visible guide: %s", got)
|
|
}
|
|
}
|
|
|
|
func TestCityReadProjectionHidesPagesLocalitiesPortalsAndRelations(t *testing.T) {
|
|
s, _, domain := configFixture(t)
|
|
s.mux.HandleFunc("/_/api/v1/city/", s.handleCityAPI)
|
|
s.mux.HandleFunc("/v/city/", s.handleCityMap)
|
|
for _, path := range []string{"/life/home/alpha/good", "/life/home/alpha/aaa-secret", "/life/home/beta/private", "/secret/vault/lock/private"} {
|
|
if _, err := s.store.CreatePageAtPathInDomain(domain, path); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if _, err := s.applyCityAutoPlan(domain, "test"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
user, err := s.store.CreatePasswordAccount(domain, "limited", "a long test password", "Limited", "")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
token, err := s.store.CreateAuthSession(domain, user.UUID)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var role string
|
|
if err := s.store.DB.QueryRow(`SELECT b.role_uuid FROM auth_role_bindings b JOIN domain_memberships m ON m.uuid=b.membership_uuid WHERE m.domain_uuid=? AND m.principal_uuid=?`, domain, user.UUID).Scan(&role); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := s.store.DB.Exec(`DELETE FROM auth_capability_grants WHERE role_uuid=?`, role); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, p := range []string{"/life", "/life/home", "/life/home/alpha", "/life/home/alpha/good"} {
|
|
if _, err := s.store.DB.Exec(`INSERT INTO auth_capability_grants(uuid,role_uuid,capability,target_type,target_id,created_at_ms) VALUES(?,?,'read','path',?,1)`, "grant:"+p, role, p); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
raw, err := s.store.GetCityMap(domain)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var alpha, beta store.CitySpot
|
|
for _, q := range raw.Quartiers {
|
|
for _, street := range q.Streets {
|
|
for _, p := range street.Spots {
|
|
if p.Title == "alpha" {
|
|
alpha = p
|
|
}
|
|
if p.Title == "beta" {
|
|
beta = p
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if err := s.store.AddSpotRelation(domain, "test", alpha.UUID, beta.UUID, "related"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
request := func(route string) *http.Request {
|
|
r := httptest.NewRequest("GET", "http://localhost"+route, nil)
|
|
r.Header.Set("Authorization", "Bearer "+token)
|
|
return r
|
|
}
|
|
call := func(route string) *httptest.ResponseRecorder {
|
|
w := httptest.NewRecorder()
|
|
s.withAuthorization(s.mux).ServeHTTP(w, request(route))
|
|
return w
|
|
}
|
|
w := call("/_/api/v1/city/map")
|
|
if w.Code != 200 {
|
|
t.Fatalf("map=%d %s", w.Code, w.Body.String())
|
|
}
|
|
for _, secret := range []string{"aaa-secret", "private", "beta", "vault", "secret"} {
|
|
if strings.Contains(w.Body.String(), secret) {
|
|
t.Fatalf("hidden metadata %q leaked: %s", secret, w.Body.String())
|
|
}
|
|
}
|
|
var projected store.CityMap
|
|
if err := json.Unmarshal(w.Body.Bytes(), &projected); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
spot := projected.Quartiers[0].Streets[0].Spots[0]
|
|
if spot.PageCount != 1 || len(spot.Pages) != 1 || spot.PortalPath != "/life/home/alpha/good" || len(projected.Relations) != 0 {
|
|
t.Fatalf("projection leaked count/portal/relation: %+v", spot)
|
|
}
|
|
if w.Header().Get("ETag") != "" || w.Header().Get("Cache-Control") != "private, no-store" {
|
|
t.Fatal("unscoped City caching")
|
|
}
|
|
if w := call("/_/api/v1/city/spots/" + beta.UUID + "/pages"); w.Code != 404 {
|
|
t.Fatalf("hidden Spot pages=%d", w.Code)
|
|
}
|
|
if w := call("/v/city/p/beta"); w.Code != 404 {
|
|
t.Fatalf("hidden detail=%d", w.Code)
|
|
}
|
|
if w := call("/v/city/p/alpha"); w.Code != 200 || strings.Contains(w.Body.String(), "aaa-secret") {
|
|
t.Fatalf("visible detail=%d %s", w.Code, w.Body.String())
|
|
}
|
|
r := cacheRequestDomain(withReadPolicyContext(request("/life/home/alpha/good")))
|
|
macro := s.expandMacrosForRequest(r, `{{spot-pages}} {{nearby}}`, "/life/home/alpha/good", domain)
|
|
if strings.Contains(macro, "aaa-secret") || strings.Contains(macro, "beta") || !strings.Contains(macro, "/good") {
|
|
t.Fatalf("macro projection=%s", macro)
|
|
}
|
|
// A new request re-evaluates revoked grants; there is no cross-request cache.
|
|
if _, err := s.store.DB.Exec(`DELETE FROM auth_capability_grants WHERE role_uuid=?`, role); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
w = call("/_/api/v1/city/map")
|
|
if w.Code != 200 || strings.Contains(w.Body.String(), "alpha") {
|
|
t.Fatalf("revoked projection=%d %s", w.Code, w.Body.String())
|
|
}
|
|
}
|