package main import ( "io" "net/http" "net/http/httptest" "os" "path/filepath" "sync/atomic" "testing" ) func TestInvalidOrUnconfirmedLiveModeNeverStartsIngestion(t *testing.T) { for _, args := range [][]string{{"--mode=live"}, {"--mode=offline", "--allow-live"}, {"--limit=0"}, {"--limit=4"}, {"--mode=unknown"}} { if run(args) != 2 { t.Fatal("invalid configuration accepted") } } } func TestInvalidScraperEndpointIsRejectedBeforeCredentialReadAndAnyPOST(t *testing.T) { var posted atomic.Int64 trap := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { posted.Add(1); w.WriteHeader(204) })) defer trap.Close() // Nonexistent fixture files make an accidental credential read observable as // credentials_unavailable, without reading any real credential or going live. for _, endpoint := range []string{trap.URL, "http://ingestion-scraper:22", "http://ingestion-scraper:8080", "http://127.0.0.1:3001"} { t.Setenv("INGESTION_SCRAPER_URL", endpoint) t.Setenv("BLOOMBERG_USERNAME_FILE", filepath.Join(t.TempDir(), "nonexistent-username")) t.Setenv("BLOOMBERG_PASSWORD_FILE", filepath.Join(t.TempDir(), "nonexistent-password")) previous := os.Stderr r, w, err := os.Pipe() if err != nil { t.Fatal(err) } os.Stderr = w code := run([]string{"--mode=live", "--allow-live"}) os.Stderr = previous w.Close() message, _ := io.ReadAll(r) r.Close() if code != 2 || string(message) != "invalid_scraper_service\n" || posted.Load() != 0 { t.Fatal("invalid service reached credential read or POST") } } }