Skip to content

Commit

Permalink
secureli-435: use new mocking pattern for pii scan test
Browse files Browse the repository at this point in the history
  • Loading branch information
Kathleen Hogan committed Mar 22, 2024
1 parent d2808e4 commit 52b1eb3
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions tests/modules/pii_scanner/test_pii_scanner_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ def mock_repo_files_repository() -> MagicMock:

@pytest.fixture()
def mock_open_fn(mocker: MockerFixture) -> MagicMock:
# TODO435: mock PII data below
# The below data wouldn't ACTUALLY count as PII, but using fake PII here would prevent this code
# from being committed (as seCureLi scans itself before commit!)
# Instead, we mock the regex search function to pretend we found a PII match so we can assert the
# scanner's behavior
mock_open = mocker.mock_open(
read_data="""
fake_email='pantsATpants.com'
Expand All @@ -29,6 +32,13 @@ def mock_open_fn(mocker: MockerFixture) -> MagicMock:
return mocker.patch("builtins.open", mock_open)


# Include the below for any tests where you want PII to be "found"
@pytest.fixture()
def mock_re(mocker: MockerFixture) -> MagicMock:
match_object = mocker.patch("re.Match", lambda *args: True)
return mocker.patch("re.search", match_object)


@pytest.fixture()
def pii_scanner_service(mock_repo_files_repository: MagicMock) -> PiiScannerService:
return PiiScannerService(mock_repo_files_repository)
Expand All @@ -38,15 +48,16 @@ def test_that_pii_scanner_service_finds_potential_pii(
pii_scanner_service: PiiScannerService,
mock_repo_files_repository: MagicMock,
mock_open_fn: MagicMock,
mock_re: MagicMock,
):
scan_result = pii_scanner_service.scan_repo(test_folder_path, ScanMode.STAGED_ONLY)

mock_repo_files_repository.list_staged_files.assert_called_once()
# TODO435: uncomment once dummy PII data available
# assert scan_result.successful == False
# assert len(scan_result.failures) == 1
# assert "Email" in scan_result.output
# assert "Phone number" in scan_result.output

assert scan_result.successful == False
assert len(scan_result.failures) == 1
assert "Email" in scan_result.output
assert "Phone number" in scan_result.output


def test_that_pii_scanner_service_scans_all_files_when_specified(
Expand Down Expand Up @@ -75,6 +86,7 @@ def test_that_pii_scanner_service_only_scans_specific_files_if_provided(
pii_scanner_service: PiiScannerService,
mock_repo_files_repository: MagicMock,
mock_open_fn: MagicMock,
mock_re: MagicMock,
):
specified_file = "fake_file_path"
ignored_file = "not-the-file-we-want"
Expand All @@ -86,7 +98,6 @@ def test_that_pii_scanner_service_only_scans_specific_files_if_provided(
test_folder_path, ScanMode.STAGED_ONLY, [specified_file]
)

# TODO435: uncomment once dummy PII data available
# assert scan_result.successful == False
# assert len(scan_result.failures) == 1
# assert scan_result.failures[0].file == specified_file
assert scan_result.successful == False
assert len(scan_result.failures) == 1
assert scan_result.failures[0].file == specified_file

0 comments on commit 52b1eb3

Please sign in to comment.