"""Feature-flag isolation: flags control INSTALLED_APPS membership and deps.""" import ast from pathlib import Path from django.apps import apps from django.core.exceptions import ImproperlyConfigured from django.test import SimpleTestCase, TestCase from django.urls import reverse def _read_settings_source() -> str: return (Path(__file__).resolve().parent.parent / "client_site/settings/base.py").read_text() class FeatureFlagSettingsTests(SimpleTestCase): def test_optional_apps_are_gated_in_settings(self): src = _read_settings_source() self.assertIn("FEATURE_EMAIL_SMS", src) self.assertIn("FEATURE_DIRECT_MAIL", src) self.assertIn("FEATURE_BLOG", src) self.assertIn("FEATURE_PAYMENTS", src) self.assertIn("FEATURE_SOCIAL", src) self.assertIn("FEATURE_SOCIAL_AI", src) self.assertIn("FEATURE_SHOP", src) self.assertIn("FEATURE_POS_SYNC", src) self.assertIn("FEATURE_EVENTS", src) self.assertIn("FEATURE_SHIPPING", src) self.assertIn("email_sms.apps.EmailSmsConfig", src) self.assertIn("directmail.apps.DirectmailConfig", src) self.assertIn("blog.apps.BlogConfig", src) self.assertIn("payments.apps.PaymentsConfig", src) self.assertIn("social_ai.apps.SocialAiConfig", src) self.assertIn("shop.apps.ShopConfig", src) self.assertIn("pos_sync.apps.PosSyncConfig", src) self.assertIn("events.apps.EventsConfig", src) self.assertIn("shipping.apps.ShippingConfig", src) def test_payments_requires_email_sms_in_source(self): src = _read_settings_source() self.assertIn("FEATURE_PAYMENTS requires FEATURE_EMAIL_SMS", src) self.assertIn("FEATURE_SOCIAL_AI requires FEATURE_SOCIAL", src) self.assertIn("FEATURE_SHOP requires FEATURE_EMAIL_SMS", src) self.assertIn("FEATURE_SHOP requires FEATURE_PAYMENTS", src) self.assertIn("FEATURE_POS_SYNC requires FEATURE_SHOP", src) self.assertIn("FEATURE_EVENTS requires FEATURE_EMAIL_SMS", src) self.assertIn("FEATURE_EVENTS requires FEATURE_PAYMENTS", src) self.assertIn("FEATURE_SHIPPING requires FEATURE_SHOP", src) class AlwaysOnImportIsolationTests(SimpleTestCase): """Always-on modules must not import optional apps at module level.""" OPTIONAL = { "email_sms", "directmail", "blog", "payments", "social", "social_ai", "shop", "pos_sync", "events", "shipping", } ALWAYS_ON = [ "dashboard/views.py", "public/views.py", "core/management/commands/dispatch_due.py", "core/shortener.py", "core/campaign_utm.py", "client_site/urls.py", ] def test_no_module_level_optional_imports(self): root = Path(__file__).resolve().parent.parent for rel in self.ALWAYS_ON: path = root / rel tree = ast.parse(path.read_text()) for node in ast.walk(tree): if isinstance(node, ast.ImportFrom) and node.module: top = node.module.split(".")[0] # Allow inside functions — ast.ImportFrom at module body only if isinstance(node, (ast.Import, ast.ImportFrom)): pass # Check only top-level for node in tree.body: if isinstance(node, ast.Import): for alias in node.names: top = alias.name.split(".")[0] self.assertNotIn(top, self.OPTIONAL, f"{rel} imports {alias.name}") if isinstance(node, ast.ImportFrom) and node.module: top = node.module.split(".")[0] self.assertNotIn(top, self.OPTIONAL, f"{rel} imports {node.module}") class InstalledOptionalAppsTests(TestCase): """Dev defaults install optional apps so the template is a working seed.""" def test_dev_installs_catalog_apps(self): labels = {c.label for c in apps.get_app_configs()} for label in ( "email_sms", "directmail", "blog", "payments", "social", "social_ai", "shop", "pos_sync", "events", "shipping", ): self.assertIn(label, labels) def test_optional_urls_resolve_when_installed(self): self.assertTrue(reverse("email_sms:campaign_list").startswith("/portal/messaging/")) self.assertTrue(reverse("directmail:postcard_designer").startswith("/portal/direct-mail/")) self.assertTrue(reverse("blog:list").startswith("/blog/")) self.assertTrue(reverse("payments:invoice_list").startswith("/portal/payments/")) self.assertTrue(reverse("social:composer").startswith("/portal/social/")) self.assertTrue(reverse("social_ai:generate").startswith("/portal/social/api/generate/")) self.assertTrue(reverse("shop:list").startswith("/shop/")) self.assertTrue(reverse("shop_portal:product_list").startswith("/portal/shop/")) self.assertTrue(reverse("shop_portal:sales").startswith("/portal/shop/sales/")) self.assertTrue(reverse("pos_sync:event_list").startswith("/portal/pos/")) self.assertTrue(reverse("events:list").startswith("/events/")) self.assertTrue(reverse("events_portal:event_list").startswith("/portal/events/")) self.assertTrue(reverse("shipping:shipment_list").startswith("/portal/shipping/"))