Skip to content
This repository has been archived by the owner on Jan 18, 2025. It is now read-only.

Commit

Permalink
Remove all uses of MagicMock
Browse files Browse the repository at this point in the history
MagicMock is a version of Mock with extra features for
tests that need to test usage of magic methods (i.e.
__nonzero__, __cmp__, etc.).

None of the oauth2client tests actually need these
extra features though.
  • Loading branch information
dhermes committed Aug 10, 2016
1 parent f774a66 commit 9ba4ea5
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 62 deletions.
12 changes: 6 additions & 6 deletions tests/contrib/django_util/test_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def test_view(request):
@mock.patch('oauth2client.client.OAuth2Credentials')
def test_has_credentials_in_storage(self, OAuth2Credentials):
request = self.factory.get('/test')
request.session = mock.MagicMock()
request.session = mock.Mock()

credentials_mock = mock.Mock(
scopes=set(django.conf.settings.GOOGLE_OAUTH2_SCOPES))
Expand All @@ -88,7 +88,7 @@ def test_view(request):
@mock.patch('oauth2client.contrib.dictionary_storage.DictionaryStorage')
def test_specified_scopes(self, dictionary_storage_mock):
request = self.factory.get('/test')
request.session = mock.MagicMock()
request.session = mock.Mock()

credentials_mock = mock.Mock(
scopes=set(django.conf.settings.GOOGLE_OAUTH2_SCOPES))
Expand Down Expand Up @@ -141,13 +141,13 @@ def test_view(request):
@mock.patch('oauth2client.contrib.django_util.UserOAuth2', autospec=True)
def test_has_credentials_in_storage(self, UserOAuth2):
request = self.factory.get('/test')
request.session = mock.MagicMock()
request.session = mock.Mock()

@decorators.oauth_required
def test_view(request):
return http.HttpResponse("test")

my_user_oauth = mock.MagicMock()
my_user_oauth = mock.Mock()

UserOAuth2.return_value = my_user_oauth
my_user_oauth.has_credentials.return_value = True
Expand All @@ -161,7 +161,7 @@ def test_has_credentials_in_storage_no_scopes(
self, OAuth2Credentials):
request = self.factory.get('/test')

request.session = mock.MagicMock()
request.session = mock.Mock()
credentials_mock = mock.Mock(
scopes=set(django.conf.settings.GOOGLE_OAUTH2_SCOPES))
credentials_mock.has_scopes.return_value = False
Expand All @@ -179,7 +179,7 @@ def test_view(request):
@mock.patch('oauth2client.client.OAuth2Credentials')
def test_specified_scopes(self, OAuth2Credentials):
request = self.factory.get('/test')
request.session = mock.MagicMock()
request.session = mock.Mock()

credentials_mock = mock.Mock(
scopes=set(django.conf.settings.GOOGLE_OAUTH2_SCOPES))
Expand Down
14 changes: 7 additions & 7 deletions tests/contrib/test_devshell.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,25 +75,25 @@ class Test_SendRecv(unittest2.TestCase):

def test_port_zero(self):
with mock.patch('oauth2client.contrib.devshell.os') as os_mod:
os_mod.getenv = mock.MagicMock(name='getenv', return_value=0)
os_mod.getenv = mock.Mock(name='getenv', return_value=0)
with self.assertRaises(devshell.NoDevshellServer):
devshell._SendRecv()
os_mod.getenv.assert_called_once_with(devshell.DEVSHELL_ENV, 0)

def test_no_newline_in_received_header(self):
non_zero_port = 1
sock = mock.MagicMock()
sock = mock.Mock()

