Skip to content

Commit 3768fea

Browse files
committed
test: use defaults.ini instead of test environment
Signed-off-by: Carl Flottmann <[email protected]>
1 parent fed64bc commit 3768fea

File tree

1 file changed

+28
-13
lines changed

1 file changed

+28
-13
lines changed

tests/malware_analyzer/pypi/test_fake_email.py

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,42 @@
44
"""Tests for the FakeEmailAnalyzer heuristic."""
55

66

7-
from unittest.mock import MagicMock, patch
7+
import os
8+
from pathlib import Path
9+
from unittest.mock import MagicMock
810

911
import pytest
1012

13+
from macaron.config.defaults import load_defaults
1114
from macaron.errors import HeuristicAnalyzerValueError
1215
from macaron.malware_analyzer.pypi_heuristics.heuristics import HeuristicResult
1316
from macaron.malware_analyzer.pypi_heuristics.metadata.fake_email import FakeEmailAnalyzer
1417

15-
# Note: throughout these unit tests we set email_validator.TEST_ENVIRONMENT to True. This sets a global
16-
# environment variable in email_validator that ensures that all calls to validate_email have
17-
# test_environment=True set. This allows test and **.test domains and disables DNS deliverability checks
18-
# (so disables check_deliverability=true). See https://github.com/JoshData/python-email-validator for more.
19-
2018

2119
@pytest.fixture(name="analyzer")
2220
def analyzer_() -> FakeEmailAnalyzer:
2321
"""Pytest fixture to create a FakeEmailAnalyzer instance."""
2422
return FakeEmailAnalyzer()
2523

2624

27-
@patch("email_validator.TEST_ENVIRONMENT", True)
25+
@pytest.fixture(name="fake_email_defaults_override")
26+
def set_defaults_(tmp_path: Path) -> None:
27+
"""Disable check_deliverability in defaults.ini so we do not make network connections.
28+
29+
Parameters
30+
----------
31+
tmp_path: Path
32+
Pytest temporary path fixture.
33+
"""
34+
defaults_file = Path(os.path.join(tmp_path, "config.ini"))
35+
content = """
36+
[heuristic.pypi]
37+
check_deliverability = False
38+
"""
39+
defaults_file.write_text(content, encoding="utf-8")
40+
assert load_defaults(str(defaults_file)) is True
41+
42+
2843
def test_missing_info(pypi_package_json: MagicMock, analyzer: FakeEmailAnalyzer) -> None:
2944
"""Test when JSON 'info' key is missing in the PyPI data (should error).
3045
@@ -40,7 +55,6 @@ def test_missing_info(pypi_package_json: MagicMock, analyzer: FakeEmailAnalyzer)
4055
analyzer.analyze(pypi_package_json)
4156

4257

43-
@patch("email_validator.TEST_ENVIRONMENT", True)
4458
def test_no_emails_present(pypi_package_json: MagicMock, analyzer: FakeEmailAnalyzer) -> None:
4559
"""Test when no author_email or maintainer_email is present (should skip).
4660
@@ -56,7 +70,6 @@ def test_no_emails_present(pypi_package_json: MagicMock, analyzer: FakeEmailAnal
5670
assert result == HeuristicResult.SKIP
5771

5872

59-
@patch("email_validator.TEST_ENVIRONMENT", True)
6073
def test_non_email(pypi_package_json: MagicMock, analyzer: FakeEmailAnalyzer) -> None:
6174
"""Test with a non-parsable email address (should fail).
6275
@@ -78,7 +91,6 @@ def test_non_email(pypi_package_json: MagicMock, analyzer: FakeEmailAnalyzer) ->
7891
assert "also not an email" in info["non_emails"]
7992

8093

81-
@patch("email_validator.TEST_ENVIRONMENT", True)
8294
def test_valid_email(pypi_package_json: MagicMock, analyzer: FakeEmailAnalyzer) -> None:
8395
"""Test with valid email address format (should pass).
8496
@@ -90,16 +102,19 @@ def test_valid_email(pypi_package_json: MagicMock, analyzer: FakeEmailAnalyzer)
90102
An initialized FakeEmailAnalyzer instance.
91103
"""
92104
pypi_package_json.package_json = {
93-
"info": {"author_email": "[email protected]", "maintainer_email": "[email protected]"}
105+
"info": {
106+
"author_email": "[email protected]",
107+
"maintainer_email": "[email protected]",
108+
}
94109
}
95110
result, info = analyzer.analyze(pypi_package_json)
96111
assert result == HeuristicResult.PASS
97112

98113
# assert types (for mypy)
99114
assert isinstance(info["valid_emails"], list)
100115

101-
assert "[email protected]" in info["valid_emails"]
102-
assert "[email protected]" in info["valid_emails"]
116+
assert "[email protected]" in info["valid_emails"]
117+
assert "[email protected]" in info["valid_emails"]
103118

104119

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

0 commit comments

Comments
 (0)