feat: atomically capture task mail into Wiki and persist destination defaults
This commit is contained in:
1 parent
52b501d1ce
commit
80d297dab4
14 files changed
+525
-30
No files matched your search
@@ -23,6 +23,20 @@ func TestProductivityChromium(t *testing.T) {
|
||||
for _, size := range sizes {
|
||||
t.Run(view+"-"+size, func(t *testing.T) {
|
||||
s, token, domain, actor := taskFixture(t)
|
||||
if view == "settings" {
|
||||
project, err := s.store.SaveTask(context.Background(), domain, actor, newTaskRecord("project", "Saved default project"), 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
p, err := s.store.TaskPreferences(context.Background(), domain, actor)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
p.ProjectUUID = project.UUID
|
||||
if _, err = s.store.SaveTaskPreferences(context.Background(), domain, actor, p, time.Now()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if size == "english" {
|
||||
if err := s.store.SetConfigInDomain(domain, "locale", "en"); err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -50,6 +64,11 @@ func TestProductivityChromium(t *testing.T) {
|
||||
path := map[string]string{"contacts": "/v/plan/contacts", "settings": "/-/sys/config?panel=organizer", "report": "/v/plan/dashboard", "tasks": "/v/plan/tasks", "home": "/", "editor": "/v/plan/task/-/new"}[view]
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
r.Header.Set("Authorization", "Bearer "+token)
|
||||
// Simulate a saved project outside the first project list page.
|
||||
if view == "settings" && size == "desktop" && r.URL.Path == "/_/api/v1/extension/organizer.tasks/items" && r.URL.Query().Get("kind") == "project" {
|
||||
writeEventJSON(w, 200, map[string]any{"items": []any{}, "next": "fixture"})
|
||||
return
|
||||
}
|
||||
if r.Method == "GET" && r.URL.RequestURI() == path {
|
||||
rec := httptest.NewRecorder()
|
||||
s.withAuthorization(s.mux).ServeHTTP(rec, r)
|
||||
|
||||
@@ -0,0 +1,173 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"pageswiki/internal/store"
|
||||
)
|
||||
|
||||
func TestTaskMailWikiCapture(t *testing.T) {
|
||||
s, token, domain, actor := taskFixture(t)
|
||||
ctx := context.Background()
|
||||
p, err := s.store.TaskPreferences(ctx, domain, actor)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, bad := range []string{"mail", "/a/../b", "/_/api", "/v/blog", "/a?x", "/a//b", "/a\nb"} {
|
||||
p.WikiPrefix = bad
|
||||
if w := taskRequest(s, token, "PUT", "preferences", p, ""); w.Code != 422 {
|
||||
t.Fatal(bad, w.Code)
|
||||
}
|
||||
}
|
||||
p.WikiPrefix = "/private/mail"
|
||||
if _, err = s.store.SaveTaskPreferences(ctx, domain, actor, p, time.Now()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
id := uuid.NewString()
|
||||
input := map[string]any{"request_id": id, "title": "Mail / {{children}}", "notes": "From: sender@example.invalid", "wiki_body": "Hello\n<script>bad()</script> {{children}}"}
|
||||
w := taskRequest(s, token, "POST", "capture", input, "")
|
||||
if w.Code != 201 {
|
||||
t.Fatal(w.Code, w.Body)
|
||||
}
|
||||
var response struct {
|
||||
Item store.TaskRecord `json:"item"`
|
||||
Path string `json:"page_path"`
|
||||
}
|
||||
if err = json.Unmarshal(w.Body.Bytes(), &response); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
target := store.TaskCaptureWikiPath(p.WikiPrefix, input["title"].(string), id)
|
||||
if response.Item.PageUUID == "" || response.Path != target || strings.Contains(response.Item.Notes, "bad()") {
|
||||
t.Fatal(w.Body)
|
||||
}
|
||||
rev, err := s.store.GetLatestRevision(response.Item.PageUUID)
|
||||
if err != nil || !strings.Contains(rev.Body, "<script>bad()</script>") || strings.Contains(rev.Body, "{{children}}") {
|
||||
t.Fatal(rev, err)
|
||||
}
|
||||
// A committed retry remains stable even if the preference changes.
|
||||
p, err = s.store.TaskPreferences(ctx, domain, actor)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
p.WikiPrefix = "/elsewhere"
|
||||
if _, err = s.store.SaveTaskPreferences(ctx, domain, actor, p, time.Now()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if w = taskRequest(s, token, "POST", "capture", input, ""); w.Code != 201 || !strings.Contains(w.Body.String(), response.Item.PageUUID) {
|
||||
t.Fatal(w.Code, w.Body)
|
||||
}
|
||||
input["wiki_body"] = "different"
|
||||
if w = taskRequest(s, token, "POST", "capture", input, ""); w.Code != 412 {
|
||||
t.Fatal(w.Code, w.Body)
|
||||
}
|
||||
input["request_id"] = uuid.NewString()
|
||||
input["wiki_prefix"] = "/explicit"
|
||||
if w = taskRequest(s, token, "POST", "capture", input, ""); w.Code != 201 || !strings.Contains(w.Body.String(), "/explicit/") {
|
||||
t.Fatal(w.Code, w.Body)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTaskMailWikiRollbackAndAccess(t *testing.T) {
|
||||
s, token, domain, actor := taskFixture(t)
|
||||
ctx := context.Background()
|
||||
id := uuid.NewString()
|
||||
title := "Mail"
|
||||
input := map[string]any{"request_id": id, "title": title, "wiki_body": "Mail body"}
|
||||
if w := taskRequest(s, token, "POST", "capture", input, ""); w.Code != 422 || !strings.Contains(w.Body.String(), "wiki_prefix_required") {
|
||||
t.Fatal(w.Code, w.Body)
|
||||
}
|
||||
input["wiki_prefix"] = "/mail"
|
||||
target := store.TaskCaptureWikiPath("/mail", title, id)
|
||||
absent := func() {
|
||||
t.Helper()
|
||||
if _, e := s.store.ResolvePathInDomain(domain, target); !errors.Is(e, sql.ErrNoRows) {
|
||||
t.Fatal("orphan Wiki", e)
|
||||
}
|
||||
if _, e := s.store.GetTask(ctx, domain, actor, id); !errors.Is(e, sql.ErrNoRows) {
|
||||
t.Fatal("orphan task", e)
|
||||
}
|
||||
}
|
||||
// Failure after document insert must roll back the entire capture, including receipt.
|
||||
if _, e := s.store.DB.Exec(`CREATE TRIGGER reject_capture BEFORE INSERT ON organizer_changes BEGIN SELECT RAISE(ABORT,'fixture'); END`); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
if w := taskRequest(s, token, "POST", "capture", input, ""); w.Code != 500 {
|
||||
t.Fatal(w.Code, w.Body)
|
||||
}
|
||||
absent()
|
||||
if _, e := s.store.DB.Exec(`DROP TRIGGER reject_capture`); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
for _, body := range []string{strings.Repeat("x", 256<<10+1), "bad\x00text"} {
|
||||
input["wiki_body"] = body
|
||||
if w := taskRequest(s, token, "POST", "capture", input, ""); w.Code != 422 {
|
||||
t.Fatal(w.Code)
|
||||
}
|
||||
absent()
|
||||
}
|
||||
input["wiki_body"] = "Mail body"
|
||||
other, e := s.store.CreatePasswordAccount(domain, "mailother", "long fixture password", "Other", "")
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
otherToken, e := s.store.CreateAuthSession(domain, other.UUID)
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
if w := taskRequest(s, otherToken, "POST", "capture", input, ""); w.Code != 403 {
|
||||
t.Fatal("Wiki ACL", w.Code, w.Body)
|
||||
}
|
||||
absent()
|
||||
existing, e := s.store.CreatePageAtPathInDomain(domain, target)
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
if w := taskRequest(s, token, "POST", "capture", input, ""); w.Code != 409 {
|
||||
t.Fatal("collision", w.Code, w.Body)
|
||||
}
|
||||
if got, e := s.store.ResolvePathInDomain(domain, target); e != nil || got != existing {
|
||||
t.Fatal("overwrite", e)
|
||||
}
|
||||
if _, e = s.store.GetTask(ctx, domain, actor, id); !errors.Is(e, sql.ErrNoRows) {
|
||||
t.Fatal("orphan collision task", e)
|
||||
}
|
||||
input["request_id"] = uuid.NewString()
|
||||
if w := taskRequest(s, token, "POST", "capture", input, ""); w.Code != 201 {
|
||||
t.Fatal(w.Code, w.Body)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTaskMailWikiProjectFallback(t *testing.T) {
|
||||
s, token, domain, actor := taskFixture(t)
|
||||
ctx := context.Background()
|
||||
root, e := s.store.CreatePageAtPathInDomain(domain, "/projects/app")
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
project := newTaskRecord("project", "App")
|
||||
project.PageUUID = root
|
||||
project.NotesSubpath = "mail"
|
||||
project, e = s.store.SaveTask(ctx, domain, actor, project, 0)
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
p, e := s.store.TaskPreferences(ctx, domain, actor)
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
p.ProjectUUID = project.UUID
|
||||
if _, e = s.store.SaveTaskPreferences(ctx, domain, actor, p, time.Now()); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
input := map[string]any{"request_id": uuid.NewString(), "title": "Project mail", "wiki_body": "Body"}
|
||||
if w := taskRequest(s, token, "POST", "capture", input, ""); w.Code != 201 || !strings.Contains(w.Body.String(), "/projects/app/mail/") {
|
||||
t.Fatal(w.Code, w.Body)
|
||||
}
|
||||
}
|
||||
@@ -2,8 +2,10 @@ package server
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"database/sql"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
@@ -15,7 +17,11 @@ import (
|
||||
)
|
||||
|
||||
func taskJSON(w http.ResponseWriter, r *http.Request, output any) bool {
|
||||
d := json.NewDecoder(http.MaxBytesReader(w, r.Body, 32768))
|
||||
return taskJSONLimit(w, r, output, 32768)
|
||||
}
|
||||
|
||||
func taskJSONLimit(w http.ResponseWriter, r *http.Request, output any, limit int64) bool {
|
||||
d := json.NewDecoder(http.MaxBytesReader(w, r.Body, limit))
|
||||
d.DisallowUnknownFields()
|
||||
if d.Decode(output) != nil || d.Decode(&struct{}{}) != io.EOF {
|
||||
taskError(w, store.ErrTaskInvalid)
|
||||
@@ -61,6 +67,8 @@ type taskCaptureInput struct {
|
||||
ReviewOn *string `json:"review_on"`
|
||||
Estimate *int `json:"estimate_minutes"`
|
||||
Complexity *int `json:"complexity"`
|
||||
WikiBody *string `json:"wiki_body,omitempty"`
|
||||
WikiPrefix *string `json:"wiki_prefix,omitempty"`
|
||||
}
|
||||
|
||||
func (s *Server) handleTaskProductivity(w http.ResponseWriter, r *http.Request, actor string, active bool, kind string) {
|
||||
@@ -181,13 +189,23 @@ func (s *Server) handleTaskProductivity(w http.ResponseWriter, r *http.Request,
|
||||
case "capture":
|
||||
if r.Method == "POST" {
|
||||
var input taskCaptureInput
|
||||
if !taskJSON(w, r, &input) {
|
||||
if !taskJSONLimit(w, r, &input, 2<<20) {
|
||||
return
|
||||
}
|
||||
if id, err := uuid.Parse(input.RequestID); err != nil || id.String() != input.RequestID {
|
||||
taskError(w, store.ErrTaskInvalid)
|
||||
return
|
||||
}
|
||||
raw, _ := json.Marshal(input)
|
||||
hash := sha256.Sum256(raw)
|
||||
requestHash := hex.EncodeToString(hash[:])
|
||||
if old, e := s.store.CapturedTask(r.Context(), domain, actor, input.RequestID, requestHash); e == nil {
|
||||
s.taskResponse(w, r, old, 201)
|
||||
return
|
||||
} else if !errors.Is(e, sql.ErrNoRows) {
|
||||
taskError(w, e)
|
||||
return
|
||||
}
|
||||
p, err := s.store.TaskPreferences(r.Context(), domain, actor)
|
||||
if err != nil {
|
||||
taskError(w, err)
|
||||
@@ -211,9 +229,49 @@ func (s *Server) handleTaskProductivity(w http.ResponseWriter, r *http.Request,
|
||||
item.Complexity = *input.Complexity
|
||||
}
|
||||
applyTaskReminderDefaults(&item, p)
|
||||
raw, _ := json.Marshal(input)
|
||||
hash := sha256.Sum256(raw)
|
||||
item, err = s.store.CaptureTask(r.Context(), domain, actor, input.RequestID, hex.EncodeToString(hash[:]), item)
|
||||
target, body := "", ""
|
||||
if input.WikiBody != nil {
|
||||
prefix := p.WikiPrefix
|
||||
if input.WikiPrefix != nil {
|
||||
prefix = *input.WikiPrefix
|
||||
}
|
||||
if prefix == "" && input.WikiPrefix == nil && item.ProjectUUID != "" {
|
||||
project, e := s.store.GetTask(r.Context(), domain, actor, item.ProjectUUID)
|
||||
if e != nil && !errors.Is(e, sql.ErrNoRows) {
|
||||
taskError(w, e)
|
||||
return
|
||||
}
|
||||
if e == nil && project.Kind == "project" && !project.Archived {
|
||||
if base := s.taskPagePath(r, project.PageUUID); base != "" {
|
||||
sub := project.NotesSubpath
|
||||
if sub == "" {
|
||||
sub = "tasks"
|
||||
}
|
||||
prefix = strings.TrimRight(base, "/") + "/" + sub
|
||||
}
|
||||
}
|
||||
}
|
||||
if prefix == "" {
|
||||
writeEventJSON(w, 422, map[string]string{"error": "wiki_prefix_required"})
|
||||
return
|
||||
}
|
||||
if !store.ValidTaskWikiPrefix(prefix) {
|
||||
taskError(w, store.ErrTaskInvalid)
|
||||
return
|
||||
}
|
||||
target = store.TaskCaptureWikiPath(prefix, item.Title, input.RequestID)
|
||||
for _, capability := range []string{"read", "create", "write"} {
|
||||
if s.authorizePathRequest(r, capability, target) != nil {
|
||||
taskError(w, store.ErrAccessDenied)
|
||||
return
|
||||
}
|
||||
}
|
||||
body = *input.WikiBody
|
||||
} else if input.WikiPrefix != nil {
|
||||
taskError(w, store.ErrTaskInvalid)
|
||||
return
|
||||
}
|
||||
item, err = s.store.CaptureTaskWithWiki(r.Context(), domain, actor, input.RequestID, requestHash, item, target, body)
|
||||
if err != nil {
|
||||
taskError(w, err)
|
||||
return
|
||||
|
||||
@@ -31,7 +31,12 @@ window.addEventListener('load',async()=>{const wait=async(fn)=>{for(let i=0;i<12
|
||||
const exported=await fetch('/_/api/v1/extension/organizer.contacts/export.vcf').then(r=>r.text());if(!exported.includes('ブラウザ連絡先'))throw Error('Export missing');
|
||||
document.querySelector('#tasks-dav-open').click();await wait(()=>document.querySelector('#tasks-dav-dialog').open);const dav=document.querySelector('#tasks-dav-dialog');if(getComputedStyle(dav.querySelector('.dav-compat-toggle')).display!=='none')throw Error('CalDAV-only compatibility visible');fill(dav.querySelector('form>input'),'Browser contact client');dav.querySelector('form').requestSubmit();await wait(()=>!dav.querySelector('section').hidden);const credentials=await fetch('/_/api/v1/caldav-credentials').then(r=>r.json());if(credentials.items.length!==1||credentials.items[0].scope!=='contacts')throw Error('Wrong credential scope');dav.querySelector('tbody button').click();await wait(()=>!dav.querySelector('tbody button'));dav.querySelector('header button').click();
|
||||
}else if(path==='/-/sys/config'){
|
||||
await wait(()=>document.querySelector('[name=project_uuid] option:checked')?.textContent==='Saved default project');
|
||||
const savedProject=document.querySelector('[name=project_uuid]').value;
|
||||
document.querySelector('.productivity-settings').reset();if(document.querySelector('[name=project_uuid]').value!==savedProject)throw Error('Default project reset lost selection');
|
||||
fill(document.querySelector('[name=wiki_prefix]'),'/private/mail');
|
||||
await wait(()=>document.querySelector('[name=due_days]'));fill(document.querySelector('[name=due_days]'),'2');document.querySelector('[name=notify_due]').click();document.querySelector('[name=notify_planned]').click();fill(document.querySelector('[name=reminder_time]'),'08:45');document.querySelector('.productivity-settings').requestSubmit();await wait(()=>document.querySelector('#productivity-status').textContent.includes('保存'));const d=await fetch('/_/api/v1/extension/organizer.tasks/preferences').then(r=>r.json());if(d.preferences.due_days!==2||!d.preferences.notify_due||!d.preferences.notify_planned||d.preferences.reminder_time!=='08:45'||!d.defaults.notify_due||d.defaults.notify_planned)throw Error('Preferences missing');
|
||||
if(d.preferences.project_uuid!==savedProject||d.preferences.wiki_prefix!=='/private/mail')throw Error('Wiki destination or existing default project lost');
|
||||
}else if(path==='/v/plan/dashboard'){
|
||||
await wait(()=>document.querySelectorAll('.productivity-day').length>300);if(!document.querySelector('.productivity-day[data-level="2"]'))throw Error('Seven-point completion missing');
|
||||
if(document.querySelectorAll('[data-widget]').length!==6)throw Error('Widget registry missing');
|
||||
|
||||
Reference in new issue
Block a user