Esempio n. 1
0
    def test_given_url_and_text_when_publish_with_media_then_file_downloaded_and_tweet_published(
            self, temp_file_mock):
        with patch.object(Twitter, 'client'):
            twitter = Twitter()
            twitter.client = MagicMock()
            twitter._download_file = MagicMock()
            url = "http://example.com/file.jpg"
            text = "this is an example"
            in_response_to = MagicMock()

            tweet_id = twitter.publish_tweet_with_media(
                text, url, in_response_to)

            self.assertEqual(twitter.client.update_with_media.return_value.id,
                             tweet_id)
            temp_file_mock.assert_called_once_with(suffix=".png")

            with temp_file_mock.return_value as temp_file:
                twitter._download_file.assert_called_once_with(url, temp_file)
                twitter.client.update_with_media.assert_called_once_with(
                    temp_file.name, text, in_reply_to_status_id=in_response_to)
Esempio n. 2
0
    def test_given_exception_downloading_file_when_publish_with_media_then_tweet_without_media_published(
            self, temp_file_mock):
        with patch.object(Twitter, 'client'):
            twitter = Twitter()
            twitter.client = MagicMock()
            twitter._download_file = MagicMock(
                side_effect=MediaNotAccessibleError())
            twitter.publish_tweet = MagicMock()
            url = "http://example.com/file.jpg"
            text = "this is an example"
            in_response_to = MagicMock()

            tweet_id = twitter.publish_tweet_with_media(
                text, url, in_response_to)

            self.assertEqual(twitter.publish_tweet.return_value, tweet_id)
            temp_file_mock.assert_called_once_with(suffix=".png")

            with temp_file_mock.return_value as temp_file:
                twitter._download_file.assert_called_once_with(url, temp_file)
                twitter.client.update_with_media.assert_not_called()
                twitter.publish_tweet.assert_called_once_with(
                    text, in_response_to)