示例#1
0
def test_pending_state_in_ownership_history(alice, alice_password,
                                            registered_edition_alice,
                                            bob_bitcoin_wallet, bob_password,
                                            invited_dan_bitcoin_wallet):
    from ..models import OwnershipTransfer
    from bitcoin.models import BitcoinWallet
    from ownership.api import TransferEndpoint
    from ownership.signals import transfer_created

    # first transfer to non-invited user
    bob = bob_bitcoin_wallet.user
    first_transfer = OwnershipTransfer.create(
        edition=registered_edition_alice,
        prev_owner=alice,
        transferee=bob,
        prev_btc_address=None,
    )
    first_transfer.ciphertext_wif = BitcoinWallet.encoded_wif_for_path(
        first_transfer, alice_password)
    first_transfer.save()
    transfer_created.send(sender=TransferEndpoint,
                          instance=first_transfer,
                          password=alice_password)
    registered_edition_alice.owner = bob
    registered_edition_alice.save()

    # second, transfer ownership to invited user
    invited_dan = invited_dan_bitcoin_wallet.user
    second_transfer = OwnershipTransfer.create(
        edition=registered_edition_alice,
        prev_owner=bob,
        transferee=invited_dan,
        prev_btc_address=None,
    )
    second_transfer.ciphertext_wif = BitcoinWallet.encoded_wif_for_path(
        second_transfer, bob_password)
    second_transfer.save()

    transfer_created.send(sender=TransferEndpoint,
                          instance=second_transfer,
                          password=bob_password)
    registered_edition_alice.pending_new_owner = invited_dan
    registered_edition_alice.save()

    _, transfer_bob_str, transfer_invited_dan_str = [
        s[1] for s in registered_edition_alice.ownership_history
    ]

    assert '(pending)' not in transfer_bob_str
    assert '(pending)' in transfer_invited_dan_str
示例#2
0
def on_unconsignment_create(sender, instance, password, *args, **kwargs):
    # Create bitcoin unconsign transaction
    unconsign = BitcoinTransaction.unconsign(instance)

    # before pushing the transaction we need to check:
    # 1. check if the edition needs migration
    # 2. the edition address is refilled

    # check if edition needs migration
    migration = check_migration(instance)
    if migration:
        instance.prev_btc_address = migration.new_btc_address
        instance.btc_tx = None
        instance.ciphertext_wif = BitcoinWallet.encoded_wif_for_path(instance, password)
        instance.save()

        # delete old btc_tx which has the wrong addresses and create a new one
        unconsign.delete()
        unconsign = BitcoinTransaction.unconsign(instance)

    # refill the edition address
    # create the transaction
    refill = BitcoinTransaction.refill(instance)
    # set the unconsign as the dependent transaction so that it is sent after the refill by the
    # transaction_monitor
    refill.dependent_tx = unconsign
    refill.save()
    tasks.refill.delay(refill.id, util.mainAdminPassword())
示例#3
0
def on_loan_piece_create(sender, instance, password, *args, **kwargs):
    # upon loan created we need to check if:
    # 1. If it requires a migration

    # check migration
    migration = check_migration(instance)
    if migration:
        instance.prev_btc_address = migration.new_btc_address
        instance.ciphertext_wif = BitcoinWallet.encoded_wif_for_path(instance, password)
        instance.save()
示例#4
0
def on_loan_edition_create(sender, instance, password, *args, **kwargs):
    # upon loan created we need to check if:
    # 1. An edition is registered
    # 2. If it requires a migration

    # check if edition is registered
    registration = ownership_models.OwnershipRegistration.objects.filter(edition=instance.edition)
    if not registration:
        registration = ownership_models.OwnershipRegistration.create(edition=instance.edition,
                                                                     new_owner=instance.edition.owner)
        registration.save()

    # check migration
    migration = check_migration(instance)
    if migration:
        instance.prev_btc_address = migration.new_btc_address
        instance.ciphertext_wif = BitcoinWallet.encoded_wif_for_path(instance, password)
        instance.save()
