Esempio n. 1
0
 def test_fetch_tweets_returns_after_cannot_acquire_lock(
         self, mock_twitter_init):
     twitter_account = TwitterAccount(name="Strassengezwitscher")
     twitter_account.fetch_tweets()
     # ToDo: This is not optimal, but I don't know of any better way to ensure that the body of fetch_tweets was not
     # executed
     self.assertFalse(mock_twitter_init.called)
Esempio n. 2
0
 def test_fetch_tweets_unlocks_if_get_utc_offset_throws_key_error(
         self, logger, unlock_mock):
     twitter_account = TwitterAccount(name="Strassengezwitscher")
     twitter_account.fetch_tweets()
     logger.assert_called_once_with(
         "Got unexpected result while fetching tweets.")
     self.assertEqual(unlock_mock.call_count, 1)
Esempio n. 3
0
 def test_fetch_tweets_unlocks_if_get_utc_offset_throws_unknown_error(
         self, logger, unlock_mock):
     twitter_account = TwitterAccount(name="Strassengezwitscher")
     twitter_account.fetch_tweets()
     logger.assert_called_once_with(
         "Got unexpected exception while fetching tweets: <class 'Exception'> - wow, much error, such bad"
     )
     self.assertEqual(unlock_mock.call_count, 1)
Esempio n. 4
0
    def test_add_account_with_same_name_but_different_capitalization(self):
        TwitterAccount.objects.create(name="Strassengezwitscher",
                                      account_id=1337)
        twitter_account_new = TwitterAccount(name="STRASSENGEZWITSCHER")

        self.assertRaisesMessage(
            ValidationError, 'Twitter account with this name already exists.',
            twitter_account_new.clean)
Esempio n. 5
0
 def test_fetch_tweets_tweet_already_in_db(self):
     twitter_account = TwitterAccount(name="Strassengezwitscher",
                                      account_id=1337)
     twitter_account.last_known_tweet_id = 10
     twitter_account.save()
     twitter_account.fetch_tweets()
     self.assertEqual(twitter_account.tweet_set.count(), 0)
Esempio n. 6
0
 def test_string_representation(self):
     twitter_account = TwitterAccount(name='Peter')
     tweet = Tweet(
         content='Erster Tweet, #cool!',
         created_at=datetime(2012, 12, 21, 19, 9, 0),
         account=twitter_account,
     )
     self.assertEqual(
         str(tweet), 'Peter at 2012-12-21 19:09:00 - Erster Tweet, #cool!')
Esempio n. 7
0
def do_send_tweet(tweet):
    """Called by send_tweets_task to make the API call into Twitter.
    """

    params = {"status": tweet.message}
    url = "http://api.twitter.com/1/statuses/update.json"
    method = "POST"
    twitter_account = TwitterAccount.get_twitter_account()

    if twitter_account is None:
        logging.error(("No twitter account object was created." "Can't send tweets!"))
        return False

    consumer = oauth.Consumer(key=twitter_account.consumer_key, secret=twitter_account.consumer_secret)
    token = oauth.Token(key=twitter_account.access_token, secret=twitter_account.access_secret)

    auth_params = {
        "oauth_version": "1.0",
        "oauth_nonce": oauth.generate_nonce(),
        "oauth_timestamp": int(time.time()),
        "oauth_signature_method": "HMAC-SHA1",
        "oauth_token": token.key,
        "oauth_consumer_key": consumer.key,
    }

    client = oauth.Client(consumer, token)
    req = oauth.Request(method=method, url=url, parameters=params)
    resp, content = client.request(
        url,
        method=method,
        body=urllib.urlencode(params, True).replace("+", "%20"),
        headers=req.to_header(),
        force_auth_header=True,
    )

    if resp["status"][0] == "5":
        return False

    return True
Esempio n. 8
0
 def test_clean_adds_account_id(self):
     twitter_account = TwitterAccount(name="Strassengezwitscher")
     twitter_account.clean()
     self.assertEqual(twitter_account.account_id, 1337)
Esempio n. 9
0
 def test_clean_removes_leading_at(self):
     twitter_account = TwitterAccount(name="@Strassengezwitscher")
     twitter_account.clean()
     self.assertEqual(twitter_account.name, "Strassengezwitscher")
Esempio n. 10
0
 def test_clean_raises_validation_error_on_user_not_found(self, _):
     twitter_account = TwitterAccount(name="Strassengezwitscher")
     self.assertRaisesMessage(ValidationError,
                              'Could not find user with provided name.',
                              twitter_account.clean)
Esempio n. 11
0
 def test_string_representation(self):
     twitter_account = TwitterAccount(name="Strassengezwitscher")
     self.assertEqual(str(twitter_account), 'Strassengezwitscher')
Esempio n. 12
0
 def test_get_utc_offset_to_return_correctly(self):
     twitter_account = TwitterAccount(name="Strassengezwitscher")
     self.assertEqual(twitter_account._get_utc_offset(None), 3600)
Esempio n. 13
0
 def test_get_utc_offset_to_return_nothing_on_no_tweets(self):
     twitter_account = TwitterAccount(name="Strassengezwitscher")
     self.assertEqual(twitter_account._get_utc_offset(None), None)
Esempio n. 14
0
 def test_fetch_tweets_unlocks_if_get_utc_offset_throws_connection_error(
         self, logger, unlock_mock):
     twitter_account = TwitterAccount(name="Strassengezwitscher")
     twitter_account.fetch_tweets()
     logger.assert_called_once_with("Could not connect to Twitter.")
     self.assertEqual(unlock_mock.call_count, 1)
Esempio n. 15
0
 def test_fetch_tweets_unlocks_if_success(self, unlock_mock):
     twitter_account = TwitterAccount(name="Strassengezwitscher")
     twitter_account.fetch_tweets()
     self.assertEqual(unlock_mock.call_count, 1)
Esempio n. 16
0
 def test_representation(self):
     twitter_account = TwitterAccount(name="Strassengezwitscher")
     self.assertEqual(repr(twitter_account),
                      '<TwitterAccount Strassengezwitscher>')
Esempio n. 17
0
 def test_fetch_tweets_from_api_max_since_id_set(self, request_mock):
     twitter_account = TwitterAccount(name="Strassengezwitscher")
     twitter = TwitterAPI('a', 'b', auth_type='oAuth2')
     twitter_account._fetch_tweets_from_api(twitter, max_id=99, since_id=17)
     self.assertEqual('since_id' in request_mock.call_args[0][1], True)
     self.assertEqual('max_id' in request_mock.call_args[0][1], True)
Esempio n. 18
0
 def test_get_absolute_url(self):
     twitter_account = TwitterAccount(name='PeterTheUnique',
                                      account_id=1337)
     twitter_account.save()
     self.assertEqual(twitter_account.get_absolute_url(),
                      '/intern/twitter_accounts/1/')
Esempio n. 19
0
 def test_clean_raises_validation_error_on_twitter_connection_error(self):
     twitter_account = TwitterAccount(name="Strassengezwitscher")
     self.assertRaisesMessage(
         ValidationError,
         'Could not connect to Twitter to retrieve user_id.',
         twitter_account.clean)