Merge "Avoid returning stale token via auth_token property"

This commit is contained in:
Jenkins
2013-11-20 22:54:38 +00:00
committed by Gerrit Code Review
2 changed files with 31 additions and 6 deletions

View File

@@ -336,12 +336,12 @@ class HTTPClient(object):
@property
def auth_token(self):
if self.auth_token_from_user:
return self.auth_token_from_user
if self.auth_ref:
if self.auth_ref.will_expire_soon(self.stale_duration):
self.authenticate()
return self.auth_ref.auth_token
elif self.auth_token_from_user:
return self.auth_token_from_user
@auth_token.setter
def auth_token(self, value):

View File

@@ -23,18 +23,22 @@ from keystoneclient import httpclient
from keystoneclient.tests import utils
RESPONSE_BODY = '{"hi": "there"}'
AUTHED_TOKEN = "token"
def get_client():
cl = httpclient.HTTPClient(username="username", password="password",
tenant_id="tenant", auth_url="auth_test")
def get_client(token=None):
if token:
cl = httpclient.HTTPClient(token=token, auth_url="auth_test")
else:
cl = httpclient.HTTPClient(username="username", password="password",
tenant_id="tenant", auth_url="auth_test")
return cl
def get_authed_client():
cl = get_client()
cl.management_url = "http://127.0.0.1:5000"
cl.auth_token = "token"
cl.auth_token = AUTHED_TOKEN
return cl
@@ -144,6 +148,27 @@ class ClientTest(utils.TestCase):
client.HTTPClient
def test_auth_token_none(self):
cl = get_client()
self.assertEqual(cl.auth_token, None)
def test_auth_token_authed(self):
cl = get_authed_client()
self.assertEqual(cl.auth_token, AUTHED_TOKEN)
def test_auth_token_reauth(self):
cl = get_client(token='initial')
self.assertEqual(cl.auth_token, 'initial')
class FakeAccessInfo(object):
auth_token = 'updated'
def will_expire_soon(self, stale_duration=None):
return False
cl.auth_ref = FakeAccessInfo()
self.assertEqual(cl.auth_token, 'updated')
class BasicRequestTests(utils.TestCase):