Remove obsolete article and scrape-job screens and APIs, preserve reports, and clear legacy API caches from the service worker. Refs #8 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
124 lines
5.9 KiB
Go
124 lines
5.9 KiB
Go
package api_test
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/tradarr/backend/internal/api"
|
|
"github.com/tradarr/backend/internal/api/handlers"
|
|
"github.com/tradarr/backend/internal/auth"
|
|
"github.com/tradarr/backend/internal/config"
|
|
"github.com/tradarr/backend/internal/models"
|
|
)
|
|
|
|
func TestRetiredArticleAndScrapeJobEndpointsReturnNotFound(t *testing.T) {
|
|
db, _ := ticket7DB(t)
|
|
for _, statement := range []string{
|
|
`CREATE TABLE sources (id UUID PRIMARY KEY, name TEXT NOT NULL)`,
|
|
`CREATE TABLE articles (id UUID PRIMARY KEY, source_id UUID NOT NULL, title TEXT NOT NULL, content TEXT NOT NULL, url TEXT NOT NULL, published_at TIMESTAMPTZ, created_at TIMESTAMPTZ NOT NULL)`,
|
|
`CREATE TABLE scrape_jobs (id UUID PRIMARY KEY, source_id UUID NOT NULL, status TEXT NOT NULL, started_at TIMESTAMPTZ, finished_at TIMESTAMPTZ, articles_found INT NOT NULL, error_msg TEXT NOT NULL, created_at TIMESTAMPTZ NOT NULL)`,
|
|
`INSERT INTO sources (id,name) VALUES ('00000000-0000-0000-0000-000000000010','Fixture source')`,
|
|
`INSERT INTO articles (id,source_id,title,content,url,created_at) VALUES ('00000000-0000-0000-0000-000000000001','00000000-0000-0000-0000-000000000010','private-title','private-body','https://private.test/article',NOW())`,
|
|
`INSERT INTO scrape_jobs (id,source_id,status,articles_found,error_msg,created_at) VALUES ('00000000-0000-0000-0000-000000000020','00000000-0000-0000-0000-000000000010','done',1,'',NOW())`,
|
|
} {
|
|
if _, err := db.Exec(statement); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
// Snapshot every stored field, so an UPDATE cannot pass a row-count check.
|
|
const snapshotSQL = `SELECT jsonb_build_object('articles',(SELECT jsonb_agg(to_jsonb(a) ORDER BY id) FROM articles a),'jobs',(SELECT jsonb_agg(to_jsonb(j) ORDER BY id) FROM scrape_jobs j))::text`
|
|
var before, after string
|
|
if err := db.QueryRow(snapshotSQL).Scan(&before); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, statement := range []string{
|
|
`CREATE FUNCTION forbid_legacy_write() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN RAISE EXCEPTION 'legacy write prohibited'; END $$`,
|
|
`CREATE TRIGGER article_write_guard BEFORE INSERT OR UPDATE OR DELETE ON articles FOR EACH STATEMENT EXECUTE FUNCTION forbid_legacy_write()`,
|
|
`CREATE TRIGGER job_write_guard BEFORE INSERT OR UPDATE OR DELETE ON scrape_jobs FOR EACH STATEMENT EXECUTE FUNCTION forbid_legacy_write()`,
|
|
} {
|
|
if _, err := db.Exec(statement); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
var articlesBefore, jobsBefore int
|
|
if err := db.QueryRow(`SELECT COUNT(*) FROM articles`).Scan(&articlesBefore); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := db.QueryRow(`SELECT COUNT(*) FROM scrape_jobs`).Scan(&jobsBefore); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
router := api.SetupRouter(handlers.New(models.NewRepository(db), &config.Config{JWTSecret: ticket7JWTSecret}, nil, nil, nil, nil), ticket7JWTSecret)
|
|
userToken, err := auth.GenerateToken("00000000-0000-0000-0000-000000000001", "reader@example.test", "user", ticket7JWTSecret)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
adminToken, err := auth.GenerateToken("00000000-0000-0000-0000-000000000002", "admin@example.test", "admin", ticket7JWTSecret)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, tc := range []struct {
|
|
name string
|
|
path string
|
|
role string
|
|
method string
|
|
}{
|
|
{name: "article collection", path: "/api/articles", role: userToken, method: http.MethodGet},
|
|
{name: "article detail", path: "/api/articles/00000000-0000-0000-0000-000000000001", role: userToken, method: http.MethodGet},
|
|
{name: "admin scrape jobs", path: "/api/admin/scrape-jobs", role: adminToken, method: http.MethodGet},
|
|
{name: "scrape trigger", path: "/api/admin/scrape-jobs/trigger", role: adminToken, method: http.MethodPost},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
request := httptest.NewRequest(tc.method, tc.path, nil)
|
|
request.Header.Set("Authorization", "Bearer "+tc.role)
|
|
response := httptest.NewRecorder()
|
|
router.ServeHTTP(response, request)
|
|
if response.Code != http.StatusNotFound {
|
|
t.Fatalf("%s %s: got %d, want 404; body=%s", tc.method, tc.path, response.Code, response.Body.String())
|
|
}
|
|
})
|
|
}
|
|
for _, route := range router.Routes() {
|
|
if route.Path == "/api/articles" || route.Path == "/api/articles/:id" || route.Path == "/api/admin/scrape-jobs" || route.Path == "/api/admin/scrape-jobs/trigger" {
|
|
t.Errorf("retired route remains callable: %s %s", route.Method, route.Path)
|
|
}
|
|
}
|
|
var articlesAfter, jobsAfter int
|
|
if err := db.QueryRow(`SELECT COUNT(*) FROM articles`).Scan(&articlesAfter); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := db.QueryRow(`SELECT COUNT(*) FROM scrape_jobs`).Scan(&jobsAfter); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if articlesAfter != articlesBefore || jobsAfter != jobsBefore {
|
|
t.Fatalf("retired requests changed persisted data: articles %d→%d, scrape jobs %d→%d", articlesBefore, articlesAfter, jobsBefore, jobsAfter)
|
|
}
|
|
if err := db.QueryRow(snapshotSQL).Scan(&after); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if before != after {
|
|
t.Fatalf("historical records changed: before=%s after=%s", before, after)
|
|
}
|
|
t.Logf("no writes from retired requests: articles=%d, scrape_jobs=%d before and after", articlesAfter, jobsAfter)
|
|
}
|
|
|
|
// Route retirement must also hold without a database or retrieval adapters.
|
|
func TestRetiredSurfacesAreAbsentWithoutDatabase(t *testing.T) {
|
|
router := api.SetupRouter(handlers.New(nil, &config.Config{}, nil, nil, nil, nil), ticket7JWTSecret)
|
|
token, err := auth.GenerateToken("admin", "admin@example.test", "admin", ticket7JWTSecret)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, path := range []string{"/api/articles", "/api/articles/legacy-id", "/api/admin/scrape-jobs", "/api/admin/scrape-jobs/trigger"} {
|
|
for _, method := range []string{http.MethodGet, http.MethodPost} {
|
|
request := httptest.NewRequest(method, path, nil)
|
|
request.Header.Set("Authorization", "Bearer "+token)
|
|
response := httptest.NewRecorder()
|
|
router.ServeHTTP(response, request)
|
|
if response.Code != http.StatusNotFound {
|
|
t.Fatalf("%s %s: status=%d", method, path, response.Code)
|
|
}
|
|
}
|
|
}
|
|
}
|