Ejemplo n.º 1
0
    async def test_none_telemetry_client(self):
        my_logger = TelemetryLoggerMiddleware(None, True)

        async def logic(context: TurnContext):
            await context.send_activity(f"echo:{context.activity.text}")

        adapter = TestAdapter(logic)
        adapter.use(my_logger)
        test_flow = TestFlow(None, adapter)
        test_flow = await test_flow.send("foo")
        test_flow = await test_flow.assert_reply("echo:foo")
        test_flow = await test_flow.send("bar")
        await test_flow.assert_reply("echo:bar")
Ejemplo n.º 2
0
    async def test_do_not_throw_on_null_from(self):
        telemetry = Mock()
        my_logger = TelemetryLoggerMiddleware(telemetry, False)

        adapter = TestAdapter(template_or_conversation=Activity(
            channel_id="test",
            recipient=ChannelAccount(id="bot", name="Bot"),
            conversation=ConversationAccount(id=str(uuid.uuid4())),
        ))
        adapter.use(my_logger)

        async def send_proactive(context: TurnContext):
            await context.send_activity("proactive")

        async def logic(context: TurnContext):
            await adapter.create_conversation(
                context.activity.channel_id,
                send_proactive,
            )

        adapter.logic = logic

        test_flow = TestFlow(None, adapter)
        await test_flow.send("foo")
        await test_flow.assert_reply("proactive")

        telemetry_calls = [
            (
                TelemetryLoggerConstants.BOT_MSG_RECEIVE_EVENT,
                {
                    "fromId": None,
                    "conversationName": None,
                    "locale": None,
                    "recipientId": "bot",
                    "recipientName": "Bot",
                },
            ),
        ]
        self.assert_telemetry_calls(telemetry, telemetry_calls)
Ejemplo n.º 3
0
    async def test_should_send_receive(self):
        telemetry = Mock()
        my_logger = TelemetryLoggerMiddleware(telemetry, True)

        async def logic(context: TurnContext):
            await context.send_activity(f"echo:{context.activity.text}")

        adapter = TestAdapter(logic)
        adapter.use(my_logger)
        test_flow = TestFlow(None, adapter)
        test_flow = await test_flow.send("foo")
        test_flow = await test_flow.assert_reply("echo:foo")
        test_flow = await test_flow.send("bar")
        await test_flow.assert_reply("echo:bar")

        # assert
        # Note: None values just check for existence of the key, not the explicit
        #     value (generated)
        telemetry_calls = [
            (
                TelemetryLoggerConstants.BOT_MSG_RECEIVE_EVENT,
                {
                    "text": "foo",
                    "fromId": "User1",
                    "conversationName": None,
                    "locale": None,
                    "recipientId": "bot",
                    "recipientName": "Bot",
                },
            ),
            (
                TelemetryLoggerConstants.BOT_MSG_SEND_EVENT,
                {
                    "text": "echo:foo",
                    "replyActivityId": None,
                    "recipientId": None,
                    "conversationName": None,
                    "locale": None,
                },
            ),
            (
                TelemetryLoggerConstants.BOT_MSG_RECEIVE_EVENT,
                {
                    "text": "bar",
                    "fromId": "User1",
                    "conversationName": None,
                    "locale": None,
                    "recipientId": "bot",
                    "recipientName": "Bot",
                    "fromName": "user",
                },
            ),
            (
                TelemetryLoggerConstants.BOT_MSG_SEND_EVENT,
                {
                    "replyActivityId": None,
                    "recipientId": "User1",
                    "conversationName": None,
                    "locale": None,
                    "fromName": "Bot",
                    "text": "echo:bar",
                },
            ),
        ]
        self.assert_telemetry_calls(telemetry, telemetry_calls)
Ejemplo n.º 4
0
 async def test_create_middleware(self):
     telemetry = NullTelemetryClient()
     my_logger = TelemetryLoggerMiddleware(telemetry, True)
     assert my_logger
Ejemplo n.º 5
0
    async def test_log_update(self):
        telemetry = Mock()
        my_logger = TelemetryLoggerMiddleware(telemetry, True)
        activity_to_update = None

        async def process(context: TurnContext) -> None:
            nonlocal activity_to_update
            if context.activity.text == "update":
                if not activity_to_update:
                    raise Exception("activity to update not set yet!")
                activity_to_update.text = "new response"
                await context.update_activity(activity_to_update)
            else:
                activity = self.create_reply(context.activity, "response")
                response = await context.send_activity(activity)
                activity.id = response.id
                # clone the activity, so we can use it to do an update
                activity_to_update = copy.copy(activity)
                # await context.send_activity(f'echo:{context.activity.text}')

        adapter = TestAdapter(process)
        adapter.use(my_logger)
        test_flow = TestFlow(None, adapter)
        test_flow = await test_flow.send("foo")
        test_flow = await test_flow.assert_reply("response")
        test_flow = await test_flow.send("update")

        # assert
        # Note: None values just check for existence of the key, not the explicit
        #     value (generated)
        telemetry_call_expected = [
            (
                TelemetryLoggerConstants.BOT_MSG_RECEIVE_EVENT,
                {
                    "text": "foo",
                    "fromId": "User1",
                    "conversationName": None,
                    "locale": None,
                    "recipientId": "bot",
                    "recipientName": "Bot",
                },
            ),
            (
                TelemetryLoggerConstants.BOT_MSG_SEND_EVENT,
                {
                    "replyActivityId": "1",
                    "recipientId": "User1",
                    "conversationName": None,
                    "locale": None,
                    "fromName": "Bot",
                    "text": "response",
                },
            ),
            (
                TelemetryLoggerConstants.BOT_MSG_RECEIVE_EVENT,
                {
                    "text": "update",
                    "fromId": "User1",
                    "conversationName": None,
                    "locale": None,
                    "recipientId": "bot",
                    "recipientName": "Bot",
                    "fromName": "user",
                },
            ),
            (
                TelemetryLoggerConstants.BOT_MSG_UPDATE_EVENT,
                {
                    "recipientId": "User1",
                    "conversationId": "Convo1",
                    "conversationName": None,
                    "locale": None,
                    "text": "new response",
                },
            ),
        ]
        self.assert_telemetry_calls(telemetry, telemetry_call_expected)