|
| 1 | +import os |
1 | 2 | from email.utils import parsedate_to_datetime |
2 | 3 | from urllib.parse import urljoin |
3 | 4 |
|
@@ -253,3 +254,58 @@ def test_metadata_if_modified_since_old_timestamp_200(synced_distro_no_cache): |
253 | 254 | r2 = requests.get(url, headers={"If-Modified-Since": "Thu, 01 Jan 2009 00:00:00 GMT"}) |
254 | 255 | assert r2.status_code == 200 |
255 | 256 | assert len(r2.content) > 0 |
| 257 | + |
| 258 | + |
| 259 | +def test_unauthorized_gets_403_not_304(synced_distro_no_cache, pulpcore_bindings): |
| 260 | + """Unauthorized client gets 403, not 304, even with conditional request headers.""" |
| 261 | + admin_auth = ( |
| 262 | + os.environ.get("ADMIN_USERNAME", "admin"), |
| 263 | + os.environ.get("ADMIN_PASSWORD", "password"), |
| 264 | + ) |
| 265 | + simple_url = urljoin(synced_distro_no_cache.base_url, "simple/") |
| 266 | + |
| 267 | + r1 = requests.get(simple_url, auth=admin_auth) |
| 268 | + assert r1.status_code == 200 |
| 269 | + last_modified = r1.headers["Last-Modified"] |
| 270 | + etag = r1.headers["ETag"] |
| 271 | + |
| 272 | + ap_response = pulpcore_bindings.AccessPoliciesApi.list(viewset_name="pypi/simple") |
| 273 | + assert ap_response.count == 1 |
| 274 | + ap_href = ap_response.results[0].pulp_href |
| 275 | + |
| 276 | + try: |
| 277 | + pulpcore_bindings.AccessPoliciesApi.partial_update( |
| 278 | + ap_href, |
| 279 | + { |
| 280 | + "statements": [ |
| 281 | + { |
| 282 | + "action": ["list", "retrieve"], |
| 283 | + "principal": "authenticated", |
| 284 | + "effect": "allow", |
| 285 | + }, |
| 286 | + { |
| 287 | + "action": ["create"], |
| 288 | + "principal": "authenticated", |
| 289 | + "effect": "allow", |
| 290 | + "condition": "index_has_repo_perm:python.modify_pythonrepository", |
| 291 | + }, |
| 292 | + ], |
| 293 | + }, |
| 294 | + ) |
| 295 | + |
| 296 | + r_ims = requests.get(simple_url, headers={"If-Modified-Since": last_modified}) |
| 297 | + assert r_ims.status_code == 403, ( |
| 298 | + f"Expected 403 for unauthorized If-Modified-Since, got {r_ims.status_code}" |
| 299 | + ) |
| 300 | + |
| 301 | + r_inm = requests.get(simple_url, headers={"If-None-Match": etag}) |
| 302 | + assert r_inm.status_code == 403, ( |
| 303 | + f"Expected 403 for unauthorized If-None-Match, got {r_inm.status_code}" |
| 304 | + ) |
| 305 | + |
| 306 | + r_authed = requests.get( |
| 307 | + simple_url, auth=admin_auth, headers={"If-Modified-Since": last_modified} |
| 308 | + ) |
| 309 | + assert r_authed.status_code == 304 |
| 310 | + finally: |
| 311 | + pulpcore_bindings.AccessPoliciesApi.reset(ap_href) |
0 commit comments