@@ -41,49 +41,56 @@ def test_is_within_directory_same_path(self):
4141 @patch ('builtins.open' , new_callable = mock_open )
4242 @patch ('tarfile.open' )
4343 @patch ('os.remove' )
44- def test_download_file_success (self , mock_remove , mock_tarfile , mock_file , mock_get ):
44+ @patch ('pathlib.Path.mkdir' )
45+ @patch ('pathlib.Path.stat' )
46+ @patch ('pathlib.Path.unlink' )
47+ def test_download_file_success (self , mock_unlink , mock_stat , mock_mkdir , mock_remove , mock_tarfile , mock_file , mock_get ):
4548 """Test successful file download and extraction."""
4649 # Mock successful HTTP response
4750 mock_response = MagicMock ()
4851 mock_response .content = b"fake tar content"
4952 mock_get .return_value = mock_response
5053
54+ # Mock file size
55+ mock_stat .return_value .st_size = 1000
56+
5157 # Mock tarfile extraction
5258 mock_tar = MagicMock ()
5359 mock_tarfile .return_value .__enter__ .return_value = mock_tar
5460
55- result = utils .download_file ("http://example.com/file.tar.gz" , "/target " , "file.tar.gz" )
61+ result = utils .download_file ("http://example.com/file.tar.gz" , "/test " , "file.tar.gz" )
5662
5763 self .assertTrue (result )
58- mock_get .assert_called_once_with ("http://example.com/file.tar.gz" , allow_redirects = True , timeout = 10 )
59- mock_file .assert_called_once_with ("/target/file.tar.gz" , "wb" )
60- mock_remove .assert_called_once_with ("/target/file.tar.gz" )
64+ mock_get .assert_called_once_with ("http://example.com/file.tar.gz" , allow_redirects = True , timeout = 30 )
65+ mock_unlink .assert_called_once ()
6166
6267 @patch ('requests.get' )
6368 def test_download_file_http_error (self , mock_get ):
6469 """Test file download with HTTP error."""
65- mock_get .side_effect = Exception ("HTTP error" )
70+ with tempfile .TemporaryDirectory () as temp_dir :
71+ mock_get .side_effect = Exception ("HTTP error" )
6672
67- result = utils .download_file ("http://example.com/file.tar.gz" , "/target" , "file.tar.gz" )
73+ result = utils .download_file ("http://example.com/file.tar.gz" , temp_dir , "file.tar.gz" )
6874
69- self .assertFalse (result )
75+ self .assertFalse (result )
7076
7177 @patch ('requests.get' )
7278 @patch ('builtins.open' , new_callable = mock_open )
7379 @patch ('tarfile.open' )
7480 def test_download_file_extraction_error (self , mock_tarfile , mock_file , mock_get ):
7581 """Test file download with extraction error."""
76- # Mock successful HTTP response
77- mock_response = MagicMock ()
78- mock_response .content = b"fake tar content"
79- mock_get .return_value = mock_response
82+ with tempfile .TemporaryDirectory () as temp_dir :
83+ # Mock successful HTTP response
84+ mock_response = MagicMock ()
85+ mock_response .content = b"fake tar content"
86+ mock_get .return_value = mock_response
8087
81- # Mock tarfile extraction error
82- mock_tarfile .side_effect = Exception ("Extraction error" )
88+ # Mock tarfile extraction error
89+ mock_tarfile .side_effect = Exception ("Extraction error" )
8390
84- result = utils .download_file ("http://example.com/file.tar.gz" , "/target" , "file.tar.gz" )
91+ result = utils .download_file ("http://example.com/file.tar.gz" , temp_dir , "file.tar.gz" )
8592
86- self .assertFalse (result )
93+ self .assertFalse (result )
8794
8895 def test_safe_extract_safe_members (self ):
8996 """Test safe_extract with safe tar members."""
@@ -128,7 +135,7 @@ def test_safe_extract_path_traversal_attempt(self):
128135 with self .assertRaises (Exception ) as context :
129136 utils .safe_extract (tar , extract_dir )
130137
131- self .assertIn ("Failed Path Traversal " , str (context .exception ))
138+ self .assertIn ("Path traversal detected " , str (context .exception ))
132139
133140 def test_repo_base_url_environment_variable (self ):
134141 """Test REPO_BASE_URL uses environment variable when set."""
@@ -156,4 +163,3 @@ def test_repo_base_url_default_value(self):
156163
157164if __name__ == "__main__" :
158165 unittest .main ()
159-
0 commit comments