示例#1
0
    def test_web_user_retrieve_loan(self):
        from ...api import LoanEndpoint
        from ...models import Loan
        from ...serializers import LoanSerializer
        from util.util import ordered_dict
        from dynamicfixtures import _djroot_user, _alice, _bob, _registered_edition_alice

        _djroot_user()
        alice = _alice()
        bob = _bob()
        edition_alice = _registered_edition_alice()
        loan = Loan.create(edition_alice, loanee=bob, owner=alice)
        loan.save()

        factory = APIRequestFactory()
        url = reverse('api:ownership:loan-detail', kwargs={'pk': loan.pk})
        request = factory.get(url)
        force_authenticate(request, user=alice)
        view = LoanEndpoint.as_view({'get': 'retrieve'})
        response = view(request, pk=loan.pk)

        serializer = LoanSerializer(loan, context={'request': request})
        response_db = json.loads(json.dumps(serializer.data))

        self.assertEqual(ordered_dict(response.data['loan']),
                         ordered_dict(response_db))
        self.assertIs(response.status_code, status.HTTP_200_OK)
        self.assertIn('edition', response.data['loan'])
        self.assertNotIn('piece', response.data['loan'])
示例#2
0
    def test_web_user_list_consignments(self):
        from ...api import ConsignEndpoint
        from ...models import Consignment
        from ...serializers import OwnershipEditionSerializer
        from util.util import ordered_dict
        from dynamicfixtures import _djroot_user, _alice, _bob, _registered_edition_alice

        _djroot_user()
        alice = _alice()
        bob = _bob()
        edition_alice = _registered_edition_alice()
        consignment = Consignment.create(edition_alice, consignee=bob, owner=alice)
        consignment.save()

        url = reverse('api:ownership:consignment-list')
        factory = APIRequestFactory()
        request = factory.get(url)
        force_authenticate(request, user=alice)
        view = ConsignEndpoint.as_view({'get': 'list'})
        response = view(request)

        qs = Consignment.objects.filter(Q(prev_owner=alice) | Q(new_owner=bob))
        serializer = OwnershipEditionSerializer(qs, many=True, context={'request': request})
        response_db = json.loads(json.dumps({'success': True,
                                             'count': len(qs),
                                             'unfiltered_count': len(qs),
                                             'next': None,
                                             'previous': None,
                                             'consignments': serializer.data}))

        self.assertEqual(ordered_dict(response.data), ordered_dict(response_db))
        self.assertIs(response.status_code, status.HTTP_200_OK)
        for consignment in response.data['consignments']:
            self.assertIn('edition', consignment)
            self.assertNotIn('piece', consignment)
示例#3
0
 def test_list_pieces_when_some(self):
     from dynamicfixtures import (
         _djroot_user,
         _prize_juror,
         _prize_piece_alice,
         _prize_piece_bob,
         _rating_piece_alice,
         _rating_piece_bob,
         _alice_bitcoin_wallet,
         _bob_bitcoin_wallet,
     )
     from ..api import PrizePieceEndpoint
     _djroot_user()
     _prize_piece_alice()
     _prize_piece_bob()
     _rating_piece_alice()
     _rating_piece_bob()
     prize_juror = _prize_juror()
     _alice_bitcoin_wallet()
     _bob_bitcoin_wallet()
     subdomain = prize_juror.prize.whitelabel_settings.subdomain
     url = reverse('api:prize:prize-pieces-list',
                   kwargs={'domain_pk': subdomain})
     view = PrizePieceEndpoint.as_view({'get': 'list'})
     factory = APIRequestFactory()
     request = factory.get(url)
     force_authenticate(request, user=prize_juror.user)
     response = view(request, domain_pk=subdomain)
     self.assertEqual(response.status_code, status.HTTP_200_OK)
     self.assertEqual(response.data['count'], 2)
