async def test_create_chat_thread(): thread_id = "19:[email protected]" async def mock_send(*_, **__): return mock_response( status_code=201, json_payload={ "chatThread": { "id": thread_id, "topic": "test topic", "createdOn": "2020-12-03T21:09:17Z", "createdBy": "8:acs:57b9bac9-df6c-4d39-a73b-26e944adf6ea_9b0110-08007f1041" } }) chat_client = ChatClient("https://endpoint", credential, transport=Mock(send=mock_send)) topic = "test topic" user = CommunicationUserIdentifier( "8:acs:57b9bac9-df6c-4d39-a73b-26e944adf6ea_9b0110-08007f1041") participants = [ ChatParticipant(identifier=user, display_name='name', share_history_time=datetime.utcnow()) ] create_chat_thread_result = await chat_client.create_chat_thread( topic, thread_participants=participants) assert create_chat_thread_result.chat_thread.id == thread_id
def test_serialize_communication_user(self): communication_identifier_model = CommunicationUserIdentifierSerializer.serialize( CommunicationUserIdentifier("an id") ) assert communication_identifier_model.kind is CommunicationIdentifierKind.CommunicationUser assert communication_identifier_model.id is "an id"
def test_add_participants(self): thread_id = "19:[email protected]" new_participant_id = "8:acs:57b9bac9-df6c-4d39-a73b-26e944adf6ea_9b0110-08007f1041" raised = False def mock_send(*_, **__): return mock_response(status_code=201) chat_thread_client = ChatThreadClient("https://endpoint", TestChatThreadClient.credential, thread_id, transport=Mock(send=mock_send)) new_participant = ChatThreadParticipant( user=CommunicationUserIdentifier(new_participant_id), display_name='name', share_history_time=datetime.utcnow()) participants = [new_participant] try: result = chat_thread_client.add_participants(participants) except: raised = True self.assertFalse(raised, 'Expected is no excpetion raised') self.assertTrue(len(result) == 0)
async def test_create_chat_thread_raises_error(): async def mock_send(*_, **__): return mock_response(status_code=400, json_payload={"msg": "some error"}) chat_client = ChatClient("https://endpoint", credential, transport=Mock(send=mock_send)) topic = "test topic", user = CommunicationUserIdentifier( "8:acs:57b9bac9-df6c-4d39-a73b-26e944adf6ea_9b0110-08007f1041") participants = [ ChatParticipant(identifier=user, display_name='name', share_history_time=datetime.utcnow()) ] raised = False try: await chat_client.create_chat_thread(topic=topic, thread_participants=participants) except: raised = True assert raised == True
def test_add_participant_w_failed_participants(self): thread_id = "19:[email protected]" new_participant_id = "8:acs:57b9bac9-df6c-4d39-a73b-26e944adf6ea_9b0110-08007f1041" raised = False error_message = "some error message" 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", TestChatThreadClient.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: chat_thread_client.add_participant(new_participant) except: raised = True self.assertTrue(raised, 'Expected is no excpetion raised')
def test_deserialize_communication_user(self): communication_identifier_actual = CommunicationUserIdentifierSerializer.deserialize( CommunicationIdentifierModel( raw_id="an id", communication_user=self.testUserModel)) communication_identifier_expected = CommunicationUserIdentifier( identifier="an id") assert isinstance(communication_identifier_actual, CommunicationUserIdentifier) assert communication_identifier_actual.identifier == communication_identifier_expected.identifier
def test_deserialize_communication_user(self): communication_identifier_actual = CommunicationUserIdentifierSerializer.deserialize( CommunicationIdentifierModel( kind=CommunicationIdentifierKind.CommunicationUser, id="an id")) communication_identifier_expected = CommunicationUserIdentifier( "an id") assert isinstance(communication_identifier_actual, CommunicationUserIdentifier) assert communication_identifier_actual == communication_identifier_actual
def test_deserialize_communication_user(self): communication_identifier_actual = deserialize_identifier( CommunicationIdentifierModel( raw_id="an id", communication_user=self.testUserModel)) communication_identifier_expected = CommunicationUserIdentifier( "an id") assert isinstance(communication_identifier_actual, CommunicationUserIdentifier) assert communication_identifier_actual.properties[ 'id'] == communication_identifier_expected.properties['id']
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
def test_add_participants_w_failed_participants_returns_nonempty_list( self): thread_id = "19:[email protected]" new_participant_id = "8:acs:57b9bac9-df6c-4d39-a73b-26e944adf6ea_9b0110-08007f1041" raised = False error_message = "some error message" def mock_send(*_, **__): return mock_response(status_code=201, json_payload={ "invalidParticipants": [{ "code": "string", "message": error_message, "target": new_participant_id, "details": [] }] }) chat_thread_client = ChatThreadClient("https://endpoint", TestChatThreadClient.credential, thread_id, transport=Mock(send=mock_send)) new_participant = ChatParticipant( identifier=CommunicationUserIdentifier(new_participant_id), display_name='name', share_history_time=datetime.utcnow()) participants = [new_participant] try: result = chat_thread_client.add_participants(participants) except: raised = True self.assertFalse(raised, 'Expected is no excpetion raised') self.assertTrue(len(result) == 1) failed_participant = result[0][0] communication_error = result[0][1] self.assertEqual(new_participant.identifier.properties['id'], failed_participant.identifier.properties['id']) self.assertEqual(new_participant.display_name, failed_participant.display_name) self.assertEqual(new_participant.share_history_time, failed_participant.share_history_time) self.assertEqual(error_message, communication_error.message)
def test_create_chat_thread_w_repeatability_request_id(self): thread_id = "19:[email protected]" chat_thread_client = None raised = False idempotency_token = "b66d6031-fdcc-41df-8306-e524c9f226b8" def mock_send(*_, **__): return mock_response( status_code=201, json_payload={ "chatThread": { "id": thread_id, "topic": "test topic", "createdOn": "2020-12-03T21:09:17Z", "createdBy": "8:acs:57b9bac9-df6c-4d39-a73b-26e944adf6ea_9b0110-08007f1041" } }) chat_client = ChatClient("https://endpoint", TestChatClient.credential, transport=Mock(send=mock_send)) topic = "test topic" user = CommunicationUserIdentifier( "8:acs:57b9bac9-df6c-4d39-a73b-26e944adf6ea_9b0110-08007f1041") participants = [ ChatParticipant(identifier=user, display_name='name', share_history_time=datetime.utcnow()) ] try: create_chat_thread_result = chat_client.create_chat_thread( topic=topic, thread_participants=participants, idempotency_token=idempotency_token) except: raised = True raise self.assertFalse(raised, 'Expected is no excpetion raised') assert create_chat_thread_result.chat_thread.id == thread_id
async def test_add_participants_w_failed_participants_returns_nonempty_list(): 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()) participants = [new_participant] try: result = await chat_thread_client.add_participants(participants) except: raised = True assert raised == False assert len(result) == 1 failed_participant = result[0][0] communication_error = result[0][1] assert new_participant.user.identifier == failed_participant.user.identifier assert new_participant.display_name == failed_participant.display_name assert new_participant.share_history_time == failed_participant.share_history_time assert error_message == communication_error.message
async def test_add_participant(): thread_id = "19:[email protected]" new_participant_id="8:acs:57b9bac9-df6c-4d39-a73b-26e944adf6ea_9b0110-08007f1041" raised = False async def mock_send(*_, **__): return mock_response(status_code=201) 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 == False
def test_create_chat_thread_raises_error(self): def mock_send(*_, **__): return mock_response(status_code=400, json_payload={"msg": "some error"}) chat_client = ChatClient("https://endpoint", TestChatClient.credential, transport=Mock(send=mock_send)) topic = "test topic", user = CommunicationUserIdentifier( "8:acs:57b9bac9-df6c-4d39-a73b-26e944adf6ea_9b0110-08007f1041") thread_participants = [ ChatParticipant(identifier=user, display_name='name', share_history_time=datetime.utcnow()) ] self.assertRaises(HttpResponseError, chat_client.create_chat_thread, topic=topic, thread_participants=thread_participants)
def test_serialize_communication_user(self): communication_identifier_model = CommunicationUserIdentifierSerializer.serialize( CommunicationUserIdentifier(identifier="an id")) assert communication_identifier_model.communication_user.id is "an id"
def test_serialize_communication_user(self): communication_identifier_model = serialize_identifier( CommunicationUserIdentifier("an id")) assert communication_identifier_model['communication_user'][ 'id'] is "an id"