Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
@responses.activate
def test_request_should_handle_401(self):
"""Test that Twython raises an auth error on 401 error"""
endpoint = 'statuses/home_timeline'
url = self.get_url(endpoint)
self.register_response(responses.GET, url, body='{"errors":[{"message":"Error"}]}', status=401)
self.assertRaises(TwythonAuthError, self.api.request, endpoint)
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_get_authentication_tokens_bad_tokens(self):
"""Test getting authentication tokens with bad tokens
raises TwythonAuthError"""
self.assertRaises(TwythonAuthError, self.bad_api.get_authentication_tokens,
callback_url='http://google.com/')
@responses.activate
def test_request_should_handle_400_for_missing_auth_data(self):
"""Test that Twython raises an auth error on 400 error when no oauth data sent"""
endpoint = 'statuses/home_timeline'
url = self.get_url(endpoint)
self.register_response(responses.GET, url,
body='{"errors":[{"message":"Bad Authentication data"}]}', status=400)
self.assertRaises(TwythonAuthError, self.api.request, endpoint)
def test_get_protected_user_timeline_not_following(self):
"""Test returning a protected user timeline who you are not following
fails and raise a TwythonAuthError"""
self.assertRaises(TwythonAuthError, self.api.get_user_timeline,
screen_name=protected_twitter_2)
def test_get_protected_user_timeline_not_following(self):
"""Test returning a protected user timeline who you are not following
fails and raise a TwythonAuthError"""
self.assertRaises(TwythonAuthError, self.api.get_user_timeline,
screen_name=protected_twitter_2)
def authenticate(APP_KEY, APP_SECRET):
while True:
write_log('Authenticating to Twitter...\n')
try:
twitter = Twython(APP_KEY, APP_SECRET, oauth_version=2)
ACCESS_TOKEN = twitter.obtain_access_token()
ret = Twython(APP_KEY, access_token=ACCESS_TOKEN)
write_log('Authentication successful\n')
return ret
except TwythonAuthError, e:
traceback.print_exc()
sleep(WAIT_BETWEEN_AUTH)
def get_screen_name(log):
global MYSELF
if not MYSELF or MYSELF == "Unknown":
log.debug(" Getting current user screen name")
check_twitter_config()
logging.captureWarnings(True)
old_level = log.getEffectiveLevel()
log.setLevel(logging.ERROR)
twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
try:
details = twitter.verify_credentials()
except TwythonAuthError as e:
log.setLevel(old_level)
log.exception(" Problem trying to get screen name")
twitter_auth_issue(e)
raise
except:
log.exception(" Problem trying to get screen name")
details = None
log.setLevel(old_level)
name = "Unknown"
if details:
name = details["screen_name"]
MYSELF = name
return MYSELF
elif twitter_obj.screen:
count = 200
search['count'] = count
search['screen_name'] = twitter_obj.screen
# call the user timeline and get his tweet
try:
if twitter_obj.fav:
count = 20
search['count'] = 20
# get the favorites https://dev.twitter.com/rest/
# reference/get/favorites/list
statuses = self.twitter_api.get_favorites(**search)
else:
statuses = self.twitter_api.get_user_timeline(**search)
except TwythonAuthError as e:
logger.error(e.msg, e.error_code)
update_result(trigger_id, msg=e.msg, status=False)
return count, search, statuses
if len(content) > 140:
return HttpResponseBadRequest(_('Message is too long'))
try:
credentials = request.twitter.api.verify_credentials()
username = credentials['screen_name']
banned = (TwitterAccount.objects
.filter(username=username, banned=True)
.first())
if banned:
return render(request, 'customercare/tweets.html',
{'tweets': []})
result = request.twitter.api.update_status(
status=content,
in_reply_to_status_id=reply_to_id)
except (TwythonError, TwythonAuthError), e:
# L10n: {message} is an error coming from our twitter api library
return HttpResponseBadRequest(
_('An error occured: {message}').format(message=e))
# Store reply in database.
# If tweepy's status models actually implemented a dictionary, it would
# be too boring.
status = result
author = result['user']
created_at = datetime.strptime(status['created_at'],
'%a %b %d %H:%M:%S +0000 %Y')
# Raw JSON blob data
# Note: The JSON for the tweet posted is different than what we get from
# the search API. This makes it similar with the fields that we use.