diff --git a/tests/test_auth.py b/tests/test_auth.py index ecdd4dc..12f54b2 100644 --- a/tests/test_auth.py +++ b/tests/test_auth.py @@ -11,6 +11,8 @@ class TwythonAuthTestCase(unittest.TestCase): self.bad_api = Twython('BAD_APP_KEY', 'BAD_APP_SECRET') self.oauth2_api = Twython(app_key, app_secret, oauth_version=2) + self.oauth2_bad_api = Twython('BAD_APP_KEY', 'BAD_APP_SECRET', + oauth_version=2) def test_get_authentication_tokens(self): """Test getting authentication tokens works""" @@ -44,6 +46,11 @@ class TwythonAuthTestCase(unittest.TestCase): """Test obtaining an Application Only OAuth 2 access token succeeds""" self.oauth2_api.obtain_access_token() + def test_obtain_access_token_bad_tokens(self): + """Test obtaining an Application Only OAuth 2 access token using bad app tokens fails""" + self.assertRaises(TwythonAuthError, + self.oauth2_bad_api.obtain_access_token) + def test_obtain_access_token_raises_error_when_oauth1(self): """Test when API is set for OAuth 1, obtain_access_token raises a TwythonError""" diff --git a/tests/test_core.py b/tests/test_core.py index 1eff01e..5466160 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -84,10 +84,28 @@ class TwythonAPITestCase(unittest.TestCase): self.api.encode('Twython is awesome!') def test_html_for_tweet(self): - """Test HTML for Twitter returns what we want""" + """Test HTML for Tweet returns what we want""" tweet_text = self.api.html_for_tweet(test_tweet_object) self.assertEqual(test_tweet_html, tweet_text) + def test_html_for_tweet_expanded_url(self): + """Test using expanded url in HTML for Tweet displays full urls""" + tweet_text = self.api.html_for_tweet(test_tweet_object, + use_expanded_url=True) + # Make sure full url is in HTML + self.assertTrue('http://google.com' in tweet_text) + + def test_html_for_tweet_short_url(self): + """Test using expanded url in HTML for Tweet displays full urls""" + tweet_text = self.api.html_for_tweet(test_tweet_object, False) + # Make sure HTML doesn't contain the display OR exapanded url + self.assertTrue(not 'http://google.com' in tweet_text) + self.assertTrue(not 'google.com' in tweet_text) + + def test_raise_error_on_bad_ssl_cert(self): + """Test TwythonError is raised by a RequestException when an actual HTTP happens""" + self.assertRaises(TwythonError, self.api.get, 'https://example.com') + # Timelines def test_get_mentions_timeline(self): """Test returning mentions timeline for authenticated user succeeds""" diff --git a/tests/test_streaming.py b/tests/test_streaming.py index a75d98c..1b8db6c 100644 --- a/tests/test_streaming.py +++ b/tests/test_streaming.py @@ -19,6 +19,16 @@ class TwythonStreamTestCase(unittest.TestCase): self.api = MyStreamer(app_key, app_secret, oauth_token, oauth_token_secret) + client_args = { + 'headers': { + 'User-Agent': '__twython__ Stream Test' + } + } + # Initialize with header for coverage checking for User-Agent + self.api_with_header = MyStreamer(app_key, app_secret, + oauth_token, oauth_token_secret, + client_args=client_args) + def test_stream_status_filter(self): self.api.statuses.filter(track='twitter') diff --git a/twython/api.py b/twython/api.py index c4dd59a..b1706f2 100644 --- a/twython/api.py +++ b/twython/api.py @@ -169,13 +169,13 @@ class Twython(EndpointsMixin, object): if errors and isinstance(errors, list): error_message = errors[0]['message'] else: - error_message = errors + error_message = errors # pragma: no cover self._last_call['api_error'] = error_message ExceptionType = TwythonError if response.status_code == 429: # Twitter API 1.1, always return 429 when rate limit is exceeded - ExceptionType = TwythonRateLimitError + ExceptionType = TwythonRateLimitError # pragma: no cover elif response.status_code == 401 or 'Bad Authentication data' in error_message: # Twitter API 1.1, returns a 401 Unauthorized or # a 400 "Bad Authentication data" for invalid/expired app keys/user tokens @@ -186,7 +186,7 @@ class Twython(EndpointsMixin, object): retry_after=response.headers.get('retry-after')) # if we have a json error here, then it's not an official Twitter API error - if json_error and not response.status_code in (200, 201, 202): + if json_error and not response.status_code in (200, 201, 202): # pragma: no cover raise TwythonError('Response was not valid JSON, unable to decode.') return content @@ -303,7 +303,7 @@ class Twython(EndpointsMixin, object): if not authorized_tokens: raise TwythonError('Unable to decode authorized tokens.') - return authorized_tokens + return authorized_tokens # pragma: no cover def obtain_access_token(self): """Returns an OAuth 2 access token to make OAuth 2 authenticated read-only calls. @@ -387,7 +387,7 @@ class Twython(EndpointsMixin, object): try: if not 'since_id' in params: params['since_id'] = (int(content['statuses'][0]['id_str']) + 1) - except (TypeError, ValueError): + except (TypeError, ValueError): # pragma: no cover raise TwythonError('Unable to generate next page of search results, `page` is not a number.') for tweet in self.search_gen(search_query, **params): diff --git a/twython/endpoints.py b/twython/endpoints.py index 4c34188..21a64d0 100644 --- a/twython/endpoints.py +++ b/twython/endpoints.py @@ -748,7 +748,7 @@ class EndpointsMixin(object): return self.get('trends/closest', params=params) # Spam Reporting - def report_spam(self, **params): + def report_spam(self, **params): # pragma: no cover """Report the specified user as a spam account to Twitter. Docs: https://dev.twitter.com/docs/api/1.1/post/users/report_spam @@ -757,7 +757,7 @@ class EndpointsMixin(object): return self.post('users/report_spam', params=params) # OAuth - def invalidate_token(self, **params): + def invalidate_token(self, **params): # pragma: no cover """Allows a registered application to revoke an issued OAuth 2 Bearer Token by presenting its client credentials. diff --git a/twython/streaming/api.py b/twython/streaming/api.py index c2fcec8..ef8924a 100644 --- a/twython/streaming/api.py +++ b/twython/streaming/api.py @@ -132,13 +132,13 @@ class TwythonStreamer(object): if is_py3: line = line.decode('utf-8') data = json.loads(line) - if self.on_success(data): + if self.on_success(data): # pragma: no cover for message_type in self.handlers: if message_type in data: handler = getattr(self, 'on_' + message_type, None) if handler and callable(handler) and not handler(data.get(message_type)): break - except ValueError: + except ValueError: # pragma: no cover self.on_error(response.status_code, 'Unable to decode response, not valid JSON.') response.close()