Summary of What Needs to be Done:
Add unit tests for the get_policy_engine singleton factory function in backend/secuscan/network_policy.py. This function creates a global NetworkPolicyEngine instance on first call and returns the cached instance on subsequent calls.
The function requires patching the backend.secuscan.config module (which imports Pydantic) so the test can inject mock settings without requiring the full Pydantic dependency tree. The pattern is to use patch.dict(sys.modules, {...}) to replace the config module before importing get_policy_engine.
Changes that Need to be Made:
Create testing/backend/unit/test_network_policy_engine_singleton.py:
- Patch
sys.modules['backend.secuscan.config'] with a mock config module before importing get_policy_engine and NetworkPolicyEngine
- Test that first call creates a new
NetworkPolicyEngine instance
- Test that second call returns the same instance (singleton behavior)
- Test that the engine has expected methods (
check_access, add_allow_rule, add_deny_rule)
- Test that calling after
engine.clear_audit_entries() returns the SAME cached instance (singleton is preserved)
- Reset the global
_policy_engine = None before each test to ensure isolation
- Import the real production functions directly (not mocked copies)
- Run with
python3 -m pytest testing/backend/unit/test_network_policy_engine_singleton.py -v --noconftest
Impact that it would Provide:
- Tests singleton initialization logic that is currently untested at the unit level
- Catches regressions if the singleton caching logic is accidentally changed
- Demonstrates the sys.modules patching pattern for testing modules that import Pydantic-dependent config
Note: This task is being handled by tmdeveloper007 — please assign to that account when picking it up.
Summary of What Needs to be Done:
Add unit tests for the
get_policy_enginesingleton factory function inbackend/secuscan/network_policy.py. This function creates a globalNetworkPolicyEngineinstance on first call and returns the cached instance on subsequent calls.The function requires patching the
backend.secuscan.configmodule (which imports Pydantic) so the test can inject mock settings without requiring the full Pydantic dependency tree. The pattern is to usepatch.dict(sys.modules, {...})to replace the config module before importingget_policy_engine.Changes that Need to be Made:
Create
testing/backend/unit/test_network_policy_engine_singleton.py:sys.modules['backend.secuscan.config']with a mock config module before importingget_policy_engineandNetworkPolicyEngineNetworkPolicyEngineinstancecheck_access,add_allow_rule,add_deny_rule)engine.clear_audit_entries()returns the SAME cached instance (singleton is preserved)_policy_engine = Nonebefore each test to ensure isolationpython3 -m pytest testing/backend/unit/test_network_policy_engine_singleton.py -v --noconftestImpact that it would Provide:
Note: This task is being handled by tmdeveloper007 — please assign to that account when picking it up.