def test_bridge_latest_tweet_to_mastodon_using_mocks(self): """Test that we can copy a Tweet to a Toot. This one hits that Twitter and Mastodon APIs. """ # GIVEN access to the Twitter and Mastodon APIs twitter = get_twitter() # WHEN writing the latest tweet to mastodon with patch('tweepy.binder.requests.Session.request') as mock_response: with open('tests/data/Twitter_User_Timeline-1519822598460.json' ) as f: json_data = f.read() mock_response.return_value.status_code = 200 mock_response.return_value.text = json_data tweet = twitter.user_timeline(count=1)[0] text = tweet.text mastodon = get_mastodon() with patch('twit2toot.Mastodon.toot') as mock_response: from .data.toots import TOOT_SIMPLE mock_response.return_value = TOOT_SIMPLE response = mastodon.toot(text) # THEN it works and returns the toot id assert isinstance(response['id'], int)
def toot_latest_tweet(): """Get the latest tweet from Twitter and toot it to Mastodon.""" twitter = get_twitter() mastodon = get_mastodon() tweet = twitter.user_timeline(count=1)[0] response = mastodon.toot(tweet.text) click.echo(response)
def sample_tweets(): """Return a list of sample tweets.""" with patch('tweepy.binder.requests.Session.request') as mock_response: with open('tests/data/sample_tweets.json') as f: json_data = f.read() mock_response.return_value.status_code = 200 mock_response.return_value.text = json_data twitter = get_twitter() return twitter.user_timeline()
def test_get_user_timeline(self): """Exploratory test to get started with using tweepy.""" # GIVEN any state # WHEN using the tweepy to get the user timeline from Twitter twitter = get_twitter() response = twitter.user_timeline(count=5) # THEN it's a list of the correct length assert isinstance(response, list) assert len(response) == 5 # AND it's members are tweet objects tweet = response[0] assert isinstance(tweet, Status)
def test_bridge_latest_tweet_to_mastodon(self): """Test that we can copy a Tweet to a Toot. This one hits that Twitter and Mastodon APIs. """ # GIVEN access to the Twitter and Mastodon APIs twitter = get_twitter() mastodon = get_mastodon() # WHEN writing the latest tweet to mastodon tweet = twitter.user_timeline(count=1)[0] text = tweet.text response = mastodon.toot(text) # THEN it works and returns the toot id assert isinstance(response.id, int) # assert text[-20:] in response['content'] mastodon.status_delete(response['id'])
def test_get_user_timeline_while_mocking_the_api_response( self, mock_response): """Test correct usage of tweepy. Mocking the GET request to the Twitter API. """ with open('tests/data/Twitter_User_Timeline-1519822598460.json') as f: json_data = f.read() mock_response.return_value.status_code = 200 mock_response.return_value.text = json_data # GIVEN any state # WHEN using the tweepy to get the user timeline from Twitter twitter = get_twitter() response = twitter.user_timeline() # THEN it's a list assert isinstance(response, list) # AND it's members are tweets assert isinstance(response[0], Status)