Skip to content

feat: add unit tests for report_generator.py (JSON, CSV, PDF, HTML)#85

Merged
advaitpatel merged 3 commits into
OWASP:mainfrom
rthakkar0555:main
May 18, 2026
Merged

feat: add unit tests for report_generator.py (JSON, CSV, PDF, HTML)#85
advaitpatel merged 3 commits into
OWASP:mainfrom
rthakkar0555:main

Conversation

@rthakkar0555
Copy link
Copy Markdown
Contributor

Summary

Adds comprehensive pytest coverage for report_generator.py across all
4 supported output formats, closing issue #43.


Changes Made

tests/test_report_generator.py (new)

14 tests covering all formats:

JSON (4 tests)

  • Output file is created at expected path
  • Required keys present: scan_info, vulnerabilities, severity_counts
  • severity_counts correctly tallies CRITICAL / HIGH / MEDIUM / LOW
  • Empty vulnerability list produces valid JSON without crashing

CSV (4 tests)

  • Output file is created
  • Header row matches: ID, Severity, Package, Version, Title, CVSS, Status, Target, URL
  • Vulnerability fields map to correct columns
  • Empty input produces header-only CSV

PDF (3 tests)

  • Output file is created
  • Generated file is non-empty (size > 0)
  • No exception raised on valid input

HTML (3 tests)

  • Output file is created
  • No {{ }} template placeholders remain in rendered output
  • HTML special characters are escaped correctly (XSS prevention)

tests/conftest.py (updated)

Added two reusable fixtures:

  • sample_vulnerabilities
  • sample_scan_info

docksec/report_generator.py (bug fixes discovered during testing)

  • Fixed fpdf2 cursor positioning crash — all multi_cell() calls
    now use new_x="LMARGIN", new_y="NEXT" as required by fpdf2 v2.8.7+
  • Without this fix, PDF generation silently crashed in the existing code

docksec/config.py (bug fix discovered during testing)

  • Wrapped langchain_core import in try/except to prevent
    pydantic_core import crash when AI features are not in use

Validation

pytest tests/test_report_generator.py -v
→ 14 passed, 0 failures
Screenshot 2026-05-18 161922

  • All tests use tmp_path (no hardcoded paths)
  • No real LLM or API calls made

Closes #43

Comment thread tests/test_report_generator.py Fixed
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented May 18, 2026

Codecov Report

❌ Patch coverage is 86.57407% with 29 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (main@ca0eba7). Learn more about missing BASE report.

Files with missing lines Patch % Lines
docksec/report_generator.py 70.52% 28 Missing ⚠️
tests/conftest.py 88.88% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main      #85   +/-   ##
=======================================
  Coverage        ?   40.47%           
=======================================
  Files           ?       16           
  Lines           ?     1905           
  Branches        ?        0           
=======================================
  Hits            ?      771           
  Misses          ?     1134           
  Partials        ?        0           
Flag Coverage Δ
unittests 40.47% <86.57%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown
Collaborator

@advaitpatel advaitpatel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm. thank you for your contributions.

@advaitpatel advaitpatel merged commit 7a2a48a into OWASP:main May 18, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[TEST] Add unit tests for report_generator.py — JSON, CSV, PDF, HTML formats

4 participants