header_without_newline = ''
sock.recv(6).decode = mock.MagicMock(
sock.recv(6).decode = mock.Mock(
name='decode', return_value=header_without_newline)

with mock.patch('oauth2client.contrib.devshell.os') as os_mod:
os_mod.getenv = mock.MagicMock(name='getenv',
return_value=non_zero_port)
os_mod.getenv = mock.Mock(name='getenv',
return_value=non_zero_port)
with mock.patch('oauth2client.contrib.devshell.socket') as socket:
socket.socket = mock.MagicMock(name='socket',
return_value=sock)
socket.socket = mock.Mock(name='socket',
return_value=sock)
with self.assertRaises(devshell.CommunicationError):
devshell._SendRecv()
os_mod.getenv.assert_called_once_with(devshell.DEVSHELL_ENV, 0)
Expand Down
10 changes: 5 additions & 5 deletions tests/contrib/test_keyring_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ def test_locked_get(self):
service_name = 'my_unit_test'
user_name = 'me'
mock_content = (object(), 'mock_content')
mock_return_creds = mock.MagicMock()
mock_return_creds.set_store = set_store = mock.MagicMock(
mock_return_creds = mock.Mock()
mock_return_creds.set_store = set_store = mock.Mock(
name='set_store')
with mock.patch.object(keyring, 'get_password',
return_value=mock_content,
autospec=True) as get_password:
class_name = 'oauth2client.client.Credentials'
with mock.patch(class_name) as MockCreds:
MockCreds.new_from_json = new_from_json = mock.MagicMock(
MockCreds.new_from_json = new_from_json = mock.Mock(
name='new_from_json', return_value=mock_return_creds)
store = keyring_storage.Storage(service_name, user_name)
credentials = store.locked_get()
Expand All @@ -82,9 +82,9 @@ def test_locked_put(self):
with mock.patch.object(keyring, 'set_password',
return_value=None,
autospec=True) as set_password:
credentials = mock.MagicMock()
credentials = mock.Mock()
to_json_ret = object()
credentials.to_json = to_json = mock.MagicMock(
credentials.to_json = to_json = mock.Mock(
name='to_json', return_value=to_json_ret)
store.locked_put(credentials)
to_json.assert_called_once_with()
Expand Down
16 changes: 8 additions & 8 deletions tests/contrib/test_xsrfutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ def test_bad_positional(self):

def test_it(self):
digest = b'foobar'
digester = mock.MagicMock()
digester.digest = mock.MagicMock(name='digest', return_value=digest)
digester = mock.Mock()
digester.digest = mock.Mock(name='digest', return_value=digest)
with mock.patch('oauth2client.contrib.xsrfutil.hmac') as hmac:
hmac.new = mock.MagicMock(name='new', return_value=digester)
hmac.new = mock.Mock(name='new', return_value=digester)
token = xsrfutil.generate_token(TEST_KEY,
TEST_USER_ID_1,
action_id=TEST_ACTION_ID_1,
Expand All @@ -78,13 +78,13 @@ def test_it(self):
def test_with_system_time(self):
digest = b'foobar'
curr_time = 1440449755.74
digester = mock.MagicMock()
digester.digest = mock.MagicMock(name='digest', return_value=digest)
digester = mock.Mock()
digester.digest = mock.Mock(name='digest', return_value=digest)
with mock.patch('oauth2client.contrib.xsrfutil.hmac') as hmac:
hmac.new = mock.MagicMock(name='new', return_value=digester)
hmac.new = mock.Mock(name='new', return_value=digester)

with mock.patch('oauth2client.contrib.xsrfutil.time') as time:
time.time = mock.MagicMock(name='time', return_value=curr_time)
time.time = mock.Mock(name='time', return_value=curr_time)
# when= is omitted
token = xsrfutil.generate_token(TEST_KEY,
TEST_USER_ID_1,
Expand Down Expand Up @@ -142,7 +142,7 @@ def test_token_too_old_implicit_current_time(self):
key = user_id = None
token = base64.b64encode(_helpers._to_bytes(str(token_time)))
with mock.patch('oauth2client.contrib.xsrfutil.time') as time:
time.time = mock.MagicMock(name='time', return_value=curr_time)
time.time = mock.Mock(name='time', return_value=curr_time)
self.assertFalse(xsrfutil.validate_token(key, token, user_id))
time.time.assert_called_once_with()

Expand Down
18 changes: 9 additions & 9 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,26 +362,26 @@ def test_environment_caching(self):

def _environment_check_gce_helper(self, status_ok=True, socket_error=False,
server_software=''):
response = mock.MagicMock()
response = mock.Mock()
if status_ok:
response.status = http_client.OK
response.getheader = mock.MagicMock(
response.getheader = mock.Mock(
name='getheader',
return_value=client._DESIRED_METADATA_FLAVOR)
else:
response.status = http_client.NOT_FOUND

connection = mock.MagicMock()
connection.getresponse = mock.MagicMock(name='getresponse',
return_value=response)
connection = mock.Mock()
connection.getresponse = mock.Mock(name='getresponse',
return_value=response)
if socket_error:
connection.getresponse.side_effect = socket.error()

with mock.patch('oauth2client.client.os') as os_module:
os_module.environ = {client._SERVER_SOFTWARE: server_software}
with mock.patch('oauth2client.client.six') as six_module:
http_client_module = six_module.moves.http_client
http_client_module.HTTPConnection = mock.MagicMock(
http_client_module.HTTPConnection = mock.Mock(
name='HTTPConnection', return_value=connection)

if server_software == '':
Expand Down Expand Up @@ -1265,7 +1265,7 @@ def test__do_refresh_request_failure_w_json_error_and_store(self):
response = http_mock.ResponseMock({'status': http_client.BAD_GATEWAY})
error_msg = 'Where are we going wearer?'
content = json.dumps({'error': error_msg})
store = mock.MagicMock()
store = mock.Mock()
self._do_refresh_request_test_helper(response, content, error_msg,
store=store)

Expand Down Expand Up @@ -1323,7 +1323,7 @@ def test__do_revoke_success(self):

def test__do_revoke_success_with_store(self):
response = http_mock.ResponseMock()
store = mock.MagicMock()
store = mock.Mock()
self._do_revoke_test_helper(response, b'', None, store=store)

def test__do_revoke_non_json_failure(self):
Expand All @@ -1349,7 +1349,7 @@ def test__do_revoke_failure_w_json_error_and_store(self):
response = http_mock.ResponseMock({'status': http_client.BAD_GATEWAY})
error_msg = 'Where are we going wearer?'
content = json.dumps({'error': error_msg})
store = mock.MagicMock()
store = mock.Mock()
self._do_revoke_test_helper(response, content, error_msg,
store=store)

Expand Down
48 changes: 24 additions & 24 deletions tests/test_crypt.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@ def test_success_single_cert(self):
message = object()
signature = object()

verifier = mock.MagicMock()
verifier.verify = mock.MagicMock(name='verify', return_value=True)
verifier = mock.Mock()
verifier.verify = mock.Mock(name='verify', return_value=True)
with mock.patch('oauth2client.crypt.Verifier') as Verifier:
Verifier.from_string = mock.MagicMock(name='from_string',
return_value=verifier)
Verifier.from_string = mock.Mock(name='from_string',
return_value=verifier)
result = crypt._verify_signature(message, signature, certs)
self.assertEqual(result, None)

Expand All @@ -101,14 +101,14 @@ def test_success_multiple_certs(self):
message = object()
signature = object()

verifier = mock.MagicMock()
verifier = mock.Mock()
# Use side_effect to force all 3 cert values to be used by failing
# to verify on the first two.
verifier.verify = mock.MagicMock(name='verify',
side_effect=[False, False, True])
verifier.verify = mock.Mock(name='verify',
side_effect=[False, False, True])
with mock.patch('oauth2client.crypt.Verifier') as Verifier:
Verifier.from_string = mock.MagicMock(name='from_string',
return_value=verifier)
Verifier.from_string = mock.Mock(name='from_string',
return_value=verifier)
result = crypt._verify_signature(message, signature, certs)
self.assertEqual(result, None)

Expand All @@ -130,11 +130,11 @@ def test_failure(self):
message = object()
signature = object()

verifier = mock.MagicMock()
verifier.verify = mock.MagicMock(name='verify', return_value=False)
verifier = mock.Mock()
verifier.verify = mock.Mock(name='verify', return_value=False)
with mock.patch('oauth2client.crypt.Verifier') as Verifier:
Verifier.from_string = mock.MagicMock(name='from_string',
return_value=verifier)
Verifier.from_string = mock.Mock(name='from_string',
return_value=verifier)
with self.assertRaises(crypt.AppIdentityError):
crypt._verify_signature(message, signature, certs)

Expand Down Expand Up @@ -204,8 +204,8 @@ def test_with_bad_token_lifetime(self):
'exp': current_time + crypt.MAX_TOKEN_LIFETIME_SECS + 1,
}
with mock.patch('oauth2client.crypt.time') as time:
time.time = mock.MagicMock(name='time',
return_value=current_time)
time.time = mock.Mock(name='time',
return_value=current_time)

exception_caught = self._exception_helper(payload_dict)
self.assertNotEqual(exception_caught, None)
Expand All @@ -219,8 +219,8 @@ def test_with_issued_at_in_future(self):
'exp': current_time + crypt.MAX_TOKEN_LIFETIME_SECS - 1,
}
with mock.patch('oauth2client.crypt.time') as time:
time.time = mock.MagicMock(name='time',
return_value=current_time)
time.time = mock.Mock(name='time',
return_value=current_time)

exception_caught = self._exception_helper(payload_dict)
self.assertNotEqual(exception_caught, None)
Expand All @@ -234,8 +234,8 @@ def test_with_expiration_in_the_past(self):
'exp': current_time - crypt.CLOCK_SKEW_SECS - 1,
}
with mock.patch('oauth2client.crypt.time') as time:
time.time = mock.MagicMock(name='time',
return_value=current_time)
time.time = mock.Mock(name='time',
return_value=current_time)

exception_caught = self._exception_helper(payload_dict)
self.assertNotEqual(exception_caught, None)
Expand All @@ -249,8 +249,8 @@ def test_success(self):
'exp': current_time + crypt.MAX_TOKEN_LIFETIME_SECS - 1,
}
with mock.patch('oauth2client.crypt.time') as time:
time.time = mock.MagicMock(name='time',
return_value=current_time)
time.time = mock.Mock(name='time',
return_value=current_time)

exception_caught = self._exception_helper(payload_dict)
self.assertEqual(exception_caught, None)
Expand Down Expand Up @@ -288,10 +288,10 @@ def test_jwt_payload_bad_json(self):
@mock.patch('oauth2client.crypt._verify_time_range')
@mock.patch('oauth2client.crypt._verify_signature')
def test_success(self, verify_sig, verify_time, check_aud):
certs = mock.MagicMock()
certs = mock.Mock()
cert_values = object()
certs.values = mock.MagicMock(name='values',
return_value=cert_values)
certs.values = mock.Mock(name='values',
return_value=cert_values)
audience = object()

header = b'header'
Expand Down
6 changes: 3 additions & 3 deletions tests/test_service_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,10 +275,10 @@ def test_access_token(self, utcnow):
utcnow.return_value = NOW

# Create a custom credentials with a mock signer.
signer = mock.MagicMock()
signer = mock.Mock()
signed_value = b'signed-content'
signer.sign = mock.MagicMock(name='sign',
return_value=signed_value)
signer.sign = mock.Mock(name='sign',
return_value=signed_value)
credentials = service_account.ServiceAccountCredentials(
self.service_account_email,
signer,
Expand Down

0 comments on commit 9ba4ea5

Please sign in to comment.