feat(scraper): bound article enrichment diagnostics (#40)
This commit is contained in:
121
backend/internal/ingestionbench/architect_blockers_test.go
Normal file
121
backend/internal/ingestionbench/architect_blockers_test.go
Normal file
@ -0,0 +1,121 @@
|
||||
package ingestionbench
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/tradarr/backend/internal/scraper"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
type fixtureAudit struct {
|
||||
feeds, articles int
|
||||
t *testing.T
|
||||
source string
|
||||
}
|
||||
|
||||
func (a *fixtureAudit) RoundTrip(r *http.Request) (*http.Response, error) {
|
||||
if r.URL.Host == "feeds.finance.yahoo.com" || r.URL.Path == "/id/10001147/device/rss/rss.html" {
|
||||
a.feeds++
|
||||
} else {
|
||||
a.articles++
|
||||
valid := r.URL.Scheme == "https"
|
||||
if a.source == "yahoo_finance" {
|
||||
valid = valid && r.URL.Host == "finance.yahoo.com" && strings.HasPrefix(r.URL.Path, "/news/") && strings.HasSuffix(r.URL.Path, ".html")
|
||||
} else {
|
||||
valid = valid && r.URL.Host == "www.cnbc.com" && strings.HasPrefix(r.URL.Path, "/2026/01/02/") && strings.HasSuffix(r.URL.Path, ".html")
|
||||
}
|
||||
if !valid {
|
||||
a.t.Error("offline fixture article URL is incompatible with production path contracts")
|
||||
}
|
||||
}
|
||||
return (OfflineTransport{}).RoundTrip(r)
|
||||
}
|
||||
func TestOfflineFixtureMatrixActuallyTraversesCollectorsAndEnrichers(t *testing.T) {
|
||||
for _, index := range []int{0, 1} {
|
||||
t.Run([]string{"yahoo_finance", "cnbc_business"}[index], func(t *testing.T) {
|
||||
key := []string{"yahoo_finance", "cnbc_business"}[index]
|
||||
audit := &fixtureAudit{t: t, source: key}
|
||||
sources := ProductionSources(&http.Client{Transport: audit}, RPCClient(), "http://127.0.0.1:1", "offline-fixture", "offline-fixture", "FIXTURE")
|
||||
listed, err := sources[index].Feed.Scrape(context.Background(), sources[index].Symbols)
|
||||
if err != nil || len(listed) != 3 {
|
||||
t.Fatal("fixture collector matrix failed")
|
||||
}
|
||||
enriched, err := sources[index].Feed.Enrich(context.Background(), listed)
|
||||
if err != nil || len(enriched) != 3 || audit.feeds != 1 || audit.articles != 3 {
|
||||
t.Fatal("production enrichment transport was not exercised three times")
|
||||
}
|
||||
for i, want := range []string{scraper.ContentComplete, scraper.ContentIncomplete, scraper.ContentUnavailable} {
|
||||
if scraper.ContentStatus(enriched[i]) != want || enriched[i].URL != listed[i].URL || enriched[i].Title != listed[i].Title {
|
||||
t.Fatal("fixture matrix state or listing association failed")
|
||||
}
|
||||
}
|
||||
if enriched[0].PublishedAt != nil {
|
||||
t.Fatal("complete fixture must retain nullable date")
|
||||
}
|
||||
expected := time.Date(2026, 1, 2, 3, 4, 5, 0, time.UTC)
|
||||
for _, a := range enriched[1:] {
|
||||
if a.PublishedAt == nil || !a.PublishedAt.Equal(expected) {
|
||||
t.Fatal("observed fixture date is not reliable")
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
func TestLiveScraperRejectsEveryUnapprovedOriginAndPort(t *testing.T) {
|
||||
for _, base := range []string{"http://ingestion-scraper:22", "http://ingestion-scraper:8080", "https://ingestion-scraper:3001", "http://127.0.0.1:3001", "http://127.0.0.1:22", "http://[::1]:3001", "http://ingestion-scraper:3001/other"} {
|
||||
if _, err := TrustedScraperURL(base); err == nil {
|
||||
t.Error("unapproved live credential destination accepted")
|
||||
}
|
||||
}
|
||||
}
|
||||
func TestRejectedLiveDestinationReceivesNoCredentialPOST(t *testing.T) {
|
||||
var requests atomic.Int64
|
||||
trap := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
requests.Add(1)
|
||||
io.Copy(io.Discard, r.Body)
|
||||
w.WriteHeader(204)
|
||||
}))
|
||||
defer trap.Close()
|
||||
req, _ := http.NewRequest("POST", trap.URL+"/bloomberg/scrape", strings.NewReader(`{"username":"synthetic-fixture","password":"synthetic-fixture"}`))
|
||||
_, err := RPCClient().Do(req)
|
||||
if err == nil || requests.Load() != 0 {
|
||||
t.Fatal("rejected endpoint received a credential request")
|
||||
}
|
||||
}
|
||||
|
||||
type rpcAuditTransport struct{ calls int }
|
||||
|
||||
func (a *rpcAuditTransport) RoundTrip(r *http.Request) (*http.Response, error) {
|
||||
a.calls++
|
||||
return &http.Response{StatusCode: 204, Header: make(http.Header), Body: io.NopCloser(strings.NewReader("")), Request: r}, nil
|
||||
}
|
||||
func TestLiveRPCGuardAllowsOnlyTheExactServiceAndBothKnownOperations(t *testing.T) {
|
||||
audit := &rpcAuditTransport{}
|
||||
guard := credentialRPCTransport{endpoint: LiveScraperEndpoint, base: audit}
|
||||
for _, target := range []string{"http://ingestion-scraper:22/bloomberg/scrape", "http://ingestion-scraper:8080/bloomberg/enrich", "http://127.0.0.1:3001/bloomberg/scrape", "https://ingestion-scraper:3001/bloomberg/scrape", "http://ingestion-scraper:3001/other", "http://ingestion-scraper:3001/bloomberg/scrape?forward=1"} {
|
||||
req, _ := http.NewRequest("POST", target, strings.NewReader("synthetic-fixture"))
|
||||
if _, err := guard.RoundTrip(req); err == nil {
|
||||
t.Error("unapproved credential request accepted")
|
||||
}
|
||||
}
|
||||
if audit.calls != 0 {
|
||||
t.Fatal("credential request reached an unapproved transport")
|
||||
}
|
||||
for _, path := range []string{"/bloomberg/scrape", "/bloomberg/enrich"} {
|
||||
req, _ := http.NewRequest("POST", LiveScraperEndpoint+path, strings.NewReader("synthetic-fixture"))
|
||||
if _, err := guard.RoundTrip(req); err != nil {
|
||||
t.Fatal("expected service operation blocked")
|
||||
}
|
||||
}
|
||||
if audit.calls != 2 {
|
||||
t.Fatal("approved operations not delegated")
|
||||
}
|
||||
if actual, err := TrustedScraperURL(LiveScraperEndpoint); err != nil || actual != LiveScraperEndpoint {
|
||||
t.Fatal("exact service endpoint rejected")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user