From 4f17315b2cd0bfce279965f4a450c3eb96fab74b Mon Sep 17 00:00:00 2001 From: Martin Vrachev Date: Mon, 26 Apr 2021 14:29:43 +0300 Subject: [PATCH] New API: Add Key/Role specific tests Signed-off-by: Martin Vrachev --- tests/test_api.py | 61 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) diff --git a/tests/test_api.py b/tests/test_api.py index 776d62bc0f..1da2c9e4e0 100755 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -25,7 +25,9 @@ Metadata, Snapshot, Timestamp, - Targets + Targets, + Key, + Role ) from tuf.api.serialization import ( @@ -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')