Repository: genericclient/genericclient-requests

Issue:
Does not handle response status code 401
When a user sends a GET request to a DRF server using invalid base authentication email/password values, a response status code of 401 is returned. The client does not raise an exception when this happens, which results in a None resource being returned. The client should raise NotAuthenticatedError for both 401 and 403 status codes. See https://www.django-rest-framework.org/api-guide/authentication/

Discussion:

Patch:
diff --git a/genericclient/__init__.py b/genericclient/__init__.py
index fc0d66a..89af323 100644
--- a/genericclient/__init__.py
+++ b/genericclient/__init__.py
@@ -24,7 +24,7 @@ class Endpoint(BaseEndpoint):
             data=self.api.hydrate_data(resp),
         )
 
-        if response.status_code == 403:
+        if response.status_code in (401, 403):
             if self.api.session.auth:
                 msg = "Failed request to `{}`. Cannot authenticate user `{}` on the API.".format(
                     url, self.api.session.auth[0],


Test patch:
diff --git a/tests/test_auth.py b/tests/test_auth.py
index c7a99cb..79c4ce9 100644
--- a/tests/test_auth.py
+++ b/tests/test_auth.py
@@ -8,6 +8,15 @@ MOCK_API_URL = 'http://dummy.org'
 
 
 class AuthClientTestCase(TestCase):
+    def test_401(self):
+        generic_client = GenericClient(
+            url=MOCK_API_URL,
+        )
+        with responses.RequestsMock() as rsps:
+            rsps.add(responses.GET, MOCK_API_URL + '/users', status=401)
+            with self.assertRaises(generic_client.NotAuthenticatedError):
+                generic_client.users.all()
+
     def test_403(self):
         generic_client = GenericClient(
             url=MOCK_API_URL,
@@ -17,7 +26,6 @@ class AuthClientTestCase(TestCase):
             with self.assertRaises(generic_client.NotAuthenticatedError):
                 generic_client.users.all()
 
-
     def test_403_auth(self):
         generic_client = GenericClient(
             url=MOCK_API_URL,
diff --git a/tests/test_endpoint.py b/tests/test_endpoint.py
index 848e0b5..d035401 100644
--- a/tests/test_endpoint.py
+++ b/tests/test_endpoint.py
@@ -126,6 +126,12 @@ class EndpointTestCase(TestCase):
             self.assertRaises(generic_client.ResourceNotFound, generic_client.users.get, uuid=9999)
 
     def test_unauthenticated(self):
+        with responses.RequestsMock() as rsps:
+            rsps.add(responses.GET, MOCK_API_URL + '/users', status=401)
+
+            with self.assertRaises(generic_client.NotAuthenticatedError):
+                generic_client.users.all()
+
         with responses.RequestsMock() as rsps:
             rsps.add(responses.GET, MOCK_API_URL + '/users', status=403)