From 8780e6807bce44008367380bd9c447e20dcc3d91 Mon Sep 17 00:00:00 2001 From: John-Mark Gurney Date: Fri, 6 Sep 2019 23:56:05 -0700 Subject: [PATCH] make common properties directly setable, as there must only be one of them.. sign the identity object w/ the generate key... prep for generating identities in the cli.. --- ui/cli.py | 47 +++++++++++++++++++++++++++++++++++------------ 1 file changed, 35 insertions(+), 12 deletions(-) diff --git a/ui/cli.py b/ui/cli.py index cd2f2fb..9b6ff5b 100644 --- a/ui/cli.py +++ b/ui/cli.py @@ -67,7 +67,7 @@ class MDBase(object): } _common_properties = [ 'type', 'created_by_ref' ] # XXX - add lang? - _common_optional = [ 'overlay_refs', 'sig' ] + _common_optional = set(('overlay_refs', 'sig')) _common_names = set(_common_properties + _generated_properties.keys()) def __init__(self, obj={}, **kwargs): @@ -124,8 +124,12 @@ class MDBase(object): obj = copy.deepcopy(self._obj) + common = self._common_names | self._common_optional for k, v in args: - obj.setdefault(k, []).append(v) + if k in common: + obj[k] = v + else: + obj.setdefault(k, []).append(v) del obj['modified'] @@ -175,8 +179,9 @@ class Identity(MDBase): # Identites don't need a created by _common_properties = [ x for x in MDBase._common_properties if x != 'created_by_ref' ] - _common_optional = [ x for x in MDBase._common_optional if x != - 'overlay_refs' ] + [ 'name', 'pubkey' ] + _common_optional = set([ x for x in MDBase._common_optional if x != + 'overlay_refs' ] + [ 'name', 'pubkey' ]) + _common_names = set(_common_properties + MDBase._generated_properties.keys()) def _trytodict(o): if isinstance(o, uuid.UUID): @@ -203,7 +208,7 @@ class Persona(object): self._pubkey = None if 'pubkey' in self._identity: - pubkeybytes = self._identity.pubkey[0] + pubkeybytes = self._identity.pubkey self._pubkey = Ed448PublicKey.from_public_bytes(pubkeybytes) self._created_by_ref = self._identity.uuid @@ -253,8 +258,8 @@ class Persona(object): self._pubkey = self._key.public_key() pubkey = self._pubkey.public_bytes(Encoding.Raw, PublicFormat.Raw) - self._identity = self._identity.new_version(('pubkey', - pubkey)) + self._identity = self.sign(self._identity.new_version(('pubkey', + pubkey))) def _makesigbytes(self, obj): obj = dict(obj.items(False)) @@ -552,6 +557,13 @@ class _TestCases(unittest.TestCase): # and that the modification is present self.assertEqual(md2['dc:creator'], [ 'Jim Bob' ]) + # that providing a value from common property + fvalue = 'fakesig' + md3 = md.new_version(('sig', fvalue)) + + # gets set directly, and is not a list + self.assertEqual(md3.sig, fvalue) + def test_mdbase_encode_decode(self): # that an object baseobj = { @@ -661,12 +673,15 @@ class _TestCases(unittest.TestCase): # that a key can be generated persona.generate_key() - # that a second time, it raises an exception - self.assertRaises(RuntimeError, persona.generate_key) - # that the pubkey property is present idobj = persona.get_identity() - self.assertIn('pubkey', idobj) + self.assertIsInstance(idobj['pubkey'], str) + + # and that there is a signature + self.assertIsInstance(idobj['sig'], str) + + # that a second time, it raises an exception + self.assertRaises(RuntimeError, persona.generate_key) # that a file object created by it testfname = os.path.join(self.tempdir, 'test.txt') @@ -771,8 +786,16 @@ class _TestCases(unittest.TestCase): # setup object store storefname = os.path.join(self.tempdir, 'storefname') + identfname = os.path.join(self.tempdir, 'identfname') shutil.copy(os.path.join('fixtures', 'sample.data.pasn1'), storefname) + # setup path mapping + def expandusermock(arg): + if arg == '~/.medashare_store.pasn1': + return storefname + elif arg == '~/.medashare_identity.pasn1': + return identfname + # setup test fname testfname = os.path.join(self.tempdir, 'test.txt') @@ -780,7 +803,7 @@ class _TestCases(unittest.TestCase): import StringIO import itertools - with mock.patch('os.path.expanduser', side_effect=itertools.repeat(storefname)) \ + with mock.patch('os.path.expanduser', side_effect=expandusermock) \ as eu: with nested(mock.patch('sys.stdout', StringIO.StringIO()), mock.patch('sys.argv',