Skip to content

Commit

Permalink
New API: Add Key/Role specific tests
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
  • Loading branch information
MVrachev committed Apr 26, 2021
1 parent 85cd5c8 commit 4f17315
Showing 1 changed file with 60 additions and 1 deletion.
61 changes: 60 additions & 1 deletion tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
Metadata,
Snapshot,
Timestamp,
Targets
Targets,
Key,
Role
)

from tuf.api.serialization import (
Expand Down Expand Up @@ -292,6 +294,63 @@ def test_metadata_timestamp(self):
self.assertEqual(timestamp.signed.meta['snapshot.json'], fileinfo)


def test_key_class(self):
keys = {
"59a4df8af818e9ed7abe0764c0b47b4240952aa0d179b5b78346c470ac30278d":{
"keytype": "ed25519",
"keyval": {
"public": "edcd0a32a07dce33f7c7873aaffbff36d20ea30787574ead335eefd337e4dacd"
},
"scheme": "ed25519"
},
}
for key_dict in keys.values():
# Testing that the workflow of deserializing and serializing
# a key dictionary doesn't change the content.
test_key_dict = key_dict.copy()
key_obj = Key.from_dict(test_key_dict)
self.assertEqual(key_dict, key_obj.to_dict())
# Test creating an instance without a required attribute.
for key in key_dict.keys():
test_key_dict = key_dict.copy()
del test_key_dict[key]
with self.assertRaises(KeyError):
Key.from_dict(test_key_dict)
# Test creating a Key instance with wrong keyval format.
key_dict["keyval"] = {}
with self.assertRaises(ValueError):
Key.from_dict(key_dict)


def test_role_class(self):
roles = {
"root": {
"keyids": [
"4e777de0d275f9d28588dd9a1606cc748e548f9e22b6795b7cb3f63f98035fcb"
],
"threshold": 1
},
"snapshot": {
"keyids": [
"59a4df8af818e9ed7abe0764c0b47b4240952aa0d179b5b78346c470ac30278d"
],
"threshold": 1
},
}
for role_dict in roles.values():
# Testing that the workflow of deserializing and serializing
# a role dictionary doesn't change the content.
test_role_dict = role_dict.copy()
role_obj = Role.from_dict(test_role_dict)
self.assertEqual(role_dict, role_obj.to_dict())
# Test creating an instance without a required attribute.
for role_attr in role_dict.keys():
test_role_dict = role_dict.copy()
del test_role_dict[role_attr]
with self.assertRaises(KeyError):
Key.from_dict(test_role_dict)


def test_metadata_root(self):
root_path = os.path.join(
self.repo_dir, 'metadata', 'root.json')
Expand Down

0 comments on commit 4f17315

Please sign in to comment.