From b65c51c038b6366a9b788fe5ae5a626dd485c6bf Mon Sep 17 00:00:00 2001 From: Samir Boulahtit Date: Sat, 9 May 2026 22:58:07 +0200 Subject: [PATCH] test: realign deps + page_context tests with current source MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two stale unit-test groups left over from earlier intentional changes: 1. tests/unit/api/test_deps.py — TestGetCurrentStoreApi get_current_store_api gained a leading `request: Request` param in commit 6276e9e3 (terminal-device pairing) so the device-token path could record last_seen IP. The three tests still passed `(creds, db)` positionally, so `creds` bound to `request` and the function blew up with `Session has no attribute 'credentials'`. Pass a mock request first using the existing `_make_request` helper. 2. tests/unit/utils/test_page_context.py — TestBaseUrlCalculation d591200d switched the storefront base_url builder to use `store.subdomain or store.store_code` (lowercase slug) because the store-context middleware resolves URLs by the lowercase slug; the uppercase `store_code` is for internal use. Tests still asserted the old uppercase shape. Update the two URL assertions to the lowercase subdomain, invert `test_base_url_uses_store_code_not_subdomain` into `test_base_url_uses_subdomain_not_store_code`, and add a fallback test that exercises the `or store.store_code` path when subdomain is None. Co-Authored-By: Claude Opus 4.7 (1M context) --- tests/unit/api/test_deps.py | 9 ++++-- tests/unit/utils/test_page_context.py | 40 +++++++++++++++++++-------- 2 files changed, 35 insertions(+), 14 deletions(-) diff --git a/tests/unit/api/test_deps.py b/tests/unit/api/test_deps.py index d9ae3e69..b853dc9c 100644 --- a/tests/unit/api/test_deps.py +++ b/tests/unit/api/test_deps.py @@ -518,24 +518,27 @@ class TestGetCurrentStoreApi: store_role="owner", ) creds = _make_credentials(token_data["access_token"]) + request = _make_request("/api/v1/store/loyalty") # Mock is_member_of to return True with patch.object(User, "is_member_of", return_value=True): - result = get_current_store_api(creds, db) + result = get_current_store_api(request, creds, db) assert result.id == test_store_user.id def test_rejects_missing_credentials(self, db): """Missing credentials raises InvalidTokenException.""" + request = _make_request("/api/v1/store/loyalty") with pytest.raises(InvalidTokenException): - get_current_store_api(None, db) + get_current_store_api(request, None, db) def test_admin_blocked(self, db, auth_manager, test_admin): """Admin user blocked from store API.""" token_data = auth_manager.create_access_token(user=test_admin) creds = _make_credentials(token_data["access_token"]) + request = _make_request("/api/v1/store/loyalty") with pytest.raises(Exception, match="Store access only"): - get_current_store_api(creds, db) + get_current_store_api(request, creds, db) # ============================================================================ diff --git a/tests/unit/utils/test_page_context.py b/tests/unit/utils/test_page_context.py index 09996913..50519aa9 100644 --- a/tests/unit/utils/test_page_context.py +++ b/tests/unit/utils/test_page_context.py @@ -68,8 +68,8 @@ class TestBaseUrlCalculation: """Tests for base_url in storefront context -- prevents double 'storefront/' regression.""" def test_base_url_dev_mode_with_platform_prefix(self): - """Dev mode: base_url includes /platforms/{code}/storefront/{store_code}/.""" - store = _make_store(store_code="FASHIONHUB") + """Dev mode: base_url includes /platforms/{code}/storefront/{subdomain}/.""" + store = _make_store(store_code="FASHIONHUB", subdomain="fashionhub") platform = _make_platform(code="loyalty") store_context = {"detection_method": "path", "full_prefix": "/storefront/"} @@ -77,20 +77,20 @@ class TestBaseUrlCalculation: store=store, store_context=store_context, platform=platform, - platform_original_path="/platforms/loyalty/storefront/FASHIONHUB/products", + platform_original_path="/platforms/loyalty/storefront/fashionhub/products", ) # Mock the module-level context building to avoid DB calls # We only need to test the base_url logic, so patch what's needed context = get_storefront_context(request) - assert context["base_url"] == "/platforms/loyalty/storefront/FASHIONHUB/" + assert context["base_url"] == "/platforms/loyalty/storefront/fashionhub/" # Verify no double storefront when building links assert "/storefront/storefront/" not in context["base_url"] def test_base_url_dev_mode_without_platform_prefix(self): - """Dev mode without /platforms/ prefix: base_url = /storefront/{store_code}/.""" - store = _make_store(store_code="TESTSHOP") + """Dev mode without /platforms/ prefix: base_url = /storefront/{subdomain}/.""" + store = _make_store(store_code="TESTSHOP", subdomain="testshop") store_context = {"detection_method": "path", "full_prefix": "/storefront/"} request = _make_storefront_request( @@ -102,7 +102,7 @@ class TestBaseUrlCalculation: context = get_storefront_context(request) - assert context["base_url"] == "/storefront/TESTSHOP/" + assert context["base_url"] == "/storefront/testshop/" assert "/storefront/storefront/" not in context["base_url"] def test_base_url_prod_mode_subdomain(self): @@ -127,12 +127,31 @@ class TestBaseUrlCalculation: assert context["base_url"] == "/" - def test_base_url_uses_store_code_not_subdomain(self): - """base_url should use store_code (uppercase) not subdomain (lowercase).""" + def test_base_url_uses_subdomain_not_store_code(self): + """base_url should use subdomain (lowercase) not store_code (uppercase) — the + store-context middleware resolves URLs by the lowercase slug.""" store = _make_store(store_code="FASHIONHUB", subdomain="fashionhub") platform = _make_platform(code="loyalty") store_context = {"detection_method": "path", "full_prefix": "/storefront/"} + request = _make_storefront_request( + store=store, + store_context=store_context, + platform=platform, + platform_original_path="/platforms/loyalty/storefront/fashionhub/products", + ) + + context = get_storefront_context(request) + + assert "fashionhub" in context["base_url"] + assert "FASHIONHUB" not in context["base_url"] + + def test_base_url_falls_back_to_store_code_when_no_subdomain(self): + """If subdomain is missing, fall back to store_code.""" + store = _make_store(store_code="FASHIONHUB", subdomain=None) + platform = _make_platform(code="loyalty") + store_context = {"detection_method": "path", "full_prefix": "/storefront/"} + request = _make_storefront_request( store=store, store_context=store_context, @@ -142,5 +161,4 @@ class TestBaseUrlCalculation: context = get_storefront_context(request) - assert "FASHIONHUB" in context["base_url"] - assert "fashionhub" not in context["base_url"] + assert context["base_url"] == "/platforms/loyalty/storefront/FASHIONHUB/"