-
Notifications
You must be signed in to change notification settings - Fork 0
🧪 QA: Add tests for HIPAA compliance monitor #325
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: staging
Are you sure you want to change the base?
Changes from all commits
34149e7
ee7f5cc
d4fd3d1
ce7247b
18d1964
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| import os | ||
| import sys | ||
| import importlib.util | ||
|
|
||
| # Load the module dynamically since it has a hyphen in the filename | ||
| spec = importlib.util.spec_from_file_location( | ||
| "compliance_monitor", | ||
| os.path.join(os.path.dirname(__file__), "..", "security", "compliance-monitor.py") | ||
| ) | ||
| compliance_monitor = importlib.util.module_from_spec(spec) | ||
| spec.loader.exec_module(compliance_monitor) | ||
|
|
||
|
Comment on lines
+1
to
+12
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The dynamic import is brittle as written:
SuggestionHarden the loader and register the module, and remove the now-purposeful from pathlib import Path
import importlib.util
import sys
module_path = (Path(__file__).resolve().parents[1] / "security" / "compliance-monitor.py")
spec = importlib.util.spec_from_file_location("compliance_monitor", module_path)
assert spec is not None and spec.loader is not None
compliance_monitor = importlib.util.module_from_spec(spec)
sys.modules[spec.name] = compliance_monitor
spec.loader.exec_module(compliance_monitor)Reply with "@CharlieHelps yes please" if you’d like me to add a commit with this change. |
||
| ComplianceMonitor = compliance_monitor.ComplianceMonitor | ||
|
|
||
| class TestComplianceMonitorHIPAA: | ||
| def test_hipaa_compliance_all_pass(self): | ||
| monitor = ComplianceMonitor() | ||
| result = monitor.monitor_hipaa_compliance() | ||
|
|
||
| assert result["framework"] == "HIPAA" | ||
| assert result["compliance_score"] == 100.0 | ||
| assert result["status"] == "compliant" | ||
| assert all(result["checks"].values()) | ||
|
|
||
|
Comment on lines
+15
to
+24
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
SuggestionPatch all check methods to deterministic values for this test (prefer import pytest
class TestComplianceMonitorHIPAA:
def test_hipaa_compliance_all_pass(self, monkeypatch):
monitor = ComplianceMonitor()
monkeypatch.setattr(monitor, "check_administrative_safeguards", lambda: True)
monkeypatch.setattr(monitor, "check_physical_safeguards", lambda: True)
monkeypatch.setattr(monitor, "check_technical_safeguards", lambda: True)
monkeypatch.setattr(monitor, "check_breach_notification", lambda: True)
monkeypatch.setattr(monitor, "check_baa", lambda: True)
result = monitor.monitor_hipaa_compliance()
...Reply with "@CharlieHelps yes please" if you’d like me to add a commit with this suggestion. |
||
| def test_hipaa_compliance_some_fail(self): | ||
| monitor = ComplianceMonitor() | ||
|
|
||
| # Override a few methods to simulate failure | ||
| monitor.check_administrative_safeguards = lambda: False | ||
| monitor.check_technical_safeguards = lambda: False | ||
|
|
||
| result = monitor.monitor_hipaa_compliance() | ||
|
|
||
| assert result["framework"] == "HIPAA" | ||
| assert result["compliance_score"] == 60.0 | ||
| assert result["status"] == "non_compliant" | ||
| assert result["checks"]["administrative_safeguards"] is False | ||
| assert result["checks"]["technical_safeguards"] is False | ||
| assert result["checks"]["physical_safeguards"] is True | ||
|
|
||
|
Comment on lines
+25
to
+40
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In SuggestionMake the test deterministic by patching all check methods, and consider asserting the score derived from import pytest
def test_hipaa_compliance_some_fail(self, monkeypatch):
monitor = ComplianceMonitor()
monkeypatch.setattr(monitor, "check_administrative_safeguards", lambda: False)
monkeypatch.setattr(monitor, "check_technical_safeguards", lambda: False)
monkeypatch.setattr(monitor, "check_physical_safeguards", lambda: True)
monkeypatch.setattr(monitor, "check_breach_notification", lambda: True)
monkeypatch.setattr(monitor, "check_baa", lambda: True)
result = monitor.monitor_hipaa_compliance()
expected = 100.0 * sum(result["checks"].values()) / len(result["checks"])
assert result["compliance_score"] == pytest.approx(expected)Reply with "@CharlieHelps yes please" if you’d like me to add a commit with this suggestion. |
||
| def test_hipaa_compliance_all_fail(self): | ||
| monitor = ComplianceMonitor() | ||
|
|
||
| # Override all methods to simulate failure | ||
| monitor.check_administrative_safeguards = lambda: False | ||
| monitor.check_physical_safeguards = lambda: False | ||
| monitor.check_technical_safeguards = lambda: False | ||
| monitor.check_breach_notification = lambda: False | ||
| monitor.check_baa = lambda: False | ||
|
|
||
| result = monitor.monitor_hipaa_compliance() | ||
|
|
||
| assert result["framework"] == "HIPAA" | ||
| assert result["compliance_score"] == 0.0 | ||
| assert result["status"] == "non_compliant" | ||
| assert not any(result["checks"].values()) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sysis imported but never used. Also, the dynamic import block assumesspecandspec.loaderare always non-null; if the file path changes or the loader can’t be constructed, this will fail with a less actionable error at import-time of the test module.Suggestion
Remove the unused
sysimport and add a defensive check with a clear error before callingexec_module.Reply with "@CharlieHelps yes please" if you’d like me to add a commit with this change.