def perform_cmd(cmd): yield Effect(EClientSubscribeEvent('eventA', 'sender1')) yield Effect(EClientSubscribeEvent('eventA', 'sender2')) yield Effect(EClientSubscribeEvent('eventB', 'sender1')) yield Effect(EEvent('eventC', 'sender1')) yield Effect(EEvent('eventA', 'sender3')) yield Effect(EEvent('eventA', 'sender1')) return b'cmd_resp'
async def _update(user): intent = EUserVlobUpdate(1, b'Next version for %s.' % user.encode()) eff = component.perform_user_vlob_update(intent) sequence = [(EGetAuthenticatedUser(), const(user)), (EEvent('user_vlob_updated', user), noop)] await asyncio_perform_sequence(sequence, eff)
async def test_message_new(self, component): intent = EMessageNew(recipient='*****@*****.**', body=b'This is for alice.') eff = component.perform_message_new(intent) sequence = [(EEvent('message_arrived', '*****@*****.**'), noop)] ret = await asyncio_perform_sequence(sequence, eff) assert ret is None
def perform_user_vlob_update(self, intent): id = yield Effect(EGetAuthenticatedUser()) vlobs = self.vlobs[id] if len(vlobs) != intent.version - 1: raise UserVlobError('Wrong blob version.') vlobs.append( UserVlobAtom(id=id, version=intent.version, blob=intent.blob)) yield Effect(EEvent('user_vlob_updated', id))
def alice_identity(): component = IdentityComponent() sequence = [ (EEvent('identity_loaded', 'Alice'), noop), ] return perform_sequence( sequence, component.perform_identity_load( EIdentityLoad('Alice', ALICE_PRIVATE_RSA)))
def perform_identity_unload(self, intent): from parsec.core.backend import EBackendReset from parsec.core.block import EBlockReset if not self.identity: raise IdentityNotLoadedError('Identity not loaded') # TODO: make block&backend reset event triggered yield Effect(EBlockReset()) yield Effect(EBackendReset()) yield Effect(EEvent('identity_unloaded', None)) self.identity = None
def perform_identity_load(self, intent): if self.identity: raise IdentityError('Identity already loaded') # TODO: handle invalid key with more precise exception try: private_key = load_private_key(intent.key, intent.password) except Exception as e: raise IdentityError('Invalid private key (%s)' % e) self.identity = Identity(intent.id, private_key, private_key.pub_key) yield Effect(EEvent('identity_loaded', self.identity.id)) return self.identity
async def test_perform_identity_load(): component = IdentityComponent() assert component.identity is None intent = EIdentityLoad('Alice', ALICE_PRIVATE_RSA) eff = component.perform_identity_load(intent) ret = perform_sequence([ (EEvent('identity_loaded', 'Alice'), noop), ], eff) assert isinstance(ret, Identity) assert ret == component.identity assert ret.id == 'Alice'
def perform_vlob_update(self, intent): try: vlob = self.vlobs[intent.id] if vlob.write_trust_seed != intent.trust_seed: raise TrustSeedError('Invalid write trust seed.') except KeyError: raise VlobNotFound('Vlob not found.') if intent.version - 1 == len(vlob.blob_versions): vlob.blob_versions.append(intent.blob) else: raise VlobNotFound('Wrong blob version.') yield Effect(EEvent('vlob_updated', intent.id))
async def test_user_vlob_update_ok(self, component): intent = EUserVlobUpdate(1, b'Next version.') eff = component.perform_user_vlob_update(intent) sequence = [(EGetAuthenticatedUser(), const('*****@*****.**')), (EEvent('user_vlob_updated', '*****@*****.**'), noop)] await asyncio_perform_sequence(sequence, eff) # Check back the value intent = EUserVlobRead(version=1) eff = component.perform_user_vlob_read(intent) sequence = [(EGetAuthenticatedUser(), const('*****@*****.**'))] ret = await asyncio_perform_sequence(sequence, eff) assert ret == UserVlobAtom('*****@*****.**', 1, b'Next version.')
async def test_vlob_read_previous_version(self, component, vlob): # Update vlob intent = EVlobUpdate(vlob.id, 2, vlob.write_trust_seed, b'Next version.') eff = component.perform_vlob_update(intent) sequence = [(EEvent('vlob_updated', vlob.id), noop)] await asyncio_perform_sequence(sequence, eff) # Read previous version intent = EVlobRead(vlob.id, vlob.read_trust_seed, version=1) eff = component.perform_vlob_read(intent) sequence = [] ret = await asyncio_perform_sequence(sequence, eff) assert ret == vlob
async def test_vlob_update_ok(self, component, vlob): intent = EVlobUpdate(vlob.id, 2, vlob.write_trust_seed, b'Next version.') eff = component.perform_vlob_update(intent) sequence = [(EEvent('vlob_updated', vlob.id), noop)] await asyncio_perform_sequence(sequence, eff) # Check back the value intent = EVlobRead(vlob.id, vlob.read_trust_seed, version=2) eff = component.perform_vlob_read(intent) ret = await asyncio_perform_sequence([], eff) assert ret.id == vlob.id assert ret.version == 2 assert ret.blob == b'Next version.'
async def test_user_vlob_read_previous_version(self, component): # Update user vlob intent = EUserVlobUpdate(1, b'Next version.') eff = component.perform_user_vlob_update(intent) sequence = [(EGetAuthenticatedUser(), const('*****@*****.**')), (EEvent('user_vlob_updated', '*****@*****.**'), noop)] await asyncio_perform_sequence(sequence, eff) # Read previous version intent = EUserVlobRead(version=0) eff = component.perform_user_vlob_read(intent) sequence = [ (EGetAuthenticatedUser(), const('*****@*****.**')), ] ret = await asyncio_perform_sequence(sequence, eff) assert ret == UserVlobAtom('*****@*****.**', 0, b'')
def perform_user_vlob_update(self, intent): id = yield Effect(EGetAuthenticatedUser()) yield AsyncFunc(self._perform_user_vlob_update(id, intent.version, intent.blob)) yield Effect(EEvent('user_vlob_updated', id))
def perform_message_new(self, intent): self._messages[intent.recipient].append(intent.body) yield Effect(EEvent('message_arrived', intent.recipient))
async def _send_msg(recipient): intent = EMessageNew(recipient=recipient, body=b'for %s' % recipient.encode()) eff = component.perform_message_new(intent) sequence = [(EEvent('message_arrived', recipient), noop)] await asyncio_perform_sequence(sequence, eff)
def perform_message_new(self, intent): yield AsyncFunc(self._perform_message_new(intent)) yield Effect(EEvent('message_arrived', intent.recipient))
def perform_vlob_update(self, intent): yield AsyncFunc(self._perform_vlob_update(intent)) yield Effect(EEvent('vlob_updated', intent.id))