|
| 1 | +# Copyright 2014 Google Inc. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Tests for oauth2client.keyring_storage tests. |
| 16 | +
|
| 17 | +Unit tests for oauth2client.keyring_storage. |
| 18 | +""" |
| 19 | + |
| 20 | +import datetime |
| 21 | +import keyring |
| 22 | +import threading |
| 23 | +import unittest |
| 24 | + |
| 25 | +import mock |
| 26 | + |
| 27 | +from oauth2client import GOOGLE_TOKEN_URI |
| 28 | +from oauth2client.client import OAuth2Credentials |
| 29 | +from oauth2client.keyring_storage import Storage |
| 30 | + |
| 31 | + |
| 32 | +__author__ = '[email protected] (Joe Gregorio)' |
| 33 | + |
| 34 | + |
| 35 | +class KeyringStorageTests(unittest.TestCase): |
| 36 | + |
| 37 | + def test_constructor(self): |
| 38 | + service_name = 'my_unit_test' |
| 39 | + user_name = 'me' |
| 40 | + store = Storage(service_name, user_name) |
| 41 | + self.assertEqual(store._service_name, service_name) |
| 42 | + self.assertEqual(store._user_name, user_name) |
| 43 | + lock_type = type(threading.Lock()) |
| 44 | + self.assertTrue(isinstance(store._lock, lock_type)) |
| 45 | + |
| 46 | + def test_acquire_lock(self): |
| 47 | + store = Storage('my_unit_test', 'me') |
| 48 | + store._lock = lock = _FakeLock() |
| 49 | + self.assertEqual(lock._acquire_count, 0) |
| 50 | + store.acquire_lock() |
| 51 | + self.assertEqual(lock._acquire_count, 1) |
| 52 | + |
| 53 | + def test_release_lock(self): |
| 54 | + store = Storage('my_unit_test', 'me') |
| 55 | + store._lock = lock = _FakeLock() |
| 56 | + self.assertEqual(lock._release_count, 0) |
| 57 | + store.release_lock() |
| 58 | + self.assertEqual(lock._release_count, 1) |
| 59 | + |
| 60 | + def test_locked_get(self): |
| 61 | + service_name = 'my_unit_test' |
| 62 | + user_name = 'me' |
| 63 | + mock_content = (object(), 'mock_content') |
| 64 | + mock_return_creds = mock.MagicMock() |
| 65 | + mock_return_creds.set_store = set_store = mock.MagicMock( |
| 66 | + name='set_store') |
| 67 | + with mock.patch.object(keyring, 'get_password', |
| 68 | + return_value=mock_content, |
| 69 | + autospec=True) as get_password: |
| 70 | + with mock.patch( |
| 71 | + 'oauth2client.keyring_storage.Credentials') as MockCreds: |
| 72 | + MockCreds.new_from_json = new_from_json = mock.MagicMock( |
| 73 | + name='new_from_json', return_value=mock_return_creds) |
| 74 | + store = Storage(service_name, user_name) |
| 75 | + credentials = store.locked_get() |
| 76 | + new_from_json.assert_called_once_with(mock_content) |
| 77 | + get_password.assert_called_once_with(service_name, user_name) |
| 78 | + self.assertEqual(credentials, mock_return_creds) |
| 79 | + set_store.assert_called_once_with(store) |
| 80 | + |
| 81 | + def test_locked_put(self): |
| 82 | + service_name = 'my_unit_test' |
| 83 | + user_name = 'me' |
| 84 | + store = Storage(service_name, user_name) |
| 85 | + with mock.patch.object(keyring, 'set_password', |
| 86 | + return_value=None, |
| 87 | + autospec=True) as set_password: |
| 88 | + credentials = mock.MagicMock() |
| 89 | + to_json_ret = object() |
| 90 | + credentials.to_json = to_json = mock.MagicMock( |
| 91 | + name='to_json', return_value=to_json_ret) |
| 92 | + store.locked_put(credentials) |
| 93 | + to_json.assert_called_once_with() |
| 94 | + set_password.assert_called_once_with(service_name, user_name, |
| 95 | + to_json_ret) |
| 96 | + |
| 97 | + def test_locked_delete(self): |
| 98 | + service_name = 'my_unit_test' |
| 99 | + user_name = 'me' |
| 100 | + store = Storage(service_name, user_name) |
| 101 | + with mock.patch.object(keyring, 'set_password', |
| 102 | + return_value=None, |
| 103 | + autospec=True) as set_password: |
| 104 | + store.locked_delete() |
| 105 | + set_password.assert_called_once_with(service_name, user_name, '') |
| 106 | + |
| 107 | + def test_get_with_no_credentials_stored(self): |
| 108 | + with mock.patch.object(keyring, 'get_password', |
| 109 | + return_value=None, |
| 110 | + autospec=True) as get_password: |
| 111 | + store = Storage('my_unit_test', 'me') |
| 112 | + credentials = store.get() |
| 113 | + self.assertEquals(None, credentials) |
| 114 | + get_password.assert_called_once_with('my_unit_test', 'me') |
| 115 | + |
| 116 | + def test_get_with_malformed_json_credentials_stored(self): |
| 117 | + with mock.patch.object(keyring, 'get_password', |
| 118 | + return_value='{', |
| 119 | + autospec=True) as get_password: |
| 120 | + store = Storage('my_unit_test', 'me') |
| 121 | + credentials = store.get() |
| 122 | + self.assertEquals(None, credentials) |
| 123 | + get_password.assert_called_once_with('my_unit_test', 'me') |
| 124 | + |
| 125 | + def test_get_and_set_with_json_credentials_stored(self): |
| 126 | + access_token = 'foo' |
| 127 | + client_id = 'some_client_id' |
| 128 | + client_secret = 'cOuDdkfjxxnv+' |
| 129 | + refresh_token = '1/0/a.df219fjls0' |
| 130 | + token_expiry = datetime.datetime.utcnow() |
| 131 | + user_agent = 'refresh_checker/1.0' |
| 132 | + |
| 133 | + credentials = OAuth2Credentials( |
| 134 | + access_token, client_id, client_secret, |
| 135 | + refresh_token, token_expiry, GOOGLE_TOKEN_URI, |
| 136 | + user_agent) |
| 137 | + |
| 138 | + # Setting autospec on a mock with an iterable side_effect is |
| 139 | + # currently broken (http://bugs.python.org/issue17826), so instead |
| 140 | + # we patch twice. |
| 141 | + with mock.patch.object(keyring, 'get_password', |
| 142 | + return_value=None, |
| 143 | + autospec=True) as get_password: |
| 144 | + with mock.patch.object(keyring, 'set_password', |
| 145 | + return_value=None, |
| 146 | + autospec=True) as set_password: |
| 147 | + store = Storage('my_unit_test', 'me') |
| 148 | + self.assertEquals(None, store.get()) |
| 149 | + |
| 150 | + store.put(credentials) |
| 151 | + |
| 152 | + set_password.assert_called_once_with( |
| 153 | + 'my_unit_test', 'me', credentials.to_json()) |
| 154 | + get_password.assert_called_once_with('my_unit_test', 'me') |
| 155 | + |
| 156 | + with mock.patch.object(keyring, 'get_password', |
| 157 | + return_value=credentials.to_json(), |
| 158 | + autospec=True) as get_password: |
| 159 | + restored = store.get() |
| 160 | + self.assertEqual('foo', restored.access_token) |
| 161 | + self.assertEqual('some_client_id', restored.client_id) |
| 162 | + get_password.assert_called_once_with('my_unit_test', 'me') |
| 163 | + |
| 164 | + |
| 165 | +class _FakeLock(object): |
| 166 | + |
| 167 | + _acquire_count = 0 |
| 168 | + _release_count = 0 |
| 169 | + |
| 170 | + def acquire(self): |
| 171 | + self._acquire_count += 1 |
| 172 | + |
| 173 | + def release(self): |
| 174 | + self._release_count += 1 |
0 commit comments