Skip to content

Commit 4eaf688

Browse files
author
jm
committed
fix: address review - error vs miss on CLARIN getters, uuid-less creates
- get_items_from_collection: a failed request returned [], the consumers' end-of-collection signal, silently truncating their paging loops. It now goes through fetch_resource and returns None (last_err set on a non-200). - get_user_by_email: DSpace answers an unknown email with 204 No Content. 204 is the clean miss (None, no log, no last_err); 404 and any other non-user answer return None but log ERROR and keep last_err; the raw email is never logged. Transport errors propagate instead of looking like a miss. - create_group / create_user / create_submit_group: return None when the parsed body has no uuid, since Group(None)/User(None) are now accepted and would be truthy, uuid-less objects. - get_owningCollection: an unparseable 200 records last_err; a transport error returns None (logged) so one timeout does not abort a prefetch loop. - add_member: correct the comment - DSpace resolves the posted eperson by its trailing UUID, the bare /epersons/ href was not broken.
1 parent 88c4c56 commit 4eaf688

4 files changed

Lines changed: 138 additions & 55 deletions

File tree

‎dspace_rest_client/client.py‎

Lines changed: 45 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,17 +1165,19 @@ def get_owningCollection(self, item_uuid):
11651165
url = f'{self.API_ENDPOINT}/core/items/{item_uuid}/owningCollection'
11661166
try:
11671167
r = self.api_get(url, None, None)
1168-
# On a non-200, verify_response records self._last_err and returns
1169-
# False - return None here (not an empty, truthy Collection) so the
1170-
# caller's `owning_col is None and last_err.status_code == 401`
1171-
# reauth path (src/repo/_audit.py) actually fires.
1172-
if not self.verify_response(r, f"item:{item_uuid}", True):
1173-
return None
1174-
r_json = parse_json(response=r)
1175-
return Collection(r_json)
1176-
except ValueError:
1177-
_logger.error(f'Invalid owningCollection for UUID: {item_uuid}')
1168+
except requests.RequestException as e:
1169+
# one timeout must not abort a caller's prefetch loop; there is no
1170+
# response to record, so last_err stays None
1171+
_logger.error(f'Error fetching owningCollection [item:{item_uuid}]: {e}')
1172+
return None
1173+
# Return None (not an empty, truthy Collection) with last_err set, so the
1174+
# caller's `owning_col is None and last_err.status_code == 401` reauth
1175+
# path (src/repo/_audit.py) fires. verify_response records last_err only
1176+
# for a non-200, so an unparseable 200 is recorded here.
1177+
if not self.verify_response(r, f"item:{item_uuid}", True):
1178+
self._last_err = r
11781179
return None
1180+
return Collection(parse_json(response=r))
11791181

11801182
def create_item(self, parent, item):
11811183
"""
@@ -1283,7 +1285,11 @@ def create_user(self, user, token=None):
12831285
params = None
12841286
if token is not None:
12851287
params = {'token': token}
1286-
return User(api_resource=parse_json(self.create_dso(url, params=params, data=data)))
1288+
r_json = parse_json(self.create_dso(url, params=params, data=data))
1289+
# User(None) / User(error body) would be truthy and uuid-less
1290+
if not r_json or not r_json.get('uuid'):
1291+
return None
1292+
return User(api_resource=r_json)
12871293

12881294
def delete_user(self, user):
12891295
if not isinstance(user, User):
@@ -1322,7 +1328,11 @@ def create_group(self, group):
13221328
data = group.as_dict()
13231329
# TODO: Validation. Note, at least here I will just allow a dict instead of the pointless cast<->cast
13241330
# that you see for other DSO types - still figuring out the best way
1325-
return Group(api_resource=parse_json(self.create_dso(url, params=None, data=data)))
1331+
r_json = parse_json(self.create_dso(url, params=None, data=data))
1332+
# Group(None) / Group(error body) would be truthy and uuid-less
1333+
if not r_json or not r_json.get('uuid'):
1334+
return None
1335+
return Group(api_resource=r_json)
13261336

13271337
def create_submit_group(self, collection):
13281338
"""
@@ -1335,7 +1345,7 @@ def create_submit_group(self, collection):
13351345
# None instead so a caller's `if not group` guard fires cleanly rather
13361346
# than passing a uuid-less Group into add_member()
13371347
j = parse_json(r)
1338-
if j:
1348+
if j and j.get('uuid'):
13391349
return Group(j)
13401350
return None
13411351

