Skip to content

Commit 2a95b6f

Browse files
jr-rkclaude
andcommitted
fix: harden CLARIN shared surface + dtq owningCollection reauth
Characterization-tested fixes (test written to the intended behaviour first, then the method corrected) to methods the main-lineage consumers call: get_bundle_by_name / get_items_from_collection / get_resource_policy non-200 or empty body -> clean None/[] instead of a NoneType subscript crash; get_resource_policy also records last_err so a caller can tell an HTTP error from a genuine empty result. get_user_by_email 404/any non-200 -> None (+ last_err), not a truthy uuid-less User that slips past a consumer `if user:` guard. create_submit_group 201 with an empty body -> None, not Group(None). add_member post the canonical /eperson/epersons/{uuid} href; the bare /epersons/ path 404s and DSpace rejects the uri-list. Group(None) / User(None) empty object, not a TypeError. get_owningCollection (dtq-only) non-200 -> None + last_err, so ISstag's src/repo/_audit.py `owning_col is None and last_err.status_code == 401` reauth retry stops being dead code. Stacked on the test/CI PR, which supplies the _helpers builders, pytest.ini markers and the differential-contract job these tests run under. Merge that PR first. The failure-mode assertions are marked dtq_only (deselected on the main leg): main carries the same bugs, but propagating these fixes to main is deliberately out of scope here. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 0c7411b commit 2a95b6f

7 files changed

Lines changed: 196 additions & 8 deletions

File tree

‎dspace_rest_client/client.py‎

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,7 +1096,12 @@ def get_owningCollection(self, item_uuid):
10961096
url = f'{self.API_ENDPOINT}/core/items/{item_uuid}/owningCollection'
10971097
try:
10981098
r = self.api_get(url, None, None)
1099-
self.verify_response(r, f"item:{item_uuid}", True)
1099+
# On a non-200, verify_response records self._last_err and returns
1100+
# False - return None here (not an empty, truthy Collection) so the
1101+
# caller's `owning_col is None and last_err.status_code == 401`
1102+
# reauth path (src/repo/_audit.py) actually fires.
1103+
if not self.verify_response(r, f"item:{item_uuid}", True):
1104+
return None
11001105
r_json = parse_json(response=r)
11011106
return Collection(r_json)
11021107
except ValueError:
@@ -1257,7 +1262,12 @@ def create_submit_group(self, collection):
12571262
url = f'{self.API_ENDPOINT}/core/collections/{collection.uuid}/submittersGroup'
12581263
r = self.api_post(url, json={}, params=None)
12591264
if r.status_code == 201:
1260-
return Group(parse_json(r))
1265+
# a 201 with an empty/invalid body would make Group(None) here; return
1266+
# None instead so a caller's `if not group` guard fires cleanly rather
1267+
# than passing a uuid-less Group into add_member()
1268+
j = parse_json(r)
1269+
if j:
1270+
return Group(j)
12611271
return None
12621272

12631273
def add_member(self, group, eperson):
@@ -1280,7 +1290,9 @@ def add_member(self, group, eperson):
12801290
return False
12811291

