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://*****:*****@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")
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 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_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 url = 'https://*****:*****@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')
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://*****:*****@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')
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)
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 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_fetch_inventory_redirection(app, status, warning, _read_from_url, read_inventory_v2): _read_from_url( ).readline.return_value = '# Sphinx inventory version 2'.encode('utf-8') # same uri and inv, not redirected _read_from_url( ).geturl.return_value = 'http://hostname/' + INVENTORY_FILENAME fetch_inventory(app, 'http://hostname/', 'http://hostname/' + INVENTORY_FILENAME) assert 'intersphinx inventory has moved' not in status.getvalue() assert read_inventory_v2.call_args[0][1] == 'http://hostname/' # same uri and inv, redirected status.seek(0) status.truncate(0) _read_from_url( ).geturl.return_value = 'http://hostname/new/' + INVENTORY_FILENAME fetch_inventory(app, 'http://hostname/', 'http://hostname/' + INVENTORY_FILENAME) assert status.getvalue() == ( 'intersphinx inventory has moved: ' 'http://hostname/%s -> http://hostname/new/%s\n' % (INVENTORY_FILENAME, INVENTORY_FILENAME)) assert read_inventory_v2.call_args[0][1] == 'http://hostname/new' # different uri and inv, not redirected status.seek(0) status.truncate(0) _read_from_url( ).geturl.return_value = 'http://hostname/new/' + INVENTORY_FILENAME fetch_inventory(app, 'http://hostname/', 'http://hostname/new/' + INVENTORY_FILENAME) assert 'intersphinx inventory has moved' not in status.getvalue() assert read_inventory_v2.call_args[0][1] == 'http://hostname/' # different uri and inv, redirected status.seek(0) status.truncate(0) _read_from_url( ).geturl.return_value = 'http://hostname/other/' + INVENTORY_FILENAME fetch_inventory(app, 'http://hostname/', 'http://hostname/new/' + INVENTORY_FILENAME) assert status.getvalue() == ( 'intersphinx inventory has moved: ' 'http://hostname/new/%s -> http://hostname/other/%s\n' % (INVENTORY_FILENAME, INVENTORY_FILENAME)) assert read_inventory_v2.call_args[0][1] == 'http://hostname/'
def test_fetch_inventory_redirection(app, status, warning, _read_from_url, read_inventory): intersphinx_setup(app) _read_from_url().readline.return_value = "# Sphinx inventory version 2".encode("utf-8") # same uri and inv, not redirected _read_from_url().url = "http://hostname/" + INVENTORY_FILENAME fetch_inventory(app, "http://hostname/", "http://hostname/" + INVENTORY_FILENAME) assert "intersphinx inventory has moved" not in status.getvalue() assert read_inventory.call_args[0][1] == "http://hostname/" # same uri and inv, redirected status.seek(0) status.truncate(0) _read_from_url().url = "http://hostname/new/" + INVENTORY_FILENAME fetch_inventory(app, "http://hostname/", "http://hostname/" + INVENTORY_FILENAME) assert status.getvalue() == ( "intersphinx inventory has moved: " "http://hostname/%s -> http://hostname/new/%s\n" % (INVENTORY_FILENAME, INVENTORY_FILENAME) ) assert read_inventory.call_args[0][1] == "http://hostname/new" # different uri and inv, not redirected status.seek(0) status.truncate(0) _read_from_url().url = "http://hostname/new/" + INVENTORY_FILENAME fetch_inventory(app, "http://hostname/", "http://hostname/new/" + INVENTORY_FILENAME) assert "intersphinx inventory has moved" not in status.getvalue() assert read_inventory.call_args[0][1] == "http://hostname/" # different uri and inv, redirected status.seek(0) status.truncate(0) _read_from_url().url = "http://hostname/other/" + INVENTORY_FILENAME fetch_inventory(app, "http://hostname/", "http://hostname/new/" + INVENTORY_FILENAME) assert status.getvalue() == ( "intersphinx inventory has moved: " "http://hostname/new/%s -> http://hostname/other/%s\n" % (INVENTORY_FILENAME, INVENTORY_FILENAME) ) assert read_inventory.call_args[0][1] == "http://hostname/"
def test_fetch_inventory_redirection(app, status, warning, _read_from_url, read_inventory): intersphinx_setup(app) _read_from_url().readline.return_value = '# Sphinx inventory version 2'.encode('utf-8') # same uri and inv, not redirected _read_from_url().url = 'http://hostname/' + INVENTORY_FILENAME fetch_inventory(app, 'http://hostname/', 'http://hostname/' + INVENTORY_FILENAME) assert 'intersphinx inventory has moved' not in status.getvalue() assert read_inventory.call_args[0][1] == 'http://hostname/' # same uri and inv, redirected status.seek(0) status.truncate(0) _read_from_url().url = 'http://hostname/new/' + INVENTORY_FILENAME fetch_inventory(app, 'http://hostname/', 'http://hostname/' + INVENTORY_FILENAME) assert status.getvalue() == ('intersphinx inventory has moved: ' 'http://hostname/%s -> http://hostname/new/%s\n' % (INVENTORY_FILENAME, INVENTORY_FILENAME)) assert read_inventory.call_args[0][1] == 'http://hostname/new' # different uri and inv, not redirected status.seek(0) status.truncate(0) _read_from_url().url = 'http://hostname/new/' + INVENTORY_FILENAME fetch_inventory(app, 'http://hostname/', 'http://hostname/new/' + INVENTORY_FILENAME) assert 'intersphinx inventory has moved' not in status.getvalue() assert read_inventory.call_args[0][1] == 'http://hostname/' # different uri and inv, redirected status.seek(0) status.truncate(0) _read_from_url().url = 'http://hostname/other/' + INVENTORY_FILENAME fetch_inventory(app, 'http://hostname/', 'http://hostname/new/' + INVENTORY_FILENAME) assert status.getvalue() == ('intersphinx inventory has moved: ' 'http://hostname/new/%s -> http://hostname/other/%s\n' % (INVENTORY_FILENAME, INVENTORY_FILENAME)) assert read_inventory.call_args[0][1] == 'http://hostname/'
def run(self): if 'url' in self.options or 'github' in self.options: conf_dict = self.env.app.config.urlinclude_config if self.env.docname in conf_dict: override = self.options self.options = conf_dict[self.env.docname].copy() self.options.update(override) build_dir = os.path.dirname(self.env.doctreedir) download_dir = os.path.join(build_dir, '_urlinclude') os.makedirs(download_dir, exist_ok=True) basename = self.arguments[0] if 'github' in self.options: branch_path = f"raw/{self.options.get('branch', 'master')}" repo_url = f"https://github.com/{self.options['github']}" url = f"{repo_url}/{branch_path}/{basename}" elif 'url' in self.options: url = f"{self.options['url']}" local_path = os.path.join( download_dir, f'{sha(url.encode("utf-8")).hexdigest(5)}.py') doc_path = os.path.dirname(self.env.doc2path(self.env.docname)) local_rel_path = os.path.relpath(local_path, doc_path) if not os.path.isfile(local_path): print(f'Downloading: {url}') contents = _read_from_url(url, self.env.app.config).read() with open(local_path, 'w') as f: f.write(contents.decode()) self.arguments[0] = local_rel_path return super().run()