package server import ( "archive/zip" "bytes" "encoding/json" "net/http" "net/http/httptest" "testing" "git.mchus.pro/mchus/logpile/internal/models" ) func samplePrivacyScan() *models.PrivacyScan { return &models.PrivacyScan{ FilesScanned: 3, Customers: []models.CustomerGuess{{Domain: "acme.ru", Confidence: "high", Hits: 4}}, Findings: []models.PrivacyFinding{ {Category: "resolv", Severity: "high", Path: "resolv.conf", Line: 1, Match: "corp.acme.ru"}, }, Summary: models.PrivacySummary{Total: 1, High: 1, ByCategory: map[string]int{"resolv": 1}}, } } func TestHandleGetPrivacyScan_NotLoaded(t *testing.T) { srv := &Server{} req := httptest.NewRequest("GET", "/api/privacy-scan", nil) w := httptest.NewRecorder() srv.handleGetPrivacyScan(w, req) if w.Code != http.StatusOK { t.Fatalf("status = %d", w.Code) } var body map[string]any if err := json.Unmarshal(w.Body.Bytes(), &body); err != nil { t.Fatal(err) } if body["loaded"] != false { t.Fatalf("want loaded:false, got %v", body) } } func TestHandleGetPrivacyScan_Loaded(t *testing.T) { srv := &Server{} srv.SetResult(&models.AnalysisResult{PrivacyScan: samplePrivacyScan()}) req := httptest.NewRequest("GET", "/api/privacy-scan", nil) w := httptest.NewRecorder() srv.handleGetPrivacyScan(w, req) if w.Code != http.StatusOK { t.Fatalf("status = %d", w.Code) } var got models.PrivacyScan if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil { t.Fatal(err) } if len(got.Customers) != 1 || got.Customers[0].Domain != "acme.ru" { t.Fatalf("unexpected payload: %+v", got) } } func TestBuildRawExportBundle_IncludesPrivacyReport(t *testing.T) { pkg := newRawExportFromUploadedFile("dump.tar.gz", "application/gzip", []byte("x"), &models.AnalysisResult{}) result := &models.AnalysisResult{PrivacyScan: samplePrivacyScan()} raw, err := buildRawExportBundle(pkg, result, "test") if err != nil { t.Fatal(err) } zr, err := zip.NewReader(bytes.NewReader(raw), int64(len(raw))) if err != nil { t.Fatal(err) } found := false for _, f := range zr.File { if f.Name == rawExportBundlePrivacyFile { found = true } } if !found { t.Fatalf("%s missing from bundle", rawExportBundlePrivacyFile) } } func TestBuildRawExportBundle_NoPrivacyReportWhenNil(t *testing.T) { pkg := newRawExportFromUploadedFile("dump.tar.gz", "application/gzip", []byte("x"), &models.AnalysisResult{}) raw, err := buildRawExportBundle(pkg, &models.AnalysisResult{}, "test") if err != nil { t.Fatal(err) } zr, err := zip.NewReader(bytes.NewReader(raw), int64(len(raw))) if err != nil { t.Fatal(err) } for _, f := range zr.File { if f.Name == rawExportBundlePrivacyFile { t.Fatalf("%s should be absent when PrivacyScan is nil", rawExportBundlePrivacyFile) } } }