Repository: CS-SI/eodag
Pull request #794 (2023-08-24 15:54:29)

fix: 500 http error for server internal auth errors
Send 500 http "Internal server error" to the client when capturing server side `AuthenticationError`

github-actions[bot]: ## Test Results
       4 files  ±0         4 suites  ±0   5m 35s [:stopwatch:](https://github.com/EnricoMi/publish-unit-test-result-action/blob/v2.9.0/README.md#the-symbols "duration of all tests") +10s
   403 tests ±0     401 [:heavy_check_mark:](https://github.com/EnricoMi/publish-unit-test-result-action/blob/v2.9.0/README.md#the-symbols "passed tests") ±0    2 [:zzz:](https://github.com/EnricoMi/publish-unit-test-result-action/blob/v2.9.0/README.md#the-symbols "skipped / disabled tests") ±0  0 [:x:](https://github.com/EnricoMi/publish-unit-test-result-action/blob/v2.9.0/README.md#the-symbols "failed tests") ±0 
1 612 runs  ±0  1 552 [:heavy_check_mark:](https://github.com/EnricoMi/publish-unit-test-result-action/blob/v2.9.0/README.md#the-symbols "passed tests") ±0  60 [:zzz:](https://github.com/EnricoMi/publish-unit-test-result-action/blob/v2.9.0/README.md#the-symbols "skipped / disabled tests") ±0  0 [:x:](https://github.com/EnricoMi/publish-unit-test-result-action/blob/v2.9.0/README.md#the-symbols "failed tests") ±0 

Results for commit dd37c9ed. ± Comparison against base commit caff0bc1.

[test-results]:data:application/gzip;base64,H4sIAPR+52QC/03Myw7CIBCF4VdpWLsApkDryxgKTELsxXBZGd9dxJZ2+X+TOW+CfnaR3Lv+1pGYfWphc9DJb2tJAFGgnFI9UjjqEbMxldhJT/8qxBug9nMB2sCFsIVdQl5/m0wyvtexyYQ46b8pj4/rZu3rpNmWxacSxFpQZnQWJwpUWhwHxLGXwkgrFAw9U5MBjZx8vpxjYDUIAQAA

github-actions[bot]: <strong>Code Coverage (Windows)</strong>

| File | Coverage |   |
| - | :-: | :-: |
| **All files** | `76%` | :white_check_mark: |

_Minimum allowed coverage is `70%`_

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

Patch:
diff --git a/eodag/rest/server.py b/eodag/rest/server.py
index 0c9ecbda..0b5817b5 100755
--- a/eodag/rest/server.py
+++ b/eodag/rest/server.py
@@ -233,12 +233,13 @@ async def handle_resource_not_found(request: Request, error):
 
 @app.exception_handler(AuthenticationError)
 async def handle_auth_error(request: Request, error):
-    """Unauthorized [401] errors handle"""
+    """AuthenticationError should be sent as internal server error to the client"""
+    logger.error(f"{type(error).__name__}: {str(error)}")
     return await default_exception_handler(
         request,
         HTTPException(
-            status_code=401,
-            detail=f"{type(error).__name__}: {str(error)}",
+            status_code=500,
+            detail="Internal server error: please contact the administrator",
         ),
     )
 


Test patch:
diff --git a/tests/units/test_http_server.py b/tests/units/test_http_server.py
index d1a8a9aa..b53b46af 100644
--- a/tests/units/test_http_server.py
+++ b/tests/units/test_http_server.py
@@ -344,16 +344,19 @@ class RequestTestCase(unittest.TestCase):
         side_effect=AuthenticationError("you are no authorized"),
     )
     def test_auth_error(self, mock_search):
-        """A request to eodag server raising a Authentication error must return a 401 HTTP error code"""
-        response = self.app.get(
-            f"search?collections={self.tested_product_type}", follow_redirects=True
-        )
-        response_content = json.loads(response.content.decode("utf-8"))
+        """A request to eodag server raising a Authentication error must return a 500 HTTP error code"""
 
-        self.assertEqual(401, response.status_code)
-        self.assertIn("description", response_content)
-        self.assertIn("AuthenticationError", response_content["description"])
-        self.assertIn("you are no authorized", response_content["description"])
+        with self.assertLogs(level="ERROR") as cm_logs:
+            response = self.app.get(
+                f"search?collections={self.tested_product_type}", follow_redirects=True
+            )
+            response_content = json.loads(response.content.decode("utf-8"))
+
+            self.assertIn("description", response_content)
+            self.assertIn("AuthenticationError", str(cm_logs.output))
+            self.assertIn("you are no authorized", str(cm_logs.output))
+
+        self.assertEqual(500, response.status_code)
 
     def test_filter(self):
         """latestIntersect filter should only keep the latest products once search area is fully covered"""