Make the kerberos support python3 ready

urllib.request.Request.get_host() is removed in python >=3.4 in favor of
urllib.request.Request.host. It works in python 2.7 too.

Update the test to use Mock with spec where applicable, to ensure that
only available attributes are accessed.

Change-Id: I0e6a4e1539f9db329b6f113207d52f171cc600a7
This commit is contained in:
Jan Hruban 2016-04-28 15:43:20 +02:00
parent f6f26ec481
commit d2cfca68d0
2 changed files with 12 additions and 4 deletions

View File

@ -52,7 +52,7 @@ class HTTPNegotiateHandler(request.BaseHandler):
self.tries += 1
try:
krb_resp = self._krb_response(req.get_host(), krb_req)
krb_resp = self._krb_response(req.host, krb_req)
req.add_unredirected_header('Authorization',
"Negotiate %s" % krb_resp)

View File

@ -1,6 +1,7 @@
import kerberos
assert kerberos # pyflakes
from mock import patch, Mock
from six.moves.urllib.request import Request
import testtools
from jenkins import urllib_kerb
@ -23,7 +24,7 @@ class KerberosTests(testtools.TestCase):
parent_return_mock.headers = {'www-authenticate': "Negotiate bar"}
parent_mock.open.return_value = parent_return_mock
request_mock = Mock()
request_mock = Mock(spec=self._get_dummy_request())
h = urllib_kerb.HTTPNegotiateHandler()
h.add_parent(parent_mock)
rv = h.http_error_401(request_mock, "", "", "", headers_from_server)
@ -48,7 +49,8 @@ class KerberosTests(testtools.TestCase):
init_mock.side_effect = kerberos.GSSError
h = urllib_kerb.HTTPNegotiateHandler()
rv = h.http_error_401(Mock(), "", "", "", headers_from_server)
rv = h.http_error_401(Mock(spec=self._get_dummy_request()), "", "", "",
headers_from_server)
self.assertEqual(rv, None)
@patch('kerberos.authGSSClientResponse')
@ -59,7 +61,8 @@ class KerberosTests(testtools.TestCase):
headers_from_server = {}
h = urllib_kerb.HTTPNegotiateHandler()
rv = h.http_error_401(Mock(), "", "", "", headers_from_server)
rv = h.http_error_401(Mock(spec=self._get_dummy_request()), "", "", "",
headers_from_server)
self.assertEqual(rv, None)
@patch('kerberos.authGSSClientResponse')
@ -114,3 +117,8 @@ class KerberosTests(testtools.TestCase):
h = urllib_kerb.HTTPNegotiateHandler()
with testtools.ExpectedException(ValueError):
h._extract_krb_value(headers_from_server)
def _get_dummy_request(self):
r = Request('http://example.com')
r.timeout = 10
return r