Exemple #1
0
 async def _get_spiral_example(self,
                               turn_context: TurnContext) -> Attachment:
     """
     Creates an "Attachment" to be sent from the bot to the user at welcome message.
     :param turn_context:
     :return: Attachment
     """
     with open(os.path.join(os.getcwd(), "img/sparch.png"),
               "rb") as in_file:
         image_data = in_file.read()
     connector = await turn_context.adapter.create_connector_client(
         turn_context.activity.service_url)
     conversation_id = turn_context.activity.conversation.id
     response = await connector.conversations.upload_attachment(
         conversation_id,
         AttachmentData(
             original_base64=image_data,
             type="image/png",
         ),
     )
     base_uri: str = connector.config.base_url
     attachment_uri = (base_uri + ("" if base_uri.endswith("/") else "/") +
                       f"v3/attachments/{response.id}/views/original")
     return Attachment(
         content_type="image/png",
         content_url=attachment_uri,
     )
    async def test_on_upload_attachment(self):
        self._conversation_id = ""

        mock_adapter = Mock()

        sut = self.create_skill_handler_for_testing(mock_adapter)
        attachment_data = AttachmentData()

        with self.assertRaises(BotActionNotImplementedError):
            await sut.test_on_upload_attachment(self._claims_identity,
                                                self._conversation_id,
                                                attachment_data)
    def test_attachments_get_attachment_view(self):
        original = read_base64("bot.png")
        attachment = AttachmentData(
            type="image/png",
            name="Bot.png",
            original_base64=original,
            thumbnail_base64=read_base64("bot_icon.png"),
        )

        connector = ConnectorClient(self.credentials, base_url=SERVICE_URL)
        response = connector.conversations.upload_attachment(
            CONVERSATION_ID, attachment)
        attachment_id = response.id
        attachment_stream = connector.attachments.get_attachment(
            attachment_id, "original")

        assert len(original) == sum(len(_) for _ in attachment_stream)
    def test_attachments_get_attachment_view_with_invalid_view_id_fails(self):
        original = read_base64("bot.png")
        attachment = AttachmentData(
            type="image/png",
            name="Bot.png",
            original_base64=original,
            thumbnail_base64=read_base64("bot_icon.png"),
        )

        with pytest.raises(msrest.exceptions.HttpOperationError) as excinfo:
            connector = ConnectorClient(self.credentials, base_url=SERVICE_URL)
            response = connector.conversations.upload_attachment(
                CONVERSATION_ID, attachment)
            attachment_id = response.id
            connector.attachments.get_attachment(attachment_id, "invalid")

        assert "not found" in str(excinfo.value)
    def test_attachments_upload_and_get_attachment(self):
        attachment = AttachmentData(
            type="image/png",
            name="Bot.png",
            original_base64=read_base64("bot.png"),
            thumbnail_base64=read_base64("bot_icon.png"),
        )

        connector = ConnectorClient(self.credentials, base_url=SERVICE_URL)
        response = connector.conversations.upload_attachment(
            CONVERSATION_ID, attachment)
        attachment_id = response.id
        attachment_info = connector.attachments.get_attachment_info(
            attachment_id)

        assert attachment_info is not None
        assert attachment_info.name == "Bot.png"
        assert attachment_info.type == "image/png"
        assert len(attachment_info.views) == 2