12821292
url = f'{self.API_ENDPOINT}/eperson/groups/{group.uuid}/epersons'
1283-
eperson_uri = f'{self.API_ENDPOINT}/epersons/{eperson.uuid}'
1293+
# canonical eperson href is /eperson/epersons/{uuid}; a bare /epersons/
1294+
# path does not resolve and DSpace rejects the uri-list with a 422
1295+
eperson_uri = f'{self.API_ENDPOINT}/eperson/epersons/{eperson.uuid}'
12841296
r = self.api_post_uri(url, params=None, uri_list=eperson_uri)
12851297
if r.status_code == 204:
12861298
return True
@@ -1350,7 +1362,9 @@ def get_items_from_collection(self, collection_id, page=0, size=1000):
13501362
items = list()
13511363
r = self.api_get(url)
13521364
r_json = parse_json(r)
1353-
if '_embedded' in r_json:
1365+
# a failed request parses to None; return the empty list rather than
1366+
# crashing on `'_embedded' in None`
1367+
if r_json and '_embedded' in r_json:
13541368
if 'searchResult' in r_json['_embedded']:
13551369
if '_embedded' in r_json['_embedded']['searchResult']:
13561370
for item_resource in r_json['_embedded']['searchResult']['_embedded']['objects']:
@@ -1367,7 +1381,9 @@ def get_bundle_by_name(self, name, item_uuid):
13671381
"""
13681382
url = f'{self.API_ENDPOINT}/core/items/{item_uuid}/bundles'
13691383
r_json = self.fetch_resource(url, params=None)
1370-
if '_embedded' in r_json:
1384+
# fetch_resource returns None on any non-200 (records self._last_err);
1385+
# guard so a failed lookup is a clean None, not a NoneType subscript crash
1386+
if r_json and '_embedded' in r_json:
13711387
if 'bundles' in r_json['_embedded']:
13721388
for bundle in r_json['_embedded']['bundles']:
13731389
if bundle['name'] == name:
@@ -1380,10 +1396,20 @@ def get_resource_policy(self, bundle_uuid):
13801396
"""
13811397
url = f'{self.API_ENDPOINT}/authz/resourcepolicies/search/resource?uuid={bundle_uuid}&embed=eperson&embed=group'
13821398
r = self.api_get(url)
1399+
# record the failing response so a caller can tell an HTTP error apart
1400+
# from a genuine "no policy" (empty list) result - both return None
1401+
if r.status_code != 200:
1402+
self._last_err = r
1403+
_logger.error(f'Error fetching resource policy [{bundle_uuid}]: {r.status_code}')
1404+
return None
13831405
r_json = parse_json(r)
1384-
if '_embedded' in r_json:
1385-
if 'resourcepolicies' in r_json['_embedded']:
1386-
return r_json['_embedded']['resourcepolicies'][0]
1406+
# guard against an unparseable body and against an empty policy list -
1407+
# both are a clean None, not a TypeError/IndexError
1408+
if r_json and '_embedded' in r_json and 'resourcepolicies' in r_json['_embedded']:
1409+
policies = r_json['_embedded']['resourcepolicies']
1410+
if policies:
1411+
return policies[0]
1412+
return None
13871413

