Merge pull request #75 from christianlouis/copilot/update-reports-with-real-data
Replace dummy report data with real API on /reports page
This commit is contained in:
@@ -132,6 +132,20 @@ class ReportSummary(BaseModel):
|
||||
failed_count: int
|
||||
|
||||
|
||||
class AllReportsItem(BaseModel):
|
||||
"""Single report item for the cross-domain reports list"""
|
||||
|
||||
report_id: str
|
||||
domain: str
|
||||
org_name: str
|
||||
begin_date: str
|
||||
end_date: str
|
||||
total_count: int
|
||||
passed_count: int
|
||||
failed_count: int
|
||||
pass_rate: float
|
||||
|
||||
|
||||
class PaginatedReportResponse(BaseModel):
|
||||
"""Paginated reports response model"""
|
||||
|
||||
@@ -216,6 +230,42 @@ async def upload_report(file: UploadFile = File(...)):
|
||||
) from e
|
||||
|
||||
|
||||
@router.get("", response_model=List[AllReportsItem])
|
||||
async def get_all_reports():
|
||||
"""
|
||||
Get all DMARC reports across all domains, sorted by end_date descending.
|
||||
"""
|
||||
store = ReportStore.get_instance()
|
||||
domains = store.get_domains()
|
||||
|
||||
all_reports: List[AllReportsItem] = []
|
||||
for domain in domains:
|
||||
domain_reports = store.get_domain_reports(domain)
|
||||
for report in domain_reports:
|
||||
summary = report.get("summary", {})
|
||||
total = summary.get("total_count", 0)
|
||||
passed = summary.get("passed_count", 0)
|
||||
pass_rate = round(passed / total * 100, 1) if total > 0 else 0.0
|
||||
all_reports.append(
|
||||
AllReportsItem(
|
||||
report_id=report.get("report_id", ""),
|
||||
domain=domain,
|
||||
org_name=report.get("org_name", ""),
|
||||
begin_date=str(report.get("begin_date", "")),
|
||||
end_date=str(report.get("end_date", "")),
|
||||
total_count=total,
|
||||
passed_count=passed,
|
||||
failed_count=summary.get("failed_count", 0),
|
||||
pass_rate=pass_rate,
|
||||
)
|
||||
)
|
||||
|
||||
# end_date is stored in ISO 8601 format (YYYY-MM-DDTHH:MM:SS), so lexicographic
|
||||
# sorting produces correct chronological order.
|
||||
all_reports.sort(key=lambda r: r.end_date, reverse=True)
|
||||
return all_reports
|
||||
|
||||
|
||||
@router.get("/domains", response_model=List[str])
|
||||
async def get_domains():
|
||||
"""
|
||||
|
||||
@@ -23,14 +23,6 @@
|
||||
</template>
|
||||
</select>
|
||||
</div>
|
||||
<div class="w-full sm:w-auto">
|
||||
<label class="block text-sm font-medium mb-1">Report Type</label>
|
||||
<select class="select select-bordered w-full" x-model="filters.reportType">
|
||||
<option value="">All Types</option>
|
||||
<option value="aggregate">Aggregate (RUA)</option>
|
||||
<option value="forensic">Forensic (RUF)</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="w-full sm:w-auto">
|
||||
<label class="block text-sm font-medium mb-1">Date Range</label>
|
||||
<select class="select select-bordered w-full" x-model="filters.dateRange">
|
||||
@@ -80,33 +72,34 @@
|
||||
<template x-for="(report, index) in filteredReports" :key="index">
|
||||
{% call tr() %}
|
||||
{% call td() %}
|
||||
<div x-text="formatDate(report.date)"></div>
|
||||
<div x-text="formatDate(report.end_date)"></div>
|
||||
{% endcall %}
|
||||
{% call td() %}
|
||||
<div class="inline-flex items-center px-2 py-1 rounded text-xs"
|
||||
:class="report.type === 'aggregate' ? 'bg-blue-100 text-blue-800' : 'bg-amber-100 text-amber-800'">
|
||||
<span x-text="report.type === 'aggregate' ? 'Aggregate' : 'Forensic'"></span>
|
||||
<div class="inline-flex items-center px-2 py-1 rounded text-xs bg-blue-100 text-blue-800">
|
||||
Aggregate
|
||||
</div>
|
||||
{% endcall %}
|
||||
{% call td() %}
|
||||
<div class="font-medium" x-text="report.domain"></div>
|
||||
{% endcall %}
|
||||
{% call td() %}
|
||||
<div x-text="report.organization"></div>
|
||||
<div x-text="report.org_name"></div>
|
||||
{% endcall %}
|
||||
{% call td() %}
|
||||
<div x-text="report.messages"></div>
|
||||
<div x-text="report.total_count"></div>
|
||||
{% endcall %}
|
||||
{% call td() %}
|
||||
<div class="inline-flex items-center px-2 py-1 rounded"
|
||||
:class="getPassRateColor(report.passRate)">
|
||||
<span x-text="report.passRate + '%'"></span>
|
||||
:class="getPassRateColor(report.pass_rate)">
|
||||
<span x-text="report.pass_rate + '%'"></span>
|
||||
</div>
|
||||
{% endcall %}
|
||||
{% call td("text-right") %}
|
||||
{% call button(variant="outline", size="sm") %}
|
||||
View Details
|
||||
{% endcall %}
|
||||
<a :href="`/reports/${report.report_id}`">
|
||||
{% call button(variant="outline", size="sm") %}
|
||||
View Details
|
||||
{% endcall %}
|
||||
</a>
|
||||
{% endcall %}
|
||||
{% endcall %}
|
||||
</template>
|
||||
@@ -124,43 +117,14 @@ function reportsApp() {
|
||||
return {
|
||||
filters: {
|
||||
domain: '',
|
||||
reportType: '',
|
||||
dateRange: '30'
|
||||
},
|
||||
domains: ['example.com', 'mydomain.com'],
|
||||
reports: [
|
||||
{
|
||||
id: 1,
|
||||
date: '2023-04-15',
|
||||
type: 'aggregate',
|
||||
domain: 'example.com',
|
||||
organization: 'Google',
|
||||
messages: 128,
|
||||
passRate: 96
|
||||
},
|
||||
{
|
||||
id: 2,
|
||||
date: '2023-04-15',
|
||||
type: 'aggregate',
|
||||
domain: 'mydomain.com',
|
||||
organization: 'Microsoft',
|
||||
messages: 64,
|
||||
passRate: 100
|
||||
},
|
||||
{
|
||||
id: 3,
|
||||
date: '2023-04-14',
|
||||
type: 'forensic',
|
||||
domain: 'example.com',
|
||||
organization: 'Yahoo',
|
||||
messages: 1,
|
||||
passRate: 0
|
||||
}
|
||||
],
|
||||
domains: [],
|
||||
reports: [],
|
||||
loading: false,
|
||||
|
||||
init() {
|
||||
// When API is ready, fetch reports from server
|
||||
// this.fetchReports();
|
||||
this.fetchReports();
|
||||
},
|
||||
|
||||
get filteredReports() {
|
||||
@@ -170,18 +134,13 @@ function reportsApp() {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Filter by report type
|
||||
if (this.filters.reportType && report.type !== this.filters.reportType) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Filter by date range
|
||||
if (this.filters.dateRange !== 'all') {
|
||||
const days = parseInt(this.filters.dateRange);
|
||||
const cutoff = new Date();
|
||||
cutoff.setDate(cutoff.getDate() - days);
|
||||
|
||||
const reportDate = new Date(report.date);
|
||||
const reportDate = new Date(report.end_date);
|
||||
if (reportDate < cutoff) {
|
||||
return false;
|
||||
}
|
||||
@@ -203,14 +162,17 @@ function reportsApp() {
|
||||
},
|
||||
|
||||
async fetchReports() {
|
||||
this.loading = true;
|
||||
try {
|
||||
const response = await fetch('/api/v1/reports');
|
||||
this.reports = await response.json();
|
||||
|
||||
// Extract unique domains
|
||||
this.domains = [...new Set(this.reports.map(r => r.domain))];
|
||||
// Extract unique domains for the filter dropdown
|
||||
this.domains = [...new Set(this.reports.map(r => r.domain))].sort();
|
||||
} catch (error) {
|
||||
console.error('Error fetching reports:', error);
|
||||
} finally {
|
||||
this.loading = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -173,3 +173,183 @@ def test_report_detail_html_page():
|
||||
response = c.get("/reports/123456789")
|
||||
assert response.status_code == 200
|
||||
assert "text/html" in response.headers["content-type"]
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Tests for GET /api/v1/reports (cross-domain reports list)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
def test_get_all_reports_empty(client: TestClient):
|
||||
"""GET /api/v1/reports returns an empty list when no reports have been uploaded."""
|
||||
response = client.get("/api/v1/reports")
|
||||
assert response.status_code == 200
|
||||
assert response.json() == []
|
||||
|
||||
|
||||
def test_get_all_reports_single_report(client: TestClient):
|
||||
"""GET /api/v1/reports returns the report after a successful upload."""
|
||||
zip_bytes = _make_zip(SAMPLE_XML)
|
||||
client.post(
|
||||
"/api/v1/reports/upload",
|
||||
files={"file": ("report.zip", zip_bytes, "application/zip")},
|
||||
)
|
||||
|
||||
response = client.get("/api/v1/reports")
|
||||
assert response.status_code == 200
|
||||
items = response.json()
|
||||
assert len(items) == 1
|
||||
item = items[0]
|
||||
assert item["report_id"] == "123456789"
|
||||
assert item["domain"] == "example.com"
|
||||
assert item["org_name"] == "google.com"
|
||||
assert "begin_date" in item
|
||||
assert "end_date" in item
|
||||
assert item["total_count"] == 2
|
||||
# SAMPLE_XML has one record with count=2 and dkim=pass (DMARC passes on dkim pass)
|
||||
assert item["passed_count"] >= 0
|
||||
assert item["failed_count"] >= 0
|
||||
assert isinstance(item["pass_rate"], float)
|
||||
|
||||
|
||||
def test_get_all_reports_multiple_domains(client: TestClient):
|
||||
"""GET /api/v1/reports returns reports from all domains."""
|
||||
from app.services.report_store import ReportStore
|
||||
|
||||
store = ReportStore.get_instance()
|
||||
|
||||
report_a = {
|
||||
"domain": "alpha.com",
|
||||
"report_id": "rpt-alpha",
|
||||
"org_name": "Google",
|
||||
"email": "",
|
||||
"begin_date": "2024-01-01T00:00:00",
|
||||
"end_date": "2024-01-01T23:59:59",
|
||||
"begin_timestamp": 1704067200,
|
||||
"end_timestamp": 1704153599,
|
||||
"policy": {"p": "none", "sp": "none", "pct": "100"},
|
||||
"records": [],
|
||||
"summary": {"total_count": 10, "passed_count": 10, "failed_count": 0},
|
||||
}
|
||||
report_b = {
|
||||
"domain": "beta.com",
|
||||
"report_id": "rpt-beta",
|
||||
"org_name": "Microsoft",
|
||||
"email": "",
|
||||
"begin_date": "2024-01-02T00:00:00",
|
||||
"end_date": "2024-01-02T23:59:59",
|
||||
"begin_timestamp": 1704153600,
|
||||
"end_timestamp": 1704239999,
|
||||
"policy": {"p": "reject", "sp": "reject", "pct": "100"},
|
||||
"records": [],
|
||||
"summary": {"total_count": 5, "passed_count": 3, "failed_count": 2},
|
||||
}
|
||||
store.add_report(report_a)
|
||||
store.add_report(report_b)
|
||||
|
||||
response = client.get("/api/v1/reports")
|
||||
assert response.status_code == 200
|
||||
items = response.json()
|
||||
assert len(items) == 2
|
||||
domains_returned = {item["domain"] for item in items}
|
||||
assert domains_returned == {"alpha.com", "beta.com"}
|
||||
|
||||
|
||||
def test_get_all_reports_sorted_by_end_date_desc(client: TestClient):
|
||||
"""GET /api/v1/reports returns items sorted by end_date descending."""
|
||||
from app.services.report_store import ReportStore
|
||||
|
||||
store = ReportStore.get_instance()
|
||||
|
||||
older = {
|
||||
"domain": "example.com",
|
||||
"report_id": "rpt-older",
|
||||
"org_name": "OrgA",
|
||||
"email": "",
|
||||
"begin_date": "2023-06-01T00:00:00",
|
||||
"end_date": "2023-06-01T23:59:59",
|
||||
"begin_timestamp": 1685577600,
|
||||
"end_timestamp": 1685663999,
|
||||
"policy": {"p": "none"},
|
||||
"records": [],
|
||||
"summary": {"total_count": 4, "passed_count": 4, "failed_count": 0},
|
||||
}
|
||||
newer = {
|
||||
"domain": "example.com",
|
||||
"report_id": "rpt-newer",
|
||||
"org_name": "OrgA",
|
||||
"email": "",
|
||||
"begin_date": "2024-01-01T00:00:00",
|
||||
"end_date": "2024-01-01T23:59:59",
|
||||
"begin_timestamp": 1704067200,
|
||||
"end_timestamp": 1704153599,
|
||||
"policy": {"p": "none"},
|
||||
"records": [],
|
||||
"summary": {"total_count": 6, "passed_count": 6, "failed_count": 0},
|
||||
}
|
||||
store.add_report(older)
|
||||
store.add_report(newer)
|
||||
|
||||
response = client.get("/api/v1/reports")
|
||||
assert response.status_code == 200
|
||||
items = response.json()
|
||||
assert len(items) == 2
|
||||
# Newest end_date should come first
|
||||
assert items[0]["report_id"] == "rpt-newer"
|
||||
assert items[1]["report_id"] == "rpt-older"
|
||||
|
||||
|
||||
def test_get_all_reports_pass_rate_computed_correctly(client: TestClient):
|
||||
"""pass_rate is computed from passed_count / total_count * 100."""
|
||||
from app.services.report_store import ReportStore
|
||||
|
||||
store = ReportStore.get_instance()
|
||||
|
||||
report = {
|
||||
"domain": "example.com",
|
||||
"report_id": "rpt-rate",
|
||||
"org_name": "OrgB",
|
||||
"email": "",
|
||||
"begin_date": "2024-03-01T00:00:00",
|
||||
"end_date": "2024-03-01T23:59:59",
|
||||
"begin_timestamp": 1709251200,
|
||||
"end_timestamp": 1709337599,
|
||||
"policy": {"p": "none"},
|
||||
"records": [],
|
||||
"summary": {"total_count": 8, "passed_count": 6, "failed_count": 2},
|
||||
}
|
||||
store.add_report(report)
|
||||
|
||||
response = client.get("/api/v1/reports")
|
||||
assert response.status_code == 200
|
||||
items = response.json()
|
||||
assert len(items) == 1
|
||||
assert items[0]["pass_rate"] == 75.0
|
||||
|
||||
|
||||
def test_get_all_reports_zero_total_gives_zero_pass_rate(client: TestClient):
|
||||
"""pass_rate is 0.0 when total_count is 0 (no division by zero)."""
|
||||
from app.services.report_store import ReportStore
|
||||
|
||||
store = ReportStore.get_instance()
|
||||
|
||||
report = {
|
||||
"domain": "example.com",
|
||||
"report_id": "rpt-zero",
|
||||
"org_name": "OrgC",
|
||||
"email": "",
|
||||
"begin_date": "2024-04-01T00:00:00",
|
||||
"end_date": "2024-04-01T23:59:59",
|
||||
"begin_timestamp": 1711929600,
|
||||
"end_timestamp": 1712015999,
|
||||
"policy": {"p": "none"},
|
||||
"records": [],
|
||||
"summary": {"total_count": 0, "passed_count": 0, "failed_count": 0},
|
||||
}
|
||||
store.add_report(report)
|
||||
|
||||
response = client.get("/api/v1/reports")
|
||||
assert response.status_code == 200
|
||||
items = response.json()
|
||||
assert len(items) == 1
|
||||
assert items[0]["pass_rate"] == 0.0
|
||||
|
||||
Reference in New Issue
Block a user