187 lines
7.3 KiB
Go
187 lines
7.3 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/url"
|
|
"pageswiki/internal/store"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestOrganizerDashboardLayoutIsolationAndCAS(t *testing.T) {
|
|
s, token, domain, actor := taskFixture(t)
|
|
w := taskRequest(s, token, "GET", "dashboard", nil, "")
|
|
if w.Code != 200 {
|
|
t.Fatal(w.Code, w.Body)
|
|
}
|
|
var layout struct {
|
|
Version int64 `json:"version"`
|
|
Widgets []store.DashboardWidget `json:"widgets"`
|
|
}
|
|
if err := json.Unmarshal(w.Body.Bytes(), &layout); err != nil || len(layout.Widgets) != 6 {
|
|
t.Fatal(layout, err)
|
|
}
|
|
widgets := []store.DashboardWidget{{ID: "tasks", Width: 7, Height: 4, Tab: "review"}}
|
|
w = taskRequest(s, token, "PUT", "dashboard", map[string]any{"version": 0, "widgets": widgets}, "")
|
|
if w.Code != 200 {
|
|
t.Fatal(w.Code, w.Body)
|
|
}
|
|
if w = taskRequest(s, token, "PUT", "dashboard", map[string]any{"version": 0, "widgets": widgets}, ""); w.Code != 412 {
|
|
t.Fatal("stale layout", w.Code)
|
|
}
|
|
p, err := s.store.TaskPreferences(context.Background(), domain, actor)
|
|
if err != nil || p.Version != 0 {
|
|
t.Fatal(p, err)
|
|
}
|
|
// A legacy CLI preferences write must preserve dashboard metadata.
|
|
p.Estimate = 90
|
|
w = taskRequest(s, token, "PUT", "preferences", p, "")
|
|
if w.Code != 200 {
|
|
t.Fatal(w.Code, w.Body)
|
|
}
|
|
p, err = s.store.TaskPreferences(context.Background(), domain, actor)
|
|
if err != nil || p.Estimate != 90 {
|
|
t.Fatal(p, err)
|
|
}
|
|
l, err := s.store.Dashboard(context.Background(), domain, actor)
|
|
if err != nil || len(l.Widgets) != 1 || l.Version != 1 {
|
|
t.Fatal(l, err)
|
|
}
|
|
// Layout updates cannot postpone the digest scheduler.
|
|
if _, err = s.store.DB.Exec(`UPDATE organizer_task_preferences SET next_digest_ms=123 WHERE domain_uuid=? AND owner_uuid=?`, domain, actor); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
w = taskRequest(s, token, "PUT", "dashboard", map[string]any{"version": l.Version, "widgets": []store.DashboardWidget{}}, "")
|
|
if w.Code != 200 {
|
|
t.Fatal(w.Code, w.Body)
|
|
}
|
|
var next int64
|
|
if err = s.store.DB.QueryRow(`SELECT next_digest_ms FROM organizer_task_preferences WHERE domain_uuid=? AND owner_uuid=?`, domain, actor).Scan(&next); err != nil || next != 123 {
|
|
t.Fatal(next, err)
|
|
}
|
|
after, err := s.store.TaskPreferences(context.Background(), domain, actor)
|
|
if err != nil || after.Version != p.Version || after.Estimate != 90 {
|
|
t.Fatal("layout changed mail consent", after, err)
|
|
}
|
|
w = taskRequest(s, token, "GET", "dashboard", nil, "")
|
|
if err = json.Unmarshal(w.Body.Bytes(), &layout); err != nil || len(layout.Widgets) != 0 {
|
|
t.Fatal("empty layout reset", layout, err)
|
|
}
|
|
for _, invalid := range [][]store.DashboardWidget{{{ID: "iframe", Width: 4, Height: 3}}} {
|
|
w = taskRequest(s, token, "PUT", "dashboard", map[string]any{"version": layout.Version, "widgets": invalid}, "")
|
|
if w.Code != 422 {
|
|
t.Fatal("invalid accepted", w.Code, w.Body)
|
|
}
|
|
}
|
|
for _, id := range []string{"commands", "tomorrow-schedule", "feeds"} {
|
|
input := []store.DashboardWidget{{ID: "tasks", Width: 13, Height: 0}, {ID: id, Width: 6, Height: 4, Tab: "obsolete"}, {ID: id, Width: 6, Height: 4}}
|
|
w = taskRequest(s, token, "PUT", "dashboard", map[string]any{"version": layout.Version, "widgets": input}, "")
|
|
if w.Code != 200 {
|
|
t.Fatal("normalizable layout rejected", w.Code, w.Body)
|
|
}
|
|
if err = json.Unmarshal(w.Body.Bytes(), &layout); err != nil || len(layout.Widgets) != 2 || layout.Widgets[0].Width != 12 || layout.Widgets[0].Height != 2 || layout.Widgets[0].Tab != "inbox" || layout.Widgets[1].Tab != "" {
|
|
t.Fatal("normalization", layout, err)
|
|
}
|
|
}
|
|
other, err := s.store.CreatePasswordAccount(domain, "dashboardother", "long test password", "Other", "")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
otherToken, err := s.store.CreateAuthSession(domain, other.UUID)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
w = taskRequest(s, otherToken, "GET", "dashboard", nil, "")
|
|
if err = json.Unmarshal(w.Body.Bytes(), &layout); err != nil || layout.Version != 0 || len(layout.Widgets) != 6 {
|
|
t.Fatal("owner leakage", layout, err)
|
|
}
|
|
if err = s.store.SetPrincipalState(domain, actor, "readonly"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
token, err = s.store.CreateAuthSession(domain, actor)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if w = taskRequest(s, token, "PUT", "dashboard", map[string]any{"version": 3, "widgets": widgets}, ""); w.Code != 403 {
|
|
t.Fatal("readonly write", w.Code)
|
|
}
|
|
if w = taskRequest(s, token, "GET", "dashboard", nil, ""); w.Code != 200 {
|
|
t.Fatal("readonly read", w.Code)
|
|
}
|
|
if w = taskRequest(s, "", "GET", "dashboard", nil, ""); w.Code == 200 {
|
|
t.Fatal("anonymous dashboard")
|
|
}
|
|
if err = s.store.SetConfigInDomain(domain, "system_extension_states", `{"organizer.tasks":false}`); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if w = taskRequest(s, token, "GET", "dashboard", nil, ""); w.Code != 404 {
|
|
t.Fatal("disabled dashboard", w.Code)
|
|
}
|
|
}
|
|
|
|
func TestOrganizerPersonalSettingsAreNotAdminAccess(t *testing.T) {
|
|
s, _, domain, _ := taskFixture(t)
|
|
u, err := s.store.CreatePasswordAccount(domain, "organizer", "long test password", "Organizer", "")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
token, err := s.store.CreateAuthSession(domain, u.UUID)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
w := configRequest(s, token, "GET", "/-/sys/config?panel=organizer", nil)
|
|
if w.Code != 200 || !strings.Contains(w.Body.String(), `id="task-productivity"`) {
|
|
t.Fatal(w.Code, w.Body)
|
|
}
|
|
if strings.Contains(w.Body.String(), `id="panel-auth"`) || strings.Contains(w.Body.String(), `id="panel-campus`) {
|
|
t.Fatal("admin data in personal settings")
|
|
}
|
|
if w = configRequest(s, token, "GET", "/-/sys/config?panel=auth", nil); w.Code != 403 {
|
|
t.Fatal("admin access", w.Code)
|
|
}
|
|
if w = configRequest(s, token, "POST", "/-/sys/config?panel=organizer", url.Values{"section": {"home"}}); w.Code != 403 {
|
|
t.Fatal("config mutation", w.Code)
|
|
}
|
|
for path, target := range map[string]string{"/v/plan/tasks/settings": "/-/sys/config?panel=organizer", "/v/plan/tasks/report": "/v/plan/dashboard#activity"} {
|
|
w = configRequest(s, token, "GET", path, nil)
|
|
if w.Code != 303 || w.Header().Get("Location") != target {
|
|
t.Fatal(path, w.Code, w.Header())
|
|
}
|
|
}
|
|
for _, path := range []string{"/v/plan/dashboard", "/v/plan/tasks", "/v/plan/contacts", "/v/plan"} {
|
|
w = configRequest(s, token, "GET", path, nil)
|
|
if w.Code != 200 || !strings.Contains(w.Body.String(), `class="organizer-nav"`) {
|
|
t.Fatal(path, w.Code, w.Body)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestOrganizerProgressExactAndPrivate(t *testing.T) {
|
|
s, token, domain, actor := taskFixture(t)
|
|
today := time.Now().In(s.getLocation(domain)).Format("2006-01-02")
|
|
for i, status := range []string{"todo", "done", "cancelled", "done"} {
|
|
item := newTaskRecord("task", "Progress test")
|
|
item.Status = status
|
|
item.PlannedOn = today
|
|
item.Archived = i == 3
|
|
if _, err := s.store.SaveTask(context.Background(), domain, actor, item, 0); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
w := taskRequest(s, token, "GET", "progress", nil, "")
|
|
var p store.DashboardProgress
|
|
if err := json.Unmarshal(w.Body.Bytes(), &p); err != nil || w.Code != 200 || p.Total != 2 || p.Done != 1 || p.Date != today {
|
|
t.Fatal(w.Code, p, err, w.Body)
|
|
}
|
|
other, err := s.store.CreatePasswordAccount(domain, "progressother", "long test password", "Other", "")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
p, err = s.store.DashboardProgress(context.Background(), domain, other.UUID, today)
|
|
if err != nil || p.Total != 0 {
|
|
t.Fatal("progress leak", p, err)
|
|
}
|
|
}
|