Skip to content

Commit 77cb11f

Browse files
committed
port tests from mox to mock because they were failing and I don't understand mox. now they are working. add mock to the python and debian deps. remove apparently left-over line of code that runs "sign()" at the end of checking a signature
1 parent a77205c commit 77cb11f

File tree

4 files changed

+39
-43
lines changed

4 files changed

+39
-43
lines changed

debian/control

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Maintainer: SimpleGeo Nerds <nerds@simplegeo.com>
55
Uploaders: Chris Lea <chris.lea@gmail.com>, Ian Eure <ian@simplegeo.com>
66
Standards-Version: 3.8.4
77
XS-Python-Version: all
8-
Build-Depends: debhelper (>= 4.1.13), cdbs (>= 0.4.49), python, python-setuptools, python-support, pyflakes, python-coverage (>= 2.85)
8+
Build-Depends: debhelper (>= 4.1.13), cdbs (>= 0.4.49), python, python-setuptools, python-support, pyflakes, python-coverage (>= 2.85), python-mock
99
Homepage: http://github.com/simplegeo/python-oauth2
1010

1111
Package: python-oauth2

oauth2/__init__.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -535,8 +535,7 @@ def __init__(self, consumer, token=None, cache=None, timeout=None,
535535
self.token = token
536536
self.method = SignatureMethod_HMAC_SHA1()
537537

538-
httplib2.Http.__init__(self, cache=cache, timeout=timeout,
539-
proxy_info=proxy_info)
538+
httplib2.Http.__init__(self, cache=cache, timeout=timeout, proxy_info=proxy_info)
540539

541540
def set_signature_method(self, method):
542541
if not isinstance(method, SignatureMethod):
@@ -667,8 +666,6 @@ def _check_signature(self, request, consumer, token):
667666
raise Error('Invalid signature. Expected signature base '
668667
'string: %s' % base)
669668

670-
signature_method.sign(request, consumer, token)
671-
672669
def _check_timestamp(self, timestamp):
673670
"""Verify that timestamp is recentish."""
674671
timestamp = int(timestamp)

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@
3030
keywords="oauth",
3131
zip_safe = True,
3232
test_suite="tests",
33-
tests_require=['coverage', 'mox'])
33+
tests_require=['coverage', 'mock'])

tests/test_oauth.py

+36-37
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import urllib
3131
import urlparse
3232
from types import ListType
33-
import mox
33+
import mock
3434
import httplib2
3535

3636
# Fix for python2.5 compatibility
@@ -796,8 +796,7 @@ def test_invalid_version(self):
796796
server = oauth.Server()
797797
server.add_signature_method(oauth.SignatureMethod_HMAC_SHA1())
798798

799-
self.assertRaises(oauth.Error, server.verify_request, request,
800-
consumer, token)
799+
self.assertRaises(oauth.Error, server.verify_request, request, consumer, token)
801800

802801
def test_invalid_signature_method(self):
803802
url = "http://sp.example.com/"
@@ -885,7 +884,6 @@ class TestClient(unittest.TestCase):
885884
host = 'http://oauth-sandbox.sevengoslings.net'
886885

887886
def setUp(self):
888-
self.mox = mox.Mox()
889887
self.consumer = oauth.Consumer(key=self.consumer_key,
890888
secret=self.consumer_secret)
891889

@@ -896,9 +894,6 @@ def setUp(self):
896894
'blah': 599999
897895
}
898896

899-
def tearDown(self):
900-
self.mox.UnsetStubs()
901-
902897
def _uri(self, type):
903898
uri = self.oauth_uris.get(type)
904899
if uri is None:
@@ -973,8 +968,8 @@ def test_two_legged_get(self):
973968
resp, content = self._two_legged("GET")
974969
self.assertEquals(int(resp['status']), 200)
975970

976-
def test_multipart_post_does_not_alter_body(self):
977-
self.mox.StubOutWithMock(httplib2.Http, 'request')
971+
@mock.patch('httplib2.Http.request')
972+
def test_multipart_post_does_not_alter_body(self, mockHttpRequest):
978973
random_result = random.randint(1,100)
979974

