11import json
2+ import uuid
23from unittest import mock
34
5+ import pytest
6+
47from aura import cache
8+ from aura import mirror
9+ from aura import exceptions
10+
11+
12+
13+ @mock .patch ("aura.cache.Cache.get_location" )
14+ def test_cache_mock_location (cache_mock , tmp_path ):
15+ cache_mock .return_value = tmp_path
16+ assert cache .Cache .get_location () == tmp_path
17+
18+
19+ @mock .patch ("aura.cache.Cache.get_location" )
20+ @pytest .mark .parametrize ("filename,content,cache_id,call" , (
21+ ("testjson_file" , "json_content" , "mirrorjson_testjson_file" , cache .Cache .proxy_mirror_json ),
22+ ("testpkg_file" , "pkg_content" , "mirror_testpkg_file" , cache .Cache .proxy_mirror )
23+ ))
24+ def test_proxy_mirror_json (cache_mock , tmp_path , filename , content , cache_id , call ):
25+ f = tmp_path / filename
26+ cache_path = tmp_path / "cache"
27+ cache_path .mkdir ()
28+ cache_file = cache_path / cache_id
29+ cache_mock .return_value = cache_path
30+
31+ assert f .exists () is False
32+ out = call (src = f )
33+ assert out == f
34+ assert cache_file .exists () is False
35+ assert len (list (cache_path .iterdir ())) == 0
36+
37+ f .write_text (content )
38+ assert f .exists () is True
39+ out = call (src = f )
40+ assert out != f
41+ assert out == cache_file
42+ assert len (list (cache_path .iterdir ())) == 1
43+ assert out .read_text () == content
544
45+ # Make sure the cache does not attempt to do any kind of file access if the cache entry exists
46+ m = mock .MagicMock (spec_set = ("name" ,), side_effect = ValueError ("Call prohibited" ))
47+ m .name = filename
48+ out = call (src = m )
49+ assert out == cache_file
650
7- def test_mirror_cache (fixtures , simulate_mirror , tmp_path ):
51+ # Original path should be returned if cache is disabled
52+ cache_mock .return_value = None
53+ out = call (src = f )
54+ assert out == f
55+
56+
57+ @mock .patch ("aura.cache.Cache.get_location" )
58+ def test_mirror_cache (cache_mock , fixtures , simulate_mirror , tmp_path ):
859 cache_content = list (tmp_path .iterdir ())
960 assert len (cache_content ) == 0
1061
11- with mock . patch . object ( cache . Cache , 'get_location' , return_value = tmp_path ) as m :
12- assert cache .Cache .get_location () == tmp_path
13- out = fixtures .get_cli_output (['scan' , '--download-only' , 'mirror://wheel' , '-f' , 'json' ])
62+ cache_mock . return_value = tmp_path
63+ assert cache .Cache .get_location () == tmp_path
64+ out = fixtures .get_cli_output (['scan' , '--download-only' , 'mirror://wheel' , '-f' , 'json' ])
1465
1566 parsed_output = json .loads (out .stdout )
1667 assert len (parsed_output ["detections" ]) == 0
@@ -19,3 +70,31 @@ def test_mirror_cache(fixtures, simulate_mirror, tmp_path):
1970 assert len (cache_content ) > 0
2071 assert "mirror_wheel-0.34.2.tar.gz" in cache_content , cache_content
2172 assert "mirror_wheel-0.34.2-py2.py3-none-any.whl" in cache_content
73+
74+
75+ @mock .patch ("aura.cache.Cache.get_location" )
76+ @mock .patch ("aura.mirror.LocalMirror.get_mirror_path" )
77+ def test_mirror_cache_no_remote_access (mirror_mock , cache_mock , fixtures , tmp_path ):
78+ """
79+ Test that if the content is fully cached, the mirror uri handler does not attempt to access the mirror but rather retrieves **all** content from cache only
80+ This is mainly to test correctness of prefetching the data for global PyPI scan to ensure no further network calls are made
81+ """
82+ pkg = str (uuid .uuid4 ())
83+ pkg_content = {"id" : pkg }
84+ mirror_path = tmp_path / "mirror"
85+ cache_path = tmp_path / "cache"
86+ cache_path .mkdir ()
87+ cache_mock .return_value = cache_path
88+ mirror_mock .return_value = mirror_path
89+ m = mirror .LocalMirror ()
90+
91+ assert cache .Cache .get_location () == cache_path
92+ assert m .get_mirror_path () == mirror_path
93+ assert mirror_path .exists () == False
94+
95+ with pytest .raises (exceptions .NoSuchPackage ):
96+ m .get_json (pkg )
97+
98+ (cache_path / f"mirrorjson_{ pkg } " ).write_text (json .dumps (pkg_content ))
99+ out = m .get_json (pkg )
100+ assert out == pkg_content
0 commit comments