Template
Keep mint/UTM in always-on core so email_sms and directmail stay optional, and brand utm_source from SITE_NAME (or UTM_SOURCE) instead of a hardcoded client name. Closes #3
95 lines
3.8 KiB
Python
95 lines
3.8 KiB
Python
"""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("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)
|
|
|
|
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)
|
|
|
|
|
|
class AlwaysOnImportIsolationTests(SimpleTestCase):
|
|
"""Always-on modules must not import optional apps at module level."""
|
|
|
|
OPTIONAL = {
|
|
"email_sms",
|
|
"directmail",
|
|
"blog",
|
|
"payments",
|
|
"social",
|
|
"social_ai",
|
|
}
|
|
|
|
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"):
|
|
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/"))
|