From 9d47f64dc0a31334a7582780641674e1ad9796ad Mon Sep 17 00:00:00 2001 From: Georg Brandl Date: Thu, 22 Oct 2015 08:09:16 +0200 Subject: [PATCH] intersphinx: style nits --- sphinx/ext/intersphinx.py | 23 +++++---- tests/test_ext_intersphinx.py | 90 +++++++++++++++++------------------ 2 files changed, 55 insertions(+), 58 deletions(-) diff --git a/sphinx/ext/intersphinx.py b/sphinx/ext/intersphinx.py index 850176d554..5f73db10dd 100644 --- a/sphinx/ext/intersphinx.py +++ b/sphinx/ext/intersphinx.py @@ -43,14 +43,14 @@ from sphinx.locale import _ from sphinx.builders.html import INVENTORY_FILENAME -handlers = [request.ProxyHandler(), request.HTTPRedirectHandler(), - request.HTTPHandler()] +default_handlers = [request.ProxyHandler(), request.HTTPRedirectHandler(), + request.HTTPHandler()] try: - handlers.append(request.HTTPSHandler) + default_handlers.append(request.HTTPSHandler) except AttributeError: pass -request.install_opener(request.build_opener(*handlers)) +default_opener = request.build_opener(*default_handlers) UTF8StreamReader = codecs.lookup('utf-8')[2] @@ -172,15 +172,14 @@ def _read_from_url(url): :rtype: ``file``-like object """ url, username, password = _strip_basic_auth(url) - handler = request.BaseHandler() - if username is not None and password is not None: # case: url contains basic auth creds password_mgr = request.HTTPPasswordMgrWithDefaultRealm() password_mgr.add_password(None, url, username, password) handler = request.HTTPBasicAuthHandler(password_mgr) - - opener = request.build_opener(handler) + opener = request.build_opener(default_handlers + [handler]) + else: + opener = default_opener return opener.open(url) @@ -207,7 +206,7 @@ def _get_safe_url(url): if username is not None: # case: url contained basic auth creds; obscure password url_parts = parse.urlsplit(url) - safe_netloc = '{0}:********@{1}'.format(username, url_parts.hostname) + safe_netloc = '{0}@{1}'.format(username, url_parts.hostname) # replace original netloc w/ obscured version frags = list(url_parts) frags[1] = safe_netloc @@ -220,13 +219,13 @@ def fetch_inventory(app, uri, inv): """Fetch, parse and return an intersphinx inventory file.""" # both *uri* (base URI of the links to generate) and *inv* (actual # location of the inventory file) can be local or remote URIs - localuri = uri.find('://') == -1 - if localuri is False: + localuri = '://' not in uri + if not localuri: # case: inv URI points to remote resource; strip any existing auth uri, _, _ = _strip_basic_auth(uri) join = localuri and path.join or posixpath.join try: - if inv.find('://') != -1: + if '://' in inv: f = _read_from_url(inv) else: f = open(path.join(app.srcdir, inv), 'rb') diff --git a/tests/test_ext_intersphinx.py b/tests/test_ext_intersphinx.py index f8ff49a75a..f1f29528c3 100644 --- a/tests/test_ext_intersphinx.py +++ b/tests/test_ext_intersphinx.py @@ -200,54 +200,52 @@ class TestStripBasicAuth(unittest.TestCase): self.assertEqual(None, actual_password) -class TestReadFromUrl(unittest.TestCase): - """Tests for sphinx.ext.intersphinx._read_from_url()""" - @mock.patch('six.moves.urllib.request.HTTPBasicAuthHandler') - @mock.patch('six.moves.urllib.request.HTTPPasswordMgrWithDefaultRealm') - @mock.patch('six.moves.urllib.request.build_opener') - def test_authed(self, m_build_opener, m_HTTPPasswordMgrWithDefaultRealm, - m_HTTPBasicAuthHandler): - """read from URL containing basic auth creds""" - password_mgr = mock.Mock() - m_HTTPPasswordMgrWithDefaultRealm.return_value = password_mgr +@mock.patch('six.moves.urllib.request.HTTPBasicAuthHandler') +@mock.patch('six.moves.urllib.request.HTTPPasswordMgrWithDefaultRealm') +@mock.patch('six.moves.urllib.request.build_opener') +def test_readfromurl_authed(m_build_opener, m_HTTPPasswordMgrWithDefaultRealm, + m_HTTPBasicAuthHandler): + # read from URL containing basic auth creds + password_mgr = mock.Mock() + m_HTTPPasswordMgrWithDefaultRealm.return_value = password_mgr - url = 'https://user:12345@domain.com/project/objects.inv' - _read_from_url(url) + url = 'https://user:12345@domain.com/project/objects.inv' + _read_from_url(url) - m_HTTPPasswordMgrWithDefaultRealm.assert_called_once_with() - password_mgr.add_password.assert_called_with( - None, 'https://domain.com/project/objects.inv', 'user', '12345') - - @mock.patch('six.moves.urllib.request.HTTPBasicAuthHandler') - @mock.patch('six.moves.urllib.request.HTTPPasswordMgrWithDefaultRealm') - @mock.patch('six.moves.urllib.request.build_opener') - def test_unauthed(self, m_build_opener, m_HTTPPasswordMgrWithDefaultRealm, - m_HTTPBasicAuthHandler): - """read from URL without auth creds""" - password_mgr = mock.Mock() - m_HTTPPasswordMgrWithDefaultRealm.return_value = password_mgr - - url = 'https://domain.com/project/objects.inv' - _read_from_url(url) - - # assert password manager not created - self.assertEqual(None, m_HTTPPasswordMgrWithDefaultRealm.call_args) - # assert no password added to the password manager - self.assertEqual(None, password_mgr.add_password.call_args) + m_HTTPPasswordMgrWithDefaultRealm.assert_called_once_with() + password_mgr.add_password.assert_called_with( + None, 'https://domain.com/project/objects.inv', 'user', '12345') -class TestGetSafeUrl(unittest.TestCase): - """Tests for sphinx.ext.intersphinx._get_safe_url()""" - def test_authed(self): - """_get_safe_url() with a url with basic auth""" - url = 'https://user:12345@domain.com/project/objects.inv' - expected = 'https://user:********@domain.com/project/objects.inv' - actual = _get_safe_url(url) - self.assertEqual(expected, actual) +@mock.patch('six.moves.urllib.request.HTTPBasicAuthHandler') +@mock.patch('six.moves.urllib.request.HTTPPasswordMgrWithDefaultRealm') +@mock.patch('sphinx.ext.intersphinx.default_opener') +def test_readfromurl_unauthed(m_default_opener, m_HTTPPasswordMgrWithDefaultRealm, + m_HTTPBasicAuthHandler): + # read from URL without auth creds + password_mgr = mock.Mock() + m_HTTPPasswordMgrWithDefaultRealm.return_value = password_mgr - def test_unauthed(self): - """_get_safe_url() with a url without basic auth""" - url = 'https://domain.com/project/objects.inv' - expected = 'https://domain.com/project/objects.inv' - actual = _get_safe_url(url) - self.assertEqual(expected, actual) + url = 'https://domain.com/project/objects.inv' + _read_from_url(url) + + # assert password manager not created + assert m_HTTPPasswordMgrWithDefaultRealm.call_args is None + # assert no password added to the password manager + assert password_mgr.add_password.call_args is None + + +def test_getsafeurl_authed(): + """_get_safe_url() with a url with basic auth""" + url = 'https://user:12345@domain.com/project/objects.inv' + expected = 'https://user@domain.com/project/objects.inv' + actual = _get_safe_url(url) + assert expected == actual + + +def test_getsafeurl_unauthed(): + """_get_safe_url() with a url without basic auth""" + url = 'https://domain.com/project/objects.inv' + expected = 'https://domain.com/project/objects.inv' + actual = _get_safe_url(url) + assert expected == actual