, msg_id = None
- timeout = None
-
- def _check_if_message_on_server(self, msg_id, timeout=None):
- """Try to load page for specific message. If there is a html tag like then there is no
- such message.
-
- :param msg_id: ID of message to be checked
- :params timeout: The value of timeout to be passed to the get request
- :return:
- """
-
- request_headers = dict()
- if self._user or self._password:
- request_headers['Cookie'] = self._make_user_and_pswd_cookie()
- try:
- > response = requests.get(self._url + str(msg_id), headers=request_headers, allow_redirects=False,
- verify=False, timeout=timeout)
-
- slic/utils/logbook.py:570:
- _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
- .pixi/envs/default/lib/python3.8/site-packages/requests/api.py:73: in get
- return request("get", url, params=params, **kwargs)
- .pixi/envs/default/lib/python3.8/site-packages/requests/api.py:59: in request
- return session.request(method=method, url=url, **kwargs)
- .pixi/envs/default/lib/python3.8/site-packages/requests/sessions.py:589: in request
- resp = self.send(prep, **send_kwargs)
- .pixi/envs/default/lib/python3.8/site-packages/requests/sessions.py:703: in send
- r = adapter.send(request, **kwargs)
- _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
-
- self =
- request = , stream = False
- timeout = Timeout(connect=None, read=None, total=None), verify = False
- cert = None, proxies = OrderedDict()
-
- def send(
- self, request, stream=False, timeout=None, verify=True, cert=None, proxies=None
- ):
- """Sends PreparedRequest object. Returns Response object.
-
- :param request: The :class:`PreparedRequest ` being sent.
- :param stream: (optional) Whether to stream the request content.
- :param timeout: (optional) How long to wait for the server to send
- data before giving up, as a float, or a :ref:`(connect timeout,
- read timeout) ` tuple.
- :type timeout: float or tuple or urllib3 Timeout object
- :param verify: (optional) Either a boolean, in which case it controls whether
- we verify the server's TLS certificate, or a string, in which case it
- must be a path to a CA bundle to use
- :param cert: (optional) Any user-provided SSL certificate to be trusted.
- :param proxies: (optional) The proxies dictionary to apply to the request.
- :rtype: requests.Response
- """
-
- try:
- conn = self.get_connection_with_tls_context(
- request, verify, proxies=proxies, cert=cert
- )
- except LocationValueError as e:
- raise InvalidURL(e, request=request)
-
- self.cert_verify(conn, request.url, verify, cert)
- url = self.request_url(request, proxies)
- self.add_headers(
- request,
- stream=stream,
- timeout=timeout,
- verify=verify,
- cert=cert,
- proxies=proxies,
- )
-
- chunked = not (request.body is None or "Content-Length" in request.headers)
-
- if isinstance(timeout, tuple):
- try:
- connect, read = timeout
- timeout = TimeoutSauce(connect=connect, read=read)
- except ValueError:
- raise ValueError(
- f"Invalid timeout {timeout}. Pass a (connect, read) timeout tuple, "
- f"or a single float to set both timeouts to the same value."
- )
- elif isinstance(timeout, TimeoutSauce):
- pass
- else:
- timeout = TimeoutSauce(connect=timeout, read=timeout)
-
- try:
- resp = conn.urlopen(
- method=request.method,
- url=url,
- body=request.body,
- headers=request.headers,
- redirect=False,
- assert_same_host=False,
- preload_content=False,
- decode_content=False,
- retries=self.max_retries,
- timeout=timeout,
- chunked=chunked,
- )
-
- except (ProtocolError, OSError) as err:
- raise ConnectionError(err, request=request)
-
- except MaxRetryError as e:
- if isinstance(e.reason, ConnectTimeoutError):
- # TODO: Remove this in 3.0.0: see #2811
- if not isinstance(e.reason, NewConnectionError):
- raise ConnectTimeout(e, request=request)
-
- if isinstance(e.reason, ResponseError):
- raise RetryError(e, request=request)
-
- if isinstance(e.reason, _ProxyError):
- raise ProxyError(e, request=request)
-
- if isinstance(e.reason, _SSLError):
- # This branch is for urllib3 v1.22 and later.
- raise SSLError(e, request=request)
-
- > raise ConnectionError(e, request=request)
- E requests.exceptions.ConnectionError: HTTPConnectionPool(host='localhost', port=8080): Max retries exceeded with url: /demo/None (Caused by NewConnectionError(': Failed to establish a new connection: [Errno 111] Connection refused'))
-
- .pixi/envs/default/lib/python3.8/site-packages/requests/adapters.py:700: ConnectionError
-
- During handling of the above exception, another exception occurred:
-
- mock_home =
- mock_getuser =
- mock_getpass =
-
- @patch("slic.utils.elog.getpass")
- @patch("slic.utils.elog.getuser")
- @patch("slic.utils.elog.Path.home")
- def test_get_default_elog_with_path_home(mock_home, mock_getuser, mock_getpass):
- fake_user = "robot"
- fake_pw = "testpassword"
- mock_getuser.return_value = fake_user
- mock_getpass.return_value = fake_pw # fallback safety
- text = "This is a message3"
+ def test_get_default_elog_instance_with_wrong_password_and_real_check():
url = "http://localhost:8080/demo"
+ user = "robot"
+ wrong_password = "wrongpassword"
- tmp_home = Path("/tmp/fake_home_for_robot")
- tmp_home.mkdir(parents=True, exist_ok=True)
- pw_file = tmp_home / ".elog_psi"
- pw_file.write_text(fake_pw)
- mock_home.return_value = tmp_home
+ elog = Elog(url, user=user, password=wrong_password)
- try:
- elog = Elog("http://localhost:8080/demo")
- try:
- > elog.post(text)
+ with pytest.raises(LogbookAuthenticationError):
+ > elog.post("This should fail due to wrong password")
+ E Failed: DID NOT RAISE
- tests/test_utils_elog.py:100:
- _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
- slic/utils/elog.py:17: in post
- return self._log.post(*args, **kwargs)
- slic/utils/logbook.py:289: in post
- self._check_if_message_on_server(msg_id) # Raises exceptions if no message or no response from server
- _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
-
- self = , msg_id = None
- timeout = None
-
- def _check_if_message_on_server(self, msg_id, timeout=None):
- """Try to load page for specific message. If there is a html tag like | then there is no
- such message.
-
- :param msg_id: ID of message to be checked
- :params timeout: The value of timeout to be passed to the get request
- :return:
- """
-
- request_headers = dict()
- if self._user or self._password:
- request_headers['Cookie'] = self._make_user_and_pswd_cookie()
- try:
- response = requests.get(self._url + str(msg_id), headers=request_headers, allow_redirects=False,
- verify=False, timeout=timeout)
-
- # If there is no message code 200 will be returned (OK) and _validate_response will not recognise it
- # but there will be some error in the html code.
- resp_message, resp_headers, resp_msg_id = _validate_response(response)
- # If there is no message, code 200 will be returned (OK) but there will be some error indication in
- # the html code.
- if re.findall(' | .*? | ',
- resp_message.decode('utf-8', 'ignore'),
- flags=re.DOTALL):
- raise LogbookInvalidMessageID('Message with ID: ' + str(msg_id) + ' does not exist on logbook.')
-
- except requests.Timeout as e:
- # Catch here a timeout o the post request.
- # Raise the logbook exception and let the user handle it
- raise LogbookServerTimeout('{0} method cannot be completed because of a network timeout:\n' +
- '{1}'.format(sys._getframe().f_code.co_name, e))
-
- except requests.RequestException as e:
- > raise LogbookServerProblem('No response from the logbook server.\nDetails: ' + '{0}'.format(e))
- E slic.utils.logbook_exceptions.LogbookServerProblem: No response from the logbook server.
- E Details: HTTPConnectionPool(host='localhost', port=8080): Max retries exceeded with url: /demo/None (Caused by NewConnectionError(': Failed to establish a new connection: [Errno 111] Connection refused'))
-
- slic/utils/logbook.py:590: LogbookServerProblem
-
- During handling of the above exception, another exception occurred:
-
- mock_home =
- mock_getuser =
- mock_getpass =
-
- @patch("slic.utils.elog.getpass")
- @patch("slic.utils.elog.getuser")
- @patch("slic.utils.elog.Path.home")
- def test_get_default_elog_with_path_home(mock_home, mock_getuser, mock_getpass):
- fake_user = "robot"
- fake_pw = "testpassword"
- mock_getuser.return_value = fake_user
- mock_getpass.return_value = fake_pw # fallback safety
- text = "This is a message3"
- url = "http://localhost:8080/demo"
-
- tmp_home = Path("/tmp/fake_home_for_robot")
- tmp_home.mkdir(parents=True, exist_ok=True)
- pw_file = tmp_home / ".elog_psi"
- pw_file.write_text(fake_pw)
- mock_home.return_value = tmp_home
-
- try:
- elog = Elog("http://localhost:8080/demo")
- try:
- elog.post(text)
- except Exception as e:
- > pytest.fail(f"elog.open() raised an unexpected exception: {e}")
- E Failed: elog.open() raised an unexpected exception: No response from the logbook server.
- E Details: HTTPConnectionPool(host='localhost', port=8080): Max retries exceeded with url: /demo/None (Caused by NewConnectionError(': Failed to establish a new connection: [Errno 111] Connection refused'))
-
- tests/test_utils_elog.py:102: Failed
+ tests/test_utils_elog.py:46: Failed
```
**_*π Teardown phase*_**
@@ -6489,1619 +284,7 @@
**duration:**
```python
- 0.0002540573477745056
- ```
-
- **outcome:**
-
- ```python
- passed
- ```
-