package server import ( "context" "encoding/json" "errors" "io" "net/http" "net/url" "strings" "testing" "github.com/google/uuid" "pageswiki/internal/store" ) const testFeedCatalog = `{"subscriptions":[{"id":"feed/1","title":"Same title","categories":[{"id":"user/alice/label/Tech","label":"技術"}]},{"id":"feed/2","title":"Same title","categories":[{"id":"user/alice/label/Other","label":"技術"}]},{"id":"feed/3","title":"Empty feed","categories":[]}]}` func TestFreshRSSCatalogValidation(t *testing.T) { catalog, e := parseFreshRSSCatalog([]byte(testFeedCatalog)) if e != nil || len(catalog) != 3 || catalog[0].Categories[0].ID == catalog[1].Categories[0].ID { t.Fatal(catalog, e) } for _, raw := range []string{`{}`, `{"subscriptions":null}`, `{"subscriptions":[{"id":""}]}`, `{"subscriptions":[{"id":"feed/1"},{"id":"feed/1"}]}`, `{"subscriptions":[{"id":"feed/1","categories":[{"id":"cat","label":"A"},{"id":"cat","label":"A"}]}]}`, `{"subscriptions":[{"id":"feed/1","categories":[{"id":"cat","label":"A"}]},{"id":"feed/2","categories":[{"id":"cat","label":"B"}]}]}`} { if _, e := parseFreshRSSCatalog([]byte(raw)); e == nil { t.Fatal("accepted", raw) } } if c, e := parseFreshRSSCatalog([]byte(`{"subscriptions":[]}`)); e != nil || len(c) != 0 { t.Fatal(c, e) } big := make([]store.FeedRemoteSubscription, 1001) raw, _ := json.Marshal(map[string]any{"subscriptions": big}) if _, e := parseFreshRSSCatalog(raw); e == nil { t.Fatal("unbounded catalog") } } func TestFeedCatalogBackgroundSnapshotIsolation(t *testing.T) { s, token, domain, actor, local := feedFixture(t) ctx := context.Background() if e := s.store.FeedSourceAction(ctx, domain, actor, local.UUID, "delete", local.Version); e != nil { t.Fatal(e) } v := store.FeedSource{UUID: uuid.NewString(), Provider: "freshrss", Title: "FreshRSS", URL: "https://example.org/api/greader.php", Enabled: true, Interval: 300} v.Credential, _ = s.store.SealFeedCredential(domain, actor, v.UUID, "reader", "secret") if _, e := s.store.SaveFeedSource(ctx, domain, actor, v); e != nil { t.Fatal(e) } calls := 0 failCatalog := false failFolders := false client := &http.Client{Transport: feedTransportFunc(func(r *http.Request) (*http.Response, error) { calls++ body := "" switch { case strings.HasSuffix(r.URL.Path, "/accounts/ClientLogin"): if r.Method != "POST" { t.Error("login method") } body = "Auth=synthetic" case strings.HasSuffix(r.URL.Path, "/stream/contents/reading-list"): if r.Method != "GET" || r.URL.Query().Get("n") != "200" { t.Error("article request") } body = `{"items":[{"id":"a","title":"First","origin":{"title":"Same title","streamId":"feed/1"},"categories":["user/alice/label/Tech"]},{"id":"b","title":"Second","origin":{"title":"Same title","streamId":"feed/2"},"categories":["user/alice/label/Other"]}]}` if failCatalog || failFolders { body = `{"items":[]}` } case strings.HasSuffix(r.URL.Path, "/subscription/list"): if r.Method != "GET" || r.Header.Get("Authorization") != "GoogleLogin auth=synthetic" { t.Error("catalog request") } if failCatalog { return nil, errors.New("synthetic timeout") } body = testFeedCatalog case strings.HasSuffix(r.URL.Path, "/tag/list"): if failFolders { return nil, errors.New("synthetic folder failure") } body = `{"tags":[{"id":"user/-/label/Empty/Folder","type":"folder"},{"id":"user/-/label/Not a folder","type":"tag"}]}` default: t.Fatalf("unexpected external request %s %s", r.Method, r.URL.Path) } return &http.Response{StatusCode: 200, Header: http.Header{}, Body: io.NopCloser(strings.NewReader(body))}, nil })} run := func() { t.Helper() if _, e := s.store.DB.Exec(`UPDATE feed_sources SET next_ms=0 WHERE uuid=?`, v.UUID); e != nil { t.Fatal(e) } if e := s.syncFeedSourcesWithClient(ctx, &store.EventJob{}, client); e != nil { t.Fatal(e) } } run() if calls != 4 { t.Fatal("fetch count", calls) } w := feedRequestTest(s, token, "GET", "catalog?source="+v.UUID, nil) if w.Code != 200 || !strings.Contains(w.Body.String(), "Empty feed") || strings.Contains(w.Body.String(), "secret") { t.Fatal(w.Code, w.Body) } for _, scope := range []string{"feed=feed%2F1", "category=" + url.QueryEscape("user/alice/label/Tech")} { w = feedRequestTest(s, token, "GET", "items?source="+v.UUID+"&"+scope, nil) var data struct { Items []store.FeedItem `json:"items"` } json.Unmarshal(w.Body.Bytes(), &data) if w.Code != 200 || len(data.Items) != 1 || data.Items[0].ID != "a" { t.Fatal(scope, w.Code, w.Body) } } w = feedRequestTest(s, token, "GET", "items?feed=feed%2F1", nil) if w.Code != 422 { t.Fatal("unscoped remote ID", w.Code, w.Body) } other, e := s.store.CreatePasswordAccount(domain, "catalogother", "long test password", "Other", "") if e != nil { t.Fatal(e) } otherToken, e := s.store.CreateAuthSession(domain, other.UUID) if e != nil { t.Fatal(e) } if w = feedRequestTest(s, otherToken, "GET", "catalog?source="+v.UUID, nil); w.Code == 200 { t.Fatal("cross-owner catalog leak") } if w = feedRequestTest(s, "", "GET", "catalog?source="+v.UUID, nil); w.Code == 200 { t.Fatal("anonymous catalog leak") } if _, e := s.store.FeedCatalog(ctx, uuid.NewString(), actor, v.UUID); e == nil { t.Fatal("cross-domain catalog leak") } if calls != 4 { t.Fatal("foreground external request") } failFolders = true run() kept, e := s.store.FeedCatalogSnapshot(ctx, domain, actor, v.UUID) if e != nil || len(kept.Folders) != 3 || len(kept.Subscriptions) != 3 { t.Fatal("folder failure erased snapshot", kept, e) } failFolders = false failCatalog = true run() catalog, e := s.store.FeedCatalog(ctx, domain, actor, v.UUID) if e != nil || len(catalog) != 3 { t.Fatal("lost catalog", catalog, e) } items, e := s.store.FeedItems(ctx, domain, actor, v.UUID, "", "", -1, "") if e != nil || len(items) != 2 { t.Fatal("lost articles", items, e) } if e := s.setSystemExtensionEnabled(domain, "organizer.feeds", false); e != nil { t.Fatal(e) } if _, e := s.store.FeedCatalog(ctx, domain, actor, v.UUID); !errors.Is(e, store.ErrAccessDenied) { t.Fatal("disabled read", e) } }