package server import ( "context" "database/sql" "encoding/json" "errors" "fmt" "strings" "testing" "time" "github.com/google/uuid" "pageswiki/internal/store" ) func feedHistoryResponse(t *testing.T, s *Server, token, query string) ([]store.FeedActionRecord, bool) { t.Helper() w := feedRequestTest(s, token, "GET", "actions"+query, nil) if w.Code != 200 { t.Fatal(w.Code, w.Body) } if w.Header().Get("Cache-Control") != "private, no-store" { t.Fatal("private response cache policy") } var data struct { Actions []store.FeedActionRecord `json:"actions"` More bool `json:"more"` } if e := json.Unmarshal(w.Body.Bytes(), &data); e != nil { t.Fatal(e) } return data.Actions, data.More } func TestFeedHistoryOrphansExportAndRecheck(t *testing.T) { s, token, domain, actor, source := feedActionFixture(t) ctx := context.Background() a, e := s.store.QueueFeedAction(ctx, domain, actor, newFeedAction(source)) if e != nil { t.Fatal(e) } queued, _ := feedHistoryResponse(t, s, token, "?status=pending") if len(queued) != 1 || queued[0].Status != "queued" || queued[0].CanCheck { t.Fatal("queued history", queued) } fake := &feedRemoteFake{timeout: true} if e = s.syncFeedActions(ctx, fake.client(t)); e != nil { t.Fatal(e) } list, _ := feedHistoryResponse(t, s, token, "") if len(list) != 1 || list[0].Status != "unknown" || list[0].CanCheck || list[0].CheckAfterMS <= time.Now().UnixMilli() || !list[0].ItemPresent || !list[0].SourcePresent { t.Fatal("unknown cooldown", list) } if _, e = s.store.DB.Exec(`DELETE FROM feed_items WHERE source_uuid=?`, source.UUID); e != nil { t.Fatal(e) } if _, e = s.store.DB.Exec(`UPDATE feed_actions SET until_ms=0 WHERE request_id=?`, a.RequestID); e != nil { t.Fatal(e) } // Read-only connector opt-out does not disable read-only result checks. source.Credential, source.WriteEnabled = "", false if _, e = s.store.SaveFeedSource(ctx, domain, actor, source); e != nil { t.Fatal(e) } list, _ = feedHistoryResponse(t, s, token, "?status=unknown") if len(list) != 1 || list[0].ItemPresent || !list[0].CanCheck || list[0].ItemTitle != "" || list[0].SourceTitle != "Remote" { t.Fatal("evicted item lost", list) } w := feedRequestTest(s, token, "GET", "action/export?request_id="+a.RequestID, nil) if w.Code != 200 || !strings.Contains(w.Header().Get("Content-Disposition"), a.RequestID+".json") || w.Header().Get("X-Content-Type-Options") != "nosniff" { t.Fatal("export", w.Code, w.Header()) } var exported struct { Format string `json:"format"` Action store.FeedActionRecord `json:"action"` } if e = json.Unmarshal(w.Body.Bytes(), &exported); e != nil || exported.Format != "pageswiki.feed-action" || exported.Action.RequestID != a.RequestID || exported.Action.Status != "unknown" { t.Fatal("export data", exported, e) } for _, secret := range []string{"credential", "secret", source.URL, `"claim"`, `"until_ms"`} { if strings.Contains(w.Body.String(), secret) { t.Fatal("export leaked internal credentials or worker metadata", secret) } } if w = feedRequestTest(s, token, "POST", "action/check", map[string]string{"request_id": a.RequestID}); w.Code != 202 { t.Fatal(w.Code, w.Body) } if e = s.syncFeedActions(ctx, fake.client(t)); e != nil || fake.edits != 1 { t.Fatal("recheck sent a second mutation", fake.edits, e) } list, _ = feedHistoryResponse(t, s, token, "") if len(list) != 0 { t.Fatal("confirmed action unresolved", list) } list, _ = feedHistoryResponse(t, s, token, "?status=confirmed") if len(list) != 1 || list[0].Status != "confirmed" || list[0].ItemPresent { t.Fatal("evicted receipt missing", list) } if _, e = s.store.DB.Exec(`UPDATE feed_actions SET status='unknown' WHERE request_id=?`, a.RequestID); e != nil { t.Fatal(e) } if _, e = s.store.DB.Exec(`DELETE FROM feed_sources WHERE uuid=?`, source.UUID); e != nil { t.Fatal(e) } list, _ = feedHistoryResponse(t, s, token, "?source="+source.UUID) if len(list) != 1 || list[0].SourcePresent || list[0].ItemPresent || list[0].CanCheck || list[0].Error != "source_deleted" { t.Fatal("deleted source receipt lost", list) } if w = feedRequestTest(s, token, "POST", "action/check", map[string]string{"request_id": a.RequestID}); w.Code != 412 { t.Fatal("deleted source recheck accepted", w.Code) } if w = feedRequestTest(s, token, "GET", "action/export?request_id="+a.RequestID, nil); w.Code != 200 { t.Fatal("deleted source export", w.Code) } source.Version, source.Credential = 0, "" source.URL = "https://different.example.org/api/greader.php" if _, e = s.store.SaveFeedSource(ctx, domain, actor, source); !errors.Is(e, store.ErrTaskConflict) { t.Fatal("rebound orphan receipt", e) } other, e := s.store.CreatePasswordAccount(domain, "historyreuse", "long test password", "Other", "") if e != nil { t.Fatal(e) } if _, e = s.store.SaveFeedSource(ctx, domain, other.UUID, source); !errors.Is(e, store.ErrTaskConflict) { t.Fatal("rebound unresolved receipt across owners", e) } if _, e = s.store.DB.Exec(`UPDATE feed_actions SET status='confirmed' WHERE request_id=?`, a.RequestID); e != nil { t.Fatal(e) } if _, e = s.store.SaveFeedSource(ctx, domain, actor, source); !errors.Is(e, store.ErrTaskConflict) { t.Fatal("rebound terminal receipt", e) } } func TestFeedHistoryPaginationValidationAndIsolation(t *testing.T) { s, token, domain, actor, source := feedActionFixture(t) ctx := context.Background() // Same millisecond, stable unique tie-breaker; well above a page boundary. for i := 0; i < 107; i++ { if _, e := s.store.DB.Exec(`INSERT INTO feed_actions(domain_uuid,owner_uuid,request_id,source_uuid,item_id,field,desired,source_version,status,created_ms) VALUES(?,?,?,?,?,'read',1,1,'confirmed',1234)`, domain, actor, uuid.NewString(), source.UUID, fmt.Sprint(i)); e != nil { t.Fatal(e) } } query, seen := "?status=all", map[string]bool{} for { list, more := feedHistoryResponse(t, s, token, query) if len(list) > 50 { t.Fatal("unbounded list") } for _, a := range list { if seen[a.RequestID] { t.Fatal("duplicate cursor record") } seen[a.RequestID] = true } if !more { break } last := list[len(list)-1] query = fmt.Sprintf("?status=all&before=%d&key=%s", last.CreatedMS, last.RequestID) } if len(seen) != 107 { t.Fatal("cursor omissions", len(seen)) } for _, query := range []string{"?status=bogus", "?source=bad", "?before=bad", "?before=-1", "?before=1", "?key=" + uuid.NewString(), "?before=1&key=bad"} { if w := feedRequestTest(s, token, "GET", "actions"+query, nil); w.Code != 422 { t.Fatal("invalid query", query, w.Code) } } other, e := s.store.CreatePasswordAccount(domain, "historyother", "long test password", "Other", "") if e != nil { t.Fatal(e) } otherToken, e := s.store.CreateAuthSession(domain, other.UUID) if e != nil { t.Fatal(e) } otherList, _ := feedHistoryResponse(t, s, otherToken, "?status=all&source="+source.UUID) if len(otherList) != 0 { t.Fatal("other owner's HTTP history leaked") } list, e := s.store.FeedActionHistory(ctx, domain, other.UUID, store.FeedActionQuery{Source: source.UUID, Status: "all", Before: -1}) if e != nil || len(list) != 0 { t.Fatal("other owner's actions leaked", list, e) } for request := range seen { if w := feedRequestTest(s, otherToken, "GET", "action/export?request_id="+request, nil); w.Code != 404 { t.Fatal("other owner's HTTP export", w.Code) } if _, e = s.store.FeedActionRecord(ctx, domain, other.UUID, request); !errors.Is(e, sql.ErrNoRows) { t.Fatal("other owner's receipt leaked", e) } break } if _, e = s.store.FeedActionHistory(ctx, uuid.NewString(), actor, store.FeedActionQuery{Status: "all", Before: -1}); !errors.Is(e, store.ErrAccessDenied) { t.Fatal("other domain", e) } for _, key := range []string{"actions", "action/export?request_id=" + uuid.NewString()} { if w := feedRequestTest(s, "", "GET", key, nil); w.Code != 401 { t.Fatal("anonymous access", w.Code) } } if w := feedRequestTest(s, token, "GET", "action/export?request_id=invalid", nil); w.Code != 422 { t.Fatal("invalid receipt identifier", w.Code) } if e = s.setSystemExtensionEnabled(domain, "organizer.feeds", false); e != nil { t.Fatal(e) } if w := feedRequestTest(s, token, "GET", "actions", nil); w.Code != 404 { t.Fatal("extension disabled", w.Code) } if _, e = s.store.FeedActionRecord(ctx, domain, actor, uuid.NewString()); !errors.Is(e, store.ErrAccessDenied) { t.Fatal("store disabled", e) } } func TestFeedHistoryReadonlyAndSourceDisabled(t *testing.T) { s, token, domain, actor, source := feedActionFixture(t) ctx := context.Background() a, e := s.store.QueueFeedAction(ctx, domain, actor, newFeedAction(source)) if e != nil { t.Fatal(e) } if _, e = s.store.DB.Exec(`UPDATE feed_actions SET status='unknown',until_ms=0 WHERE request_id=?`, a.RequestID); e != nil { t.Fatal(e) } if e = s.store.SetPrincipalState(domain, actor, "readonly"); e != nil { t.Fatal(e) } token, e = s.store.CreateAuthSession(domain, actor) if e != nil { t.Fatal(e) } list, _ := feedHistoryResponse(t, s, token, "") if len(list) != 1 || list[0].CanCheck { t.Fatal("readonly check permission", list) } if w := feedRequestTest(s, token, "POST", "action/check", map[string]string{"request_id": a.RequestID}); w.Code != 403 { t.Fatal("readonly mutation", w.Code) } if w := feedRequestTest(s, token, "GET", "action/export?request_id="+a.RequestID, nil); w.Code != 200 { t.Fatal("readonly export", w.Code) } if e = s.store.SetPrincipalState(domain, actor, "active"); e != nil { t.Fatal(e) } token, e = s.store.CreateAuthSession(domain, actor) if e != nil { t.Fatal(e) } source.Credential, source.Enabled = "", false if _, e = s.store.SaveFeedSource(ctx, domain, actor, source); e != nil { t.Fatal(e) } list, _ = feedHistoryResponse(t, s, token, "") if len(list) != 1 || list[0].CanCheck || !list[0].SourcePresent { t.Fatal("disabled source recheck", list) } }