Use requests in http check instead of urllib

The requests interface is much nicer and easier to
use so we might as well use it instead of direct urllib
usage.

Change-Id: I364ddb5f86900a3e166f4480d9f4889a68de247f
This commit is contained in:
Joshua Harlow 2015-09-21 18:27:42 -07:00
parent 4e2004ec03
commit 70675997e1
4 changed files with 41 additions and 33 deletions

View File

@ -14,14 +14,15 @@
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import abc import abc
import ast import ast
import contextlib
import copy import copy
from oslo_serialization import jsonutils from oslo_serialization import jsonutils
import requests
import six import six
import six.moves.urllib.parse as urlparse
import six.moves.urllib.request as urlrequest
registered_checks = {} registered_checks = {}
@ -268,12 +269,10 @@ class HttpCheck(Check):
element = target.get(key) element = target.get(key)
if type(element) is object: if type(element) is object:
temp_target[key] = {} temp_target[key] = {}
data = {'target': jsonutils.dumps(temp_target), data = {'target': jsonutils.dumps(temp_target),
'credentials': jsonutils.dumps(creds)} 'credentials': jsonutils.dumps(creds)}
post_data = urlparse.urlencode(data) with contextlib.closing(requests.post(url, data=data)) as r:
f = urlrequest.urlopen(url, post_data) return r.text == 'True'
return f.read() == 'True'
@register(None) @register(None)

View File

@ -13,13 +13,11 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import httpretty
import mock import mock
from oslo_serialization import jsonutils from oslo_serialization import jsonutils
from oslotest import base as test_base from oslotest import base as test_base
import six
import six.moves.urllib.parse as urlparse import six.moves.urllib.parse as urlparse
import six.moves.urllib.request as urlrequest
from oslo_policy import _checks from oslo_policy import _checks
from oslo_policy.tests import base from oslo_policy.tests import base
@ -88,47 +86,52 @@ class HttpCheckTestCase(base.PolicyBaseTestCase):
for item in post_data.split('&'): for item in post_data.split('&'):
key, _sep, value = item.partition('=') key, _sep, value = item.partition('=')
result[key] = jsonutils.loads(urlparse.unquote_plus(value)) result[key] = jsonutils.loads(urlparse.unquote_plus(value))
return result return result
@mock.patch.object(urlrequest, 'urlopen', @httpretty.activate
return_value=six.StringIO('True')) def test_accept(self):
def test_accept(self, mock_urlopen): httpretty.register_uri(httpretty.POST,
"http://example.com/target",
body='True')
httpretty.HTTPretty.allow_net_connect = False
check = _checks.HttpCheck('http', '//example.com/%(name)s') check = _checks.HttpCheck('http', '//example.com/%(name)s')
self.assertTrue(check(dict(name='target', spam='spammer'), self.assertTrue(check(dict(name='target', spam='spammer'),
dict(user='user', roles=['a', 'b', 'c']), dict(user='user', roles=['a', 'b', 'c']),
self.enforcer)) self.enforcer))
self.assertEqual(1, mock_urlopen.call_count)
args = mock_urlopen.call_args[0] last_request = httpretty.last_request()
self.assertEqual('POST', last_request.method)
self.assertEqual('http://example.com/target', args[0])
self.assertEqual(dict( self.assertEqual(dict(
target=dict(name='target', spam='spammer'), target=dict(name='target', spam='spammer'),
credentials=dict(user='user', roles=['a', 'b', 'c']), credentials=dict(user='user', roles=['a', 'b', 'c']),
), self.decode_post_data(args[1])) ), self.decode_post_data(last_request.body.decode("utf8")))
@httpretty.activate
def test_reject(self):
httpretty.register_uri(httpretty.POST,
"http://example.com/target",
body='other')
httpretty.HTTPretty.allow_net_connect = False
@mock.patch.object(urlrequest, 'urlopen',
return_value=six.StringIO('other'))
def test_reject(self, mock_urlopen):
check = _checks.HttpCheck('http', '//example.com/%(name)s') check = _checks.HttpCheck('http', '//example.com/%(name)s')
self.assertFalse(check(dict(name='target', spam='spammer'), self.assertFalse(check(dict(name='target', spam='spammer'),
dict(user='user', roles=['a', 'b', 'c']), dict(user='user', roles=['a', 'b', 'c']),
self.enforcer)) self.enforcer))
self.assertEqual(1, mock_urlopen.call_count)
args = mock_urlopen.call_args[0] last_request = httpretty.last_request()
self.assertEqual('POST', last_request.method)
self.assertEqual('http://example.com/target', args[0])
self.assertEqual(dict( self.assertEqual(dict(
target=dict(name='target', spam='spammer'), target=dict(name='target', spam='spammer'),
credentials=dict(user='user', roles=['a', 'b', 'c']), credentials=dict(user='user', roles=['a', 'b', 'c']),
), self.decode_post_data(args[1])) ), self.decode_post_data(last_request.body.decode("utf8")))
@mock.patch.object(urlrequest, 'urlopen', @httpretty.activate
return_value=six.StringIO('True')) def test_http_with_objects_in_target(self):
def test_http_with_objects_in_target(self, mock_urlopen): httpretty.register_uri(httpretty.POST,
"http://example.com/target",
body='True')
httpretty.HTTPretty.allow_net_connect = False
check = _checks.HttpCheck('http', '//example.com/%(name)s') check = _checks.HttpCheck('http', '//example.com/%(name)s')
target = {'a': object(), target = {'a': object(),
@ -138,9 +141,13 @@ class HttpCheckTestCase(base.PolicyBaseTestCase):
dict(user='user', roles=['a', 'b', 'c']), dict(user='user', roles=['a', 'b', 'c']),
self.enforcer)) self.enforcer))
@mock.patch.object(urlrequest, 'urlopen', @httpretty.activate
return_value=six.StringIO('True')) def test_http_with_strings_in_target(self):
def test_http_with_strings_in_target(self, mock_urlopen): httpretty.register_uri(httpretty.POST,
"http://example.com/target",
body='True')
httpretty.HTTPretty.allow_net_connect = False
check = _checks.HttpCheck('http', '//example.com/%(name)s') check = _checks.HttpCheck('http', '//example.com/%(name)s')
target = {'a': 'some_string', target = {'a': 'some_string',
'name': 'target', 'name': 'target',

View File

@ -2,6 +2,7 @@
# of appearance. Changing the order has an impact on the overall integration # of appearance. Changing the order has an impact on the overall integration
# process, which may cause wedges in the gate later. # process, which may cause wedges in the gate later.
requests>=2.5.2
oslo.config>=2.3.0 # Apache-2.0 oslo.config>=2.3.0 # Apache-2.0
oslo.i18n>=1.5.0 # Apache-2.0 oslo.i18n>=1.5.0 # Apache-2.0
oslo.serialization>=1.4.0 # Apache-2.0 oslo.serialization>=1.4.0 # Apache-2.0

View File

@ -4,6 +4,7 @@
hacking<0.11,>=0.10.0 hacking<0.11,>=0.10.0
oslotest>=1.10.0 # Apache-2.0 oslotest>=1.10.0 # Apache-2.0
httpretty>=0.8.4,<0.8.7
# These are needed for docs generation # These are needed for docs generation
oslosphinx>=2.5.0 # Apache-2.0 oslosphinx>=2.5.0 # Apache-2.0