Skip to content

Instantly share code, notes, and snippets.

@splee
Created August 13, 2011 01:01
Show Gist options
  • Save splee/1143348 to your computer and use it in GitHub Desktop.
Save splee/1143348 to your computer and use it in GitHub Desktop.
Odd mock.side_effect issue
# code under test:
class TwitterOAuth(BaseOAuth):
def get_profile(self, access_token):
"""Retrieves the profile of the Twitter user who just authenticated.
Requires the raw access token from the API as Twitter returns the user
ID baked in.
"""
# get data from twitter
profile_url = 'http://api.twitter.com/1/users/show.json?user_id=%s'
profile_url %= (access_token['user_id'],)
try:
tw_profile = json.decode(urllib2.urlopen(profile_url).read())
except urllib2.URLError, e:
raise ExternalServiceError("Could not contact Twitter")
# split the name into first and (if available) last name
name_parts = tw_profile['name'].split(' ')
first_name = name_parts[0]
last_name = ' '.join(name_parts[1:])
return dict(user_id=access_token['user_id'],
display_name='@%s' % tw_profile['screen_name'],
first_name=first_name,
last_name=last_name,
profile_photo=tw_profile['profile_image_url'],
oauth_token=access_token['oauth_token'],
oauth_token_secret=access_token['oauth_token_secret'])
# test class
class TestTwitterOAuth(ApiCase):
def setup(self):
super(TestTwitterOAuth, self).setup()
self.mock_request = MockRequest()
self.provider_name = 'twitter'
self.provider = EndUserLoginProvider.objects.get(provider=self.provider_name)
self.urllib2_patcher = patch('bigdoor.api.oauth.twitter.urllib2')
self.mock_urllib = self.urllib2_patcher.start()
self.json_patcher = patch('bigdoor.api.oauth.twitter.json')
self.mock_json = self.json_patcher.start()
self.profile = dict(screen_name='example',
name='Example User',
profile_image_url='http://example.com/1.png')
self.mock_json.decode.return_value = self.profile
self.access_token = dict(user_id='1234',
oauth_token=uuid4().hex,
oauth_token_secret=uuid4().hex)
self.oauth = oauth.TwitterOAuth(self.mock_request,
self.publisher.app_key,
self.provider_name)
def teardown(self):
self.urllib2_patcher.stop()
self.json_patcher.stop()
def test_get_profile_with_service_error(self):
self.mock_urllib.urlopen.side_effect = urllib2.URLError('something')
try:
self.oauth.get_profile(self.access_token)
assert False, "This should not have succeeded!"
except ExternalServiceError, e:
pass
# test results
======================================================================
ERROR: test_framework.test_oauth.TestTwitterOAuth.test_get_profile_with_service_error
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/splee/envs/publisher/lib/python2.6/site-packages/nose/case.py", line 186, in runTest
self.test(*self.arg)
File "/Users/splee/bigdoor/publisher/bigdoor/tests/test_framework/test_oauth.py", line 200, in test_get_profile_with_service_error
self.oauth.get_profile(self.access_token)
File "/Users/splee/bigdoor/publisher/bigdoor/api/oauth/twitter.py", line 22, in get_profile
tw_profile = json.decode(urllib2.urlopen(profile_url).read())
File "/Users/splee/envs/publisher/lib/python2.6/site-packages/mock.py", line 335, in __call__
raise self.side_effect
URLError: <urlopen error something>
----------------------------------------------------------------------
Ran 12 tests in 0.134s
FAILED (errors=1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment