def test_basic_auth_handler(self): """ Test that the opener can create a basic auth handler. """ urllib.request._opener = unittest.mock.Mock() urllib.request._opener.open = unittest.mock.Mock( return_value='url contents') opener = url_opener.UrlOpener(username='******', password='******') self.assertEqual('url contents', opener.url_open('http://bla'))
def test_url_opener(self): """ Checks if the UrlOpener, that is mocked in the tests, fulfill requirements """ opener = url_opener.UrlOpener() self.assertTrue(opener is not None) self.assertTrue(callable(getattr(opener, "url_read", None)))
def test_exception_while_opening(self): """ Test an exception during opening. """ logging.warning = unittest.mock.MagicMock() opener = url_opener.UrlOpener() opener._UrlOpener__opener = unittest.mock.Mock( side_effect=urllib.error.HTTPError(None, None, None, None, None)) self.assertRaises(urllib.error.HTTPError, opener.url_open, 'http://bla') logging.warning.assert_called_once()
def test_basic_auth_handler_empty_credentials(self, mock_add_header): """ Test that the opener does not create a basic auth handler, when credentials are empty. """ urllib.request._opener = unittest.mock.Mock() urllib.request._opener.open = unittest.mock.Mock( return_value='url contents') opener = url_opener.UrlOpener(username='', password='') # nosec self.assertEqual('url contents', opener.url_open('http://bla')) urllib.request._opener.open.assert_called_once() mock_add_header.assert_not_called()
def test_opener_with_password_mgr(self): """ Test that the opener can create a basic auth handler with password manager. """ urlopener = MagicMock() urlopener.open = MagicMock(return_value='url contents') urllib.request.build_opener = MagicMock(return_value=urlopener) opener = url_opener.UrlOpener('http://uri', username='******', password='******') # nosec self.assertEqual('url contents', opener.url_open('url'))
def test_basic_auth_handler_empty_user(self, mock_add_header): """ Test that the opener can create a basic auth handler, when user name is empty. """ urllib.request._opener = unittest.mock.Mock() urllib.request._opener.open = unittest.mock.Mock( return_value='url contents') opener = url_opener.UrlOpener(username='', password='******') # nosec self.assertEqual('url contents', opener.url_open('http://bla')) urllib.request._opener.open.assert_called_once() mock_add_header.assert_called_once_with('Authorization', 'Basic OnBhc3M=')
def test_multiple_timeouts(self, mock_signal): # pylint: disable=unused-argument """ Test that a timed out host isn't contacted again. """ opener = url_opener.UrlOpener() self.assertRaises(TimeoutError, opener.url_open, 'http://bla:9000/url1') try: opener.url_open('http://bla:9000/url2') self.fail("Expected TimeoutError") # pragma: no cover except TimeoutError as reason: self.assertEqual('Skipped because bla:9000 timed out before.', str(reason))
def test_token_bearer_auth_handler(self, mock_add_header): """ Test that the opener can create a token bearer auth handler. """ urllib.request._opener = unittest.mock.Mock() urllib.request._opener.open = unittest.mock.Mock( return_value='url contents') opener = url_opener.UrlOpener(authorization_token='auth.token') self.assertEqual('url contents', opener.url_open('http://bla')) urllib.request._opener.open.assert_called_once() self.assertEqual( 'GET', urllib.request._opener.open.call_args[0][0].get_method()) mock_add_header.assert_called_once_with('Authorization', 'Bearer auth.token')
def test_exception_while_opening_without_logging(self): """ Test an exception during opening without logging. """ logging.error = MagicMock() logging.warning = MagicMock() opener = url_opener.UrlOpener() opener._UrlOpener__opener = unittest.mock.Mock( side_effect=urllib.error.HTTPError(None, None, None, None, None)) self.assertRaises(urllib.error.HTTPError, opener.url_open, 'http://bla', log_error=False) logging.error.assert_not_called() logging.warning.assert_not_called()
def test_basic_auth_handler(self, mock_add_header): """ Test that the opener can create a basic auth handler. """ urllib.request._opener = unittest.mock.Mock() urllib.request._opener.open = unittest.mock.Mock( return_value='url contents') opener = url_opener.UrlOpener(username='******', password='******') # nosec self.assertEqual('url contents', opener.url_open('http://bla')) urllib.request._opener.open.assert_called_once() self.assertEqual( 'GET', urllib.request._opener.open.call_args[0][0].get_method()) mock_add_header.assert_called_once_with('Authorization', 'Basic dXNlcjpwYXNz')
def test_timeout(self, mock_signal): # pylint: disable=unused-argument """ Test the timeout exception. """ self.assertRaises(TimeoutError, url_opener.UrlOpener().url_open, 'http://bla')
def test_opener_without_auth(self): """ Test that the opener can open urls without authentication. """ opener = url_opener.UrlOpener() opener._UrlOpener__opener = unittest.mock.Mock( return_value='url contents') self.assertEqual('url contents', opener.url_open('http://bla'))
def test_username_password(self): """ Test that the username and password can be set. """ opener = url_opener.UrlOpener(username='******', password='******') self.assertEqual('user', opener.username()) self.assertEqual('pass', opener.password())
def test_url_read(self): """ Test reading an url. """ opener = url_opener.UrlOpener() opener._UrlOpener__opener = unittest.mock.Mock( return_value=io.StringIO('contents')) self.assertEqual('contents', opener.url_read('http://bla'))