2020URL = f"https://files.example.com/packages/ab/cd/{ FILENAME } "
2121
2222
23+ def _remote (** kwargs ):
24+ kwargs .setdefault ("pk" , uuid4 ())
25+ kwargs .setdefault ("url" , "https://pypi.org/" )
26+ kwargs .setdefault ("includes" , [])
27+ kwargs .setdefault ("excludes" , [])
28+ return SimpleNamespace (** kwargs )
29+
30+
31+ def _redis_available ():
32+ if not settings .CACHE_ENABLED :
33+ return False
34+ try :
35+ return SyncContentCache ().redis .ping ()
36+ except Exception :
37+ return False
38+
39+
40+ requires_redis = pytest .mark .skipif (not _redis_available (), reason = "Redis is not available" )
41+
42+
2343def _token (remote , ** kwargs ):
2444 path = sign_pull_through_path (remote , FILENAME , URL , ** kwargs )
2545 filename , token = path .split (f"?{ PULL_THROUGH_PARAM } =" )
@@ -28,7 +48,7 @@ def _token(remote, **kwargs):
2848
2949
3050def test_round_trip ():
31- remote = SimpleNamespace ( pk = uuid4 () )
51+ remote = _remote ( )
3252 token = _token (remote , sha256 = "a" * 64 , metadata_sha256 = "b" * 64 )
3353
3454 assert unsign_pull_through_token (remote , token , FILENAME ) == (URL , "a" * 64 )
@@ -39,37 +59,50 @@ def test_round_trip():
3959
4060
4161def test_metadata_requires_signed_digest ():
42- remote = SimpleNamespace ( pk = uuid4 () )
62+ remote = _remote ( )
4363 token = _token (remote , sha256 = "a" * 64 )
4464
4565 assert unsign_pull_through_token (remote , token , f"{ FILENAME } .metadata" ) is None
4666
4767
4868def test_rejects_other_remote ():
49- token = _token (SimpleNamespace ( pk = uuid4 () ), sha256 = "a" * 64 )
69+ token = _token (_remote ( ), sha256 = "a" * 64 )
5070
51- assert unsign_pull_through_token (SimpleNamespace (pk = uuid4 ()), token , FILENAME ) is None
71+ assert unsign_pull_through_token (_remote (), token , FILENAME ) is None
72+
73+
74+ @pytest .mark .parametrize (
75+ "changes" ,
76+ [{"url" : "https://example.com/" }, {"includes" : ["shelf-reader" ]}, {"excludes" : ["django" ]}],
77+ )
78+ def test_rejects_changed_remote (changes ):
79+ remote = _remote ()
80+ token = _token (remote , sha256 = "a" * 64 )
81+
82+ assert (
83+ unsign_pull_through_token (_remote (** {** vars (remote ), ** changes }), token , FILENAME ) is None
84+ )
5285
5386
5487def test_rejects_other_filename ():
55- remote = SimpleNamespace ( pk = uuid4 () )
88+ remote = _remote ( )
5689 token = _token (remote , sha256 = "a" * 64 )
5790
5891 assert unsign_pull_through_token (remote , token , "evil-1.0-py3-none-any.whl" ) is None
5992
6093
6194def test_rejects_tampered_token ():
62- remote = SimpleNamespace ( pk = uuid4 () )
95+ remote = _remote ( )
6396 token = _token (remote , sha256 = "a" * 64 )
6497 tampered = token [:- 1 ] + ("A" if token [- 1 ] != "A" else "B" )
6598
6699 assert unsign_pull_through_token (remote , tampered , FILENAME ) is None
67100 assert unsign_pull_through_token (remote , "not-a-token" , FILENAME ) is None
68101
69102
70- @pytest . mark . skipif ( not settings . CACHE_ENABLED , reason = "CACHE_ENABLED is not set" )
103+ @requires_redis
71104def test_stored_entries ():
72- remote = SimpleNamespace ( pk = uuid4 () )
105+ remote = _remote ( )
73106 sdist = "shelf-reader-0.1.tar.gz"
74107 legacy = "pytz-2011k.tar.bz2"
75108 entry = {"url" : URL , "sha256" : "a" * 64 , "metadata_sha256" : None }
@@ -83,7 +116,7 @@ def test_stored_entries():
83116 assert lookup_pull_through_entry (remote , sdist ) == (f"{ URL } /{ sdist } " , None )
84117 assert lookup_pull_through_entry (remote , legacy ) == (f"{ URL } /{ legacy } " , None )
85118 assert lookup_pull_through_entry (remote , "evil-1.0-py3-none-any.whl" ) is None
86- assert lookup_pull_through_entry (SimpleNamespace ( pk = uuid4 () ), FILENAME ) is None
119+ assert lookup_pull_through_entry (_remote ( ), FILENAME ) is None
87120
88121 entry ["metadata_sha256" ] = "b" * 64
89122 assert store_pull_through_entries (remote , {FILENAME : entry })
@@ -98,11 +131,11 @@ def test_stored_entries():
98131 assert lookup_pull_through_entry (remote , legacy ) is None
99132
100133
101- @pytest . mark . skipif ( not settings . CACHE_ENABLED , reason = "CACHE_ENABLED is not set" )
134+ @requires_redis
102135@pytest .mark .parametrize ("ttl" , [1200 , None ])
103136def test_stored_entries_ttl (monkeypatch , ttl ):
104137 monkeypatch .setattr (utils , "PULL_THROUGH_TTL" , ttl )
105- remote = SimpleNamespace ( pk = uuid4 () )
138+ remote = _remote ( )
106139 assert store_pull_through_entries (remote , {FILENAME : {"url" : URL }})
107140
108141 redis_ttl = SyncContentCache ().redis .ttl (utils ._pull_through_key (remote , FILENAME ))
0 commit comments