13881414
def create_resource_policy(self, resource_uuid, data, group_uuid=None, eperson_uuid=None):
13891415
"""
@@ -1475,7 +1501,19 @@ def get_user_by_email(self, email):
14751501
params = {'email': email}
14761502
try:
14771503
response = self.api_get(url, params=params)
1504+
# a miss returns 404 (with a JSON error body): building a User from
1505+
# that yields a truthy, uuid-less object that passes `if user:` and
1506+
# fails much later. Treat any non-200 as "no such user" -> None, but
1507+
# record last_err so callers keep the HTTP-error diagnostics, and
1508+
# don't log a plain 404 miss at error level (nor the raw email).
1509+
if response.status_code != 200:
1510+
self._last_err = response
1511+
if response.status_code != 404:
1512+
_logger.error(f"Error retrieving user by email: HTTP {response.status_code}")
1513+
return None
14781514
user_data = parse_json(response)
1515+
if not user_data:
1516+
return None
14791517
return User(user_data)
14801518
except Exception as e:
14811519
_logger.error(f"Error retrieving user by email {email}: {e}")

‎dspace_rest_client/models.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,7 @@ def __init__(self, api_resource=None):
407407
@param api_resource: API result object to use as initial data
408408
"""
409409
super().__init__(api_resource)
410+
api_resource = api_resource or {}
410411
self.type = 'group'
411412
if 'name' in api_resource:
412413
self.name = api_resource['name']
@@ -442,6 +443,7 @@ def __init__(self, api_resource=None):
442443
@param api_resource: API result object to use as initial data
443444
"""
444445
super().__init__(api_resource)
446+
api_resource = api_resource or {}
445447
self.type = 'user'
446448
if 'name' in api_resource:
447449
self.name = api_resource['name']

‎tests/test_clarin_read.py‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,26 @@ def test_sends_uuid_and_both_embeds(self):
5252
self.assertEqual(qs["uuid"], [BUNDLE_UUID])
5353
self.assertEqual(sorted(qs["embed"]), ["eperson", "group"])
5454

55+
@pytest.mark.dtq_only
56+
def test_empty_list_returns_none(self):
57+
"""D2: an empty policy list must be a clean None, not an IndexError."""
58+
c = make_client()
59+
with requests_mock.Mocker() as m:
60+
m.get(self.URL, json=embedded("resourcepolicies", []))
61+
self.assertIsNone(c.get_resource_policy(BUNDLE_UUID))
62+
63+
@pytest.mark.dtq_only
64+
def test_non_200_returns_none_and_records_last_err(self):
65+
"""D3: a failed request must be None, not a NoneType subscript crash -
66+
and last_err is recorded so a caller can tell an HTTP error apart from a
67+
genuine empty result (both return None)."""
68+
c = make_client()
69+
with requests_mock.Mocker() as m:
70+
m.get(self.URL, status_code=500, text="upstream boom")
71+
self.assertIsNone(c.get_resource_policy(BUNDLE_UUID))
72+
self.assertIsNotNone(c.last_err)
73+
self.assertEqual(c.last_err.status_code, 500)
74+
5575

5676
class TestGetBundleByName(unittest.TestCase):
5777
"""Mirrors dspace-import-clarin - get_bundle_by_name('ORIGINAL', item)."""
@@ -74,6 +94,14 @@ def test_no_match_returns_none(self):
7494
m.get(self.URL, json=embedded("bundles", [bundle_json("b1", "LICENSE")]))
7595
self.assertIsNone(c.get_bundle_by_name("ORIGINAL", ITEM_UUID))
7696

97+
@pytest.mark.dtq_only
98+
def test_non_200_returns_none(self):
99+
"""D1: a failed lookup must be None, not a NoneType subscript crash."""
100+
c = make_client()
101+
with requests_mock.Mocker() as m:
102+
m.get(self.URL, status_code=500, text="boom")
103+
self.assertIsNone(c.get_bundle_by_name("ORIGINAL", ITEM_UUID))
104+
77105

78106
class TestGetItemsFromCollection(unittest.TestCase):
79107
"""Mirrors dspace-import-clarin - get_items_from_collection(collection)."""
@@ -102,6 +130,14 @@ def test_sends_scope_dsotype_sort_embed(self):
102130
self.assertEqual(qs["sort"], ["dc.date.accessioned,DESC"])
103131
self.assertEqual(qs["embed"], ["thumbnail"])
104132

133+
@pytest.mark.dtq_only
134+
def test_non_200_returns_empty(self):
135+
"""D6: a failed request must yield [], not a NoneType subscript crash."""
136+
c = make_client()
137+
with requests_mock.Mocker() as m:
138+
m.get(self.URL, status_code=500, text="boom")
139+
self.assertEqual(c.get_items_from_collection(COLLECTION_UUID), [])
140+
105141

106142
class TestGetItemByHandle(unittest.TestCase):
107143
"""Mirrors dspace-rest-test - get_item_by_handle(handle)."""
@@ -150,6 +186,20 @@ def test_returns_user_with_uuid_and_email(self):
150186
self.assertEqual((u.uuid, u.email), (EPERSON_UUID, "a@b.c"))
151187
self.assertEqual(sent_params(m.last_request)["email"], ["a@b.c"])
152188

189+
@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."""
193+
c = make_client()
194+
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)
202+
153203

