From 7c954516f52761596aa39c5a7f5ed7b25ec0eacd Mon Sep 17 00:00:00 2001 From: Brant Knudson Date: Sun, 17 Aug 2014 16:34:33 -0500 Subject: [PATCH] Add a test for re-caching a token There was no test that showed that when a token was in the cache and a request is made to validate it again, it gets cached again. Change-Id: I1992fa6b5ddc0792dfd1ce90fb888c365ecb1612 Partial-Bug: #1289075 --- .../tests/test_auth_token_middleware.py | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/keystonemiddleware/tests/test_auth_token_middleware.py b/keystonemiddleware/tests/test_auth_token_middleware.py index e2dff21c..0e4689a9 100644 --- a/keystonemiddleware/tests/test_auth_token_middleware.py +++ b/keystonemiddleware/tests/test_auth_token_middleware.py @@ -1212,6 +1212,38 @@ class CommonAuthTokenMiddlewareTest(object): use_kerberos=use_kerberos, success=False) + def test_caching_token_on_verify(self): + # When the token is cached, it's cached again when it's verified. + # NOTE(blk-u): This behavior is incorrect and inefficient, see + # bug 1289075. + + # The token cache has to be initialized with our cache instance. + self.middleware._token_cache._env_cache_name = 'cache' + cache = memorycache.Client() + self.middleware._token_cache.initialize(env={'cache': cache}) + + # Mock cache.set since then the test can verify call_count. + orig_cache_set = cache.set + cache.set = mock.Mock(side_effect=orig_cache_set) + + token = self.token_dict['signed_token_scoped'] + + req = webob.Request.blank('/') + req.headers['X-Auth-Token'] = token + self.middleware(req.environ, self.start_fake_response) + self.assertEqual(200, self.response_status) + + self.assertThat(1, matchers.Equals(cache.set.call_count)) + + req = webob.Request.blank('/') + req.headers['X-Auth-Token'] = token + self.middleware(req.environ, self.start_fake_response) + self.assertEqual(200, self.response_status) + + # FIXME(blk-u): This should be 1 since the token shouldn't be cached + # again. + self.assertThat(2, matchers.Equals(cache.set.call_count)) + class V2CertDownloadMiddlewareTest(BaseAuthTokenMiddlewareTest, testresources.ResourcedTestCase):