Skip to content

Commit

Permalink
Use ibapauth cookies for the entire session (#214)
Browse files Browse the repository at this point in the history
* use ibapauth cookies for the entire session

* change location where session cookies are checked, this fixes the unit tests

* fix: E303 too many blank lines

* add unittest for cookies

* test http cookies for both get_object and create_object

* test http cookies for both get_object and create_object (add comment to trigger ci)
  • Loading branch information
badnetmask authored and saiprasannasastry committed Mar 5, 2019
1 parent afdaa8a commit d85e570
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
8 changes: 8 additions & 0 deletions infoblox_client/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,10 @@ def _handle_get_object(self, obj_type, query_params, extattrs,
def _get_object(self, obj_type, url):
opts = self._get_request_options()
self._log_request('get', url, opts)
if(self.session.cookies):
# the first 'get' or 'post' action will generate a cookie
# after that, we don't need to re-authenticate
self.session.auth = None
r = self.session.get(url, **opts)

self._validate_authorized(r)
Expand Down Expand Up @@ -351,6 +355,10 @@ def create_object(self, obj_type, payload, return_fields=None):
url = self._construct_url(obj_type, query_params)
opts = self._get_request_options(data=payload)
self._log_request('post', url, opts)
if(self.session.cookies):
# the first 'get' or 'post' action will generate a cookie
# after that, we don't need to re-authenticate
self.session.auth = None
r = self.session.post(url, **opts)

self._validate_authorized(r)
Expand Down
12 changes: 12 additions & 0 deletions tests/test_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def test_create_object(self):

with patch.object(requests.Session, 'post',
return_value=mock.Mock()) as patched_create:
self.connector.session.cookies = ['cookies']
patched_create.return_value.status_code = 201
patched_create.return_value.content = '{}'
self.connector.create_object(objtype, payload)
Expand All @@ -64,6 +65,8 @@ def test_create_object(self):
timeout=self.default_opts.http_request_timeout,
verify=self.default_opts.ssl_verify,
)
# test if cookies have been set
self.assertEqual(None, self.connector.session.auth)

def test_create_object_with_extattrs(self):
objtype = 'network'
Expand Down Expand Up @@ -488,6 +491,15 @@ def test__check_service_availability(self):
self.connector._check_service_availability, "delete",
resp, '_ref')

def test_get_object_with_cookies(self):
objtype = 'network'
with patch.object(requests.Session, 'get',
return_value=mock.Mock()) as patched_get:
self.connector.session.cookies = ['cookies']
patched_get.return_value.status_code = 200
patched_get.return_value.content = '{}'
self.connector.get_object(objtype, {})
self.assertEqual(None, self.connector.session.auth)

class TestInfobloxConnectorStaticMethods(unittest.TestCase):
def test_neutron_exception_is_raised_on_any_request_error(self):
Expand Down

0 comments on commit d85e570

Please sign in to comment.