154204
class TestGetClarinAllowances(unittest.TestCase):
155205
"""Mirrors dspace-rest-test - get_clarinlruallowances[_by_bitstream_and_user]."""
@@ -197,6 +247,17 @@ def test_returns_typed_collection(self):
197247
self.assertIsInstance(col, Collection)
198248
self.assertEqual(col.uuid, COLLECTION_UUID)
199249

250+
def test_non_200_returns_none_and_sets_last_err(self):
251+
"""D8: on 401 the method must return None (not an empty truthy
252+
Collection) and expose last_err, or _audit.py's reauth branch is dead."""
253+
c = make_client()
254+
with requests_mock.Mocker() as m:
255+
m.get(self.URL, status_code=401, json={"message": "Unauthorized"})
256+
col = c.get_owningCollection(ITEM_UUID)
257+
self.assertIsNone(col)
258+
self.assertIsNotNone(c.last_err)
259+
self.assertEqual(c.last_err.status_code, 401)
260+
200261

201262
if __name__ == "__main__":
202263
unittest.main()

‎tests/test_clarin_usage_contract.py‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,16 @@ def test_bundle_then_policy_dict_then_group_update(self):
5353
self.assertEqual(r.status_code, 200)
5454
self.assertEqual(m.last_request.method, "PUT")
5555

56+
@pytest.mark.dtq_only
57+
def test_missing_bundle_aborts_cleanly(self):
58+
"""D1: when the bundle lookup fails, get_bundle_by_name is None and the
59+
chain stops - it must not crash on a NoneType subscript."""
60+
c = make_client()
61+
with requests_mock.Mocker() as m:
62+
m.get(f"{API}/core/items/{ITEM_UUID}/bundles",
63+
status_code=500, text="boom")
64+
self.assertIsNone(c.get_bundle_by_name("ORIGINAL", ITEM_UUID))
65+
5666

5767
class TestImportClarinLicenseIngest(unittest.TestCase):
5868
"""Mirrors dspace-import-clarin - read a collection's items, then build the
@@ -119,6 +129,21 @@ def test_email_group_member(self):
119129

120130
self.assertTrue(c.add_member(group, user))
121131

132+
@pytest.mark.dtq_only
133+
def test_unknown_email_stops_before_add_member(self):
134+
"""D4: an unknown email must resolve to None so the consumer's
135+
`if user:` guard skips group creation - not proceed with a uuid-less
136+
User and fail deep inside add_member."""
137+
c = make_client()
138+
with requests_mock.Mocker() as m:
139+
m.get(f"{API}/eperson/epersons/search/byEmail",
140+
status_code=404, json={"timestamp": "now"})
141+
142+
user = c.get_user_by_email("ghost@nowhere")
143+
self.assertIsNone(user)
144+
# consumer guard: nothing past the lookup should have been requested
145+
self.assertEqual(len(m.request_history), 1)
146+
122147

123148
class TestRestTestBitstreamPolicyFlow(unittest.TestCase):
124149
"""Mirrors dspace-rest-test - resolve an item by handle, find its ORIGINAL

‎tests/test_clarin_write.py‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,16 @@ def test_non_201_returns_none(self):
145145
m.post(url, status_code=500, text="boom")
146146
self.assertIsNone(c.create_submit_group(self._collection()))
147147

148+
@pytest.mark.dtq_only
149+
def test_empty_body_returns_none(self):
150+
"""D5: a 201 with an empty body must be None, not a Group(None) crash /
151+
a uuid-less Group that would poison a following add_member call."""
152+
c = make_client()
153+
url = f"{API}/core/collections/{COLLECTION_UUID}/submittersGroup"
154+
with requests_mock.Mocker() as m:
155+
m.post(url, status_code=201, text="")
156+
self.assertIsNone(c.create_submit_group(self._collection()))
157+
148158

149159
class TestAddMember(unittest.TestCase):
150160
"""Mirrors dspace-rest-test - add_member(group, eperson)."""
@@ -155,6 +165,21 @@ def _group(self):
155165
def _user(self):
156166
return User({"uuid": EPERSON_UUID, "email": "a@b.c"})
157167

