diff --git a/tests/test_api.py b/tests/test_api.py index 6e591a9e..a3b2381e 100755 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -450,21 +450,23 @@ def test_delegated_role_class(self): with self.assertRaises(ValueError): DelegatedRole.from_dict(role.copy()) - # Test creating DelegatedRole only with "path_hash_prefixes" + # Test creating DelegatedRole only with "path_hash_prefixes" (an empty one) del role["paths"] - DelegatedRole.from_dict(role.copy()) - role["paths"] = "foo" + role["path_hash_prefixes"] = [] + role_obj = DelegatedRole.from_dict(role.copy()) + self.assertEqual(role_obj.to_dict(), role) - # Test creating DelegatedRole only with "paths" + # Test creating DelegatedRole only with "paths" (now an empty one) del role["path_hash_prefixes"] - DelegatedRole.from_dict(role.copy()) - role["path_hash_prefixes"] = "foo" + role["paths"] = [] + role_obj = DelegatedRole.from_dict(role.copy()) + self.assertEqual(role_obj.to_dict(), role) # Test creating DelegatedRole without "paths" and # "path_hash_prefixes" set del role["paths"] - del role["path_hash_prefixes"] - DelegatedRole.from_dict(role) + role_obj = DelegatedRole.from_dict(role.copy()) + self.assertEqual(role_obj.to_dict(), role) def test_delegation_class(self): diff --git a/tuf/api/metadata.py b/tuf/api/metadata.py index 361630ef..6cb654b9 100644 --- a/tuf/api/metadata.py +++ b/tuf/api/metadata.py @@ -770,7 +770,7 @@ def __init__( super().__init__(keyids, threshold, unrecognized_fields) self.name = name self.terminating = terminating - if paths and path_hash_prefixes: + if paths is not None and path_hash_prefixes is not None: raise ValueError( "Only one of the attributes 'paths' and" "'path_hash_prefixes' can be set!" @@ -806,9 +806,9 @@ def to_dict(self) -> Dict[str, Any]: "terminating": self.terminating, **base_role_dict, } - if self.paths: + if self.paths is not None: res_dict["paths"] = self.paths - elif self.path_hash_prefixes: + elif self.path_hash_prefixes is not None: res_dict["path_hash_prefixes"] = self.path_hash_prefixes return res_dict