Skip to content

Commit ded92fc

Browse files
committed
New API: Add Key/Role specific tests
Signed-off-by: Martin Vrachev <[email protected]>
1 parent d8c8e56 commit ded92fc

File tree

1 file changed

+66
-1
lines changed

1 file changed

+66
-1
lines changed

tests/test_api.py

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@
2525
Metadata,
2626
Snapshot,
2727
Timestamp,
28-
Targets
28+
Targets,
29+
Key,
30+
Role
2931
)
3032

3133
from tuf.api.serialization import (
@@ -292,6 +294,69 @@ def test_metadata_timestamp(self):
292294
self.assertEqual(timestamp.signed.meta['snapshot.json'], fileinfo)
293295

294296

297+
def test_key_class(self):
298+
keys = {
299+
"59a4df8af818e9ed7abe0764c0b47b4240952aa0d179b5b78346c470ac30278d":{
300+
"keytype": "ed25519",
301+
"keyval": {
302+
"public": "edcd0a32a07dce33f7c7873aaffbff36d20ea30787574ead335eefd337e4dacd"
303+
},
304+
"scheme": "ed25519"
305+
},
306+
}
307+
for key_dict in keys.values():
308+
# Testing that the workflow of deserializing and serializing
309+
# a key dictionary doesn't change the content.
310+
test_key_dict = key_dict.copy()
311+
key_obj = Key.from_dict(test_key_dict)
312+
self.assertEqual(key_dict, key_obj.to_dict())
313+
# Test creating an instance without a required attribute.
314+
for key in key_dict.keys():
315+
test_key_dict = key_dict.copy()
316+
del test_key_dict[key]
317+
with self.assertRaises(KeyError):
318+
Key.from_dict(test_key_dict)
319+
# Test creating a Key instance with wrong keyval format.
320+
key_dict["keyval"] = {}
321+
with self.assertRaises(ValueError):
322+
Key.from_dict(key_dict)
323+
324+
325+
def test_role_class(self):
326+
roles = {
327+
"root": {
328+
"keyids": [
329+
"4e777de0d275f9d28588dd9a1606cc748e548f9e22b6795b7cb3f63f98035fcb"
330+
],
331+
"threshold": 1
332+
},
333+
"snapshot": {
334+
"keyids": [
335+
"59a4df8af818e9ed7abe0764c0b47b4240952aa0d179b5b78346c470ac30278d"
336+
],
337+
"threshold": 1
338+
},
339+
}
340+
for role_dict in roles.values():
341+
# Testing that the workflow of deserializing and serializing
342+
# a role dictionary doesn't change the content.
343+
test_role_dict = role_dict.copy()
344+
role_obj = Role.from_dict(test_role_dict)
345+
self.assertEqual(role_dict, role_obj.to_dict())
346+
# Test creating an instance without a required attribute.
347+
for role_attr in role_dict.keys():
348+
test_role_dict = role_dict.copy()
349+
del test_role_dict[role_attr]
350+
with self.assertRaises(KeyError):
351+
Key.from_dict(test_role_dict)
352+
# Test creating a Role instance with keyid dublicates.
353+
# for keyid in role_dict["keyids"]:
354+
role_dict["keyids"].append(role_dict["keyids"][0])
355+
test_role_dict = role_dict.copy()
356+
with self.assertRaises(ValueError):
357+
Role.from_dict(test_role_dict)
358+
359+
295360
def test_metadata_root(self):
296361
root_path = os.path.join(
297362
self.repo_dir, 'metadata', 'root.json')

0 commit comments

Comments
 (0)