Fix SMTP2GO SMS send payload for current API schema.
Use destination/content with E.164 numbers and surface provider error bodies so 400s are actionable.
This commit is contained in:
@@ -1329,3 +1329,96 @@ class CampaignRecipientTableTests(TestCase):
|
||||
response = self.client.get(url)
|
||||
self.assertContains(response, "Recent PCM Integrations events")
|
||||
self.assertNotContains(response, "Recent SMTP2GO events")
|
||||
|
||||
|
||||
class Smtp2goSmsSendTests(TestCase):
|
||||
def setUp(self):
|
||||
self.contact = Contact.objects.create(
|
||||
phone="330-402-2675",
|
||||
first_name="Rufus",
|
||||
last_name="Firefly",
|
||||
)
|
||||
set_channel_consent(
|
||||
self.contact, Channel.SMS, opted_in=True, reason="test"
|
||||
)
|
||||
self.campaign = create_campaign_draft(
|
||||
name="SMS blast",
|
||||
audience=Campaign.Audience.SMS_OPT_IN,
|
||||
body="Hello {{first_name}}",
|
||||
)
|
||||
self.message = self.campaign.messages.get(contact=self.contact)
|
||||
|
||||
def test_format_destination_us_local(self):
|
||||
from messaging.providers.sms.smtp2go import _format_destination
|
||||
|
||||
self.assertEqual(_format_destination("330-402-2675"), "+13304022675")
|
||||
self.assertEqual(_format_destination("+1 (330) 402-2675"), "+13304022675")
|
||||
self.assertEqual(_format_destination("13304022675"), "+13304022675")
|
||||
|
||||
def test_send_sms_uses_destination_and_content(self):
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
from messaging.providers.sms.smtp2go import send_sms
|
||||
|
||||
resp = MagicMock()
|
||||
resp.ok = True
|
||||
resp.content = b'{"request_id":"req-1","data":{"messages":[{"message_id":"sms-abc","destination":"+13304022675","status":"queued"}],"total_sent":1}}'
|
||||
resp.json.return_value = {
|
||||
"request_id": "req-1",
|
||||
"data": {
|
||||
"messages": [
|
||||
{
|
||||
"message_id": "sms-abc",
|
||||
"destination": "+13304022675",
|
||||
"status": "queued",
|
||||
}
|
||||
],
|
||||
"total_sent": 1,
|
||||
},
|
||||
}
|
||||
|
||||
with self.settings(SMTP2GO_SMS_API_KEY="api-test-key"):
|
||||
with patch(
|
||||
"messaging.providers.sms.smtp2go.requests.post",
|
||||
return_value=resp,
|
||||
) as post:
|
||||
provider_id = send_sms(self.message)
|
||||
|
||||
self.assertEqual(provider_id, "sms-abc")
|
||||
payload = post.call_args.kwargs["json"]
|
||||
self.assertEqual(payload["api_key"], "api-test-key")
|
||||
self.assertEqual(payload["destination"], ["+13304022675"])
|
||||
self.assertEqual(payload["content"], "Hello Rufus")
|
||||
self.assertNotIn("to", payload)
|
||||
self.assertNotIn("text", payload)
|
||||
|
||||
def test_send_sms_includes_api_error_body(self):
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import requests
|
||||
|
||||
from messaging.providers.sms.smtp2go import send_sms
|
||||
|
||||
resp = MagicMock()
|
||||
resp.ok = False
|
||||
resp.status_code = 400
|
||||
resp.url = "https://api.smtp2go.com/v3/sms/send"
|
||||
resp.reason = "Bad Request"
|
||||
resp.text = '{"data":{"error":"Missing required field","error_code":"E_ApiResponseCodes.INVALID_REQUEST"}}'
|
||||
resp.json.return_value = {
|
||||
"data": {
|
||||
"error": "Missing required field",
|
||||
"error_code": "E_ApiResponseCodes.INVALID_REQUEST",
|
||||
}
|
||||
}
|
||||
|
||||
with self.settings(SMTP2GO_SMS_API_KEY="api-test-key"):
|
||||
with patch(
|
||||
"messaging.providers.sms.smtp2go.requests.post",
|
||||
return_value=resp,
|
||||
):
|
||||
with self.assertRaises(requests.HTTPError) as ctx:
|
||||
send_sms(self.message)
|
||||
|
||||
self.assertIn("Missing required field", str(ctx.exception))
|
||||
self.assertIn("INVALID_REQUEST", str(ctx.exception))
|
||||
|
||||
Reference in New Issue
Block a user