Spaces:
Runtime error
Runtime error
Commit Β·
0cbb7b5
1
Parent(s): 523436b
email testing
Browse files- scripts/seed_templates.py +2 -2
- test_email_flow.py +100 -0
scripts/seed_templates.py
CHANGED
|
@@ -88,7 +88,7 @@ TEMPLATES = [
|
|
| 88 |
# ββ Generic / Service Partner OTP ββββββββββββββββββββββββββββββββββββ
|
| 89 |
{
|
| 90 |
"channel": "sms",
|
| 91 |
-
"name": "
|
| 92 |
"subject": None,
|
| 93 |
"body": "{{otp}} is your OTP. Valid for {{expiry_minutes}} minutes. Do not share this code.",
|
| 94 |
"html_body": None,
|
|
@@ -97,7 +97,7 @@ TEMPLATES = [
|
|
| 97 |
},
|
| 98 |
{
|
| 99 |
"channel": "email",
|
| 100 |
-
"name": "
|
| 101 |
"subject": "Your OTP - {{otp}}",
|
| 102 |
"body": "Your OTP is {{otp}}. It is valid for {{expiry_minutes}} minutes. Do not share this code with anyone.",
|
| 103 |
"html_body": (
|
|
|
|
| 88 |
# ββ Generic / Service Partner OTP ββββββββββββββββββββββββββββββββββββ
|
| 89 |
{
|
| 90 |
"channel": "sms",
|
| 91 |
+
"name": "otp",
|
| 92 |
"subject": None,
|
| 93 |
"body": "{{otp}} is your OTP. Valid for {{expiry_minutes}} minutes. Do not share this code.",
|
| 94 |
"html_body": None,
|
|
|
|
| 97 |
},
|
| 98 |
{
|
| 99 |
"channel": "email",
|
| 100 |
+
"name": "otp",
|
| 101 |
"subject": "Your OTP - {{otp}}",
|
| 102 |
"body": "Your OTP is {{otp}}. It is valid for {{expiry_minutes}} minutes. Do not share this code with anyone.",
|
| 103 |
"html_body": (
|
test_email_flow.py
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Standalone test for the SendGrid email flow.
|
| 3 |
+
Tests both EmailChannel.is_configured() and EmailChannel.send() directly.
|
| 4 |
+
|
| 5 |
+
Usage:
|
| 6 |
+
cd cuatrolabs-notification-ms
|
| 7 |
+
python test_email_flow.py --to recipient@example.com
|
| 8 |
+
"""
|
| 9 |
+
import asyncio
|
| 10 |
+
import argparse
|
| 11 |
+
import sys
|
| 12 |
+
|
| 13 |
+
# ---------------------------------------------------------------------------
|
| 14 |
+
# Config β edit these or pass via CLI args
|
| 15 |
+
# ---------------------------------------------------------------------------
|
| 16 |
+
TEST_CONFIG = {
|
| 17 |
+
"enabled": True,
|
| 18 |
+
"api_key": "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",
|
| 19 |
+
"from_email": "no-reply@example.com",
|
| 20 |
+
"from_name": "Cuatro Notifications",
|
| 21 |
+
"reply_to": "support@example.com",
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
TEST_TEMPLATE_DATA = {
|
| 25 |
+
"subject": "Test Email β Cuatro Notification MS",
|
| 26 |
+
"body": "Hello {{ name }},\n\nThis is a test email from the Cuatro notification service.\n\nRegards,\nCuatro Team",
|
| 27 |
+
"html_body": "<p>Hello <strong>{{ name }}</strong>,</p><p>This is a <em>test email</em> from the Cuatro notification service.</p><p>Regards,<br>Cuatro Team</p>",
|
| 28 |
+
"name": "Test User",
|
| 29 |
+
}
|
| 30 |
+
# ---------------------------------------------------------------------------
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
async def run_test(recipient: str):
|
| 34 |
+
# Patch sys.path so we can import app modules without installing the package
|
| 35 |
+
import os
|
| 36 |
+
sys.path.insert(0, os.path.dirname(__file__))
|
| 37 |
+
|
| 38 |
+
# Minimal stub for app.core.logging so we don't need the full app stack
|
| 39 |
+
import types
|
| 40 |
+
import logging
|
| 41 |
+
|
| 42 |
+
logging.basicConfig(level=logging.INFO, format="%(levelname)s %(message)s")
|
| 43 |
+
|
| 44 |
+
core_mod = types.ModuleType("app.core")
|
| 45 |
+
core_logging_mod = types.ModuleType("app.core.logging")
|
| 46 |
+
core_logging_mod.get_logger = lambda name: logging.getLogger(name)
|
| 47 |
+
sys.modules.setdefault("app", types.ModuleType("app"))
|
| 48 |
+
sys.modules["app.core"] = core_mod
|
| 49 |
+
sys.modules["app.core.logging"] = core_logging_mod
|
| 50 |
+
|
| 51 |
+
from app.channels.email import EmailChannel
|
| 52 |
+
|
| 53 |
+
print("\n=== SendGrid Email Flow Test ===\n")
|
| 54 |
+
|
| 55 |
+
# 1. is_configured check
|
| 56 |
+
configured = EmailChannel.is_configured(TEST_CONFIG)
|
| 57 |
+
print(f"[1] is_configured: {'β PASS' if configured else 'β FAIL'}")
|
| 58 |
+
if not configured:
|
| 59 |
+
print(" Channel config is missing required fields (enabled, api_key, from_email).")
|
| 60 |
+
return
|
| 61 |
+
|
| 62 |
+
# 2. Send test email
|
| 63 |
+
print(f"[2] Sending email to: {recipient}")
|
| 64 |
+
success, message, provider_id = await EmailChannel.send(
|
| 65 |
+
recipient=recipient,
|
| 66 |
+
template_name="test_notification",
|
| 67 |
+
template_data=TEST_TEMPLATE_DATA,
|
| 68 |
+
channel_config=TEST_CONFIG,
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
status = "β PASS" if success else "β FAIL"
|
| 72 |
+
print(f" Result : {status}")
|
| 73 |
+
print(f" Message : {message}")
|
| 74 |
+
if provider_id:
|
| 75 |
+
print(f" Provider ID: {provider_id}")
|
| 76 |
+
|
| 77 |
+
# 3. Misconfigured channel test (should fail gracefully)
|
| 78 |
+
print("\n[3] Testing misconfigured channel (no api_key)...")
|
| 79 |
+
bad_config = {"enabled": True, "from_email": "no-reply@example.com"}
|
| 80 |
+
configured_bad = EmailChannel.is_configured(bad_config)
|
| 81 |
+
print(f" is_configured with missing api_key: {'β FAIL (should be False)' if configured_bad else 'β PASS (correctly False)'}")
|
| 82 |
+
|
| 83 |
+
# 4. Disabled channel test
|
| 84 |
+
print("\n[4] Testing disabled channel...")
|
| 85 |
+
disabled_config = {**TEST_CONFIG, "enabled": False}
|
| 86 |
+
configured_disabled = EmailChannel.is_configured(disabled_config)
|
| 87 |
+
print(f" is_configured when disabled: {'β FAIL (should be False)' if configured_disabled else 'β PASS (correctly False)'}")
|
| 88 |
+
|
| 89 |
+
print("\n=== Done ===\n")
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
if __name__ == "__main__":
|
| 93 |
+
parser = argparse.ArgumentParser(description="Test SendGrid email flow")
|
| 94 |
+
parser.add_argument(
|
| 95 |
+
"--to",
|
| 96 |
+
required=True,
|
| 97 |
+
help="Recipient email address for the test send",
|
| 98 |
+
)
|
| 99 |
+
args = parser.parse_args()
|
| 100 |
+
asyncio.run(run_test(args.to))
|