66 lines
2.1 KiB
Python
66 lines
2.1 KiB
Python
from unittest.mock import Mock, patch
|
|
|
|
from django.test import TestCase
|
|
|
|
from apps.users.tasks import _send_sms, send_verification_sms
|
|
|
|
|
|
class UserTaskTests(TestCase):
|
|
def test_send_verification_sms_skips_real_delivery_without_api_key(self):
|
|
with self.settings(SMS_APIKEY=""):
|
|
result = send_verification_sms("09123456789", "12345")
|
|
|
|
self.assertEqual(
|
|
result,
|
|
{
|
|
"mobile": "09123456789",
|
|
"code": "12345",
|
|
"sent": False,
|
|
},
|
|
)
|
|
|
|
@patch("apps.users.tasks._send_sms")
|
|
def test_send_verification_sms_calls_sender_when_api_key_exists(self, send_sms):
|
|
send_sms.return_value = Mock(status_code=200)
|
|
|
|
with self.settings(SMS_APIKEY="configured-key"):
|
|
send_verification_sms("09123456789", "12345")
|
|
|
|
send_sms.assert_called_once_with(
|
|
"09123456789",
|
|
570574,
|
|
variables=[{"name": "OTP", "value": "12345"}],
|
|
)
|
|
|
|
@patch("apps.users.tasks._send_sms", return_value=None)
|
|
def test_send_verification_sms_raises_when_delivery_fails(self, send_sms):
|
|
with self.settings(SMS_APIKEY="configured-key"):
|
|
with self.assertRaises(Exception):
|
|
send_verification_sms("09123456789", "12345")
|
|
|
|
send_sms.assert_called_once()
|
|
|
|
@patch("apps.users.tasks.requests.post")
|
|
def test_send_sms_posts_verify_payload(self, requests_post):
|
|
response = Mock(status_code=200, text="ok")
|
|
response.json.return_value = {"status": "1"}
|
|
requests_post.return_value = response
|
|
|
|
with self.settings(SMS_APIKEY="configured-key"):
|
|
result = _send_sms(
|
|
"09123456789",
|
|
570574,
|
|
variables=[{"name": "OTP", "value": "12345"}],
|
|
)
|
|
|
|
self.assertEqual(result, response)
|
|
requests_post.assert_called_once()
|
|
self.assertEqual(
|
|
requests_post.call_args.kwargs["json"],
|
|
{
|
|
"mobile": "09123456789",
|
|
"templateId": 570574,
|
|
"parameters": [{"name": "OTP", "value": "12345"}],
|
|
},
|
|
)
|