Skip to content

Commit 8c2e7e4

Browse files
committed
test: fake email analyzer tests now run offline in a test environment
Signed-off-by: Carl Flottmann <carl.flottmann@oracle.com>
1 parent 86745ac commit 8c2e7e4

File tree

2 files changed

+10
-30
lines changed

2 files changed

+10
-30
lines changed

src/macaron/malware_analyzer/pypi_heuristics/metadata/fake_email.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ def analyze(self, pypi_package_json: PyPIPackageJsonAsset) -> tuple[HeuristicRes
133133
continue
134134

135135
logger.debug("Email %s normalized to %s", email, email_info.normalized)
136-
detail_info["valid_emails"].append(email)
136+
detail_info["valid_emails"].append(email_info.normalized)
137137
# Optimistic, so if there exists a valid email, we will pass this heuristic
138138
result = HeuristicResult.PASS
139139

tests/malware_analyzer/pypi/test_fake_email.py

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"""Tests for the FakeEmailAnalyzer heuristic."""
55

66

7-
from unittest.mock import MagicMock
7+
from unittest.mock import MagicMock, patch
88

99
import pytest
1010

@@ -19,6 +19,7 @@ def analyzer_() -> FakeEmailAnalyzer:
1919
return FakeEmailAnalyzer()
2020

2121

22+
@patch("email_validator.TEST_ENVIRONMENT", True)
2223
def test_missing_info(pypi_package_json: MagicMock, analyzer: FakeEmailAnalyzer) -> None:
2324
"""Test when JSON 'info' key is missing in the PyPI data (should error).
2425
@@ -34,6 +35,7 @@ def test_missing_info(pypi_package_json: MagicMock, analyzer: FakeEmailAnalyzer)
3435
analyzer.analyze(pypi_package_json)
3536

3637

38+
@patch("email_validator.TEST_ENVIRONMENT", True)
3739
def test_no_emails_present(pypi_package_json: MagicMock, analyzer: FakeEmailAnalyzer) -> None:
3840
"""Test when no author_email or maintainer_email is present (should skip).
3941
@@ -49,6 +51,7 @@ def test_no_emails_present(pypi_package_json: MagicMock, analyzer: FakeEmailAnal
4951
assert result == HeuristicResult.SKIP
5052

5153

54+
@patch("email_validator.TEST_ENVIRONMENT", True)
5255
def test_non_email(pypi_package_json: MagicMock, analyzer: FakeEmailAnalyzer) -> None:
5356
"""Test with a non-parsable email address (should fail).
5457
@@ -70,32 +73,9 @@ def test_non_email(pypi_package_json: MagicMock, analyzer: FakeEmailAnalyzer) ->
7073
assert "also not an email" in info["non_emails"]
7174

7275

73-
def test_invalid_email(pypi_package_json: MagicMock, analyzer: FakeEmailAnalyzer) -> None:
74-
"""Test with an invalid email address that doesn't accept mail (should fail).
75-
76-
Parameters
77-
----------
78-
pypi_package_json: MagicMock
79-
The PyPIPackageJsonAsset MagicMock fixture.
80-
analyzer: FakeEmailAnalyzer
81-
An initialized FakeEmailAnalyzer instance.
82-
"""
83-
pypi_package_json.package_json = {
84-
"info": {"author_email": "user@example.com", "maintainer_email": "other@example.com"}
85-
}
86-
87-
result, info = analyzer.analyze(pypi_package_json)
88-
assert result == HeuristicResult.FAIL
89-
90-
# assert types (for mypy)
91-
assert isinstance(info["invalid_emails"], list)
92-
93-
assert "user@example.com" in info["invalid_emails"]
94-
assert "other@example.com" in info["invalid_emails"]
95-
96-
76+
@patch("email_validator.TEST_ENVIRONMENT", True)
9777
def test_valid_email(pypi_package_json: MagicMock, analyzer: FakeEmailAnalyzer) -> None:
98-
"""Test with valid email address that does accept mail (should pass).
78+
"""Test with valid email address format (should pass).
9979
10080
Parameters
10181
----------
@@ -107,16 +87,16 @@ def test_valid_email(pypi_package_json: MagicMock, analyzer: FakeEmailAnalyzer)
10787
# TODO: change this to use a test domain instead of turning off deliverability
10888
analyzer.check_deliverability = False
10989
pypi_package_json.package_json = {
110-
"info": {"author_email": "user@example.net", "maintainer_email": "other@example.net"}
90+
"info": {"author_email": "user@example.test", "maintainer_email": "other@example.test"}
11191
}
11292
result, info = analyzer.analyze(pypi_package_json)
11393
assert result == HeuristicResult.PASS
11494

11595
# assert types (for mypy)
11696
assert isinstance(info["valid_emails"], list)
11797

118-
assert "user@example.net" in info["valid_emails"]
119-
assert "other@example.net" in info["valid_emails"]
98+
assert "user@example.test" in info["valid_emails"]
99+
assert "other@example.test" in info["valid_emails"]
120100

121101

122102
def test_get_emails(analyzer: FakeEmailAnalyzer) -> None:

0 commit comments

Comments
 (0)