Ejemplo n.º 1
0
    def test_transaction_not_found(self):
        # Arrange
        # Act
        bc = make_blockchain()

        # Assert
        assert bc._contain_tx(random_transaction()) is False
Ejemplo n.º 2
0
    def test_use_given_date(self):
        bc = make_blockchain(end_with_empty_block=True)
        dt = date(2001, 3, 22)

        tx = bc.make_daily_guziboxes_tx(dt)

        assert tx.guzis_positions == [[[dt.isoformat()], [0]]]
Ejemplo n.º 3
0
    def test_transaction_found(self):
        # Arrange
        # Act
        bc = make_blockchain(days=2, tx_per_block=1)

        # Assert
        assert bc._contain_tx(bc[2].transactions[0]) is True
Ejemplo n.º 4
0
    def test_change_transactions_after_signed_ko(self):
        bc = make_blockchain(days=4, tx_per_block=4)
        bc.last_block().transactions[0].amount = 12

        result = bc.is_valid()

        assert result is False
Ejemplo n.º 5
0
    def test_return_0_for_total_0(self):
        bc = make_blockchain(end_with_empty_block=True)
        bc.last_block().total = 0

        result = bc._get_guzis_amount()

        assert result == 1
Ejemplo n.º 6
0
    def test_create_6_guzibox_if_total_is_125(self):
        bc = make_blockchain(end_with_empty_block=True)
        bc.last_block().total = 125

        tx = bc.make_daily_guziboxes_tx()

        assert tx.guzis_positions == [[[date.today().isoformat()],
                                       list(range(6))]]
Ejemplo n.º 7
0
    def test_last_block_takes_the_transaction(self):
        bc = make_blockchain(days=1, tx_per_block=2, close_last_block=False)
        tx = Transaction(VERSION, Transaction.GUZI_CREATE, NEW_USER_PUB_KEY, 0)

        bc._add_transaction(tx)

        assert len(bc.last_block().transactions) == 3
        assert tx in bc.last_block().transactions
Ejemplo n.º 8
0
    def test_ok(self):
        bc = make_blockchain(total=32**3)
        tx = bc.engage_guzis_to_user(target=REF_PUB_KEY,
                                     numdays=1,
                                     daily_amount=1)

        assert tx.tx_type == Transaction.GUZI_ENGAGEMENT
        assert tx.guzis_positions == [[[date(2000, 1, 1).isoformat()], [32]]]
Ejemplo n.º 9
0
    def test_return_None_if_not_found(self):
        # Arrange
        blockchain = make_blockchain()

        # Act
        result = blockchain._find_tx(random_transaction())

        # Assert
        assert result is None
Ejemplo n.º 10
0
    def test_return_block_containing_transaction(self):
        # Arrange
        blockchain = make_blockchain(days=2, tx_per_block=1)

        # Act
        result = blockchain._find_tx(blockchain[2].transactions[0])

        # Assert
        assert result == blockchain[2]
Ejemplo n.º 11
0
    def test_hex_format(self):
        blockchain_ref = make_blockchain()
        outfile = BytesIO()
        blockchain_ref.save_to_file(outfile)
        outfile.seek(0)

        blockchain = Blockchain(NEW_USER_PUB_KEY)
        blockchain.from_file(outfile)

        assert blockchain == blockchain_ref
Ejemplo n.º 12
0
    def test_hex_format(self):

        # Arrange
        blockchain_ref = make_blockchain()
        b = blockchain_ref.pack()

        # Act
        blockchain = Blockchain(NEW_USER_PUB_KEY)
        blockchain.from_bytes(b)

        # Assert
        assert blockchain == blockchain_ref
Ejemplo n.º 13
0
    def test_all_blocks_be_in(self):
        bc = make_blockchain(days=2, tx_per_block=1)
        block0 = bc.last_block().pack().hex()
        block1 = bc[1].pack().hex()
        outfile = BytesIO()

        bc.save_to_file(outfile)
        outfile.seek(0)
        content = outfile.read().hex()

        assert block0 in content
        assert block1 in content
Ejemplo n.º 14
0
    def test_fill_init_block(self):
        vk = ecdsa.VerifyingKey.from_string(REF_PUB_KEY, curve=ecdsa.SECP256k1)

        bc = make_blockchain()

        init_block = bc.last_block()
        expected_data = init_block.hash()

        assert init_block.version == VERSION
        assert init_block.close_date == date(2000, 1, 1)
        assert len(
            init_block.transactions) == 3  # 1.birth, 2.guzis 3.guziboxes
        assert bc.is_valid() is True
        assert vk.verify(init_block.signature, expected_data) is True