980975
data = {
@@ -985,49 +980,53 @@ def test_multipart_post_does_not_alter_body(self):
985980
client = oauth.Client(self.consumer, None)
986981
uri = self._uri('two_legged')
987982

988-
expected_kwargs = {
989-
'method':'POST',
990-
'body':body,
991-
'redirections':httplib2.DEFAULT_MAX_REDIRECTS,
992-
'connection_type':None,
993-
'headers':mox.IsA(dict),
994-
}
995-
httplib2.Http.request(client, uri, **expected_kwargs).AndReturn(random_result)
983+
def mockrequest(cl, ur, **kw):
984+
self.failUnless(cl is client)
985+
self.failUnless(ur is uri)
986+
self.failUnlessEqual(frozenset(kw.keys()), frozenset(['method', 'body', 'redirections', 'connection_type', 'headers']))
987+
self.failUnlessEqual(kw['body'], body)
988+
self.failUnlessEqual(kw['connection_type'], None)
989+
self.failUnlessEqual(kw['method'], 'POST')
990+
self.failUnlessEqual(kw['redirections'], httplib2.DEFAULT_MAX_REDIRECTS)
991+
self.failUnless(isinstance(kw['headers'], dict))
992+
993+
return random_result
994+
995+
mockHttpRequest.side_effect = mockrequest
996996

997-
self.mox.ReplayAll()
998997
result = client.request(uri, 'POST', headers={'Content-Type':content_type}, body=body)
999998
self.assertEqual(result, random_result)
1000-
self.mox.VerifyAll()
1001999

1002-
def test_url_with_query_string(self):
1003-
self.mox.StubOutWithMock(httplib2.Http, 'request')
1000+
@mock.patch('httplib2.Http.request')
1001+
def test_url_with_query_string(self, mockHttpRequest):
10041002
uri = 'http://example.com/foo/bar/?show=thundercats&character=snarf'
10051003
client = oauth.Client(self.consumer, None)
1006-
expected_kwargs = {
1007-
'method': 'GET',
1008-
'body': None,
1009-
'redirections': httplib2.DEFAULT_MAX_REDIRECTS,
1010-
'connection_type': None,
1011-
'headers': mox.IsA(dict),
1012-
}
1013-
def oauth_verifier(url):
1004+
random_result = random.randint(1,100)
1005+
1006+
def mockrequest(cl, ur, **kw):
1007+
self.failUnless(cl is client)
1008+
self.failUnless(ur is uri)
1009+
self.failUnlessEqual(frozenset(kw.keys()), frozenset(['method', 'body', 'redirections', 'connection_type', 'headers']))
1010+
self.failUnlessEqual(kw['body'], None)
1011+
self.failUnlessEqual(kw['connection_type'], None)
1012+
self.failUnlessEqual(kw['method'], 'GET')
1013+
self.failUnlessEqual(kw['redirections'], httplib2.DEFAULT_MAX_REDIRECTS)
1014+
self.failUnless(isinstance(kw['headers'], dict))
1015+
10141016
req = oauth.Request.from_consumer_and_token(self.consumer, None,
10151017
http_method='GET', http_url=uri, parameters={})
10161018
req.sign_request(oauth.SignatureMethod_HMAC_SHA1(), self.consumer, None)
10171019
expected = parse_qsl(urlparse.urlparse(req.to_url()).query)
10181020
actual = parse_qsl(urlparse.urlparse(url).query)
1019-
if len(expected) != len(actual):
1020-
return False
1021+
self.failUnlessEqual(len(expected), len(actual))
10211022
actual = dict(actual)
10221023
for key, value in expected:
10231024
if key not in ('oauth_signature', 'oauth_nonce', 'oauth_timestamp'):
1024-
if actual[key] != value:
1025-
return False
1026-
return True
1027-
httplib2.Http.request(client, mox.Func(oauth_verifier), **expected_kwargs)
1028-
self.mox.ReplayAll()
1025+
self.failUnlessEqual(actual[key], value)
1026+
1027+
return random_result
1028+
10291029
client.request(uri, 'GET')
1030-
self.mox.VerifyAll()
10311030

10321031
if __name__ == "__main__":
10331032
unittest.main()

0 commit comments

Comments
 (0)