@@ -1359,8 +1369,8 @@ def add_member(self, group, eperson):
13591369
return False
13601370

13611371
url = f'{self.API_ENDPOINT}/eperson/groups/{group.uuid}/epersons'
1362-
# canonical eperson href is /eperson/epersons/{uuid}; a bare /epersons/
1363-
# path does not resolve and DSpace rejects the uri-list with a 422
1372+
# canonical eperson href; DSpace resolves the uri-list by its trailing
1373+
# UUID only, so the former bare /epersons/{uuid} worked too
13641374
eperson_uri = f'{self.API_ENDPOINT}/eperson/epersons/{eperson.uuid}'
13651375
r = self.api_post_uri(url, params=None, uri_list=eperson_uri)
13661376
if r.status_code == 204:
@@ -1429,11 +1439,12 @@ def get_items_from_collection(self, collection_id, page=0, size=1000):
14291439
url = f'{self.API_ENDPOINT}/discover/search/objects?sort=dc.date.accessioned,DESC&page={page}&size={size}&scope={collection_id}&dsoType=ITEM&embed=thumbnail'
14301440

14311441
items = list()
1432-
r = self.api_get(url)
1433-
r_json = parse_json(r)
1434-
# a failed request parses to None; return the empty list rather than
1435-
# crashing on `'_embedded' in None`
1436-
if r_json and '_embedded' in r_json:
1442+
r_json = self.fetch_resource(url)
1443+
# [] is the callers' end-of-collection signal: a failed page must be
1444+
# None (with last_err on a non-200) or their paging loop silently stops
1445+
if r_json is None:
1446+
return None
1447+
if '_embedded' in r_json:
14371448
if 'searchResult' in r_json['_embedded']:
14381449
if '_embedded' in r_json['_embedded']['searchResult']:
14391450
for item_resource in r_json['_embedded']['searchResult']['_embedded']['objects']:
@@ -1568,22 +1579,18 @@ def get_user_by_email(self, email):
15681579
"""
15691580
url = f'{self.API_ENDPOINT}/eperson/epersons/search/byEmail'
15701581
params = {'email': email}
1571-
try:
1572-
response = self.api_get(url, params=params)
1573-
# a miss returns 404 (with a JSON error body): building a User from
1574-
# that yields a truthy, uuid-less object that passes `if user:` and
1575-
# fails much later. Treat any non-200 as "no such user" -> None, but
1576-
# record last_err so callers keep the HTTP-error diagnostics, and
1577-
# don't log a plain 404 miss at error level (nor the raw email).
1578-
if response.status_code != 200:
1579-
self._last_err = response
1580-
if response.status_code != 404:
1581-
_logger.error(f"Error retrieving user by email: HTTP {response.status_code}")
1582-
return None
1583-
user_data = parse_json(response)
1584-
if not user_data:
1585-
return None
1586-
return User(user_data)
1587-
except Exception as e:
1588-
_logger.error(f"Error retrieving user by email {email}: {e}")
1582+
# a transport error (eg. a timeout) propagates: returning None would
1583+
# make it indistinguishable from "no such user"
1584+
response = self.api_get(url, params=params)
1585+
# DSpace answers an unknown email with 204 No Content: a clean miss
1586+
if response.status_code == 204:
1587+
return None
1588+
# anything else that is not a user (404 = misconfigured endpoint,
1589+
# 401/403/5xx, an unparseable or uuid-less body) is an error: None, but
1590+
# logged and kept in last_err. The raw email is never logged.
1591+
user_data = parse_json(response) if response.status_code == 200 else None
1592+
if not user_data or not user_data.get('uuid'):
1593+
self._last_err = response
1594+
_logger.error(f"Error retrieving user by email: HTTP {response.status_code}")
15891595
return None
1596+
return User(user_data)

‎tests/test_clarin_read.py‎

Lines changed: 52 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@
1414
import unittest
1515

1616
import pytest
17+
import requests
1718
import requests_mock
1819

1920
import _helpers # noqa: F401
2021
from _helpers import (
21-
make_client, sent_params, embedded, item_json, bundle_json, raw_policy_json,
22+
make_client, sent_params, embedded, no_warnings_logged, item_json, bundle_json, raw_policy_json,
2223
user_json, clarin_allowance_json, search_envelope,
2324
API, ITEM_UUID, COLLECTION_UUID, BUNDLE_UUID, EPERSON_UUID)
2425
from dspace_rest_client.models import Bundle, Item, Collection, User
@@ -131,12 +132,15 @@ def test_sends_scope_dsotype_sort_embed(self):
131132
self.assertEqual(qs["embed"], ["thumbnail"])
132133

133134
@pytest.mark.dtq_only
134-
def test_non_200_returns_empty(self):
135-
"""D6: a failed request must yield [], not a NoneType subscript crash."""
135+
def test_non_200_returns_none_and_records_last_err(self):
136+
"""D6: a failed page must not look like an empty one - [] is the
137+
consumers' end-of-collection signal, so an error would silently
138+
truncate their paging loop. None + last_err tells the two apart."""
136139
c = make_client()
137140
with requests_mock.Mocker() as m:
138141
m.get(self.URL, status_code=500, text="boom")
139-
self.assertEqual(c.get_items_from_collection(COLLECTION_UUID), [])
142+
self.assertIsNone(c.get_items_from_collection(COLLECTION_UUID))
143+
self.assertEqual(c.last_err.status_code, 500)
140144

141145

142146
class TestGetItemByHandle(unittest.TestCase):
@@ -187,18 +191,38 @@ def test_returns_user_with_uuid_and_email(self):
187191
self.assertEqual(sent_params(m.last_request)["email"], ["a@b.c"])
188192

189193
@pytest.mark.dtq_only
190-
def test_404_returns_none(self):
191-
"""D4: a miss (404) must be falsy, not a truthy uuid-less User that
192-
slips past the consumer's `if user:` guard."""
194+
def test_204_no_content_is_a_clean_miss(self):
195+
"""D4: DSpace answers an unknown email with 204 No Content. That is a
196+
clean miss - None, nothing logged, no last_err - not a truthy
197+
uuid-less User that slips past the consumer's `if user:` guard."""
198+
c = make_client()
199+
with requests_mock.Mocker() as m, no_warnings_logged(self, "dspace.client"):
200+
m.get(self.URL, status_code=204)
201+
self.assertIsNone(c.get_user_by_email("nobody@nowhere"))
202+
self.assertIsNone(c.last_err)
203+
204+
@pytest.mark.dtq_only
205+
def test_error_status_returns_none_logs_error_and_records_last_err(self):
206+
"""A 404 means a misconfigured endpoint, 401/403/5xx a real failure:
207+
None, but logged at ERROR (without the raw email) and kept in last_err
208+
so it is distinguishable from the clean 204 miss."""
209+
for status in (404, 401, 403, 500):
210+
with self.subTest(status=status):
211+
c = make_client()
212+
with requests_mock.Mocker() as m, self.assertLogs("dspace.client", "ERROR") as logs:
213+
m.get(self.URL, status_code=status, json={"timestamp": "now"})
214+
self.assertIsNone(c.get_user_by_email("nobody@nowhere"))
215+
self.assertEqual(c.last_err.status_code, status)
216+
self.assertNotIn("nobody@nowhere", " ".join(logs.output))
217+
218+
@pytest.mark.dtq_only
219+
def test_transport_error_is_not_a_clean_miss(self):
220+
"""A timeout must not return the clean-miss None: it propagates."""
193221
c = make_client()
194222
with requests_mock.Mocker() as m:
195-
m.get(self.URL, status_code=404, json={"timestamp": "now"})
196-
u = c.get_user_by_email("nobody@nowhere")
197-
self.assertIsNone(u)
198-
self.assertFalse(bool(u))
199-
# the failing response is retained for callers, even for a 404 miss
200-
self.assertIsNotNone(c.last_err)
201-
self.assertEqual(c.last_err.status_code, 404)
223+
m.get(self.URL, exc=requests.exceptions.ConnectTimeout)
224+
with self.assertRaises(requests.exceptions.ConnectTimeout):
225+
c.get_user_by_email("nobody@nowhere")
202226

