def test_from_signed_confirmed(self): """ This test checks the functionality of making a block with the payload from a message. """ meta = self.community.get_meta_message(u"signed_confirm") message = meta.impl( authentication=([self.member, self.member_bank], ), distribution=(self.community.claim_global_time(), ), payload=self.payload, destination=(LoopbackCandidate(), )) block = DatabaseBlock.from_signed_confirm_message(message) self.assertEqual(block.benefactor, message.payload.benefactor) self.assertEqual(block.beneficiary, message.payload.beneficiary) self.assertEqual(DatabaseModel.decode(block.agreement_benefactor), message.payload.agreement_benefactor) self.assertEqual(DatabaseModel.decode(block.agreement_beneficiary), message.payload.agreement_beneficiary) self.assertEqual(block.sequence_number_benefactor, message.payload.sequence_number_benefactor) self.assertEqual(block.sequence_number_beneficiary, message.payload.sequence_number_beneficiary) self.assertEqual(block.previous_hash_benefactor, message.payload.previous_hash_benefactor) self.assertEqual(block.previous_hash_beneficiary, message.payload.previous_hash_beneficiary) self.assertEqual(block.insert_time, message.payload.insert_time)
def test_put_success(self): self.backend.clear() self.backend.post('test', self.block1.id, self.block1.encode()) self.assertEqual( DatabaseModel.decode(self.backend.get('test', self.block1.id)), self.block1) self.assertTrue( self.backend.put('test', self.block1.id, self.block2.encode())) self.assertEqual( DatabaseModel.decode(self.backend.get('test', self.block1.id)), self.block2)
def get_all(self, _type): try: items = self.backend.get_all(_type) if items: return [DatabaseModel.decode(t) for t in items] except KeyError: return None
def test_on_loan_request_reject(self): """ Test a bank rejecting a users loan_request bank --> user """ # Save the user-side initial data which is a pending loan request. self.loan_request.status[self.bank.id] = STATUS.PENDING self.user.loan_request_ids.append(self.loan_request.id) self.user.post_or_put(self.api.db) self.assertIn(self.loan_request.id, self.user.loan_request_ids) # Deep copy the loan request loan_request_bank = DatabaseModel.decode(self.loan_request.encode()) loan_request_bank.status[self.bank.id] = STATUS.REJECTED # Make the payload payload = FakePayload() payload.request = APIMessage.LOAN_REQUEST_REJECT payload.models = { self.loan_request.type: loan_request_bank, self.user.type: self.bank } self.community.on_loan_request_reject(payload) # Now let's pull the loan request from the user database self.assertTrue(self.isModelInDB(self.api, loan_request_bank)) loan_request = self.api.db.get(loan_request_bank.type, loan_request_bank.id) self.assertEqual(loan_request.status[self.bank.id], STATUS.REJECTED) self.assertNotIn(self.loan_request.id, self.user.loan_request_ids)
def _decode_signed_confirm(self, placeholder, offset, data): try: offset, payload = decode(data, offset) except ValueError: raise DropPacket("Unable to decode the SignedConfirm-payload") if not isinstance(payload, tuple): raise DropPacket("Invalid payload type") # benefactor, 0 # beneficiary, 1 # agreement_benefactor_encoded, 2 # agreement_beneficiary_encoded, 3 # sequence_number_benefactor, 4 # sequence_number_beneficiary, 5 # previous_hash_benefactor, 6 # previous_hash_beneficiary, 7 # signature_benefactor, 8 # signature_beneficiary, 9 # insert_time 10 if not isinstance(payload[0], str): raise DropPacket("Invalid 'benefactor' type") if not isinstance(payload[1], str): raise DropPacket("Invalid 'beneficiary' type") #TODO: Do the rest. agreement_benefactor = DatabaseModel.decode(payload[2]) agreement_beneficiary = DatabaseModel.decode(payload[3]) return offset, placeholder.meta.payload.implement( payload[0], payload[1], agreement_benefactor, agreement_beneficiary, payload[4], payload[5], payload[6], payload[7], payload[8], payload[9], payload[10], )
def _decode_model(self, placeholder, offset, data): try: offset, payload = decode(data, offset) except ValueError: raise DropPacket("Unable to decode the model payload") if not isinstance(payload, tuple): raise DropPacket("Invalid payload type") fields, encoded_models = payload if not isinstance(fields, list): raise DropPacket("Invalid 'fields' type") if not isinstance(encoded_models, dict): raise DropPacket("Invalid 'models' type") decoded_models = dict() for field in fields: decoded_models[field] = DatabaseModel.decode(encoded_models[field]) return offset, placeholder.meta.payload.implement( fields, decoded_models)
def test_get(self): self.backend.clear() self.backend.post('test', self.block1.id, self.block1.encode()) self.backend.post('test', self.block2.id, self.block2.encode()) self.backend.post('test', self.block3.id, self.block3.encode()) with self.assertRaises(IndexError): self.backend.post('test2', self.block1.id, self.block1.encode()) self.assertEqual(DatabaseModel.decode(self.backend.get('test', '1')), self.block1) self.assertEqual( DatabaseModel.decode(self.backend.get('test', self.block2.id)), self.block2) self.assertNotEqual( DatabaseModel.decode(self.backend.get('test', '1')), DatabaseModel.decode(self.backend.get('test', '2'))) with self.assertRaises(IndexError): self.assertEqual( DatabaseModel.decode(self.backend.get('test', '1')), DatabaseModel.decode(self.backend.get('test2', '1')))
def get(self, _type, _id): try: return DatabaseModel.decode(self._backend.get(_type, _id)) except IndexError: return None
def test_post(self): self.backend.clear() self.backend.post('test', self.block1.id, self.block1.encode()) self.assertEqual( self.block1, DatabaseModel.decode(self.backend.get('test', self.block1.id)))