Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_update_twitter_picture_user_lookup_404s(self):
auth_entity = oauth_dropins.twitter.TwitterAuth(
id='id', token_key='key', token_secret='secret',
user_json=json_dumps({'name': 'Bad',
'screen_name': 'bad',
'profile_image_url': 'http://pi.ct/ure',
}))
auth_entity.put()
source = Twitter.new(None, auth_entity=auth_entity).put()
lookup_url = gr_twitter.API_BASE + cron.TWITTER_API_USER_LOOKUP
self.expect_urlopen(lookup_url % 'bad', status=404)
self.mox.ReplayAll()
resp = tasks.application.get_response('/cron/update_twitter_pictures')
self.assertEqual(200, resp.status_int)
self.assertEqual('http://pi.ct/ure', source.get().picture)
user_json=json_dumps({'name': 'Ryan',
'screen_name': screen_name,
'profile_image_url': 'http://pi.ct/ure',
}))
auth_entity.put()
sources.append(Twitter.new(None, auth_entity=auth_entity).put())
user_objs = [{'screen_name': sources[0].id(),
'profile_image_url': 'http://pi.ct/ure',
}, {'screen_name': sources[1].id(),
'profile_image_url_https': 'http://new/pic_normal.jpg',
'profile_image_url': 'http://bad/http',
}]
cron.TWITTER_USERS_PER_LOOKUP = 2
lookup_url = gr_twitter.API_BASE + cron.TWITTER_API_USER_LOOKUP
self.expect_urlopen(lookup_url % 'a,c', json_dumps(user_objs))
self.expect_urlopen(lookup_url % 'b', json_dumps(user_objs))
self.mox.ReplayAll()
resp = cron.application.get_response('/cron/update_twitter_pictures')
self.assertEqual(200, resp.status_int)
self.assertEquals('http://pi.ct/ure', sources[0].get().picture)
self.assertEquals('http://new/pic.jpg', sources[1].get().picture)
def test_load_blocklist_rate_limited(self):
"""If we get rate limited, we should use the partial result."""
api_url = gr_twitter.API_BASE + gr_twitter.API_BLOCK_IDS % '-1'
self.expect_urlopen(api_url, json_dumps({
'ids': ['1', '2'],
'next_cursor_str': '2',
}))
api_url = gr_twitter.API_BASE + gr_twitter.API_BLOCK_IDS % '2'
self.expect_urlopen(api_url, status=429)
self.mox.ReplayAll()
self.tw.load_blocklist()
self.assert_equals(['1', '2'], self.tw.blocked_ids)
def test_update_twitter_picture_user_lookup_404s(self):
auth_entity = oauth_twitter.TwitterAuth(
id='id', token_key='key', token_secret='secret',
user_json=json_dumps({'name': 'Bad',
'screen_name': 'bad',
'profile_image_url': 'http://pi.ct/ure',
}))
auth_entity.put()
source = Twitter.new(None, auth_entity=auth_entity).put()
lookup_url = gr_twitter.API_BASE + cron.TWITTER_API_USER_LOOKUP
self.expect_urlopen(lookup_url % 'bad', status=404)
self.mox.ReplayAll()
resp = cron.application.get_response('/cron/update_twitter_pictures')
self.assertEqual(200, resp.status_int)
self.assertEquals('http://pi.ct/ure', source.get().picture)
def test_load_blocklist_rate_limited(self):
"""If we get rate limited, we should use the partial result."""
api_url = gr_twitter.API_BASE + gr_twitter.API_BLOCK_IDS % '-1'
self.expect_urlopen(api_url, json_dumps({
'ids': ['1', '2'],
'next_cursor_str': '2',
}))
api_url = gr_twitter.API_BASE + gr_twitter.API_BLOCK_IDS % '2'
self.expect_urlopen(api_url, status=429)
self.mox.ReplayAll()
self.tw.load_blocklist()
self.assert_equals(['1', '2'], self.tw.blocked_ids)
user_json=json_dumps({'name': 'Ryan',
'screen_name': screen_name,
'profile_image_url': 'http://pi.ct/ure',
}))
auth_entity.put()
sources.append(Twitter.new(None, auth_entity=auth_entity).put())
user_objs = [{'screen_name': sources[0].id(),
'profile_image_url': 'http://pi.ct/ure',
}, {'screen_name': sources[1].id(),
'profile_image_url_https': 'http://new/pic_normal.jpg',
'profile_image_url': 'http://bad/http',
}]
cron.TWITTER_USERS_PER_LOOKUP = 2
lookup_url = gr_twitter.API_BASE + cron.TWITTER_API_USER_LOOKUP
self.expect_urlopen(lookup_url % 'a,b', json_dumps(user_objs))
self.expect_urlopen(lookup_url % 'c', json_dumps(user_objs))
self.mox.ReplayAll()
resp = tasks.application.get_response('/cron/update_twitter_pictures')
self.assertEqual(200, resp.status_int)
self.assertEqual('http://pi.ct/ure', sources[0].get().picture)
self.assertEqual('http://new/pic.jpg', sources[1].get().picture)
def test_load_blocklist_rate_limited(self):
"""If we get rate limited, we should use the partial result."""
api_url = gr_twitter.API_BASE + gr_twitter.API_BLOCK_IDS % '-1'
self.expect_urlopen(api_url, json_dumps({
'ids': ['1', '2'],
'next_cursor_str': '2',
}))
api_url = gr_twitter.API_BASE + gr_twitter.API_BLOCK_IDS % '2'
self.expect_urlopen(api_url, status=429)
self.mox.ReplayAll()
self.tw.load_blocklist()
self.assert_equals(['1', '2'], self.tw.blocked_ids)
def get(self):
"""Handles an API GET.
Request path is of the form /site/user_id/group_id/app_id/activity_id ,
where each element except site is an optional string object id.
"""
# parse path
args = urllib.parse.unquote(self.request.path).strip('/').split('/')
if not args or len(args) > MAX_PATH_LEN:
raise exc.HTTPNotFound('Expected 1-%d path elements; found %d' %
(MAX_PATH_LEN, len(args)))
# make source instance
site = args.pop(0)
if site == 'twitter':
src = twitter.Twitter(
access_token_key=util.get_required_param(self, 'access_token_key'),
access_token_secret=util.get_required_param(self, 'access_token_secret'))
elif site == 'facebook':
self.abort(400, 'Sorry, Facebook is no longer available in the REST API. Try the library instead!')
elif site == 'flickr':
src = flickr.Flickr(
access_token_key=util.get_required_param(self, 'access_token_key'),
access_token_secret=util.get_required_param(self, 'access_token_secret'))
elif site == 'github':
src = github.GitHub(
access_token=util.get_required_param(self, 'access_token'))
elif site == 'instagram':
if self.request.get('interactive').lower() == 'true':
src = instagram.Instagram(scrape=True)
else:
self.abort(400, 'Sorry, Instagram is not currently available in the REST API. Try https://instagram-atom.appspot.com/ instead!')
def get(self):
sources = []
# if FACEBOOK_ACCESS_TOKEN:
# sources.append(facebook.Facebook(FACEBOOK_ACCESS_TOKEN))
# if INSTAGRAM_ACCESS_TOKEN:
# sources.append(instagram.Instagram(INSTAGRAM_ACCESS_TOKEN))
if TWITTER_ACCESS_TOKEN:
sources.append(twitter.Twitter(TWITTER_ACCESS_TOKEN,
TWITTER_ACCESS_TOKEN_SECRET))
for source in sources:
self.poll(source)
def new(handler, auth_entity=None, **kwargs):
"""Creates and returns a :class:`Twitter` entity.
Args:
handler: the current :class:`webapp2.RequestHandler`
auth_entity: :class:`oauth_dropins.twitter.TwitterAuth`
kwargs: property values
"""
user = json_loads(auth_entity.user_json)
gr_source = gr_twitter.Twitter(*auth_entity.access_token())
actor = gr_source.user_to_actor(user)
return Twitter(id=user['screen_name'],
auth_entity=auth_entity.key,
url=actor.get('url'),
name=actor.get('displayName'),
picture=actor.get('image', {}).get('url'),
**kwargs)