1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
|
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
import pytest
import asyncio
from datetime import datetime, timezone
from devtools_testutils import AzureRecordedTestCase, is_live
from devtools_testutils.aio import recorded_by_proxy_async
from azure.communication.identity import CommunicationIdentityClient
from azure.communication.chat.aio import ChatClient, CommunicationTokenCredential
from azure.communication.chat import ChatParticipant, ChatMessageType
from azure.communication.chat._shared.utils import parse_connection_str
from chat_e2e_helper import get_connection_str
from _shared.utils import get_http_logging_policy
class TestChatThreadClientAsync(AzureRecordedTestCase):
def setup_method(self):
connection_str = get_connection_str()
endpoint, _ = parse_connection_str(connection_str)
self.endpoint = endpoint
self.identity_client = CommunicationIdentityClient.from_connection_string(connection_str)
self.users = []
self.user_tokens = []
self.chat_clients = []
# create user 1
self.user = self.identity_client.create_user()
token_response = self.identity_client.get_token(self.user, scopes=["chat"])
self.token = token_response.token
# create user 2
self.new_user = self.identity_client.create_user()
token_response = self.identity_client.get_token(self.new_user, scopes=["chat"])
self.token_new_user = token_response.token
# create ChatClient
self.chat_client = ChatClient(
self.endpoint, CommunicationTokenCredential(self.token), http_logging_policy=get_http_logging_policy()
)
self.chat_client_new_user = ChatClient(
self.endpoint,
CommunicationTokenCredential(self.token_new_user),
http_logging_policy=get_http_logging_policy(),
)
def teardown_method(self):
# delete created users
if is_live():
self.identity_client.delete_user(self.user)
self.identity_client.delete_user(self.new_user)
async def _create_thread(self):
# create chat thread
topic = "test topic"
share_history_time = datetime.utcnow()
share_history_time = share_history_time.replace(tzinfo=timezone.utc)
participants = [
ChatParticipant(identifier=self.user, display_name="name", share_history_time=share_history_time)
]
create_chat_thread_result = await self.chat_client.create_chat_thread(topic, thread_participants=participants)
self.chat_thread_client = self.chat_client.get_chat_thread_client(create_chat_thread_result.chat_thread.id)
self.thread_id = self.chat_thread_client.thread_id
async def _create_thread_w_two_users(self):
# create chat thread
topic = "test topic"
share_history_time = datetime.utcnow()
share_history_time = share_history_time.replace(tzinfo=timezone.utc)
participants = [
ChatParticipant(identifier=self.user, display_name="name", share_history_time=share_history_time),
ChatParticipant(identifier=self.new_user, display_name="name", share_history_time=share_history_time),
]
create_chat_thread_result = await self.chat_client.create_chat_thread(topic, thread_participants=participants)
self.chat_thread_client = self.chat_client.get_chat_thread_client(create_chat_thread_result.chat_thread.id)
self.thread_id = self.chat_thread_client.thread_id
async def _send_message(self):
# send a message
content = "hello world"
sender_display_name = "sender name"
create_message_result = await self.chat_thread_client.send_message(
content, sender_display_name=sender_display_name
)
message_id = create_message_result.id
return message_id
@pytest.mark.live_test_only
@pytest.mark.asyncio
@recorded_by_proxy_async
async def test_update_topic(self):
async with self.chat_client:
await self._create_thread()
topic = "update topic"
async with self.chat_thread_client:
await self.chat_thread_client.update_topic(topic=topic)
# delete chat threads
if not self.is_playback():
await self.chat_client.delete_chat_thread(self.thread_id)
@pytest.mark.live_test_only
@pytest.mark.asyncio
@recorded_by_proxy_async
async def test_send_message(self):
async with self.chat_client:
await self._create_thread()
async with self.chat_thread_client:
content = "hello world"
sender_display_name = "sender name"
create_message_result = await self.chat_thread_client.send_message(
content, sender_display_name=sender_display_name
)
create_message_result_id = create_message_result.id
assert create_message_result_id
# delete chat threads
if not self.is_playback():
await self.chat_client.delete_chat_thread(self.thread_id)
@pytest.mark.live_test_only
@pytest.mark.asyncio
@recorded_by_proxy_async
async def test_get_message(self):
async with self.chat_client:
await self._create_thread()
async with self.chat_thread_client:
message_id = await self._send_message()
message = await self.chat_thread_client.get_message(message_id)
assert message.id == message_id
assert message.type == ChatMessageType.TEXT
assert message.content.message == "hello world"
# delete chat threads
if not self.is_playback():
await self.chat_client.delete_chat_thread(self.thread_id)
@pytest.mark.live_test_only
@pytest.mark.asyncio
@recorded_by_proxy_async
async def test_list_messages(self):
async with self.chat_client:
await self._create_thread()
async with self.chat_thread_client:
await self._send_message()
chat_messages = self.chat_thread_client.list_messages(results_per_page=1)
items = []
async for item in chat_messages:
items.append(item)
assert len(items) > 0
# delete chat threads
if not self.is_playback():
await self.chat_client.delete_chat_thread(self.thread_id)
@pytest.mark.live_test_only
@pytest.mark.asyncio
@recorded_by_proxy_async
async def test_update_message(self):
async with self.chat_client:
await self._create_thread()
async with self.chat_thread_client:
message_id = await self._send_message()
content = "updated message content"
await self.chat_thread_client.update_message(message_id, content=content)
# delete chat threads
if not self.is_playback():
await self.chat_client.delete_chat_thread(self.thread_id)
@pytest.mark.live_test_only
@pytest.mark.asyncio
@recorded_by_proxy_async
async def test_delete_message(self):
async with self.chat_client:
await self._create_thread()
async with self.chat_thread_client:
message_id = await self._send_message()
await self.chat_thread_client.delete_message(message_id)
# delete chat threads
if not self.is_playback():
await self.chat_client.delete_chat_thread(self.thread_id)
@pytest.mark.live_test_only
@pytest.mark.asyncio
@recorded_by_proxy_async
async def test_list_participants(self):
async with self.chat_client:
await self._create_thread()
async with self.chat_thread_client:
# add another participant
share_history_time = datetime.utcnow()
share_history_time = share_history_time.replace(tzinfo=timezone.utc)
new_participant = ChatParticipant(
identifier=self.new_user, display_name="name", share_history_time=share_history_time
)
await self.chat_thread_client.add_participants([new_participant])
chat_thread_participants = self.chat_thread_client.list_participants(results_per_page=1, skip=1)
items = []
async for item in chat_thread_participants:
items.append(item)
assert len(items) == 1
# delete chat threads
if not self.is_playback():
await self.chat_client.delete_chat_thread(self.thread_id)
@pytest.mark.live_test_only
@pytest.mark.asyncio
@recorded_by_proxy_async
async def test_add_participants(self):
async with self.chat_client:
await self._create_thread()
async with self.chat_thread_client:
share_history_time = datetime.utcnow()
share_history_time = share_history_time.replace(tzinfo=timezone.utc)
new_participant = ChatParticipant(
identifier=self.new_user, display_name="name", share_history_time=share_history_time
)
participants = [new_participant]
failed_participants = await self.chat_thread_client.add_participants(participants)
# no error occured while adding participants
assert len(failed_participants) == 0
if not self.is_playback():
await self.chat_client.delete_chat_thread(self.thread_id)
@pytest.mark.live_test_only
@pytest.mark.asyncio
@recorded_by_proxy_async
async def test_remove_participant(self):
async with self.chat_client:
await self._create_thread()
async with self.chat_thread_client:
# add participant first
share_history_time = datetime.utcnow()
share_history_time = share_history_time.replace(tzinfo=timezone.utc)
new_participant = ChatParticipant(
identifier=self.new_user, display_name="name", share_history_time=share_history_time
)
participants = [new_participant]
await self.chat_thread_client.add_participants(participants)
# test remove participant
await self.chat_thread_client.remove_participant(self.new_user)
if not self.is_playback():
await self.chat_client.delete_chat_thread(self.thread_id)
@pytest.mark.live_test_only
@pytest.mark.asyncio
@recorded_by_proxy_async
async def test_send_typing_notification(self):
async with self.chat_client:
await self._create_thread()
async with self.chat_thread_client:
await self.chat_thread_client.send_typing_notification()
if not self.is_playback():
await self.chat_client.delete_chat_thread(self.thread_id)
@pytest.mark.live_test_only
@pytest.mark.asyncio
@recorded_by_proxy_async
async def test_send_typing_notification_with_sender_display_name(self):
async with self.chat_client:
await self._create_thread()
async with self.chat_thread_client:
await self.chat_thread_client.send_typing_notification(sender_display_name="John")
if not self.is_playback():
await self.chat_client.delete_chat_thread(self.thread_id)
@pytest.mark.live_test_only
@pytest.mark.asyncio
@recorded_by_proxy_async
async def test_send_read_receipt(self):
async with self.chat_client:
await self._create_thread()
async with self.chat_thread_client:
message_id = await self._send_message()
await self.chat_thread_client.send_read_receipt(message_id)
if not self.is_playback():
await self.chat_client.delete_chat_thread(self.thread_id)
async def _wait_on_thread(self, chat_client, thread_id, message_id):
# print("Read Receipts Sent: ", read_receipts_sent)
chat_thread_client = chat_client.get_chat_thread_client(thread_id)
for _ in range(10):
read_receipts_paged = chat_thread_client.list_read_receipts()
chat_message_ids = []
async for page in read_receipts_paged.by_page():
async for item in page:
chat_message_ids.append(item.chat_message_id)
if message_id in chat_message_ids:
return
else:
print("Sleeping for additional 2 secs")
await asyncio.sleep(2)
raise Exception("Read receipts not updated in 20 seconds. Failing.")
@pytest.mark.live_test_only
@pytest.mark.asyncio
@recorded_by_proxy_async
async def test_list_read_receipts(self):
async with self.chat_client:
await self._create_thread_w_two_users()
async with self.chat_thread_client:
# first user sends 2 messages
for i in range(2):
message_id = await self._send_message()
# send read receipts first
await self.chat_thread_client.send_read_receipt(message_id)
if self.is_live:
await self._wait_on_thread(
chat_client=self.chat_client, thread_id=self.thread_id, message_id=message_id
)
# get chat thread client for second user
chat_thread_client_new_user = self.chat_client_new_user.get_chat_thread_client(self.thread_id)
# second user sends 1 message
message_result_new_user = await chat_thread_client_new_user.send_message(
"content", sender_display_name="sender_display_name"
)
message_id_new_user = message_result_new_user.id
# send read receipt
await chat_thread_client_new_user.send_read_receipt(message_id_new_user)
if self.is_live:
await self._wait_on_thread(
chat_client=self.chat_client_new_user, thread_id=self.thread_id, message_id=message_id_new_user
)
# list read receipts
read_receipts = self.chat_thread_client.list_read_receipts(results_per_page=2, skip=0)
items = []
async for page in read_receipts.by_page():
async for item in page:
items.append(item)
assert len(items) == 2
if not self.is_playback():
await self.chat_client.delete_chat_thread(self.thread_id)
@pytest.mark.live_test_only
@pytest.mark.asyncio
@recorded_by_proxy_async
async def test_get_properties(self):
async with self.chat_client:
await self._create_thread()
async with self.chat_thread_client:
get_thread_result = await self.chat_thread_client.get_properties()
assert get_thread_result.id == self.thread_id
# delete created users and chat threads
if not self.is_playback():
await self.chat_client.delete_chat_thread(self.thread_id)
|