Don't mock the session.request function
The exact parameters we pass to request change occasionally for reasons unrelated to either of these tests. Requests-mock supports testing for cert and verify values directly and we can test the auth plugin via what it sets on the request. Change-Id: I771db5a061c355b9324e46f601c2dd98cce1ace0
This commit is contained in:
parent
049cf2dc6c
commit
61b49d0ae8
@ -13,7 +13,6 @@
|
||||
import abc
|
||||
import uuid
|
||||
|
||||
import mock
|
||||
import six
|
||||
|
||||
from keystoneauth1 import _utils
|
||||
@ -532,24 +531,17 @@ class GenericAuthPluginTests(utils.TestCase):
|
||||
|
||||
def test_setting_connection_params(self):
|
||||
text = uuid.uuid4().hex
|
||||
self.stub_url('GET', base_url=self.auth.url('prefix'), text=text)
|
||||
|
||||
with mock.patch.object(self.session.session, 'request') as mocked:
|
||||
mocked.return_value = utils.TestResponse({'status_code': 200,
|
||||
'text': text})
|
||||
resp = self.session.get('prefix',
|
||||
endpoint_filter=self.ENDPOINT_FILTER)
|
||||
resp = self.session.get('prefix',
|
||||
endpoint_filter=self.ENDPOINT_FILTER)
|
||||
|
||||
self.assertEqual(text, resp.text)
|
||||
self.assertEqual(text, resp.text)
|
||||
|
||||
# the cert and verify values passed to request are those that were
|
||||
# returned from the auth plugin as connection params.
|
||||
|
||||
mocked.assert_called_once_with('GET',
|
||||
self.auth.url('prefix'),
|
||||
headers=mock.ANY,
|
||||
allow_redirects=False,
|
||||
cert=self.auth.cert,
|
||||
verify=False)
|
||||
# the cert and verify values passed to request are those that were
|
||||
# returned from the auth plugin as connection params.
|
||||
self.assertEqual(self.auth.cert, self.requests_mock.last_request.cert)
|
||||
self.assertFalse(self.requests_mock.last_request.verify)
|
||||
|
||||
def test_setting_bad_connection_params(self):
|
||||
# The uuid name parameter here is unknown and not in the allowed params
|
||||
|
@ -17,6 +17,7 @@ import uuid
|
||||
|
||||
import mock
|
||||
import requests
|
||||
import requests.auth
|
||||
import six
|
||||
from testtools import matchers
|
||||
|
||||
@ -28,6 +29,20 @@ from keystoneauth1.tests.unit import utils
|
||||
from keystoneauth1 import token_endpoint
|
||||
|
||||
|
||||
class RequestsAuth(requests.auth.AuthBase):
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(RequestsAuth, self).__init__(*args, **kwargs)
|
||||
self.header_name = uuid.uuid4().hex
|
||||
self.header_val = uuid.uuid4().hex
|
||||
self.called = False
|
||||
|
||||
def __call__(self, request):
|
||||
request.headers[self.header_name] = self.header_val
|
||||
self.called = True
|
||||
return request
|
||||
|
||||
|
||||
class SessionTests(utils.TestCase):
|
||||
|
||||
TEST_URL = 'http://127.0.0.1:5000/'
|
||||
@ -532,20 +547,16 @@ class SessionAuthTests(utils.TestCase):
|
||||
|
||||
def test_requests_auth_plugin(self):
|
||||
sess = client_session.Session()
|
||||
requests_auth = RequestsAuth()
|
||||
|
||||
requests_auth = object()
|
||||
self.requests_mock.get(self.TEST_URL, text='resp')
|
||||
|
||||
FAKE_RESP = utils.TestResponse({'status_code': 200, 'text': 'resp'})
|
||||
RESP = mock.Mock(return_value=FAKE_RESP)
|
||||
sess.get(self.TEST_URL, requests_auth=requests_auth)
|
||||
last = self.requests_mock.last_request
|
||||
|
||||
with mock.patch.object(sess.session, 'request', RESP) as mocked:
|
||||
sess.get(self.TEST_URL, requests_auth=requests_auth)
|
||||
|
||||
mocked.assert_called_once_with('GET', self.TEST_URL,
|
||||
headers=mock.ANY,
|
||||
allow_redirects=mock.ANY,
|
||||
auth=requests_auth,
|
||||
verify=mock.ANY)
|
||||
self.assertEqual(requests_auth.header_val,
|
||||
last.headers[requests_auth.header_name])
|
||||
self.assertTrue(requests_auth.called)
|
||||
|
||||
def test_reauth_called(self):
|
||||
auth = CalledAuthPlugin(invalidate=True)
|
||||
|
Loading…
Reference in New Issue
Block a user