Exemplo n.º 1
0
    def test_retrieve_registration_with_alphanumerical_parameter(self):
        """
        The goal of this test is to verify that a 400 (bad request) is returned
        when passing an alphanumerical id instead of a numerical one.

        """
        from ..api import RegistrationEndpoint
        from ..models import OwnershipRegistration
        from dynamicfixtures import _registered_edition_alice
        edition = _registered_edition_alice()
        alice = edition.owner
        registration = OwnershipRegistration.objects.create(
            edition=edition,
            new_owner=alice,
            piece=edition.parent,
            type=OwnershipRegistration.__name__,
        )
        url = reverse('api:ownership:ownershipregistration-detail',
                      kwargs={'pk': registration.prev_btc_address})
        factory = APIRequestFactory()
        request = factory.get(url)
        force_authenticate(request, user=alice)
        view = RegistrationEndpoint.as_view({'get': 'retrieve'})
        response = view(request, pk=registration.prev_btc_address)
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
        self.assertIn('reason', response.data)
        self.assertEqual(response.data['reason'],
                         'id of object must be a number')
Exemplo n.º 2
0
 def test_retrieve_registration_with_piece_field_only(self):
     """
     An `OwnershipRegistration` is always executed on a Piece level,
     which is why we need to make sure only to deliver the Piece as a
     `piece` property, and not also `edition`.
     """
     from ..api import RegistrationEndpoint
     from ..models import OwnershipRegistration
     from dynamicfixtures import _registered_edition_alice
     edition = _registered_edition_alice()
     alice = edition.owner
     registration = OwnershipRegistration.objects.create(
         edition=edition,
         new_owner=alice,
         piece=edition.parent,
         type=OwnershipRegistration.__name__,
     )
     url = reverse('api:ownership:ownershipregistration-detail',
                   kwargs={'pk': registration.pk})
     factory = APIRequestFactory()
     request = factory.get(url)
     force_authenticate(request, user=alice)
     view = RegistrationEndpoint.as_view({'get': 'retrieve'})
     response = view(request, pk=registration.pk)
     self.assertEqual(response.status_code, status.HTTP_200_OK)
     self.assertIn('piece', response.data['registration'])
     self.assertIsNotNone('piece', response.data['registration'])
     self.assertNotIn('edition', response.data['registration'])
Exemplo n.º 3
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'])
Exemplo n.º 4
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)
Exemplo n.º 5
0
 def test_verfiy_coa_without_authentication(self):
     from ..api import CoaEndpoint
     from ..models import generate_crypto_message
     from dynamicfixtures import _registered_edition_alice
     edition = _registered_edition_alice()
     url = reverse('api:coa:coafile-verify-coa')
     message, _, signature = generate_crypto_message(edition)
     data = {'message': message, 'signature': signature}
     request = APIRequestFactory().post(url, data, format='json')
     view = CoaEndpoint.as_view({'post': 'verify_coa'},
                                **CoaEndpoint.verify_coa.kwargs)
     response = view(request)
     self.assertEqual(response.status_code, status.HTTP_200_OK)
Exemplo n.º 6
0
 def test_create_coa(self):
     from ..api import CoaEndpoint
     from ..models import CoaFile
     from dynamicfixtures import _registered_edition_alice, _s3_bucket
     responses.add(
         responses.POST,
         settings.ASCRIBE_PDF_URL,
         json={},
         status=200,
         content_type='application/json',
     )
     edition = _registered_edition_alice()
     bucket = _s3_bucket()
     k = Key(bucket)
     k.key = edition.digital_work.key
     k.set_contents_from_string('white mountains')
     alice = edition.owner
     url = reverse('api:coa:coafile-list')
     factory = APIRequestFactory()
     data = {
         'bitcoin_id': edition.bitcoin_id,
     }
     request = factory.post(url, data, format='json')
     force_authenticate(request, user=alice)
     view = CoaEndpoint.as_view({'post': 'create'})
     response = view(request)
     self.assertEqual(response.status_code, status.HTTP_200_OK)
     self.assertIn('coa', response.data)
     self.assertIn('coa_file', response.data['coa'])
     self.assertIn('url', response.data['coa'])
     self.assertIn('url_safe', response.data['coa'])
     coa_key_prefix = edition.digital_work.key.replace(
         'digitalwork',
         'coa',
     ).rpartition('/')[0]
     stored_coa = (k for k in bucket.list(prefix=coa_key_prefix)).next()
     cloudfront_url = 'https://' + settings.AWS_CLOUDFRONT_DOMAIN
     coa_url = cloudfront_url + '/' + stored_coa.name
     coa_url_safe = cloudfront_url + '/' + urlquote_plus(stored_coa.name)
     self.assertEqual(response.data['coa']['coa_file'], stored_coa.name)
     self.assertEqual(response.data['coa']['url'], coa_url)
     self.assertEqual(response.data['coa']['url_safe'], coa_url_safe)
     coa_obj = CoaFile.objects.get()
     self.assertEqual(coa_obj.user, alice)
     self.assertEqual(coa_obj.edition, edition.pk)
     self.assertEqual(coa_obj.coa_file, stored_coa.name)
Exemplo n.º 7
0
 def test_retrieve_registration_with_string_pk(self):
     from ..api import RegistrationEndpoint
     from ..models import OwnershipRegistration
     from dynamicfixtures import _registered_edition_alice
     edition = _registered_edition_alice()
     alice = edition.owner
     registration = OwnershipRegistration.objects.create(
         edition=edition,
         new_owner=alice,
         piece=edition.parent,
         type=OwnershipRegistration.__name__,
     )
     url = reverse('api:ownership:ownershipregistration-detail',
                   kwargs={'pk': registration.pk})
     factory = APIRequestFactory()
     request = factory.get(url)
     force_authenticate(request, user=alice)
     view = RegistrationEndpoint.as_view({'get': 'retrieve'})
     response = view(request, pk=str(registration.pk))
     self.assertEqual(response.status_code, status.HTTP_200_OK)
Exemplo n.º 8
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'])