Files
gapido-grpc-auth/tests/test_kavenegar_client.py

58 lines
2.2 KiB
Python

import httpx
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
async def test_kavenegar_success() -> None:
def handler(request: httpx.Request) -> httpx.Response:
assert request.url.path == "/v1/test-key/verify/lookup.json"
form = dict(item.split("=") for item in request.content.decode().split("&"))
assert form["receptor"] == "989120000000"
assert form["template"] == "login-otp"
return httpx.Response(200, json={"return": {"status": 200}})
client = KavenegarSmsClient("test-key", transport=httpx.MockTransport(handler))
await client.send_otp("989120000000", "123456", "login-otp")
@pytest.mark.asyncio
async def test_kavenegar_api_error() -> None:
def handler(request: httpx.Request) -> httpx.Response:
return httpx.Response(200, json={"return": {"status": 418}})
client = KavenegarSmsClient("test-key", transport=httpx.MockTransport(handler))
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")