Skip to content

Commit be51463

Browse files
committed
Implement Helpdesk chat messages
1 parent f543726 commit be51463

File tree

8 files changed

+256
-0
lines changed

8 files changed

+256
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from mpt_api_client.http import AsyncService, Service
2+
from mpt_api_client.http.mixins import (
3+
AsyncCollectionMixin,
4+
AsyncCreateMixin,
5+
AsyncDeleteMixin,
6+
AsyncUpdateMixin,
7+
CollectionMixin,
8+
CreateMixin,
9+
DeleteMixin,
10+
UpdateMixin,
11+
)
12+
from mpt_api_client.models import Model
13+
14+
15+
class ChatMessage(Model):
16+
"""Helpdesk Chat Message resource."""
17+
18+
19+
class ChatMessagesServiceConfig:
20+
"""Helpdesk Chat Messages service configuration."""
21+
22+
_endpoint = "/public/v1/helpdesk/chats/{chat_id}/messages"
23+
_model_class = ChatMessage
24+
_collection_key = "data"
25+
26+
27+
class ChatMessagesService(
28+
CreateMixin[ChatMessage],
29+
UpdateMixin[ChatMessage],
30+
DeleteMixin,
31+
CollectionMixin[ChatMessage],
32+
Service[ChatMessage],
33+
ChatMessagesServiceConfig,
34+
):
35+
"""Helpdesk Chat Messages service."""
36+
37+
38+
class AsyncChatMessagesService(
39+
AsyncCreateMixin[ChatMessage],
40+
AsyncUpdateMixin[ChatMessage],
41+
AsyncDeleteMixin,
42+
AsyncCollectionMixin[ChatMessage],
43+
AsyncService[ChatMessage],
44+
ChatMessagesServiceConfig,
45+
):
46+
"""Async Helpdesk Chat Messages service."""

mpt_api_client/resources/helpdesk/chats.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
UpdateMixin,
1111
)
1212
from mpt_api_client.models import Model
13+
from mpt_api_client.resources.helpdesk.chat_messages import (
14+
AsyncChatMessagesService,
15+
ChatMessagesService,
16+
)
1317

1418

1519
class Chat(Model):
@@ -34,6 +38,12 @@ class ChatsService(
3438
):
3539
"""Helpdesk Chats service."""
3640

41+
def messages(self, chat_id: str) -> ChatMessagesService:
42+
"""Return chat messages service."""
43+
return ChatMessagesService(
44+
http_client=self.http_client, endpoint_params={"chat_id": chat_id}
45+
)
46+
3747

