Skip to content

Commit

Permalink
secureli-435: remaining unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Kathleen Hogan committed Mar 22, 2024
1 parent f2899f5 commit 1a17a88
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
1 change: 1 addition & 0 deletions secureli/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ class Container(containers.DeclarativeContainer):
pii_scanner_service = providers.Factory(
PiiScannerService,
repo_files=repo_files_repository,
echo=echo,
)

updater_service = providers.Factory(
Expand Down
7 changes: 5 additions & 2 deletions secureli/modules/pii_scanner/pii_scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from pathlib import Path
import pydantic

from secureli.modules.shared.abstractions.echo import EchoAbstraction
from secureli.modules.shared.models.scan import (
ScanFailure,
ScanMode,
Expand All @@ -37,8 +38,10 @@ class PiiScannerService:
def __init__(
self,
repo_files: RepoFilesRepository,
echo: EchoAbstraction,
):
self.repo_files = repo_files
self.echo = echo

def scan_repo(
self,
Expand Down Expand Up @@ -85,7 +88,7 @@ def scan_repo(
current_line_num = 0

except Exception as e:
print(f"Error scanning {file_name}: {e}")
self.echo.print(f"Error PII scanning {file_name}: {e}")
scan_failures = self._generate_scan_failures(pii_found_files)
output = self._generate_scan_output(pii_found, not pii_found)

Expand Down Expand Up @@ -125,7 +128,7 @@ def _get_files_list(
if files:
file_paths = list(filter(lambda file: file in file_paths, files))

elif scan_mode == ScanMode.ALL_FILES:
if scan_mode == ScanMode.ALL_FILES:
file_paths = self.repo_files.list_repo_files(folder_path)

return list(
Expand Down
32 changes: 29 additions & 3 deletions tests/modules/pii_scanner/test_pii_scanner_service.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import pytest
from pytest_mock import MockerFixture
from unittest.mock import MagicMock
from unittest.mock import MagicMock, Mock
import builtins
import contextlib, io
from pathlib import Path
from secureli.modules.pii_scanner.pii_scanner import PiiScannerService
from secureli.modules.shared.models.scan import ScanMode
Expand All @@ -17,6 +19,12 @@ def mock_repo_files_repository() -> MagicMock:
return mock_repo_files_repository


@pytest.fixture()
def mock_echo() -> MagicMock:
mock_echo = MagicMock()
return mock_echo


@pytest.fixture()
def mock_open_fn(mocker: MockerFixture) -> MagicMock:
# The below data wouldn't ACTUALLY count as PII, but using fake PII here would prevent this code
Expand All @@ -40,8 +48,10 @@ def mock_re(mocker: MockerFixture) -> MagicMock:


@pytest.fixture()
def pii_scanner_service(mock_repo_files_repository: MagicMock) -> PiiScannerService:
return PiiScannerService(mock_repo_files_repository)
def pii_scanner_service(
mock_repo_files_repository: MagicMock, mock_echo: MagicMock
) -> PiiScannerService:
return PiiScannerService(mock_repo_files_repository, mock_echo)


def test_that_pii_scanner_service_finds_potential_pii(
Expand Down Expand Up @@ -74,6 +84,7 @@ def test_that_pii_scanner_service_ignores_excluded_file_extensions(
pii_scanner_service: PiiScannerService,
mock_repo_files_repository: MagicMock,
mock_open_fn: MagicMock,
mock_re: MagicMock,
):
mock_repo_files_repository.list_staged_files.return_value = ["fake_file_path.md"]

Expand Down Expand Up @@ -101,3 +112,18 @@ def test_that_pii_scanner_service_only_scans_specific_files_if_provided(
assert scan_result.successful == False
assert len(scan_result.failures) == 1
assert scan_result.failures[0].file == specified_file


def test_that_pii_scanner_prints_when_exceptions_encountered(
pii_scanner_service: PiiScannerService,
mock_open_fn: MagicMock,
mock_echo: MagicMock,
):
mock_open_fn.side_effect = Exception("Oh no")
pii_scanner_service.scan_repo(
test_folder_path,
ScanMode.STAGED_ONLY,
)

mock_echo.print.assert_called_once()
assert "Error PII scanning" in mock_echo.print.call_args.args[0]

0 comments on commit 1a17a88

Please sign in to comment.