168+
@pytest.mark.dtq_only
169+
def test_posts_eperson_uri_and_returns_true_on_204(self):
170+
"""The eperson uri-list body must be the canonical /eperson/epersons/
171+
href. dtq_only: the fix (dropping the bare /epersons/ path) lands on
172+
dtq; main still emits the malformed URI, so this is deselected on the
173+
main leg of the differential-contract job."""
174+
c = make_client()
175+
url = f"{API}/eperson/groups/{GROUP_UUID}/epersons"
176+
with requests_mock.Mocker() as m:
177+
m.post(url, status_code=204)
178+
self.assertTrue(c.add_member(self._group(), self._user()))
179+
# canonical eperson href - /eperson/epersons/, not a bare /epersons/
180+
self.assertEqual(m.last_request.text,
181+
f"{API}/eperson/epersons/{EPERSON_UUID}")
182+
158183
def test_non_204_returns_false(self):
159184
c = make_client()
160185
url = f"{API}/eperson/groups/{GROUP_UUID}/epersons"

‎tests/test_models_clarin.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,13 @@ def test_fields_and_as_dict(self):
7979
self.assertEqual((g.uuid, g.name, g.permanent), (GROUP_UUID, "submitters", True))
8080
self.assertEqual(g.as_dict()["name"], "submitters")
8181

82+
@pytest.mark.dtq_only
83+
def test_from_none_does_not_crash(self):
84+
"""D7: Group(None) must yield an empty object, not a TypeError - the
85+
create_submit_group / parse-failure paths can hand it None."""
86+
g = Group(None)
87+
self.assertIsNone(g.name)
88+
8289

8390
class TestUser(unittest.TestCase):
8491
"""User is built by get_user_by_email / consumed by add_member."""
@@ -89,6 +96,12 @@ def test_fields_and_as_dict(self):
8996
self.assertTrue(u.canLogIn)
9097
self.assertEqual(u.as_dict()["email"], "a@b.c")
9198

99+
@pytest.mark.dtq_only
100+
def test_from_none_does_not_crash(self):
101+
"""D7: User(None) must yield an empty object, not a TypeError."""
102+
u = User(None)
103+
self.assertIsNone(u.email)
104+
92105

93106
if __name__ == "__main__":
94107
unittest.main()

‎tests/test_repo_usage_contract.py‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,5 +169,29 @@ def test_group_search_shape(self):
169169
self.assertEqual(groups[0]["uuid"], "anon-uuid")
170170

171171

172+
class TestOwningCollectionReauthContract(unittest.TestCase):
173+
"""Mirrors src/repo/_audit.py:105-111 - get_owningCollection, then the
174+
`owning_col is None and last_err.status_code == 401` reauth decision. If the
175+
method ever returns a truthy empty Collection on failure, that branch dies."""
176+
177+
def test_success_returns_collection(self):
178+
c = make_client()
179+
url = f"{API}/core/items/{ITEM_UUID}/owningCollection"
180+
with requests_mock.Mocker() as m:
181+
m.get(url, json={"uuid": "col-1", "name": "Coll", "type": "collection"})
182+
col = c.get_owningCollection(ITEM_UUID)
183+
self.assertEqual(col.uuid, "col-1")
184+
185+
def test_401_yields_none_and_last_err_drives_reauth(self):
186+
c = make_client()
187+
url = f"{API}/core/items/{ITEM_UUID}/owningCollection"
188+
with requests_mock.Mocker() as m:
189+
m.get(url, status_code=401, json={"message": "Unauthorized"})
190+
col = c.get_owningCollection(ITEM_UUID)
191+
# the exact predicate _audit.py evaluates
192+
self.assertTrue(col is None and c.last_err is not None
193+
and c.last_err.status_code == 401)
194+
195+
172196
if __name__ == "__main__":
173197
unittest.main()

0 commit comments

Comments
 (0)