Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sinch/core/clients/sinch_client_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def __init__(
self._voice_domain = "https://{}.api.sinch.com"
self._voice_region = None
self._conversation_region = conversation_region
self._conversation_domain = "https://{}.conversation.api.sinch.com/"
self._conversation_domain = "https://{}.conversation.api.sinch.com"
self._sms_region = sms_region
self._sms_region_with_service_plan_id = sms_region
self._sms_domain = "https://zt.{}.sms.api.sinch.com"
Expand Down
42 changes: 29 additions & 13 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@
ListDeliveryReportsRequest,
ListDeliveryReportsResponse,
)
from sinch.domains.sms.models.v1.response import RecipientDeliveryReport

import pytest

from sinch import SinchClient
from sinch.core.models.base_model import SinchBaseModel, SinchRequestBaseModel
from sinch.core.models.base_model import SinchRequestBaseModel
from sinch.core.models.http_response import HTTPResponse
from sinch.domains.authentication.models.v1.authentication import OAuthToken
from sinch.domains.numbers.models.v1.response import ActiveNumber
Expand Down Expand Up @@ -266,8 +265,10 @@ class MockSinchClient:
return MockSinchClient()


@pytest.fixture
def mock_sinch_client_sms():
def _create_mock_sinch_client(**config_kwargs):
"""
Helper function to create a mock Sinch client with the given configuration.
"""
from sinch.core.clients.sinch_client_configuration import Configuration
from sinch.core.ports.http_transport import HTTPTransport
from sinch.core.token_manager import TokenManager
Expand All @@ -277,16 +278,16 @@ def mock_sinch_client_sms():

mock_token_manager = MagicMock(spec=TokenManager)

config = Configuration(
transport=mock_transport,
token_manager=mock_token_manager,
project_id="test_project_id",
key_id="test_key_id",
key_secret="test_key_secret",
service_plan_id="test_service_plan_id",
sms_region="eu"
)
default_config = {
"transport": mock_transport,
"token_manager": mock_token_manager,
"project_id": "test_project_id",
"key_id": "test_key_id",
"key_secret": "test_key_secret",
}
default_config.update(config_kwargs)

config = Configuration(**default_config)
config._authentication_method = "project_auth"

class MockSinchClient:
Expand All @@ -295,6 +296,21 @@ class MockSinchClient:
return MockSinchClient()


@pytest.fixture
def mock_sinch_client_sms():
return _create_mock_sinch_client(
service_plan_id="test_service_plan_id",
sms_region="eu"
)


@pytest.fixture
def mock_sinch_client_conversation():
return _create_mock_sinch_client(
conversation_region="us"
)


@pytest.fixture
def mock_pagination_active_number_responses():
return [
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import pytest
from sinch.core.models.http_response import HTTPResponse
from sinch.domains.conversation.api.v1.internal import DeleteMessageEndpoint
from sinch.domains.conversation.models.v1.messages.internal.request import MessageIdRequest
from sinch.domains.conversation.api.v1.exceptions import ConversationException


@pytest.fixture
def request_data():
return MessageIdRequest(message_id="01FC66621XXXXX119Z8PMV1QPQ")


@pytest.fixture
def mock_response():
return HTTPResponse(
status_code=204,
body=None,
headers={"Content-Type": "application/json"},
)


@pytest.fixture
def mock_error_response():
return HTTPResponse(
status_code=404,
body={
"error": {
"message": "Message not found",
"status": "NotFound"
}
},
headers={"Content-Type": "application/json"},
)


@pytest.fixture
def endpoint(request_data):
return DeleteMessageEndpoint("test_project_id", request_data)


def test_build_url_expects_correct_url(endpoint, mock_sinch_client_conversation):
"""
Test that the URL is built correctly.
"""
assert (
endpoint.build_url(mock_sinch_client_conversation)
== "https://us.conversation.api.sinch.com/v1/projects/test_project_id/messages/01FC66621XXXXX119Z8PMV1QPQ"
)


def test_messages_source_query_param_expects_parsed_params():
"""
Test that the messages_source query parameter is parsed correctly.
"""
request_data = MessageIdRequest(
message_id="01FC66621XXXXX119Z8PMV1QPQ",
messages_source="CONVERSATION_SOURCE"
)
endpoint = DeleteMessageEndpoint("test_project_id", request_data)

query_params = endpoint.build_query_params()
assert query_params["messages_source"] == "CONVERSATION_SOURCE"


def test_handle_response_expects_success(endpoint, mock_response):
"""
Test that a successful delete response (204 No Content) is handled correctly.
"""
result = endpoint.handle_response(mock_response)
assert result is None


def test_handle_response_expects_conversation_exception_on_error(
endpoint, mock_error_response
):
"""
Test that ConversationException is raised when server returns an error.
"""
with pytest.raises(ConversationException) as exc_info:
endpoint.handle_response(mock_error_response)

assert exc_info.value.is_from_server is True
assert exc_info.value.http_response.status_code == 404
Loading