3848
class AsyncChatsService(
3949
AsyncCreateMixin[Chat],
@@ -44,3 +54,9 @@ class AsyncChatsService(
4454
ChatsServiceConfig,
4555
):
4656
"""Async Helpdesk Chats service."""
57+
58+
def messages(self, chat_id: str) -> AsyncChatMessagesService:
59+
"""Return async chat messages service."""
60+
return AsyncChatMessagesService(
61+
http_client=self.http_client, endpoint_params={"chat_id": chat_id}
62+
)

tests/e2e/helpdesk/chats/messages/__init__.py

Whitespace-only changes.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import pytest
2+
3+
from tests.e2e.helper import (
4+
async_create_fixture_resource_and_delete,
5+
create_fixture_resource_and_delete,
6+
)
7+
8+
9+
@pytest.fixture
10+
def chat_messages_service(mpt_ops, chat_id):
11+
return mpt_ops.helpdesk.chats.messages(chat_id)
12+
13+
14+
@pytest.fixture
15+
def async_chat_messages_service(async_mpt_ops, chat_id):
16+
return async_mpt_ops.helpdesk.chats.messages(chat_id)
17+
18+
19+
@pytest.fixture
20+
def chat_message_data(short_uuid):
21+
return {
22+
"content": f"e2e message - {short_uuid}",
23+
}
24+
25+
26+
@pytest.fixture
27+
def created_chat_message(chat_messages_service, chat_message_data):
28+
with create_fixture_resource_and_delete(
29+
chat_messages_service, chat_message_data
30+
) as chat_message:
31+
yield chat_message
32+
33+
34+
@pytest.fixture
35+
async def async_created_chat_message(async_chat_messages_service, chat_message_data):
36+
async with async_create_fixture_resource_and_delete(
37+
async_chat_messages_service, chat_message_data
38+
) as chat_message:
39+
yield chat_message
40+
41+
42+
@pytest.fixture
43+
def invalid_chat_message_id():
44+
return "MSG-0000-0000-0000"
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import pytest
2+
3+
from mpt_api_client.exceptions import MPTAPIError
4+
5+
pytestmark = [pytest.mark.flaky]
6+
7+
8+
async def test_list_chat_messages(async_chat_messages_service):
9+
result = await async_chat_messages_service.fetch_page(limit=1)
10+
11+
assert len(result) > 0
12+
13+
14+
def test_create_chat_message(async_created_chat_message, chat_message_data): # noqa: AAA01
15+
assert async_created_chat_message.id is not None
16+
assert async_created_chat_message.to_dict().get("content") == chat_message_data["content"]
17+
18+
19+
async def test_update_chat_message_visibility(
20+
async_chat_messages_service, async_created_chat_message
21+
):
22+
result = await async_chat_messages_service.update(
23+
async_created_chat_message.id,
24+
{"visibility": "Public"},
25+
)
26+
27+
assert result.id == async_created_chat_message.id
28+
29+
30+
async def test_delete_chat_message(async_chat_messages_service, async_created_chat_message):
31+
result = async_created_chat_message
32+
33+
await async_chat_messages_service.delete(result.id)
34+
35+
36+
async def test_update_chat_message_not_found(async_chat_messages_service, invalid_chat_message_id):
37+
with pytest.raises(MPTAPIError, match=r"404 Not Found"):
38+
await async_chat_messages_service.update(
39+
invalid_chat_message_id,
40+
{"visibility": "Public"},
41+
)
42+
43+
44+
async def test_delete_chat_message_not_found(async_chat_messages_service, invalid_chat_message_id):
45+
with pytest.raises(MPTAPIError, match=r"404 Not Found"):
46+
await async_chat_messages_service.delete(invalid_chat_message_id)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import pytest
2+
3+
from mpt_api_client.exceptions import MPTAPIError
4+
5+
pytestmark = [pytest.mark.flaky]
6+
7+
8+
def test_list_chat_messages(chat_messages_service):
9+
result = chat_messages_service.fetch_page(limit=1)
10+
11+
assert len(result) > 0
12+
13+
14+
def test_create_chat_message(created_chat_message, chat_message_data): # noqa: AAA01
15+
assert created_chat_message.id is not None
16+
assert created_chat_message.to_dict().get("content") == chat_message_data["content"]
17+
18+
19+
def test_update_chat_message_visibility(chat_messages_service, created_chat_message):
20+
result = chat_messages_service.update(created_chat_message.id, {"visibility": "Public"})
21+
22+
assert result.id == created_chat_message.id
23+
24+
25+
def test_delete_chat_message(chat_messages_service, created_chat_message):
26+
result = created_chat_message
27+
28+
chat_messages_service.delete(result.id)
29+
30+
31+
def test_update_chat_message_not_found(chat_messages_service, invalid_chat_message_id):
32+
with pytest.raises(MPTAPIError, match=r"404 Not Found"):
33+
chat_messages_service.update(invalid_chat_message_id, {"visibility": "Public"})
34+
35+
36+
def test_delete_chat_message_not_found(chat_messages_service, invalid_chat_message_id):
37+
with pytest.raises(MPTAPIError, match=r"404 Not Found"):
38+
chat_messages_service.delete(invalid_chat_message_id)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import pytest
2+
3+
from mpt_api_client.resources.helpdesk.chat_messages import (
4+
AsyncChatMessagesService,
5+
ChatMessagesService,
6+
)
7+
8+
9+
@pytest.fixture
10+
def chat_messages_service(http_client) -> ChatMessagesService:
11+
return ChatMessagesService(
12+
http_client=http_client, endpoint_params={"chat_id": "CHT-0000-0000-0001"}
13+
)
14+
15+
16+
@pytest.fixture
17+
def async_chat_messages_service(async_http_client) -> AsyncChatMessagesService:
18+
return AsyncChatMessagesService(
19+
http_client=async_http_client, endpoint_params={"chat_id": "CHT-0000-0000-0001"}
20+
)
21+
22+
23+
def test_endpoint(chat_messages_service) -> None:
24+
result = chat_messages_service.path == "/public/v1/helpdesk/chats/CHT-0000-0000-0001/messages"
25+
26+
assert result is True
27+
28+
29+
def test_async_endpoint(async_chat_messages_service) -> None:
30+
result = (
31+
async_chat_messages_service.path == "/public/v1/helpdesk/chats/CHT-0000-0000-0001/messages"
32+
)
33+
34+
assert result is True
35+
36+
37+
@pytest.mark.parametrize("method", ["create", "update", "delete", "iterate"])
38+
def test_methods_present(chat_messages_service, method: str) -> None:
39+
result = hasattr(chat_messages_service, method)
40+
41+
assert result is True
42+
43+
44+
@pytest.mark.parametrize("method", ["create", "update", "delete", "iterate"])
45+
def test_async_methods_present(async_chat_messages_service, method: str) -> None:
46+
result = hasattr(async_chat_messages_service, method)
47+
48+
assert result is True

tests/unit/resources/helpdesk/test_chats.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import pytest
22

3+
from mpt_api_client.resources.helpdesk.chat_messages import (
4+
AsyncChatMessagesService,
5+
ChatMessagesService,
6+
)
37
from mpt_api_client.resources.helpdesk.chats import AsyncChatsService, ChatsService
48

59

@@ -31,3 +35,17 @@ def test_async_mixins_present(async_chats_service, method):
3135
result = hasattr(async_chats_service, method)
3236

3337
assert result is True
38+
39+
40+
def test_messages_service(chats_service):
41+
result = chats_service.messages("CHT-0000-0000-0001")
42+
43+
assert isinstance(result, ChatMessagesService)
44+
assert result.endpoint_params == {"chat_id": "CHT-0000-0000-0001"}
45+
46+
47+
def test_async_messages_service(async_chats_service):
48+
result = async_chats_service.messages("CHT-0000-0000-0001")
49+
50+
assert isinstance(result, AsyncChatMessagesService)
51+
assert result.endpoint_params == {"chat_id": "CHT-0000-0000-0001"}

0 commit comments

Comments
 (0)