summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJamie Lennox <jamielennox@redhat.com>2015-01-09 10:57:13 +1000
committerBrant Knudson <bknudson@us.ibm.com>2015-04-19 14:07:17 -0500
commit62208d6a9ac067b2329b0e655fe529d1f62e32b6 (patch)
tree209379d8eefad7871b11365c10931d9e7c1862c1
parentc97be0a9ee778f25e2931aeb1f678c145c2f1d2b (diff)
downloadkeystonemiddleware-62208d6a9ac067b2329b0e655fe529d1f62e32b6.tar.gz
Use a test fixture for mocking time
Use a fixture and the mocking functions provided in oslo.utils.timeutils rather than mocking the function directly. This broke when the functions moved. Change-Id: I65b6ae58c6b4e02e235f992c21502aa377992985 Closes-Bug: #1408838 (cherry picked from commit a4a46764491aa36ec4440c91c435578e178f14f8)
-rw-r--r--keystonemiddleware/tests/test_auth_token_middleware.py60
1 files changed, 31 insertions, 29 deletions
diff --git a/keystonemiddleware/tests/test_auth_token_middleware.py b/keystonemiddleware/tests/test_auth_token_middleware.py
index 0506202..38d124a 100644
--- a/keystonemiddleware/tests/test_auth_token_middleware.py
+++ b/keystonemiddleware/tests/test_auth_token_middleware.py
@@ -157,6 +157,22 @@ class TimezoneFixture(fixtures.Fixture):
time.tzset()
+class TimeFixture(fixtures.Fixture):
+
+ def __init__(self, new_time, normalize=True):
+ super(TimeFixture, self).__init__()
+ if isinstance(new_time, six.string_types):
+ new_time = timeutils.parse_isotime(new_time)
+ if normalize:
+ new_time = timeutils.normalize_time(new_time)
+ self.new_time = new_time
+
+ def setUp(self):
+ super(TimeFixture, self).setUp()
+ timeutils.set_time_override(self.new_time)
+ self.addCleanup(timeutils.clear_time_override)
+
+
class FakeApp(object):
"""This represents a WSGI app protected by the auth_token middleware."""
@@ -1094,16 +1110,14 @@ class CommonAuthTokenMiddlewareTest(object):
token = self.token_dict['signed_token_scoped']
req.headers['X-Auth-Token'] = token
req.environ.update(extra_environ)
- utcnow = 'oslo.utils.timeutils.utcnow'
+
now = datetime.datetime.utcnow()
- with mock.patch(utcnow) as mock_utcnow:
- mock_utcnow.return_value = now
- self.middleware(req.environ, self.start_fake_response)
- self.assertIsNotNone(self._get_cached_token(token))
- expired = now + datetime.timedelta(seconds=token_cache_time)
- with mock.patch(utcnow) as mock_utcnow:
- mock_utcnow.return_value = expired
- self.assertIsNone(self._get_cached_token(token))
+ self.useFixture(TimeFixture(now))
+ self.middleware(req.environ, self.start_fake_response)
+ self.assertIsNotNone(self._get_cached_token(token))
+
+ timeutils.advance_time_seconds(token_cache_time)
+ self.assertIsNone(self._get_cached_token(token))
def test_swift_memcache_set_expired(self):
extra_conf = {'cache': 'swift.cache'}
@@ -1915,22 +1929,16 @@ class TokenExpirationTest(BaseAuthTokenMiddlewareTest):
auth_token._confirm_token_not_expired,
data)
- @mock.patch('oslo.utils.timeutils.utcnow')
- def test_v2_token_with_timezone_offset_not_expired(self, mock_utcnow):
- current_time = timeutils.parse_isotime('2000-01-01T00:01:10.000123Z')
- current_time = timeutils.normalize_time(current_time)
- mock_utcnow.return_value = current_time
+ def test_v2_token_with_timezone_offset_not_expired(self):
+ self.useFixture(TimeFixture('2000-01-01T00:01:10.000123Z'))
data = self.create_v2_token_fixture(
expires='2000-01-01T00:05:10.000123-05:00')
expected_expires = '2000-01-01T05:05:10.000123Z'
actual_expires = auth_token._confirm_token_not_expired(data)
self.assertEqual(actual_expires, expected_expires)
- @mock.patch('oslo.utils.timeutils.utcnow')
- def test_v2_token_with_timezone_offset_expired(self, mock_utcnow):
- current_time = timeutils.parse_isotime('2000-01-01T00:01:10.000123Z')
- current_time = timeutils.normalize_time(current_time)
- mock_utcnow.return_value = current_time
+ def test_v2_token_with_timezone_offset_expired(self):
+ self.useFixture(TimeFixture('2000-01-01T00:01:10.000123Z'))
data = self.create_v2_token_fixture(
expires='2000-01-01T00:05:10.000123+05:00')
data['access']['token']['expires'] = '2000-01-01T00:05:10.000123+05:00'
@@ -1950,11 +1958,8 @@ class TokenExpirationTest(BaseAuthTokenMiddlewareTest):
auth_token._confirm_token_not_expired,
data)
- @mock.patch('oslo.utils.timeutils.utcnow')
- def test_v3_token_with_timezone_offset_not_expired(self, mock_utcnow):
- current_time = timeutils.parse_isotime('2000-01-01T00:01:10.000123Z')
- current_time = timeutils.normalize_time(current_time)
- mock_utcnow.return_value = current_time
+ def test_v3_token_with_timezone_offset_not_expired(self):
+ self.useFixture(TimeFixture('2000-01-01T00:01:10.000123Z'))
data = self.create_v3_token_fixture(
expires='2000-01-01T00:05:10.000123-05:00')
expected_expires = '2000-01-01T05:05:10.000123Z'
@@ -1962,11 +1967,8 @@ class TokenExpirationTest(BaseAuthTokenMiddlewareTest):
actual_expires = auth_token._confirm_token_not_expired(data)
self.assertEqual(actual_expires, expected_expires)
- @mock.patch('oslo.utils.timeutils.utcnow')
- def test_v3_token_with_timezone_offset_expired(self, mock_utcnow):
- current_time = timeutils.parse_isotime('2000-01-01T00:01:10.000123Z')
- current_time = timeutils.normalize_time(current_time)
- mock_utcnow.return_value = current_time
+ def test_v3_token_with_timezone_offset_expired(self):
+ self.useFixture(TimeFixture('2000-01-01T00:01:10.000123Z'))
data = self.create_v3_token_fixture(
expires='2000-01-01T00:05:10.000123+05:00')
self.assertRaises(auth_token.InvalidToken,