Repository: CS-SI/eodag

Issue:
TokenAuth should raise AuthenticationError
In `TokenAuth`, catch `RequestException` the same way as it was done for https://github.com/CS-SI/eodag/pull/555 and redirect to an `AuthenticationError`

Discussion:
github-actions[bot]: ## Unit Test Results
    2 files  ±0      2 suites  ±0   10m 32s [:stopwatch:](https://github.com/EnricoMi/publish-unit-test-result-action/blob/v1.20/README.md#the-symbols "duration of all tests") - 1m 38s
332 tests +1  329 [:heavy_check_mark:](https://github.com/EnricoMi/publish-unit-test-result-action/blob/v1.20/README.md#the-symbols "passed tests") +1  3 [:zzz:](https://github.com/EnricoMi/publish-unit-test-result-action/blob/v1.20/README.md#the-symbols "skipped / disabled tests") ±0  0 [:x:](https://github.com/EnricoMi/publish-unit-test-result-action/blob/v1.20/README.md#the-symbols "failed tests") ±0 
664 runs  +2  658 [:heavy_check_mark:](https://github.com/EnricoMi/publish-unit-test-result-action/blob/v1.20/README.md#the-symbols "passed tests") +2  6 [:zzz:](https://github.com/EnricoMi/publish-unit-test-result-action/blob/v1.20/README.md#the-symbols "skipped / disabled tests") ±0  0 [:x:](https://github.com/EnricoMi/publish-unit-test-result-action/blob/v1.20/README.md#the-symbols "failed tests") ±0 

Results for commit 5341847d. ± Comparison against base commit c91cba77.

[test-results]:data:application/gzip;base64,H4sIACnSoWMC/03MSw6DIBSF4a0Yxh0AFy7azTTKIyFVaRBGTfdeaBUd/t9Jzps4P9uN3Dt+68iWfWphchyTD2tJhAplSnWEsx5b1roSH056+lelBm70cwHawMYY4i4xr/UTUexxXKLsm/wf8ejL4a+vfzosi08liATBeqGMFZJqbq1RCMoNVDpAwxTIiUmhJiCfL2RcS88FAQAA

github-actions[bot]: <strong> </strong>

| File | Coverage |   |
| - | :-: | :-: |
| **All files** | `85%` | :white_check_mark: |
| eodag/plugins/authentication/header.py | `84%` | :white_check_mark: |
| eodag/plugins/authentication/openid\_connect.py | `27%` | :x: |

<strong> </strong>

| File | Coverage |   |
| - | :-: | :-: |
| **All files** | `85%` | :white_check_mark: |
| eodag/plugins/authentication/header.py | `84%` | :white_check_mark: |
| eodag/plugins/authentication/openid\_connect.py | `27%` | :x: |
| eodag/plugins/authentication/header.py | `84%` | :white_check_mark: |
| eodag/plugins/authentication/openid\_connect.py | `26%` | :x: |

_Minimum allowed coverage is `70%`_

<p align="right">Generated by :monkey: cobertura-action against 5341847de450c2eed7637f905f36d1735b1547b3 </p>

Patch:
diff --git a/eodag/plugins/authentication/header.py b/eodag/plugins/authentication/header.py
index 48d27cf9..0913296d 100644
--- a/eodag/plugins/authentication/header.py
+++ b/eodag/plugins/authentication/header.py
@@ -22,7 +22,7 @@ from eodag.plugins.authentication import Authentication
 
 
 class HTTPHeaderAuth(Authentication):
-    """A Generic Authentication plugin.
+    """HTTPHeaderAuth Authentication plugin.
 
     This plugin enables implementation of custom HTTP authentication scheme (other than Basic, Digest, Token
     negotiation et al.) using HTTP headers.
@@ -65,7 +65,7 @@ class HTTPHeaderAuth(Authentication):
 
 
 class HeaderAuth(requests.auth.AuthBase):
-    """HeaderAuth authentication plugin"""
+    """HeaderAuth custom authentication class to be used with requests module"""
 
     def __init__(self, authentication_headers):
         self.auth_headers = authentication_headers
diff --git a/eodag/plugins/authentication/openid_connect.py b/eodag/plugins/authentication/openid_connect.py
index 73f53c8a..effbea6e 100644
--- a/eodag/plugins/authentication/openid_connect.py
+++ b/eodag/plugins/authentication/openid_connect.py
@@ -279,7 +279,7 @@ class OIDCAuthorizationCodeFlowAuth(Authentication):
 
 
 class CodeAuthorizedAuth(AuthBase):
-    """CodeAuthorizedAuth authentication plugin"""
+    """CodeAuthorizedAuth custom authentication class to be used with requests module"""
 
     def __init__(self, token, where, key=None):
         self.token = token
diff --git a/eodag/plugins/authentication/token.py b/eodag/plugins/authentication/token.py
index a9e8f1cd..9419f85c 100644
--- a/eodag/plugins/authentication/token.py
+++ b/eodag/plugins/authentication/token.py
@@ -21,7 +21,7 @@ from requests import RequestException
 
 from eodag.plugins.authentication.base import Authentication
 from eodag.utils import RequestsTokenAuth
-from eodag.utils.exceptions import MisconfiguredError
+from eodag.utils.exceptions import AuthenticationError, MisconfiguredError
 from eodag.utils.stac_reader import HTTP_REQ_TIMEOUT
 
 
@@ -49,18 +49,17 @@ class TokenAuth(Authentication):
         req_kwargs = (
             {"headers": self.config.headers} if hasattr(self.config, "headers") else {}
         )
-
-        # First get the token
-        response = requests.post(
-            self.config.auth_uri,
-            data=self.config.credentials,
-            timeout=HTTP_REQ_TIMEOUT,
-            **req_kwargs,
-        )
         try:
+            # First get the token
+            response = requests.post(
+                self.config.auth_uri,
+                data=self.config.credentials,
+                timeout=HTTP_REQ_TIMEOUT,
+                **req_kwargs,
+            )
             response.raise_for_status()
         except RequestException as e:
-            raise e
+            raise AuthenticationError(f"Could no get authentication token: {str(e)}")
         else:
             if getattr(self.config, "token_type", "text") == "json":
                 token = response.json()[self.config.token_key]


Test patch:
diff --git a/tests/units/test_auth_plugins.py b/tests/units/test_auth_plugins.py
index a1ad599b..60c093b1 100644
--- a/tests/units/test_auth_plugins.py
+++ b/tests/units/test_auth_plugins.py
@@ -20,9 +20,10 @@ import unittest
 from unittest import mock
 
 from requests.auth import AuthBase
+from requests.exceptions import RequestException
 
 from eodag.config import override_config_from_mapping
-from tests.context import MisconfiguredError, PluginManager
+from tests.context import AuthenticationError, MisconfiguredError, PluginManager
 
 
 class BaseAuthPluginTest(unittest.TestCase):
@@ -167,3 +168,18 @@ class TestAuthPluginTokenAuth(BaseAuthPluginTest):
         req = mock.Mock(headers={})
         auth(req)
         assert req.headers["Authorization"] == "Bearer this_is_test_token"
+
+    @mock.patch("eodag.plugins.authentication.token.requests.post", autospec=True)
+    def test_plugins_auth_tokenauth_request_error(self, mock_requests_post):
+        """TokenAuth.authenticate must raise an AuthenticationError if a request error occurs"""
+        auth_plugin = self.get_auth_plugin("provider_json_token_simple_url")
+
+        auth_plugin.config.credentials = {"foo": "bar"}
+
+        # mock token post request response
+        mock_requests_post.side_effect = RequestException
+
+        self.assertRaises(
+            AuthenticationError,
+            auth_plugin.authenticate,
+        )