|
19 | 19 | ConversationReference,
|
20 | 20 | ConversationResourceResponse,
|
21 | 21 | ChannelAccount,
|
| 22 | + DeliveryModes, |
22 | 23 | )
|
23 | 24 | from botframework.connector.aio import ConnectorClient
|
24 | 25 | from botframework.connector.auth import (
|
@@ -58,6 +59,7 @@ def __init__(self, settings=None):
|
58 | 59 | self.fail_operation = False
|
59 | 60 | self.expect_auth_header = ""
|
60 | 61 | self.new_service_url = None
|
| 62 | + self.connector_client_mock = None |
61 | 63 |
|
62 | 64 | def aux_test_authenticate_request(self, request: Activity, auth_header: str):
|
63 | 65 | return super()._authenticate_request(request, auth_header)
|
@@ -102,7 +104,10 @@ def _get_or_create_connector_client(
|
102 | 104 | self.tester.assertIsNotNone(
|
103 | 105 | service_url, "create_connector_client() not passed service_url."
|
104 | 106 | )
|
105 |
| - connector_client_mock = Mock() |
| 107 | + |
| 108 | + if self.connector_client_mock: |
| 109 | + return self.connector_client_mock |
| 110 | + self.connector_client_mock = Mock() |
106 | 111 |
|
107 | 112 | async def mock_reply_to_activity(conversation_id, activity_id, activity):
|
108 | 113 | nonlocal self
|
@@ -160,23 +165,23 @@ async def mock_create_conversation(parameters):
|
160 | 165 | )
|
161 | 166 | return response
|
162 | 167 |
|
163 |
| - connector_client_mock.conversations.reply_to_activity.side_effect = ( |
| 168 | + self.connector_client_mock.conversations.reply_to_activity.side_effect = ( |
164 | 169 | mock_reply_to_activity
|
165 | 170 | )
|
166 |
| - connector_client_mock.conversations.send_to_conversation.side_effect = ( |
| 171 | + self.connector_client_mock.conversations.send_to_conversation.side_effect = ( |
167 | 172 | mock_send_to_conversation
|
168 | 173 | )
|
169 |
| - connector_client_mock.conversations.update_activity.side_effect = ( |
| 174 | + self.connector_client_mock.conversations.update_activity.side_effect = ( |
170 | 175 | mock_update_activity
|
171 | 176 | )
|
172 |
| - connector_client_mock.conversations.delete_activity.side_effect = ( |
| 177 | + self.connector_client_mock.conversations.delete_activity.side_effect = ( |
173 | 178 | mock_delete_activity
|
174 | 179 | )
|
175 |
| - connector_client_mock.conversations.create_conversation.side_effect = ( |
| 180 | + self.connector_client_mock.conversations.create_conversation.side_effect = ( |
176 | 181 | mock_create_conversation
|
177 | 182 | )
|
178 | 183 |
|
179 |
| - return connector_client_mock |
| 184 | + return self.connector_client_mock |
180 | 185 |
|
181 | 186 |
|
182 | 187 | async def process_activity(
|
@@ -572,3 +577,88 @@ async def callback(context: TurnContext):
|
572 | 577 | await adapter.continue_conversation(
|
573 | 578 | refs, callback, claims_identity=skills_identity, audience=skill_2_app_id
|
574 | 579 | )
|
| 580 | + |
| 581 | + async def test_delivery_mode_buffered_replies(self): |
| 582 | + mock_credential_provider = unittest.mock.create_autospec(CredentialProvider) |
| 583 | + |
| 584 | + settings = BotFrameworkAdapterSettings( |
| 585 | + app_id="bot_id", credential_provider=mock_credential_provider |
| 586 | + ) |
| 587 | + adapter = AdapterUnderTest(settings) |
| 588 | + |
| 589 | + async def callback(context: TurnContext): |
| 590 | + await context.send_activity("activity 1") |
| 591 | + await context.send_activity("activity 2") |
| 592 | + await context.send_activity("activity 3") |
| 593 | + |
| 594 | + inbound_activity = Activity( |
| 595 | + type=ActivityTypes.message, |
| 596 | + channel_id="emulator", |
| 597 | + service_url="http://tempuri.org/whatever", |
| 598 | + delivery_mode=DeliveryModes.buffered_replies, |
| 599 | + text="hello world", |
| 600 | + ) |
| 601 | + |
| 602 | + identity = ClaimsIdentity( |
| 603 | + claims={ |
| 604 | + AuthenticationConstants.AUDIENCE_CLAIM: "bot_id", |
| 605 | + AuthenticationConstants.APP_ID_CLAIM: "bot_id", |
| 606 | + AuthenticationConstants.VERSION_CLAIM: "1.0", |
| 607 | + }, |
| 608 | + is_authenticated=True, |
| 609 | + ) |
| 610 | + |
| 611 | + invoke_response = await adapter.process_activity_with_identity( |
| 612 | + inbound_activity, identity, callback |
| 613 | + ) |
| 614 | + assert invoke_response |
| 615 | + assert invoke_response.status == 200 |
| 616 | + activities = invoke_response.body |
| 617 | + assert len(activities) == 3 |
| 618 | + assert activities[0].text == "activity 1" |
| 619 | + assert activities[1].text == "activity 2" |
| 620 | + assert activities[2].text == "activity 3" |
| 621 | + assert ( |
| 622 | + adapter.connector_client_mock.conversations.send_to_conversation.call_count |
| 623 | + == 0 |
| 624 | + ) |
| 625 | + |
| 626 | + async def test_delivery_mode_normal(self): |
| 627 | + mock_credential_provider = unittest.mock.create_autospec(CredentialProvider) |
| 628 | + |
| 629 | + settings = BotFrameworkAdapterSettings( |
| 630 | + app_id="bot_id", credential_provider=mock_credential_provider |
| 631 | + ) |
| 632 | + adapter = AdapterUnderTest(settings) |
| 633 | + |
| 634 | + async def callback(context: TurnContext): |
| 635 | + await context.send_activity("activity 1") |
| 636 | + await context.send_activity("activity 2") |
| 637 | + await context.send_activity("activity 3") |
| 638 | + |
| 639 | + inbound_activity = Activity( |
| 640 | + type=ActivityTypes.message, |
| 641 | + channel_id="emulator", |
| 642 | + service_url="http://tempuri.org/whatever", |
| 643 | + delivery_mode=DeliveryModes.normal, |
| 644 | + text="hello world", |
| 645 | + conversation=ConversationAccount(id="conversationId"), |
| 646 | + ) |
| 647 | + |
| 648 | + identity = ClaimsIdentity( |
| 649 | + claims={ |
| 650 | + AuthenticationConstants.AUDIENCE_CLAIM: "bot_id", |
| 651 | + AuthenticationConstants.APP_ID_CLAIM: "bot_id", |
| 652 | + AuthenticationConstants.VERSION_CLAIM: "1.0", |
| 653 | + }, |
| 654 | + is_authenticated=True, |
| 655 | + ) |
| 656 | + |
| 657 | + invoke_response = await adapter.process_activity_with_identity( |
| 658 | + inbound_activity, identity, callback |
| 659 | + ) |
| 660 | + assert not invoke_response |
| 661 | + assert ( |
| 662 | + adapter.connector_client_mock.conversations.send_to_conversation.call_count |
| 663 | + == 3 |
| 664 | + ) |
0 commit comments