async def test_list_messages_with_start_time(): thread_id = "19:[email protected]" raised = False async def mock_send(*_, **__): return mock_response(status_code=200, json_payload={ "value": [{ "id": "message_id1", "createdOn": "2020-08-17T18:05:44Z" }, { "id": "message_id2", "createdOn": "2020-08-17T23:13:33Z" }] }) chat_thread_client = ChatThreadClient("https://endpoint", credential, thread_id, transport=Mock(send=mock_send)) chat_messages = None try: chat_messages = chat_thread_client.list_messages( start_time=datetime(2020, 8, 17, 18, 0, 0)) except: raised = True assert raised == False items = [] async for item in chat_messages: items.append(item) assert len(items) == 2
async def test_get_properties(): thread_id = "19:[email protected]" raised = False async def mock_send(*_, **__): return mock_response(status_code=200, json_payload={ "id": thread_id, "topic": "Lunch Chat thread", "createdOn": "2020-10-30T10:50:50Z", "deletedOn": "2020-10-30T10:50:50Z", "createdByCommunicationIdentifier": { "rawId": "string", "communicationUser": { "id": "string" } } }) chat_thread_client = ChatThreadClient("https://endpoint", credential, thread_id, transport=Mock(send=mock_send)) get_thread_result = None try: get_thread_result = await chat_thread_client.get_properties() except: raised = True assert raised == False assert get_thread_result.id == thread_id
async def test_list_members(): thread_id = "19:[email protected]" member_id = "8:acs:57b9bac9-df6c-4d39-a73b-26e944adf6ea_9b0110-08007f1041" raised = False async def mock_send(*_, **__): return mock_response(status_code=200, json_payload={"value": [{ "id": member_id }]}) chat_thread_client = ChatThreadClient("https://endpoint", credential, thread_id, transport=Mock(send=mock_send)) chat_thread_members = None try: chat_thread_members = chat_thread_client.list_members() except: raised = True assert raised == False items = [] async for item in chat_thread_members: items.append(item) assert len(items) == 1
async def test_list_read_receipts(): thread_id = "19:[email protected]" message_id = "1596823919339" raised = False async def mock_send(*_, **__): return mock_response( status_code=200, json_payload={"value": [{ "chatMessageId": message_id }]}) chat_thread_client = ChatThreadClient("https://endpoint", credential, thread_id, transport=Mock(send=mock_send)) read_receipts = None try: read_receipts = chat_thread_client.list_read_receipts() except: raised = True assert raised == False items = [] async for item in read_receipts: items.append(item) assert len(items) == 1
async def test_add_members(): thread_id = "19:[email protected]" new_member_id = "8:acs:57b9bac9-df6c-4d39-a73b-26e944adf6ea_9b0110-08007f1041" raised = False async def mock_send(*_, **__): return mock_response(status_code=207) chat_thread_client = ChatThreadClient("https://endpoint", credential, thread_id, transport=Mock(send=mock_send)) new_member = ChatThreadMember( user=CommunicationUserIdentifier(new_member_id), display_name='name', share_history_time=datetime.utcnow()) members = [new_member] try: await chat_thread_client.add_members(members) except: raised = True assert raised == False
async def test_send_message(): thread_id = "19:[email protected]" message_id = '1596823919339' raised = False async def mock_send(*_, **__): return mock_response(status_code=201, json_payload={"id": message_id}) chat_thread_client = ChatThreadClient("https://endpoint", credential, thread_id, transport=Mock(send=mock_send)) create_message_result = None try: priority = ChatMessagePriority.NORMAL content = 'hello world' sender_display_name = 'sender name' create_message_result = await chat_thread_client.send_message( content, priority=priority, sender_display_name=sender_display_name) except: raised = True assert raised == False assert create_message_result.id == message_id
async def test_list_participants_with_results_per_page(): thread_id = "19:[email protected]" participant_id_1 = "8:acs:9b665d53-8164-4923-ad5d-5e983b07d2e7_00000006-5399-552c-b274-5a3a0d0000dc" participant_id_2 = "8:acs:9b665d53-8164-4923-ad5d-5e983b07d2e7_00000006-9d32-35c9-557d-5a3a0d0002f1" raised = False async def mock_send(*_, **__): return mock_response(status_code=200, json_payload={ "value": [ {"id": participant_id_1}, {"id": participant_id_2} ]}) chat_thread_client = ChatThreadClient("https://endpoint", credential, thread_id, transport=Mock(send=mock_send)) chat_thread_participants = None try: chat_thread_participants = chat_thread_client.list_participants(results_per_page=2) except: raised = True assert raised == False items = [] async for item in chat_thread_participants: items.append(item) assert len(items) == 2
async def test_send_message_w_invalid_type_throws_error(): thread_id = "19:[email protected]" message_id='1596823919339' raised = False # the payload is irrelevant - it'll fail before async def mock_send(*_, **__): return mock_response(status_code=201, json_payload={"id": message_id}) chat_thread_client = ChatThreadClient("https://endpoint", credential, thread_id, transport=Mock(send=mock_send)) create_message_result_id = None chat_message_types = [ChatMessageType.PARTICIPANT_ADDED, ChatMessageType.PARTICIPANT_REMOVED, ChatMessageType.TOPIC_UPDATED, "participant_added", "participant_removed", "topic_updated", "ChatMessageType.TEXT", "ChatMessageType.HTML", "ChatMessageType.PARTICIPANT_ADDED", "ChatMessageType.PARTICIPANT_REMOVED", "ChatMessageType.TOPIC_UPDATED"] for chat_message_type in chat_message_types: try: content='hello world' sender_display_name='sender name' create_message_result_id = await chat_thread_client.send_message( content, chat_message_type=chat_message_type, sender_display_name=sender_display_name) except: raised = True assert raised == True
async def test_list_read_receipts_with_results_per_page_and_skip(): thread_id = "19:[email protected]" message_id_1 = "1596823919339" raised = False async def mock_send(*_, **__): return mock_response(status_code=200, json_payload={ "value": [ { "chatMessageId": message_id_1, "senderCommunicationIdentifier": { "rawId": "string", "communicationUser": { "id": "string" } } } ]}) chat_thread_client = ChatThreadClient("https://endpoint", credential, thread_id, transport=Mock(send=mock_send)) read_receipts = None try: read_receipts = chat_thread_client.list_read_receipts(results_per_page=1, skip=1) except: raised = True assert raised == False items = [] async for item in read_receipts: items.append(item) assert len(items) == 1
async def test_add_participant_w_failed_participants(): thread_id = "19:[email protected]" new_participant_id="8:acs:57b9bac9-df6c-4d39-a73b-26e944adf6ea_9b0110-08007f1041" raised = False error_message = "some error message" async def mock_send(*_, **__): return mock_response(status_code=201, json_payload={ "errors": { "invalidParticipants": [ { "code": "string", "message": error_message, "target": new_participant_id, "details": [] } ] } }) chat_thread_client = ChatThreadClient("https://endpoint", credential, thread_id, transport=Mock(send=mock_send)) new_participant = ChatThreadParticipant( user=CommunicationUserIdentifier(new_participant_id), display_name='name', share_history_time=datetime.utcnow()) try: await chat_thread_client.add_participant(new_participant) except: raised = True assert raised == True
async def test_list_participants(): thread_id = "19:[email protected]" participant_id="8:acs:57b9bac9-df6c-4d39-a73b-26e944adf6ea_9b0110-08007f1041" raised = False async def mock_send(*_, **__): return mock_response(status_code=200, json_payload={"value": [ { "communicationIdentifier": { "rawId": participant_id, "communicationUser": { "id": participant_id } }, "displayName": "Bob", "shareHistoryTime": "2020-10-30T10:50:50Z" } ]}) chat_thread_client = ChatThreadClient("https://endpoint", credential, thread_id, transport=Mock(send=mock_send)) chat_thread_participants = None try: chat_thread_participants = chat_thread_client.list_participants() except: raised = True assert raised == False items = [] async for item in chat_thread_participants: items.append(item) assert len(items) == 1
async def send_message_async(self): from azure.communication.chat.aio import ChatThreadClient, CommunicationTokenCredential, CommunicationTokenRefreshOptions refresh_options = CommunicationTokenRefreshOptions(self.token) chat_thread_client = ChatThreadClient( self.endpoint, CommunicationTokenCredential(refresh_options), self._thread_id) async with chat_thread_client: # [START send_message] from azure.communication.chat import ChatMessagePriority priority = ChatMessagePriority.NORMAL content = 'hello world' sender_display_name = 'sender name' send_message_result_id = await chat_thread_client.send_message( content, priority=priority, sender_display_name=sender_display_name) send_message_result_w_type_id = await chat_thread_client.send_message( content, sender_display_name=sender_display_name, chat_message_type=ChatMessageType.TEXT) # [END send_message] self._message_id = send_message_result_id print("send_message succeeded, message id:", self._message_id) print("send_message succeeded with type specified, message id:", send_message_result_w_type_id)
async def delete_message_async(self): from azure.communication.chat.aio import ChatThreadClient, CommunicationUserCredential chat_thread_client = ChatThreadClient(self.endpoint, CommunicationUserCredential(self.token), self._thread_id) async with chat_thread_client: # [START delete_message] await chat_thread_client.delete_message(self._message_id) # [END delete_message] print("delete_message succeeded")
async def remove_member_async(self): from azure.communication.chat.aio import ChatThreadClient, CommunicationUserCredential chat_thread_client = ChatThreadClient(self.endpoint, CommunicationUserCredential(self.token), self._thread_id) async with chat_thread_client: # [START remove_member] await chat_thread_client.remove_member(self.new_user) # [END remove_member] print("remove_member_async succeeded")
async def send_typing_notification_async(self): from azure.communication.chat.aio import ChatThreadClient, CommunicationUserCredential chat_thread_client = ChatThreadClient(self.endpoint, CommunicationUserCredential(self.token), self._thread_id) async with chat_thread_client: # [START send_typing_notification] await chat_thread_client.send_typing_notification() # [END send_typing_notification] print("send_typing_notification succeeded")
async def list_members_async(self): from azure.communication.chat.aio import ChatThreadClient, CommunicationUserCredential chat_thread_client = ChatThreadClient(self.endpoint, CommunicationUserCredential(self.token), self._thread_id) async with chat_thread_client: # [START list_members] chat_thread_members = chat_thread_client.list_members() print("list_members succeeded, members:") async for chat_thread_member in chat_thread_members: print(chat_thread_member)
async def update_message_async(self): from azure.communication.chat.aio import ChatThreadClient, CommunicationUserCredential chat_thread_client = ChatThreadClient(self.endpoint, CommunicationUserCredential(self.token), self._thread_id) async with chat_thread_client: # [START update_message] content = "updated message content" await chat_thread_client.update_message(self._message_id, content=content) # [END update_message] print("update_message succeeded")
async def get_message_async(self): from azure.communication.chat.aio import ChatThreadClient, CommunicationUserCredential chat_thread_client = ChatThreadClient(self.endpoint, CommunicationUserCredential(self.token), self._thread_id) async with chat_thread_client: # [START get_message] chat_message = await chat_thread_client.get_message(self._message_id) # [END get_message] print("get_message succeeded, message id:", chat_message.id, \ "content: ", chat_message.content)
async def send_read_receipt_async(self): from azure.communication.chat.aio import ChatThreadClient, CommunicationUserCredential chat_thread_client = ChatThreadClient(self.endpoint, CommunicationUserCredential(self.token), self._thread_id) async with chat_thread_client: # [START send_read_receipt] await chat_thread_client.send_read_receipt(self._message_id) # [END send_read_receipt] print("send_read_receipt succeeded")
async def list_read_receipts_async(self): from azure.communication.chat.aio import ChatThreadClient, CommunicationUserCredential chat_thread_client = ChatThreadClient(self.endpoint, CommunicationUserCredential(self.token), self._thread_id) async with chat_thread_client: # [START list_read_receipts] read_receipts = chat_thread_client.list_read_receipts() # [END list_read_receipts] print("list_read_receipts succeeded, receipts:") async for read_receipt in read_receipts: print(read_receipt)
async def update_thread_async(self): from azure.communication.chat.aio import ChatThreadClient, CommunicationUserCredential chat_thread_client = ChatThreadClient(self.endpoint, CommunicationUserCredential(self.token), self._thread_id) async with chat_thread_client: # [START update_thread] topic = "updated thread topic" await chat_thread_client.update_thread(topic=topic) # [END update_thread] print("update_thread succeeded")
async def delete_message_async(self): from azure.communication.chat.aio import ChatThreadClient, CommunicationTokenCredential, CommunicationTokenRefreshOptions refresh_options = CommunicationTokenRefreshOptions(self.token) chat_thread_client = ChatThreadClient( self.endpoint, CommunicationTokenCredential(refresh_options), self._thread_id) async with chat_thread_client: # [START delete_message] await chat_thread_client.delete_message(self._message_id) # [END delete_message] print("delete_message succeeded")
async def remove_participant_async(self): from azure.communication.chat.aio import ChatThreadClient, CommunicationTokenCredential, \ CommunicationTokenRefreshOptions refresh_options = CommunicationTokenRefreshOptions(self.token) chat_thread_client = ChatThreadClient( self.endpoint, CommunicationTokenCredential(refresh_options), self._thread_id) async with chat_thread_client: # [START remove_participant] await chat_thread_client.remove_participant(self.new_user) # [END remove_participant] print("remove_participant_async succeeded")
async def list_participants_async(self): from azure.communication.chat.aio import ChatThreadClient, CommunicationTokenCredential, CommunicationTokenRefreshOptions refresh_options = CommunicationTokenRefreshOptions(self.token) chat_thread_client = ChatThreadClient( self.endpoint, CommunicationTokenCredential(refresh_options), self._thread_id) async with chat_thread_client: # [START list_participants] chat_thread_participants = chat_thread_client.list_participants() print("list_participants succeeded, participants:") async for chat_thread_participant in chat_thread_participants: print(chat_thread_participant)
async def list_messages_async(self): from azure.communication.chat.aio import ChatThreadClient, CommunicationUserCredential chat_thread_client = ChatThreadClient(self.endpoint, CommunicationUserCredential(self.token), self._thread_id) async with chat_thread_client: # [START list_messages] from datetime import datetime, timedelta start_time = datetime.utcnow() - timedelta(days=1) chat_messages = chat_thread_client.list_messages(results_per_page=1, start_time=start_time) print("list_messages succeeded with results_per_page is 1, and start time is yesterday UTC") async for chat_message_page in chat_messages.by_page(): l = [ i async for i in chat_message_page] print("page size: ", len(l))
async def test_send_message_w_type(): thread_id = "19:[email protected]" message_id='1596823919339' raised = False message_str = "Hi I am Bob." create_message_result_id = None chat_message_types = [ChatMessageType.TEXT, ChatMessageType.HTML, "text", "html"] for chat_message_type in chat_message_types: async def mock_send(*_, **__): return mock_response(status_code=201, json_payload={ "id": message_id, "type": chat_message_type, "sequenceId": "3", "version": message_id, "content": { "message": message_str, "topic": "Lunch Chat thread", "participants": [ { "id": "8:acs:8540c0de-899f-5cce-acb5-3ec493af3800_0e59221d-0c1d-46ae-9544-c963ce56c10b", "displayName": "Bob", "shareHistoryTime": "2020-10-30T10:50:50Z" } ], "initiator": "8:acs:8540c0de-899f-5cce-acb5-3ec493af3800_0e59221d-0c1d-46ae-9544-c963ce56c10b" }, "senderDisplayName": "Bob", "createdOn": "2021-01-27T01:37:33Z", "senderId": "8:acs:46849534-eb08-4ab7-bde7-c36928cd1547_00000007-e155-1f06-1db7-3a3a0d00004b" }) chat_thread_client = ChatThreadClient("https://endpoint", credential, thread_id, transport=Mock(send=mock_send)) try: content='hello world' sender_display_name='sender name' create_message_result = await chat_thread_client.send_message( content, chat_message_type=chat_message_type, sender_display_name=sender_display_name) create_message_result_id = create_message_result.id except: raised = True assert raised == False assert create_message_result_id == message_id
async def test_send_typing_notification(): thread_id = "19:[email protected]" raised = False async def mock_send(*_, **__): return mock_response(status_code=200) chat_thread_client = ChatThreadClient("https://endpoint", credential, thread_id, transport=Mock(send=mock_send)) try: await chat_thread_client.send_typing_notification() except: raised = True assert raised == False
async def update_topic_async(self): from azure.communication.chat.aio import ChatThreadClient, CommunicationTokenCredential, \ CommunicationTokenRefreshOptions refresh_options = CommunicationTokenRefreshOptions(self.token) chat_thread_client = ChatThreadClient( self.endpoint, CommunicationTokenCredential(refresh_options), self._thread_id) async with chat_thread_client: # [START update_topic] topic = "updated thread topic" await chat_thread_client.update_topic(topic=topic) # [END update_topic] print("update_topic succeeded")
async def test_delete_message(): thread_id = "19:[email protected]" message_id='1596823919339' raised = False async def mock_send(*_, **__): return mock_response(status_code=204) chat_thread_client = ChatThreadClient("https://endpoint", credential, thread_id, transport=Mock(send=mock_send)) try: await chat_thread_client.delete_message(message_id) except: raised = True assert raised == False
async def test_remove_participant(): thread_id = "19:[email protected]" participant_id="8:acs:57b9bac9-df6c-4d39-a73b-26e944adf6ea_9b0110-08007f1041" raised = False async def mock_send(*_, **__): return mock_response(status_code=204) chat_thread_client = ChatThreadClient("https://endpoint", credential, thread_id, transport=Mock(send=mock_send)) try: await chat_thread_client.remove_participant(user=CommunicationUserIdentifier(participant_id)) except: raised = True assert raised == False