203227

204228
class TestGetClarinAllowances(unittest.TestCase):
@@ -258,6 +282,20 @@ def test_non_200_returns_none_and_sets_last_err(self):
258282
self.assertIsNotNone(c.last_err)
259283
self.assertEqual(c.last_err.status_code, 401)
260284

285+
def test_unparseable_200_returns_none_and_sets_last_err(self):
286+
c = make_client()
287+
with requests_mock.Mocker() as m:
288+
m.get(self.URL, status_code=200, text="<html>proxy page</html>")
289+
self.assertIsNone(c.get_owningCollection(ITEM_UUID))
290+
self.assertEqual(c.last_err.status_code, 200)
291+
292+
def test_transport_error_returns_none_without_raising(self):
293+
"""One timeout must not abort the consumer's prefetch loop."""
294+
c = make_client()
295+
with requests_mock.Mocker() as m, self.assertLogs("dspace.client", "ERROR"):
296+
m.get(self.URL, exc=requests.exceptions.ReadTimeout)
297+
self.assertIsNone(c.get_owningCollection(ITEM_UUID))
298+
261299

262300
if __name__ == "__main__":
263301
unittest.main()

‎tests/test_clarin_usage_contract.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ def test_unknown_email_stops_before_add_member(self):
136136
User and fail deep inside add_member."""
137137
c = make_client()
138138
with requests_mock.Mocker() as m:
139-
m.get(f"{API}/eperson/epersons/search/byEmail",
140-
status_code=404, json={"timestamp": "now"})
139+
# DSpace answers an unknown email with 204 No Content
140+
m.get(f"{API}/eperson/epersons/search/byEmail", status_code=204)
141141

142142
user = c.get_user_by_email("ghost@nowhere")
143143
self.assertIsNone(user)

‎tests/test_clarin_write.py‎

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
import _helpers # noqa: F401
1515
from _helpers import (
16-
make_client, sent_params, item_json, embedded,
16+
make_client, sent_params, item_json, embedded, group_json, user_json,
1717
API, ITEM_UUID, BITSTREAM_UUID, COLLECTION_UUID, EPERSON_UUID, GROUP_UUID)
1818
from dspace_rest_client.models import Group, User, Collection, Item, DSpaceObject
1919

@@ -156,6 +156,44 @@ def test_empty_body_returns_none(self):
156156
self.assertIsNone(c.create_submit_group(self._collection()))
157157

158158

159+
@pytest.mark.dtq_only
160+
def test_201_without_uuid_returns_none(self):
161+
"""A parsed body without a uuid is not a usable group either."""
162+
c = make_client()
163+
url = f"{API}/core/collections/{COLLECTION_UUID}/submittersGroup"
164+
with requests_mock.Mocker() as m:
165+
m.post(url, status_code=201, json={"type": "group"})
166+
self.assertIsNone(c.create_submit_group(self._collection()))
167+
168+
169+
class TestCreateGroupAndUser(unittest.TestCase):
170+
"""Group(None)/User(None) are accepted, so a failed create must not hand
171+
back a truthy uuid-less object that passes an `if group:` guard."""
172+
173+
CASES = (("create_group", "eperson/groups", Group, group_json),
174+
("create_user", "eperson/epersons", User, user_json))
175+
176+
def test_201_returns_typed_object(self):
177+
for method, path, cls, body in self.CASES:
178+
with self.subTest(method=method):
179+
c = make_client()
180+
with requests_mock.Mocker() as m:
181+
m.post(f"{API}/{path}", status_code=201, json=body())
182+
obj = getattr(c, method)({"name": "x"})
183+
self.assertIsInstance(obj, cls)
184+
self.assertIsNotNone(obj.uuid)
185+
186+
@pytest.mark.dtq_only
187+
def test_failed_create_returns_none(self):
188+
for method, path, _cls, _body in self.CASES:
189+
with self.subTest(method=method):
190+
c = make_client()
191+
with requests_mock.Mocker() as m:
192+
m.post(f"{API}/{path}", status_code=422,
193+
json={"message": "Unprocessable"})
194+
self.assertIsNone(getattr(c, method)({"name": "x"}))
195+
196+
159197
class TestAddMember(unittest.TestCase):
160198
"""Mirrors dspace-rest-test - add_member(group, eperson)."""
161199

0 commit comments

Comments
 (0)