Skip to content

Commit f98f4f7

Browse files
committed
Add Delegation/DelegatednRole specific tests
Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
1 parent 8e4d3b9 commit f98f4f7

File tree

2 files changed

+76
-6
lines changed

2 files changed

+76
-6
lines changed

tests/test_api.py

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@
2828
Timestamp,
2929
Targets,
3030
Key,
31-
Role
31+
Role,
32+
Delegations,
33+
DelegatedRole,
3234
)
3335

3436
from tuf.api.serialization import (
@@ -328,7 +330,7 @@ def test_key_class(self):
328330
"public": "edcd0a32a07dce33f7c7873aaffbff36d20ea30787574ead335eefd337e4dacd"
329331
},
330332
"scheme": "ed25519"
331-
},
333+
},
332334
}
333335
for key_dict in keys.values():
334336
# Testing that the workflow of deserializing and serializing
@@ -422,6 +424,76 @@ def test_metadata_root(self):
422424
with self.assertRaises(KeyError):
423425
root.signed.remove_key('root', 'nosuchkey')
424426

427+
def test_delegated_role_class(self):
428+
roles = [
429+
{
430+
"keyids": [
431+
"c8022fa1e9b9cb239a6b362bbdffa9649e61ad2cb699d2e4bc4fdf7930a0e64a"
432+
],
433+
"name": "role1",
434+
"paths": [
435+
"file3.txt"
436+
],
437+
"terminating": False,
438+
"threshold": 1
439+
}
440+
]
441+
for role in roles:
442+
# Testing that the workflow of deserializing and serializing
443+
# a delegation role dictionary doesn't change the content.
444+
key_obj = DelegatedRole.from_dict(role.copy())
445+
self.assertEqual(role, key_obj.to_dict())
446+
447+
# Test creating a DelegatedRole object with both "paths" and
448+
# "path_hash_prefixes" set.
449+
role["path_hash_prefixes"] = "foo"
450+
with self.assertRaises(ValueError):
451+
DelegatedRole.from_dict(role.copy())
452+
453+
# Test creating DelegatedRole only with "path_hash_prefixes"
454+
del role["paths"]
455+
DelegatedRole.from_dict(role.copy())
456+
role["paths"] = "foo"
457+
458+
# Test creating DelegatedRole only with "paths"
459+
del role["path_hash_prefixes"]
460+
DelegatedRole.from_dict(role.copy())
461+
role["path_hash_prefixes"] = "foo"
462+
463+
# Test creating DelegatedRole without "paths" and
464+
# "path_hash_prefixes" set
465+
del role["paths"]
466+
del role["path_hash_prefixes"]
467+
DelegatedRole.from_dict(role)
468+
469+
470+
def test_delegation_class(self):
471+
roles = [
472+
{
473+
"keyids": [
474+
"c8022fa1e9b9cb239a6b362bbdffa9649e61ad2cb699d2e4bc4fdf7930a0e64a"
475+
],
476+
"name": "role1",
477+
"paths": [
478+
"file3.txt"
479+
],
480+
"terminating": False,
481+
"threshold": 1
482+
}
483+
]
484+
keys = {
485+
"59a4df8af818e9ed7abe0764c0b47b4240952aa0d179b5b78346c470ac30278d":{
486+
"keytype": "ed25519",
487+
"keyval": {
488+
"public": "edcd0a32a07dce33f7c7873aaffbff36d20ea30787574ead335eefd337e4dacd"
489+
},
490+
"scheme": "ed25519"
491+
},
492+
}
493+
delegations_dict = {"keys": keys, "roles": roles}
494+
delegations = Delegations.from_dict(copy.deepcopy(delegations_dict))
495+
self.assertEqual(delegations_dict, delegations.to_dict())
496+
425497

426498
def test_metadata_targets(self):
427499
targets_path = os.path.join(

tuf/api/metadata.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -775,10 +775,8 @@ def __init__(
775775
"Only one of the attributes 'paths' and"
776776
"'path_hash_prefixes' can be set!"
777777
)
778-
if paths:
779-
self.paths = paths
780-
elif path_hash_prefixes:
781-
self.path_hash_prefixes = path_hash_prefixes
778+
self.paths = paths
779+
self.path_hash_prefixes = path_hash_prefixes
782780

783781
@classmethod
784782
def from_dict(cls, role_dict: Mapping[str, Any]) -> "Role":

0 commit comments

Comments
 (0)