Implement Knowledge City platform, organizer and DAV integration
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.
This commit is contained in:
1 parent
288e6e94ac
commit
c1175d389f
699 files changed
+110197
-4117
No files matched your search
@@ -0,0 +1,163 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"pageswiki/internal/store"
|
||||
)
|
||||
|
||||
func objectListFixture(t *testing.T, count int) (*Server, string, string, []string) {
|
||||
t.Helper()
|
||||
s, token, domain := configFixture(t)
|
||||
s.mux.HandleFunc("/_/api/v1/object/list", s.handleListObjectsAPI)
|
||||
tx, err := s.store.DB.Begin()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ids := make([]string, count)
|
||||
for i := range count {
|
||||
ids[i] = fmt.Sprintf("00000000-0000-0000-0000-%012d", i)
|
||||
if _, err := tx.Exec(`INSERT INTO objects(uuid,kind,created_at) VALUES(?,'page',100)`, ids[i]); err != nil {
|
||||
tx.Rollback()
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := tx.Exec(`INSERT INTO paths(domain_uuid,path,object_uuid,created_at,is_primary) VALUES(?,?,?,1,1)`, domain, fmt.Sprintf("/private/page-%04d", i), ids[i]); err != nil {
|
||||
tx.Rollback()
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if err := tx.Commit(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return s, token, domain, ids
|
||||
}
|
||||
|
||||
func decodeObjectPage(t *testing.T, response *httptest.ResponseRecorder) []store.Object {
|
||||
t.Helper()
|
||||
var objects []store.Object
|
||||
if response.Code != 200 || json.Unmarshal(response.Body.Bytes(), &objects) != nil || !strings.HasPrefix(response.Body.String(), "[") {
|
||||
t.Fatalf("object list: %d %s", response.Code, response.Body.String())
|
||||
}
|
||||
return objects
|
||||
}
|
||||
|
||||
func TestObjectListKeysetAndLimits(t *testing.T) {
|
||||
s, token, _, ids := objectListFixture(t, 225)
|
||||
first := configRequest(s, token, "GET", "/_/api/v1/object/list", nil)
|
||||
objects := decodeObjectPage(t, first)
|
||||
if len(objects) != 100 || objects[0].UUID != ids[0] || objects[99].UUID != ids[99] {
|
||||
t.Fatalf("default page: %d %#v", len(objects), objects)
|
||||
}
|
||||
cursor := first.Header().Get("X-Next-Cursor")
|
||||
if cursor == "" || !strings.Contains(first.Header().Get("Link"), `rel="next"`) || !strings.Contains(first.Header().Get("Cache-Control"), "no-store") {
|
||||
t.Fatal("pagination headers missing")
|
||||
}
|
||||
second := configRequest(s, token, "GET", "/_/api/v1/object/list?cursor="+url.QueryEscape(cursor), nil)
|
||||
objects = decodeObjectPage(t, second)
|
||||
if len(objects) != 100 || objects[0].UUID != ids[100] {
|
||||
t.Fatal("keyset did not resume without duplication")
|
||||
}
|
||||
third := configRequest(s, token, "GET", "/_/api/v1/object/list?cursor="+url.QueryEscape(second.Header().Get("X-Next-Cursor")), nil)
|
||||
objects = decodeObjectPage(t, third)
|
||||
if len(objects) != 25 || third.Header().Get("X-Next-Cursor") != "" {
|
||||
t.Fatal("final page contract incorrect")
|
||||
}
|
||||
large := configRequest(s, token, "GET", "/_/api/v1/object/list?limit=999", nil)
|
||||
if len(decodeObjectPage(t, large)) != 200 {
|
||||
t.Fatal("max page size not enforced")
|
||||
}
|
||||
head := configRequest(s, token, "HEAD", "/_/api/v1/object/list?limit=2", nil)
|
||||
if head.Code != 200 || head.Body.Len() != 0 || head.Header().Get("X-Next-Cursor") == "" {
|
||||
t.Fatal("HEAD metadata contract incorrect")
|
||||
}
|
||||
for _, query := range []string{"limit=0", "limit=-1", "limit=abc", "cursor=invalid", "cursor=" + strings.Repeat("a", 2049)} {
|
||||
if response := configRequest(s, token, "GET", "/_/api/v1/object/list?"+query, nil); response.Code != 400 {
|
||||
t.Fatalf("invalid pagination accepted: %s %d", query, response.Code)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestObjectListHiddenPageAdvancesAndExactGrants(t *testing.T) {
|
||||
s, token, domain, ids := objectListFixture(t, store.ObjectListCandidateLimit+3)
|
||||
principal, err := s.store.PrincipalForSession(domain, token)
|
||||
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=? LIMIT 1`, domain, principal.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 _, grant := range []struct{ id, kind, target string }{{"page-read", "path", "/private/page-0401"}, {"object-read", "object", ids[402]}} {
|
||||
if _, err := s.store.DB.Exec(`INSERT INTO auth_capability_grants(uuid,role_uuid,capability,target_type,target_id,created_at_ms) VALUES(?,?,'read',?,?,1)`, grant.id, role, grant.kind, grant.target); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
first := configRequest(s, token, "GET", "/_/api/v1/object/list", nil)
|
||||
if objects := decodeObjectPage(t, first); len(objects) != 0 {
|
||||
t.Fatal("unreadable first batch leaked metadata")
|
||||
}
|
||||
cursor := first.Header().Get("X-Next-Cursor")
|
||||
if cursor == "" {
|
||||
t.Fatal("denied-only page cannot continue")
|
||||
}
|
||||
decoded, err := base64.RawURLEncoding.DecodeString(cursor)
|
||||
if err != nil || strings.Contains(string(decoded), ids[399]) || strings.Contains(first.Header().Get("Link"), ids[399]) {
|
||||
t.Fatal("hidden bookmark metadata exposed")
|
||||
}
|
||||
second := configRequest(s, token, "GET", "/_/api/v1/object/list?cursor="+url.QueryEscape(cursor), nil)
|
||||
objects := decodeObjectPage(t, second)
|
||||
if len(objects) != 2 || objects[0].UUID != ids[401] || objects[1].UUID != ids[402] || strings.Contains(second.Body.String(), "/private/") {
|
||||
t.Fatalf("Path/exact grants: %#v", objects)
|
||||
}
|
||||
// Changing grants between pages must revoke visibility immediately.
|
||||
if _, err := s.store.DB.Exec(`DELETE FROM auth_capability_grants WHERE uuid='object-read'`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
objects = decodeObjectPage(t, configRequest(s, token, "GET", "/_/api/v1/object/list?cursor="+url.QueryEscape(cursor), nil))
|
||||
if len(objects) != 1 || objects[0].UUID != ids[401] {
|
||||
t.Fatal("cursor cached revoked Object authority")
|
||||
}
|
||||
}
|
||||
|
||||
func TestObjectListCursorScopeAndMethod(t *testing.T) {
|
||||
s, token, domain, ids := objectListFixture(t, 4)
|
||||
first := configRequest(s, token, "GET", "/_/api/v1/object/list?limit=1", nil)
|
||||
cursor := first.Header().Get("X-Next-Cursor")
|
||||
principal, err := s.store.PrincipalForSession(domain, token)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := s.store.DecodeObjectListCursor(cursor, "different-domain", principal.UUID); err == nil {
|
||||
t.Fatal("cursor crossed Domain")
|
||||
}
|
||||
if _, err := s.store.DecodeObjectListCursor(cursor, domain, "different-principal"); err == nil {
|
||||
t.Fatal("cursor crossed principal")
|
||||
}
|
||||
bytes, _ := base64.RawURLEncoding.DecodeString(cursor)
|
||||
bytes[len(bytes)-1] ^= 1
|
||||
if response := configRequest(s, token, "GET", "/_/api/v1/object/list?cursor="+base64.RawURLEncoding.EncodeToString(bytes), nil); response.Code != 400 {
|
||||
t.Fatal("tampered cursor accepted")
|
||||
}
|
||||
// Deleting the boundary object does not invalidate the tuple bookmark.
|
||||
if _, err := s.store.DB.Exec(`DELETE FROM paths WHERE object_uuid=?`, ids[0]); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
objects := decodeObjectPage(t, configRequest(s, token, "GET", "/_/api/v1/object/list?cursor="+url.QueryEscape(cursor), nil))
|
||||
if len(objects) != 3 || objects[0].UUID != ids[1] {
|
||||
t.Fatal("deleted boundary broke keyset pagination")
|
||||
}
|
||||
response := httptest.NewRecorder()
|
||||
s.handleListObjectsAPI(response, httptest.NewRequest("POST", "http://localhost/_/api/v1/object/list", nil))
|
||||
if response.Code != 405 || response.Header().Get("Allow") != "GET, HEAD" {
|
||||
t.Fatal("mutation method accepted")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user