package api_test import ( "bytes" "database/sql" "encoding/json" "fmt" "net/http" "net/http/httptest" "testing" "time" "github.com/lib/pq" "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 TestTicket42AdminCannotReactivateOrModifyRetiredSourcesPostgres(t *testing.T) { db, err := sql.Open("postgres", postgresTestURL(t)) if err != nil { t.Fatal(err) } defer db.Close() db.SetMaxOpenConns(1) schema := fmt.Sprintf("retired42_%d", time.Now().UnixNano()) if _, err = db.Exec(`CREATE SCHEMA ` + pq.QuoteIdentifier(schema)); err != nil { t.Fatal(err) } defer db.Exec(`DROP SCHEMA ` + pq.QuoteIdentifier(schema) + ` CASCADE`) if _, err = db.Exec(`SET search_path TO ` + pq.QuoteIdentifier(schema)); err != nil { t.Fatal(err) } for _, q := range []string{ `CREATE TABLE users(id TEXT PRIMARY KEY,email TEXT,role TEXT,password_hash TEXT,created_at TIMESTAMPTZ DEFAULT NOW(),updated_at TIMESTAMPTZ DEFAULT NOW())`, `INSERT INTO users(id,email,role,password_hash) VALUES('admin','admin@example.test','admin','hash')`, `CREATE TABLE sources(id TEXT PRIMARY KEY,name TEXT,type TEXT,source_key TEXT,enabled BOOLEAN,article_limit INT,created_at TIMESTAMPTZ DEFAULT NOW())`, `INSERT INTO sources(id,name,type,source_key,enabled,article_limit) VALUES('y','Yahoo Finance','stocktwits','yahoo_finance',false,83),('c','CNBC Business','cnbc_business','cnbc_business',false,31)`, } { if _, err = db.Exec(q); err != nil { t.Fatal(err) } } const secret = "retired42-fixture" token, _ := auth.GenerateToken("admin", "admin@example.test", "admin", secret) router := api.SetupRouter(handlers.New(models.NewRepository(db), &config.Config{JWTSecret: secret}, nil, nil, nil, nil), secret) request := func(method, path, body string) *httptest.ResponseRecorder { r := httptest.NewRequest(method, path, bytes.NewBufferString(body)) r.Header.Set("Authorization", "Bearer "+token) r.Header.Set("Content-Type", "application/json") w := httptest.NewRecorder() router.ServeHTTP(w, r) return w } for _, id := range []string{"y", "c"} { for _, body := range []string{`{"enabled":true}`, `{"enabled":false,"article_limit":1}`, `{"enabled":true,"feed_urls":["https://feed.quanwenrss.com/feed/bloomberg-latest.xml"]}`} { w := request(http.MethodPut, "/api/admin/sources/"+id, body) if w.Code != 409 || w.Body.String() != `{"error":"source_not_supported"}` { t.Error("retired source modification/re-activation not explicitly rejected") } } } // Even a direct SQL reactivation must not make a legacy row operational. if _, err = db.Exec(`UPDATE sources SET enabled=true`); err != nil { t.Fatal(err) } w := request(http.MethodGet, "/api/admin/sources", "") var result struct { Data []struct { ID, Name string SourceKey *string `json:"source_key"` Enabled, Supported, Operational bool Limit int `json:"article_limit"` } } if w.Code != 200 || json.Unmarshal(w.Body.Bytes(), &result) != nil || len(result.Data) != 2 { t.Fatal("retired admin rows were removed") } for _, s := range result.Data { if !s.Enabled || s.Supported || s.Operational || s.SourceKey == nil || (s.ID == "y" && (s.Limit != 83 || s.Name != "Yahoo Finance" || *s.SourceKey != "yahoo_finance")) || (s.ID == "c" && (s.Limit != 31 || s.Name != "CNBC Business" || *s.SourceKey != "cnbc_business")) { t.Fatal("historical metadata was changed or source still operational") } } }