package checker import ( "context" "encoding/json" "net/http" "net/http/httptest" "strings" "testing" sdk "git.happydns.org/checker-sdk-go/checker" ) func newVTServer(t *testing.T, status int, body string) (string, func()) { t.Helper() srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.Header.Get("x-apikey") == "" { t.Errorf("missing x-apikey header") } w.WriteHeader(status) _, _ = w.Write([]byte(body)) })) return srv.URL + "/", srv.Close } func TestVTSource_NoKey(t *testing.T) { s := &virusTotalSource{endpoint: virusTotalEndpoint} r := s.Query(context.Background(), "example.com", "example.com", sdk.CheckerOptions{})[0] if r.Enabled { t.Errorf("expected disabled without API key, got %+v", r) } } func TestVTSource_Listed(t *testing.T) { body := `{"data":{"attributes":{ "reputation":-25, "last_analysis_stats":{"harmless":50,"malicious":3,"suspicious":1,"undetected":40,"timeout":0}, "last_analysis_results":{ "E1":{"category":"malicious","result":"phishing","engine_name":"E1"}, "E2":{"category":"suspicious","result":"susp","engine_name":"E2"}, "E3":{"category":"harmless","result":"clean","engine_name":"E3"} } }}}` endpoint, stop := newVTServer(t, http.StatusOK, body) defer stop() s := &virusTotalSource{endpoint: endpoint} r := s.Query(context.Background(), "example.com", "example.com", sdk.CheckerOptions{"virustotal_api_key": "k"})[0] if !r.Listed || r.Severity != SeverityCrit { t.Errorf("expected listed+crit, got %+v", r) } var d vtDetails if err := json.Unmarshal(r.Details, &d); err != nil { t.Fatalf("details decode: %v", err) } if d.Malicious != 3 || d.Suspicious != 1 || d.Reputation != -25 { t.Errorf("counts wrong: %+v", d) } if len(d.Vendors) != 2 || d.Vendors[0].Category != "malicious" { t.Errorf("vendor ordering wrong: %+v", d.Vendors) } html, err := s.RenderDetail(r) if err != nil || !strings.Contains(string(html), "malicious") { t.Errorf("RenderDetail html=%q err=%v", html, err) } } func TestVTSource_NotFound(t *testing.T) { endpoint, stop := newVTServer(t, http.StatusNotFound, `{"error":{"code":"NotFoundError"}}`) defer stop() s := &virusTotalSource{endpoint: endpoint} r := s.Query(context.Background(), "example.com", "example.com", sdk.CheckerOptions{"virustotal_api_key": "k"})[0] if r.Listed || r.Error != "" { t.Errorf("404 should be quiet not-listed: %+v", r) } if !strings.Contains(r.Reference, "example.com") { t.Errorf("reference URL missing: %+v", r) } }