示例#5
0
def on_ownership_transfer_create(sender, instance, password, *args, **kwargs):
    # Create bitcoin transfer transaction
    transfer = BitcoinTransaction.transfer(instance)

    # if a user needs to register and there is no wallet yet no transaction will be created
    if transfer:

        # before pushing the transaction we need to check:
        # 1. the edition is already registered (because of lazy editions)
        # 2. check if an edition needs migration (due to a password change)
        # 3. the edition address is refilled

        # check if edition is registered
        registration = ownership_models.OwnershipRegistration.objects.filter(edition=instance.edition)
        if not registration:
            registration = ownership_models.OwnershipRegistration.create(edition=instance.edition,
                                                                         new_owner=instance.edition.owner)
            registration.save()

        # check if edition needs migration
        migration = check_migration(instance)
        if migration:
            instance.prev_btc_address = migration.new_btc_address
            instance.btc_tx = None
            instance.ciphertext_wif = BitcoinWallet.encoded_wif_for_path(instance, password)
            instance.save()

            # delete old btc_tx which has the wrong addresses and create a new one
            transfer.delete()
            transfer = BitcoinTransaction.transfer(instance)

        # refill the edition address
        # create the transaction
        refill = BitcoinTransaction.refill(instance)
        # set the transfer as the dependent transaction so that it is sent after the refill by the
        # transaction_monitor
        refill.dependent_tx = transfer
        refill.save()
        tasks.refill.delay(refill.id, util.mainAdminPassword())
示例#6
0
    def test_acl_edit_of_a_retrieved_transferred_edition(self):
        from dynamicfixtures import (
            _djroot_user,
            _alice,
            _bob,
            _bob_bitcoin_wallet,
            _registered_edition_pair_alice,
            _whitelabel_merlin,
        )
        from bitcoin import tasks
        from bitcoin.models import BitcoinTransaction, BitcoinWallet
        from ownership.models import OwnershipRegistration, OwnershipTransfer
        from util import util
        _djroot_user()
        alice, bob = _alice(), _bob()
        _bob_bitcoin_wallet()
        edition_one, edition_two = _registered_edition_pair_alice()

        # TODO Extract, and/or simplify to the essentials.
        #
        # What are the essentials?
        # - Having two editions.
        # - The two editions belong to the same piece.
        # - The piece has been registered by alice.
        # - One edition has been transferred to bob.
        # - The transferred edition should have its acl_edit set accordingly.
        #
        # So, it may very well be possible to avoid going through all the
        # transfer related operations. Waht matters is that the transferred
        # edition has its fields set like it would have if it would have been
        # transferred. Related objects, which are created and/or modified
        # during a transfer may alos need to be created.
        OwnershipRegistration.objects.create(
            edition=edition_one,
            new_owner=edition_one.owner,
            piece=edition_one.parent,
            type=OwnershipRegistration.__name__,
        )
        transfer = OwnershipTransfer(
            edition=edition_one,
            prev_owner=edition_one.owner,
            new_owner=bob,
            prev_btc_address=None,
            piece=edition_one.parent,
            type=OwnershipTransfer.__name__,
        )
        transfer.ciphertext_wif = BitcoinWallet.encoded_wif_for_path(
            transfer,
            'alice-secret',
        )
        transfer.save()
        transfer_tx = BitcoinTransaction.transfer(transfer)
        refill = BitcoinTransaction.refill(transfer)
        refill.dependent_tx = transfer_tx
        refill.save()
        tasks.refill(refill.id, util.mainAdminPassword())
        edition_one.owner = bob
        edition_one.save()
        # END of transfer

        whitelabel = _whitelabel_merlin()
        subdomain = whitelabel.subdomain
        view = MarketEditionEndpoint.as_view({'get': 'retrieve'})

        url = reverse(
            'api:whitelabel:market:edition-detail',
            kwargs={'domain_pk': subdomain, 'pk': edition_two.bitcoin_id},
        )
        factory = APIRequestFactory()
        request = factory.get(url)
        force_authenticate(request, user=alice)

        response = view(
            request, pk=edition_two.bitcoin_id, domain_pk=subdomain)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertFalse(response.data['edition']['acl']['acl_edit'])
        self.assertTrue(response.data['edition']['acl']['acl_wallet_submit'])