Skip to content

Commit

Permalink
Add type annotations for test_reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
Dhwaniartefact committed Oct 10, 2024
1 parent 6eb4807 commit 8f4c98a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 15 deletions.
20 changes: 18 additions & 2 deletions fixity/reporting.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
import calendar
import json
from datetime import datetime
from typing import Optional

import requests

from .models import Report
from .models import Session
from .utils import check_valid_uuid


class ReportServiceException(Exception):
pass


def post_pre_scan_report(aip, start_time, report_url, report_auth=(), session_id=None):
def post_pre_scan_report(
aip: str,
start_time: Optional[datetime],
report_url: Optional[str],
report_auth: Optional[str] = (),
session_id: Session = None,
) -> bool:
"""
Post a pre-scan report to a remote system.
Expand Down Expand Up @@ -51,7 +61,13 @@ def post_pre_scan_report(aip, start_time, report_url, report_auth=(), session_id
return True


def post_success_report(aip, report, report_url, report_auth=(), session_id=None):
def post_success_report(
aip: str,
report: Optional[Report],
report_url: Optional[str],
report_auth: Optional[str] = (),
session_id: Session = None,
) -> Optional[bool]:
"""
POST a JSON fixity scan report to a remote system.
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ ignore_errors = true
[[tool.mypy.overrides]]
module = [
"tests.test_fixity",
"tests.test_reporting",
]
ignore_errors = false

26 changes: 13 additions & 13 deletions tests/test_reporting.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
REPORT_URL = "http://localhost:8003/"


def json_string(filename):
def json_string(filename: str) -> str:
path = os.path.normpath(
os.path.join(__file__, "..", "..", "fixtures", "json", filename)
)
Expand All @@ -24,22 +24,22 @@ def json_string(filename):
@mock.patch(
"requests.post", side_effect=[mock.Mock(status_code=201, spec=requests.Response)]
)
def test_posting_prescan_report(_post):
def test_posting_prescan_report(_post: mock.Mock) -> None:
aip = "be1074fe-217b-46e0-afec-400ea1a2eb36"
start_time = datetime.fromtimestamp(1400022946)
result = reporting.post_pre_scan_report(aip, start_time, REPORT_URL)
assert result is True


def test_posting_prescan_report_raises_on_invalid_uuid():
def test_posting_prescan_report_raises_on_invalid_uuid() -> None:
with pytest.raises(InvalidUUID):
reporting.post_pre_scan_report("foo", None, None)


@mock.patch(
"requests.post", side_effect=[mock.Mock(status_code=500, spec=requests.Response)]
)
def test_posting_prescan_report_raises_on_500(_post):
def test_posting_prescan_report_raises_on_500(_post: mock.Mock) -> None:
with pytest.raises(reporting.ReportServiceException):
aip = "90cf0850-f5d2-4023-95e2-0e2b7a1e1b8e"
start_time = datetime.fromtimestamp(1400022946)
Expand All @@ -49,14 +49,14 @@ def test_posting_prescan_report_raises_on_500(_post):
@mock.patch(
"requests.post", side_effect=[mock.Mock(status_code=404, spec=requests.Response)]
)
def test_posting_prescan_report_raises_on_404(_post):
def test_posting_prescan_report_raises_on_404(_post: mock.Mock) -> None:
with pytest.raises(reporting.ReportServiceException):
aip = "90cf0850-f5d2-4023-95e2-0e2b7a1e1b8e"
start_time = datetime.fromtimestamp(1400022946)
reporting.post_pre_scan_report(aip, start_time, REPORT_URL)


def test_posting_prescan_report_raises_when_unable_to_connect():
def test_posting_prescan_report_raises_when_unable_to_connect() -> None:
with pytest.raises(reporting.ReportServiceException):
aip = "90cf0850-f5d2-4023-95e2-0e2b7a1e1b8e"
start_time = datetime.fromtimestamp(1400022946)
Expand All @@ -66,7 +66,7 @@ def test_posting_prescan_report_raises_when_unable_to_connect():
@mock.patch(
"requests.post", side_effect=[mock.Mock(status_code=201, spec=requests.Response)]
)
def test_posting_success_report(_post):
def test_posting_success_report(_post: mock.Mock) -> None:
json_report = json_string("test_failed_report.json")
aip = AIP(uuid="ed42aadc-d854-46c6-b455-cd384eef1618")
report = Report(
Expand All @@ -80,15 +80,15 @@ def test_posting_success_report(_post):
reporting.post_success_report(aip.uuid, report, REPORT_URL)


def test_posting_success_report_raises_on_invalid_uuid():
def test_posting_success_report_raises_on_invalid_uuid() -> None:
with pytest.raises(InvalidUUID):
reporting.post_success_report("foo", None, None)


@mock.patch(
"requests.post", side_effect=[mock.Mock(status_code=500, spec=requests.Response)]
)
def test_posting_success_report_raises_on_500(_post):
def test_posting_success_report_raises_on_500(_post: mock.Mock) -> None:
with pytest.raises(reporting.ReportServiceException):
json_report = json_string("test_failed_report.json")
aip = AIP(uuid="ed42aadc-d854-46c6-b455-cd384eef1618")
Expand All @@ -106,7 +106,7 @@ def test_posting_success_report_raises_on_500(_post):
@mock.patch(
"requests.post", side_effect=[mock.Mock(status_code=404, spec=requests.Response)]
)
def test_posting_success_report_raises_on_404(_post):
def test_posting_success_report_raises_on_404(_post: mock.Mock) -> None:
with pytest.raises(reporting.ReportServiceException):
json_report = json_string("test_failed_report.json")
aip = AIP(uuid="ed42aadc-d854-46c6-b455-cd384eef1618")
Expand All @@ -121,7 +121,7 @@ def test_posting_success_report_raises_on_404(_post):
reporting.post_success_report(aip.uuid, report, REPORT_URL)


def test_posting_success_report_raises_when_unable_to_connect():
def test_posting_success_report_raises_when_unable_to_connect() -> None:
with pytest.raises(reporting.ReportServiceException):
json_report = json_string("test_failed_report.json")
aip = AIP(uuid="ed42aadc-d854-46c6-b455-cd384eef1618")
Expand All @@ -136,7 +136,7 @@ def test_posting_success_report_raises_when_unable_to_connect():
reporting.post_success_report(aip.uuid, report, "http://foo")


def test_posting_success_report_posted_is_false_on_raise():
def test_posting_success_report_posted_is_false_on_raise() -> None:
try:
json_report = json_string("test_failed_report.json")
aip = AIP(uuid="ed42aadc-d854-46c6-b455-cd384eef1618")
Expand All @@ -155,7 +155,7 @@ def test_posting_success_report_posted_is_false_on_raise():
assert report.posted is False


def test_posting_success_report_success_none():
def test_posting_success_report_success_none() -> None:
json_report = json_string("test_failed_report.json")
aip = AIP(uuid="ed42aadc-d854-46c6-b455-cd384eef1618")
report = Report(
Expand Down

0 comments on commit 8f4c98a

Please sign in to comment.