feat(sms): add selectable provider strategy

This commit is contained in:
2026-07-14 09:45:40 +03:30
parent 2264b7fc95
commit e382c4f93a
10 changed files with 171 additions and 12 deletions

View File

@@ -3,6 +3,7 @@ import pytest
from gapido_auth.domain.errors import ExternalServiceError
from gapido_auth.infrastructure.kavenegar_client import KavenegarSmsClient
from gapido_auth.infrastructure.sms_ir_client import SmsIrSmsClient
@pytest.mark.asyncio
@@ -28,3 +29,29 @@ async def test_kavenegar_api_error() -> None:
with pytest.raises(ExternalServiceError):
await client.send_otp("989120000000", "123456", "login-otp")
@pytest.mark.asyncio
async def test_sms_ir_success() -> None:
def handler(request: httpx.Request) -> httpx.Response:
assert request.url == "https://api.sms.ir/v1/send/verify"
assert request.headers["x-api-key"] == "sms-ir-key"
payload = request.read()
assert b'"mobile":"989120000000"' in payload
assert b'"templateId":570574' in payload
assert b'"name":"OTP"' in payload
assert b'"value":"123456"' in payload
return httpx.Response(200, json={"status": "1"})
client = SmsIrSmsClient("sms-ir-key", transport=httpx.MockTransport(handler))
await client.send_otp("989120000000", "123456", "570574")
@pytest.mark.asyncio
async def test_sms_ir_api_error() -> None:
def handler(request: httpx.Request) -> httpx.Response:
return httpx.Response(200, json={"status": "0"})
client = SmsIrSmsClient("sms-ir-key", transport=httpx.MockTransport(handler))
with pytest.raises(ExternalServiceError):
await client.send_otp("989120000000", "123456", "570574")

View File

@@ -0,0 +1,26 @@
from gapido_auth.config import Settings
from gapido_auth.infrastructure.kavenegar_client import KavenegarSmsClient
from gapido_auth.infrastructure.sms_ir_client import SmsIrSmsClient
from gapido_auth.infrastructure.sms_provider import create_sms_client, get_sms_template
def test_sms_provider_factory_selects_kavenegar() -> None:
settings = Settings(
sms_provider="kavenegar",
kavenegar_api_key="kavenegar-key",
kavenegar_login_template="login-otp",
)
assert isinstance(create_sms_client(settings), KavenegarSmsClient)
assert get_sms_template(settings) == "login-otp"
def test_sms_provider_factory_selects_sms_ir() -> None:
settings = Settings(
sms_provider="sms_ir",
sms_ir_api_key="sms-ir-key",
sms_ir_verify_template_id=570574,
)
assert isinstance(create_sms_client(settings), SmsIrSmsClient)
assert get_sms_template(settings) == "570574"