Files
web_django_template/site/core/tests_features.py
T
westfarn 97b8607bf2 Add campaign UTM links and piha.li shortener (#4)
## Summary
- Port Monica campaign UTM + piha.li minting into always-on `core` (`shortener.py`, `campaign_utm.py`) so `email_sms` and `directmail` stay optional and never import each other.
- `utm_source` is a slug of `SITE_NAME` (override with `UTM_SOURCE`). Email gets an HTML `data-campaign-utm` link; SMS gets a plain URL; postcard QR only — no body inject.
- Live composer mints through login+CSRF `POST /portal/campaigns/short-link/` (registered only when an outreach app is installed). Empty `SHORTENER_*` falls back to the long UTM URL.

Reference: [monica_site PR #10](ai_ml_operations/monica_site#10)

Closes #3

## Test plan
- [x] `cd site && uv run python manage.py test` (125 tests)
- [ ] Email composer: type a name, confirm HTML link + `utm_campaign` updates, save draft
- [ ] SMS composer: plain `piha.li` (or long UTM if shortener unset) in the body
- [ ] Postcard composer: QR copies the tracked URL; campaign body has no `utm_source`
- [ ] Campaign report pages show the same panel
- [ ] With `FEATURE_EMAIL_SMS` and `FEATURE_DIRECT_MAIL` off, no extra nav and no `/portal/campaigns/short-link/` route

Reviewed-on: #4
2026-09-01 13:36:01 -07:00

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/"))