Skip to content

Commit f80b4ca

Browse files
author
Martin Vrachev
committed
Clarifications and simplifications
Clarify explicitly that exactly one of "paths" and "path_hash_prefixes" must be set inside DelegatedRole. Also simplify the check for "paths" and "path_hash_prefixes". Finally, add a test case inside the "test_metadata_serialization.py" test file about wrong keyids type for "Role" serialization. Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
1 parent 0a92cb9 commit f80b4ca

2 files changed

Lines changed: 8 additions & 7 deletions

File tree

tests/test_metadata_serialization.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ def test_invalid_key_serialization(self, test_case_data: str) -> None:
176176
"no threshold": '{"keyids": ["keyid"]}',
177177
"no keyids": '{"threshold": 3}',
178178
"wrong threshold type": '{"keyids": ["keyid"], "threshold": "a"}',
179+
"wrong keyids type": '{"keyids": 1, "threshold": 3}',
179180
"threshold below 1": '{"keyids": ["keyid"], "threshold": 0}',
180181
"duplicate keyids": '{"keyids": ["keyid", "keyid"], "threshold": 3}',
181182
}

tuf/api/metadata.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1297,7 +1297,7 @@ class DelegatedRole(Role):
12971297
paths: Path patterns. See note above.
12981298
path_hash_prefixes: Hash prefixes. See note above.
12991299
unrecognized_fields: Dictionary of all attributes that are not managed
1300-
by TUF Metadata API
1300+
by TUF Metadata API.
13011301
13021302
Raises:
13031303
ValueError: Invalid arguments.
@@ -1316,11 +1316,11 @@ def __init__(
13161316
super().__init__(keyids, threshold, unrecognized_fields)
13171317
self.name = name
13181318
self.terminating = terminating
1319-
if paths is not None and path_hash_prefixes is not None:
1320-
raise ValueError("Either paths or path_hash_prefixes can be set")
1321-
1322-
if paths is None and path_hash_prefixes is None:
1323-
raise ValueError("One of paths or path_hash_prefixes must be set")
1319+
exclusive_vars = [paths, path_hash_prefixes]
1320+
if sum(1 for var in exclusive_vars if var is not None) != 1:
1321+
raise ValueError(
1322+
"Only one of (paths, path_hash_prefixes) must be set"
1323+
)
13241324

13251325
if paths is not None and any(not isinstance(p, str) for p in paths):
13261326
raise ValueError("Paths must be strings")
@@ -1349,7 +1349,7 @@ def from_dict(cls, role_dict: Dict[str, Any]) -> "DelegatedRole":
13491349
"""Creates ``DelegatedRole`` object from its json/dict representation.
13501350
13511351
Raises:
1352-
ValueError, KeyError: Invalid arguments.
1352+
ValueError, KeyError, TypeError: Invalid arguments.
13531353
"""
13541354
name = role_dict.pop("name")
13551355
keyids = role_dict.pop("keyids")

0 commit comments

Comments
 (0)