Ejemplo n.º 15
0
    def test_return_a_refusal_transaction(self):
        bc = make_blockchain(days=1, close_last_block=False)

        refused = bc.last_block().transactions[0]
        refusal = bc.refuse_transaction(refused)

        assert refusal.tx_type == Transaction.REFUSAL
        assert refusal.date == date(2011, 12, 13)
        assert refusal.signer == refused.target_user
        assert refusal.amount == refused.amount
        assert refusal.target_company is None
        assert refusal.target_user == refused.signer
        assert refusal.guzis_positions == []
        assert refusal.detail == refused.signature
Ejemplo n.º 16
0
    def test_transactions_are_the_same(self):
        blockchain_ref = make_blockchain()
        ref_transactions = blockchain_ref[0].transactions
        outfile = BytesIO()
        blockchain_ref.save_to_file(outfile)
        outfile.seek(0)

        blockchain = Blockchain(NEW_USER_PUB_KEY)
        blockchain.from_file(outfile)
        transactions = blockchain[0].transactions

        assert len(transactions) == 3
        assert transactions[0] == ref_transactions[0]
        assert transactions[1] == ref_transactions[1]
        assert transactions[2] == ref_transactions[2]
Ejemplo n.º 17
0
    def init(self, amount=2):
        self.bc = make_blockchain(days=1, close_last_block=False)

        return self.bc.pay_to_user(REF_PUB_KEY, amount)
Ejemplo n.º 18
0
    def test_different_signature(self):
        bc1 = make_blockchain()
        bc2 = make_blockchain()

        assert bc1 != bc2
        assert bc2 != bc1
Ejemplo n.º 19
0
    def test_blockchain_with_multiple_tx_per_block_ok(self):
        bc = make_blockchain(days=4, tx_per_block=4)

        result = bc.is_valid()

        assert result is True
Ejemplo n.º 20
0
    def test_blockchain_with_empty_ending_block_ok(self):
        bc = make_blockchain(days=1, tx_per_block=1, end_with_empty_block=True)

        result = bc.is_valid()

        assert result is True
Ejemplo n.º 21
0
    def test_blockchain_with_some_transactions_ok(self):
        bc = make_blockchain(days=5, tx_per_block=1)

        result = bc.is_valid()

        assert result is True
Ejemplo n.º 22
0
    def test_validated_blockchain_ok(self):
        bc = make_blockchain()

        result = bc.is_valid()

        assert result is True
Ejemplo n.º 23
0
    def test_raise_error_for_existing_guzi_creation(self):
        bc = make_blockchain(days=1, close_last_block=False)
        tx = bc.make_daily_guzis_tx(date(2000, 1, 2))

        with pytest.raises(GuziError):
            bc._add_transaction(tx)
Ejemplo n.º 24
0
    def test_raise_error_for_existing_guzi_creation_bis(self):
        bc = make_blockchain()
        tx = bc.make_daily_guzis_tx(date(2000, 1, 1))

        with pytest.raises(GuziError):
            bc._add_transaction(tx)
Ejemplo n.º 25
0
    def test_raise_error_if_transaction_is_sealed_in_old_block(self):
        bc = make_blockchain(days=2)

        with pytest.raises(NotRemovableTransactionError):
            bc.refuse_transaction(bc[1].transactions[0])
Ejemplo n.º 26
0
    def test_raise_error_if_transaction_is_sealed(self):
        bc = make_blockchain(days=1)

        with pytest.raises(NotRemovableTransactionError):
            bc.refuse_transaction(bc.last_block().transactions[0])
Ejemplo n.º 27
0
    def test_remove_tx_from_last_block(self):
        bc = make_blockchain(days=1, tx_per_block=1, close_last_block=False)
        assert len(bc.last_block().transactions) == 1

        bc.refuse_transaction(bc.last_block().transactions[0])
        assert len(bc.last_block().transactions) == 0
Ejemplo n.º 28
0
    def test_create_1_guzibox_if_total_is_0(self):
        bc = make_blockchain(end_with_empty_block=True)
        bc.last_block().total = 0
        tx = bc.make_daily_guziboxes_tx(date(2000, 1, 2))

        assert tx.guzis_positions == [[[date(2000, 1, 2).isoformat()], [0]]]
Ejemplo n.º 29
0
    def test_equal_itself(self):
        bc = make_blockchain()

        assert bc == bc
Ejemplo n.º 30
0
    def test_raise_error_for_negative_total(self):
        bc = make_blockchain(end_with_empty_block=True)
        bc.last_block().total = -125

        with pytest.raises(InvalidBlockchainError):
            bc._get_guzis_amount()