4
4
"""Tests for the FakeEmailAnalyzer heuristic."""
5
5
6
6
7
- from unittest .mock import MagicMock , patch
7
+ import os
8
+ from pathlib import Path
9
+ from unittest .mock import MagicMock
8
10
9
11
import pytest
10
12
13
+ from macaron .config .defaults import load_defaults
11
14
from macaron .errors import HeuristicAnalyzerValueError
12
15
from macaron .malware_analyzer .pypi_heuristics .heuristics import HeuristicResult
13
16
from macaron .malware_analyzer .pypi_heuristics .metadata .fake_email import FakeEmailAnalyzer
14
17
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
-
20
18
21
19
@pytest .fixture (name = "analyzer" )
22
20
def analyzer_ () -> FakeEmailAnalyzer :
23
21
"""Pytest fixture to create a FakeEmailAnalyzer instance."""
24
22
return FakeEmailAnalyzer ()
25
23
26
24
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
+
28
43
def test_missing_info (pypi_package_json : MagicMock , analyzer : FakeEmailAnalyzer ) -> None :
29
44
"""Test when JSON 'info' key is missing in the PyPI data (should error).
30
45
@@ -40,7 +55,6 @@ def test_missing_info(pypi_package_json: MagicMock, analyzer: FakeEmailAnalyzer)
40
55
analyzer .analyze (pypi_package_json )
41
56
42
57
43
- @patch ("email_validator.TEST_ENVIRONMENT" , True )
44
58
def test_no_emails_present (pypi_package_json : MagicMock , analyzer : FakeEmailAnalyzer ) -> None :
45
59
"""Test when no author_email or maintainer_email is present (should skip).
46
60
@@ -56,7 +70,6 @@ def test_no_emails_present(pypi_package_json: MagicMock, analyzer: FakeEmailAnal
56
70
assert result == HeuristicResult .SKIP
57
71
58
72
59
- @patch ("email_validator.TEST_ENVIRONMENT" , True )
60
73
def test_non_email (pypi_package_json : MagicMock , analyzer : FakeEmailAnalyzer ) -> None :
61
74
"""Test with a non-parsable email address (should fail).
62
75
@@ -78,7 +91,6 @@ def test_non_email(pypi_package_json: MagicMock, analyzer: FakeEmailAnalyzer) ->
78
91
assert "also not an email" in info ["non_emails" ]
79
92
80
93
81
- @patch ("email_validator.TEST_ENVIRONMENT" , True )
82
94
def test_valid_email (pypi_package_json : MagicMock , analyzer : FakeEmailAnalyzer ) -> None :
83
95
"""Test with valid email address format (should pass).
84
96
@@ -90,16 +102,19 @@ def test_valid_email(pypi_package_json: MagicMock, analyzer: FakeEmailAnalyzer)
90
102
An initialized FakeEmailAnalyzer instance.
91
103
"""
92
104
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
+ }
94
109
}
95
110
result , info = analyzer .analyze (pypi_package_json )
96
111
assert result == HeuristicResult .PASS
97
112
98
113
# assert types (for mypy)
99
114
assert isinstance (info ["valid_emails" ], list )
100
115
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" ]
103
118
104
119
105
120
def test_get_emails (analyzer : FakeEmailAnalyzer ) -> None :
0 commit comments