示例#4
0
 def test_list_pieces_for_second_round_of_portfolioreview(self):
     from dynamicfixtures import (
         _djroot_user,
         _prize_juror,
         _prize_piece_alice,
         _prize_piece_bob,
         _rating_piece_alice,
         _rating_piece_bob,
         _alice_bitcoin_wallet,
         _bob_bitcoin_wallet,
     )
     from ..api import PrizePieceEndpoint
     _djroot_user()
     _alice_bitcoin_wallet()
     _bob_bitcoin_wallet()
     subdomain = 'portfolioreview'
     prize_piece_alice = _prize_piece_alice(subdomain=subdomain)
     prize_piece_alice.round = 2
     prize_piece_alice.save()
     _prize_piece_bob(subdomain=subdomain)
     round_two_starttime = datetime.strptime(
         settings.PORTFOLIO_REVIEW_ROUND_TWO_STARTTIME,
         settings.PORTFOLIO_REVIEW_ROUND_TIMEFORMAT,
     ).replace(tzinfo=pytz.UTC)
     rating_piece_alice = _rating_piece_alice(subdomain=subdomain)
     rating_piece_bob = _rating_piece_bob(subdomain=subdomain)
     rating_piece_alice.datetime = round_two_starttime
     rating_piece_alice.save()
     rating_piece_bob.datetime = round_two_starttime - timedelta(seconds=1)
     rating_piece_bob.save()
     prize_juror = _prize_juror(subdomain=subdomain)
     prize = prize_juror.prize
     prize.active_round = 2
     prize.save()
     url = reverse('api:prize:prize-pieces-list',
                   kwargs={'domain_pk': subdomain})
     view = PrizePieceEndpoint.as_view({'get': 'list'})
     factory = APIRequestFactory()
     request = factory.get(url)
     force_authenticate(request, user=prize_juror.user)
     response = view(request, domain_pk=subdomain)
     self.assertEqual(response.status_code, status.HTTP_200_OK)
     self.assertEqual(response.data['count'], 1)
     self.assertIsNotNone(response.data['pieces'][0]['ratings'])
示例#5
0
    def test_create_loan_edition(self):
        from ...api import LoanEndpoint
        from ...models import Loan
        from dynamicfixtures import _djroot_user, _alice, _bob, _registered_edition_alice

        password = '******'
        _djroot_user()
        alice = _alice()
        bob = _bob()
        edition_alice = _registered_edition_alice()
        data = {
            'loanee': bob.email,
            'bitcoin_id': edition_alice,
            'startdate': datetime.utcnow().date(),
            'enddate': datetime.utcnow().date() + timedelta(days=1),
            'password': password,
            'terms': True
        }
        view = LoanEndpoint.as_view({'post': 'create'})
        factory = APIRequestFactory()
        url = reverse('api:ownership:loan-list')
        request = factory.post(url, data)
        force_authenticate(request, user=alice)
        response = view(request)

        self.assertEqual(response.status_code, status.HTTP_201_CREATED)

        # check that the loan exists
        loans = Loan.objects.filter(edition=edition_alice)
        self.assertEqual(len(loans), 1)
        loan = loans[0]

        # check owners
        self.assertEqual(loan.prev_owner, alice)
        self.assertEqual(loan.new_owner, bob)

        # check the status is None (meaning unconfirmed)
        self.assertIsNone(loan.status)

        self.assertIn('loan', response.data)
        self.assertIn('edition', response.data['loan'])
示例#6
0
 def test_create_prize_piece(self):
     from dynamicfixtures import (
         _djroot_user,
         _djroot_bitcoin_wallet,
         _alice,
         _alice_bitcoin_wallet,
         _license,
         _digital_work_alice,
         _prize_with_whitelabel,
     )
     from ..api import PrizePieceEndpoint
     subdomain = 'portfolioreview'
     _djroot_user()
     _djroot_bitcoin_wallet()
     alice = _alice()
     _alice_bitcoin_wallet()
     license = _license()
     digital_work = _digital_work_alice()
     _prize_with_whitelabel(subdomain=subdomain)
     data = {
         'prize_name': subdomain,
         'terms': True,
         'digital_work_key': digital_work.key,
         'artist_name': 'alice',
         'title': 'green stars',
         'license': license.code,
     }
     url = reverse('api:prize:prize-pieces-list',
                   kwargs={'domain_pk': subdomain})
     view = PrizePieceEndpoint.as_view({'post': 'create'})
     factory = APIRequestFactory()
     headers = {
         'HTTP_ORIGIN': 'http://{}.ascribe.test'.format(subdomain),
     }
     request = factory.post(url, data=data, **headers)
     force_authenticate(request, user=alice)
     response = view(request, domain_pk=subdomain)
     self.assertEqual(response.status_code, status.HTTP_201_CREATED)
示例#7
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'])