Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions maigret/web/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,11 +341,18 @@ def build_reports(general_results, usernames, session_key):
os.makedirs(session_folder, exist_ok=True)

graph_path = os.path.join(session_folder, "combined_graph.html")
maigret.report.save_graph_report(
graph_path,
general_results,
MaigretDatabase().load_from_path(app.config["MAIGRET_DB_FILE"]),
)
# pyvis is optional: without it the other reports still get written and
# results.html simply leaves the graph out.
try:
maigret.report.save_graph_report(
graph_path,
general_results,
MaigretDatabase().load_from_path(app.config["MAIGRET_DB_FILE"]),
)
graph_file = os.path.join(f"search_{session_key}", "combined_graph.html")
except ImportError:
logging.warning("pyvis is not installed, skipping the combined graph")
graph_file = None

individual_reports = []
found_count = 0
Expand Down Expand Up @@ -429,7 +436,7 @@ def build_reports(general_results, usernames, session_key):
return {
'status': 'completed',
'session_folder': f"search_{session_key}",
'graph_file': os.path.join(f"search_{session_key}", "combined_graph.html"),
'graph_file': graph_file,
'usernames': usernames,
'individual_reports': individual_reports,
'found_count': found_count,
Expand Down
41 changes: 41 additions & 0 deletions tests/test_web.py
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,7 @@ def test_real_report_generation_does_not_crash(client, web_app, monkeypatch):
the unmocked report functions throws, the task records a failed status and
this assertion catches it.
"""
pytest.importorskip("pyvis") # asserts on the combined graph file

async def fake_search(*args, **kwargs):
return {}
Expand Down Expand Up @@ -689,6 +690,46 @@ def test_build_reports_computes_found_count(web_app, monkeypatch):
assert report['individual_reports'][0]['claimed_profiles'][0]['site_name'] == 'GitHub'


def _stub_reports_and_graph(monkeypatch, graph):
for name in ('save_csv_report', 'save_json_report', 'save_pdf_report', 'save_html_report'):
monkeypatch.setattr(maigret.report, name, lambda *a, **kw: None)
monkeypatch.setattr(maigret.report, 'generate_report_context', lambda *a, **kw: {})
monkeypatch.setattr(maigret.report, 'save_graph_report', graph)


def _one_claimed_result():
claimed = MaigretCheckResult(
username='soxoj',
site_name='GitHub',
site_url_user='https://github.com/soxoj',
status=MaigretCheckStatus.CLAIMED,
)
return [('soxoj', 'username', {'GitHub': {'status': claimed, 'url_user': claimed.site_url_user}})]


def test_build_reports_without_pyvis_skips_graph(web_app, monkeypatch):
"""pyvis is optional: the other reports are still built, without a graph."""

def no_pyvis(*a, **kw):
raise ImportError("No module named 'pyvis'")

_stub_reports_and_graph(monkeypatch, no_pyvis)

report = web_app.build_reports(_one_claimed_result(), ['soxoj'], 'testkey')

assert report['graph_file'] is None
assert report['found_count'] == 1
assert len(report['individual_reports']) == 1


def test_build_reports_with_graph_keeps_graph_file(web_app, monkeypatch):
_stub_reports_and_graph(monkeypatch, lambda *a, **kw: None)

report = web_app.build_reports(_one_claimed_result(), ['soxoj'], 'testkey')

assert report['graph_file'].endswith('combined_graph.html')


def test_process_search_task_records_started_at_on_success(web_app, monkeypatch):
async def fake_search_multi(usernames, options):
return []
Expand Down
1 change: 1 addition & 0 deletions tests/test_web_optional_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def completed_results(*names):


def test_missing_pdf_extra_keeps_other_reports(isolated_web, monkeypatch, tmp_path):
pytest.importorskip("pyvis") # asserts on the combined graph file
monkeypatch.setitem(sys.modules, 'xhtml2pdf', None)
result = web.build_reports(
completed_results('alice', 'bob'), ['alice', 'bob'], 'nopdf'
Expand Down
1 change: 1 addition & 0 deletions tests/test_web_session_isolation.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ def test_same_tick_searches_keep_independent_job_records(isolated_web):
def test_same_tick_searches_keep_results_and_reports_separate(
isolated_web, monkeypatch, tmp_path, reverse_completion
):
pytest.importorskip("pyvis") # asserts on the combined graph file
async def local_search(username, options, query_notify=None):
url = f"https://profiles.example/{username}"
return {
Expand Down