Ejemplo n.º 1
0
def test_handle_tweets_success(update_status, create_ticket,
                               tweepy_search_result, expected_parsed_data):
    tweet = Tweet.objects.create(source_id='777',
                                 source_created_at=now(),
                                 user_identifier='fooman')
    Feedback.objects.create(ticket_id='abc123', tweet=tweet)

    with mock.patch('tweepy.API.search',
                    return_value=tweepy_search_result) as search:
        twitter_handler = TwitterHandler()
        twitter_handler.handle_tweets()

        search.assert_called_with(settings.SEARCH_STRING,
                                  rpp=100,
                                  since_id='777')
        create_ticket.assert_called_with(expected_parsed_data)
        update_status.assert_called_with(
            'Kiitos @ViljamiTesti! Seuraa etenemistä osoitteessa: http://test_OPEN311_FEEDBACK_URL?fid=7',
            in_reply_to_status_id='874885713845735424')

        new_feedback = Feedback.objects.latest('id')
        assert new_feedback.ticket_id == '7'

        new_tweet = Tweet.objects.latest('id')
        assert new_tweet.user_identifier == 'ViljamiTesti'
        assert new_feedback.tweet == new_tweet
Ejemplo n.º 2
0
def test_handle_tweets_no_tweets(update_status, create_ticket):
    with mock.patch('tweepy.API.search', return_value=[]):
        twitter_handler = TwitterHandler()
        twitter_handler.handle_tweets()

        update_status.assert_not_called()
        create_ticket.assert_not_called()
        assert not Feedback.objects.count()
        assert not Tweet.objects.count()
Ejemplo n.º 3
0
def test_handle_tweets_ignore_own_tweets(update_status, create_ticket, tweepy_search_result):
    with mock.patch('tweepy.API.search', return_value=tweepy_search_result), \
            mock.patch('tweepy.API.me', return_value=tweepy_search_result[0].user):
        twitter_handler = TwitterHandler()
        twitter_handler.handle_tweets()

        update_status.assert_not_called()
        create_ticket.assert_not_called()
        assert not Feedback.objects.count()
        assert Tweet.objects.count() == 1
Ejemplo n.º 4
0
def test_handle_tweets_create_ticket_failure(update_status, create_ticket, tweepy_search_result, tweepy_user):
    with mock.patch('tweepy.API.search', return_value=tweepy_search_result) as search, \
            mock.patch('tweepy.API.me', return_value=tweepy_user):
        twitter_handler = TwitterHandler()
        twitter_handler.handle_tweets()

        search.assert_called_with(settings.SEARCH_STRING, rpp=100, since_id=None)
        update_status.assert_called_with(
            'Pahoittelut @ViljamiTesti! Palautteen tallennus epäonnistui',
            in_reply_to_status_id='874885713845735424'
        )
        assert not Feedback.objects.count()
        assert Tweet.objects.count() == 1
Ejemplo n.º 5
0
def test_handle_tweets_retweet_ignoring(update_status, create_ticket,
                                        tweepy_search_result, monkeypatch):
    monkeypatch.setattr(tweepy_search_result[0],
                        'retweeted_status', {'foo': 'bar'},
                        raising=False)

    with mock.patch('tweepy.API.search', return_value=tweepy_search_result):
        twitter_handler = TwitterHandler()
        twitter_handler.handle_tweets()

        update_status.assert_not_called()
        create_ticket.assert_not_called()
        assert not Feedback.objects.count()
        assert Tweet.objects.count() == 1
Ejemplo n.º 6
0
def test_handle_tweets_reply_ignoring(
    update_status, create_ticket, tweepy_search_result, tweepy_user, monkeypatch
):
    monkeypatch.setattr(tweepy_search_result[0], 'in_reply_to_status_id', '123345678', raising=False)

    with mock.patch('tweepy.API.search', return_value=tweepy_search_result), \
            mock.patch('tweepy.API.me', return_value=tweepy_user):
        twitter_handler = TwitterHandler()
        twitter_handler.handle_tweets()

        update_status.assert_not_called()
        create_ticket.assert_not_called()
        assert not Feedback.objects.count()
        assert Tweet.objects.count() == 1
Ejemplo n.º 7
0
def test_handle_tweets_rate_limit_exceeded(
    warning, update_status, create_ticket, tweepy_search_result, tweepy_user, settings
):
    settings.TWITTER_USER_RATE_LIMIT_AMOUNT = 1
    settings.TWITTER_USER_RATE_LIMIT_PERIOD = 60*24*365*100  # "forever"

    tweet = Tweet.objects.create(source_id='777', source_created_at=now(), user_identifier='ViljamiTesti')
    Feedback.objects.create(ticket_id='abc123', tweet=tweet)

    with mock.patch('tweepy.API.search', return_value=tweepy_search_result), \
            mock.patch('tweepy.API.me', return_value=tweepy_user):
        twitter_handler = TwitterHandler()
        twitter_handler.handle_tweets()

        warning.assert_called_with(SubstringMatcher('User exceeded feedback post rate limit'))
        update_status.assert_not_called()
        create_ticket.assert_not_called()
        assert Feedback.objects.count() == 1
        assert Tweet.objects.count() == 2