Пример #1
0
    def test_save_an_invoice(self):
        from apps.invoices import views

        test_user = User.objects.create_user(username="******",
                                             email="*****@*****.**",
                                             password="******")
        email = '*****@*****.**'

        address = WhitelistAddress.objects.create(
            address='0xEA674fdDe714fd979de3EdF0F56AA9716B898ec8',
            nickname='Barbara',
            user=test_user,
            status=WhitelistAddress.AddressStatus.verified)

        invoice_response = views.InvoiceViewSet.as_view({'post': 'create'})(
            self._post(
                test_user, '/invoices/', {
                    'user': test_user.id,
                    'pay_to': address.id,
                    'recipient_email': email,
                    'invoice_amount': 1000000000,
                    'currency': PaymentCurrency.ETH().id,
                }))

        #   should have an outbox of 0
        self.assertEqual(len(mail.outbox), 2)
        #   should have added an invoice to the invoice list
        self.assertEqual(Invoice.objects.count(), 1)
        #   should have an invoice status- new
        self.assertEqual(Invoice.objects.first().status, 'new')
Пример #2
0
    def test_send_invoice(self):
        from apps.invoices import views

        test_user = User.objects.create_user(username="******",
                                             email="*****@*****.**",
                                             password="******")
        email = '*****@*****.**'
        address = WhitelistAddress.objects.create(
            address='0xEA674fdDe714fd979de3EdF0F56AA9716B898ec8',
            nickname='Barbara',
            user=test_user,
            status=WhitelistAddress.AddressStatus.verified)

        response = views.InvoiceViewSet.as_view({'post': 'create'})(self._post(
            test_user, '/invoices/', {
                'user': test_user.id,
                'pay_to': address.id,
                'recipient_email': email,
                'invoice_amount': 1000000000,
                'currency': PaymentCurrency.ETH().id,
                'status': 'published'
            }))
        self.assertEqual(Invoice.objects.count(), 1)

        # Email subjects: New User Registration, New Invoice Created, Someone has sent you a bill on PayWei
        self.assertEqual(len(mail.outbox), 3)
        self.assertEqual(mail.outbox[-1].subject,
                         'Someone has sent you a bill on PayWei')

        response = views.InvoiceViewSet.as_view({'post': 'create'})(self._post(
            test_user, '/invoices/', {
                'user': test_user.id,
                'pay_to': address.id,
                'recipient_email': email,
                'invoice_amount': 1000000000,
                'currency': PaymentCurrency.ETH().id,
                'delivery': Invoice.DeliveryChoices.link
            }))
        self.assertEqual(Invoice.objects.count(), 2)
        self.assertEqual(len(mail.outbox), 4)
        self.assertNotEqual(mail.outbox[-1].subject, 'New Invoice Created')
Пример #3
0
    def test_whitelist_address(self):
        from apps.invoices import views
        factory = APIRequestFactory()

        test_user = User.objects.create_user(username="******", email="*****@*****.**", password="******")
        address1 = WhitelistAddress.objects.create(
            address='0xC2C255932A77F4831566822c1f01d9F735CC152E',
            nickname='Mark',
            user=test_user
        )
        address2 = WhitelistAddress.objects.create(
            address='0xEA674fdDe714fd979de3EdF0F56AA9716B898ec8',
            nickname='Barbara',
            user=test_user,
            status=WhitelistAddress.AddressStatus.verified
        )
        
        request = factory.post('/invoices/', {
            'user': test_user.id,
            'pay_to': address1.id,
            'invoice_amount': 1000000000,
            'currency': PaymentCurrency.ETH().id,
        })

        force_authenticate(request, user=test_user)
        response= views.InvoiceViewSet.as_view({'post': 'create'})(request)
        self.assertEqual(Invoice.objects.count(), 0)

        request = factory.post('/invoices/', {
            'user': test_user.id,
            'pay_to': address2.id,
            'invoice_amount': 2000000000,
            'currency': PaymentCurrency.ETH().id,
        })
        
        force_authenticate(request, user=test_user)
        response= views.InvoiceViewSet.as_view({'post': 'create'})(request)
        self.assertEqual(Invoice.objects.count(), 1)
Пример #4
0
    def test_pay_invoice_in_full(self):
        from apps.invoices import views

        factory = APIRequestFactory()
        test_user = User.objects.create_user(username="******",
                                             email="*****@*****.**",
                                             password="******")
        email = '*****@*****.**'

        address = WhitelistAddress.objects.create(
            address='0xEA674fdDe714fd979de3EdF0F56AA9716B898ec8',
            nickname='Barbara',
            user=test_user,
            status=WhitelistAddress.AddressStatus.verified)

        invoice_response = views.InvoiceViewSet.as_view({'post': 'create'})(
            self._post(
                test_user, '/invoices/', {
                    'user': test_user.id,
                    'pay_to': address.id,
                    'recipient_email': email,
                    'invoice_amount': 1000000000,
                    'currency': PaymentCurrency.ETH().id,
                    'status': 'agreed',
                }))
        self.assertEqual(Invoice.objects.count(), 1)

        invoice = Invoice.objects.first()
        self.assertEqual(invoice.status, 'agreed')

        payment = factory.post('/api/payment_received/',
                               get_payment_data(invoice),
                               format='json')
        payment_response = views.PaymentNotification.as_view()(payment)

        #   should mark the invoice status paid
        invoice = Invoice.objects.first()
        self.assertEqual(invoice.status, 'paid_in_full')
        #   should have no balance due
        self.assertEqual(invoice.invoice_amount - invoice.paid_amount, 0)
        #   should have an associated payment
        self.assertEqual(Payment.objects.count(), 1)
Пример #5
0
    def post(self, request, format=None):
        from .models import PaymentCurrency
        tx = request.data['transaction']
        if not tx['parameters_json']:
            return Response({
                'status': 'failed',
                'errors': {
                    'parameters_json': 'No function parameters'
                }
            })

        parameters = json.loads(tx['parameters_json'])
        try:
            invoice = Invoice.objects.get(pk=parameters['values']['invoiceId'])
        except:
            return Response({
                'status': 'failed',
                'errors': {
                    'invoice_id': 'No such invoice'
                }
            })
        if tx['is_token']:
            try:
                currency = PaymentCurrency.objects.get(
                    contract_address=tx['to_address'])
            except PaymentCurrency.DoesNotExist:
                from django.core.mail import mail_admins
                mail_admins('Unexpected Currency Recieved For Invoice',
                            'https://etherscan.io/tx/' + tx['tx_hash'])
                raise
        else:
            currency = PaymentCurrency.ETH()

        amount = int(tx['is_token'] and tx['token_amount'] or tx['value'])

        obj = PaymentSerializer(
            data={
                'invoice':
                invoice.id,
                'block_hash':
                tx['block_hash'],
                'block_number':
                tx['block_number'],
                'from_address':
                tx['from_address'],
                'gas':
                tx['gas'],
                'gas_price':
                tx['gas_price'],
                'tx_hash':
                tx['tx_hash'],
                'tx_input':
                tx['tx_input'],
                'nonce':
                tx['nonce'],
                'to_address':
                tx['is_token'] and tx['token_to'] or tx['to_address'],
                'transaction_date_time':
                tx['created_at'],
                'amount':
                amount,
                'conversion_rate':
                currency.convert_rate(invoice.currency,
                                      eth_usd_price=tx['pricing_info']
                                      ['price']),
                'converted_amount':
                currency.convert_amount(invoice.currency,
                                        amount,
                                        eth_usd_price=tx['pricing_info']
                                        ['price']),
                'parameters_json':
                tx['parameters_json'],
                'currency':
                currency.id,
                'status':
                'confirmed',
                'created_at':
                timezone.now()
            })
        if not obj.is_valid():
            return Response({'status': 'failed', 'errors': obj.errors})
        obj.save()
        invoice.save()
        return Response({'status': 'ok', 'details': obj.data})
Пример #6
0
    def test_invoice_lineitem_serializers(self):

        test_user = User.objects.create_user(username="******",
                                             email="*****@*****.**",
                                             password="******")
        email = '*****@*****.**'

        address = WhitelistAddress.objects.create(
            address='0xEA674fdDe714fd979de3EdF0F56AA9716B898ec8',
            nickname='Audrey',
            user=test_user,
            status=WhitelistAddress.AddressStatus.verified)
        # inv = Invoice.objects.create()
        invoice = Invoice.objects.create(user=test_user,
                                         pay_to=address,
                                         recipient_email=email,
                                         invoice_amount=3000000000,
                                         currency=PaymentCurrency.ETH())
        # item_a = inv.items.create()
        InvoiceItem.objects.create(order=1,
                                   title='First',
                                   quantity=1,
                                   price=1000000000,
                                   invoice=invoice)
        # item_b = inv.items.create()
        InvoiceItem.objects.create(order=2,
                                   title='Second',
                                   quantity=1,
                                   price=2000000000,
                                   invoice=invoice)
        # ser = InvoiceSerializer(inv)
        serializer = InvoiceSerializer(invoice)
        # assert(2, len(ser.data['items']))
        self.assertEqual(2, len(serializer.data['line_items']))

        # make = InvoiceSerializer( { ...invoice_details, items: [...] })
        make = InvoiceSerializer(
            data={
                'user':
                test_user.id,
                'pay_to':
                address.id,
                'recipient_email':
                email,
                'invoice_amount':
                10000000000,
                'currency':
                PaymentCurrency.ETH().id,
                'line_items': [
                    {
                        'order': 1,
                        'title': 'Item1',
                        'quantity': 1,
                        'price': 500000000
                    },
                    {
                        'order': 2,
                        'title': 'Item2',
                        'quantity': 1,
                        'price': 250000000
                    },
                    {
                        'order': 3,
                        'title': 'Item3',
                        'quantity': 1,
                        'price': 250000000
                    },
                ],
            })

        self.assertEqual(True, make.is_valid(),
                         "Serializer should be valid: %s" % make.errors)
        obj = make.save()
        # assert(Invoice.objects.count(), 2)
        self.assertEqual(2, Invoice.objects.count())
        # assert(obj.items.count(), 2)
        self.assertEqual(3, obj.line_items.count())
Пример #7
0
    def test_user_permissions(self):
        from apps.invoices import views

        test_user1 = User.objects.create_user(username="******",
                                              email="*****@*****.**",
                                              password="******")
        test_user2 = User.objects.create_user(username="******",
                                              email="*****@*****.**",
                                              password="******")
        email = '*****@*****.**'

        address1 = WhitelistAddress.objects.create(
            address='0xEA674fdDe714fd979de3EdF0F56AA9716B898ec8',
            nickname='Audrey',
            user=test_user1,
            status=WhitelistAddress.AddressStatus.verified)

        address2 = WhitelistAddress.objects.create(
            address='0xEA674fdDe714fd979de3EdF0F56AA9716B898ec8',
            nickname='Bob',
            user=test_user2,
            status=WhitelistAddress.AddressStatus.verified)

        invoice_a = Invoice.objects.create(user=test_user1,
                                           pay_to=address1,
                                           recipient_email=email,
                                           invoice_amount=1000000000,
                                           currency=PaymentCurrency.ETH())

        invoice_b = Invoice.objects.create(user=test_user1,
                                           pay_to=address1,
                                           recipient_email=email,
                                           invoice_amount=2000000000,
                                           currency=PaymentCurrency.ETH())

        invoice_c = Invoice.objects.create(user=test_user2,
                                           pay_to=address2,
                                           recipient_email=email,
                                           invoice_amount=3000000000,
                                           currency=PaymentCurrency.ETH())

        list_response = views.InvoiceViewSet.as_view({'get': 'list'})(
            self._get(test_user2, '/invoices'))

        self.assertEqual(len(list_response.data['results']), 1)
        #   they should not be able to list eachothers invoices
        self.assertEqual(list_response.data['results'][0]['id'], invoice_c.id)

        self._make_payment(invoice_a)
        self._make_payment(invoice_b)
        self._make_payment(invoice_c)

        self.assertEqual(Payment.objects.count(), 3)

        #   they should not be able to list eachothers payments
        payment_list_response = views.PaymentViewSet.as_view({'get': 'list'})(
            self._get(test_user1, '/payments'))

        self.assertEqual(len(payment_list_response.data['results']), 2)

        payment_list_response = views.PaymentViewSet.as_view({'get': 'list'})(
            self._get(test_user2, '/payments/'))

        self.assertEqual(len(payment_list_response.data['results']), 1)

        #   they should not be able to see eachothers invoice or payment details
        list_response = views.InvoiceViewSet.as_view({'get': 'retrieve'})(
            self._get(test_user2, '/invoices/' + invoice_a.id),
            pk=invoice_a.id)
        self.assertEqual(list_response.data['detail'], 'Not found.')

        payment_id = str(invoice_a.payments.first().id)
        list_response = views.PaymentViewSet.as_view({'get': 'retrieve'})(
            self._get(test_user2, '/payments/' + payment_id), pk=payment_id)
        self.assertEqual(list_response.data['detail'], 'Not found.')

        #   they should not be able to create an invoice with the other user's id
        cross_account_invoice = views.InvoiceViewSet.as_view(
            {'post': 'create'})(self._post(
                test_user1, '/invoices/', {
                    'user': test_user2.id,
                    'pay_to': address2.id,
                    'recipient_email': email,
                    'invoice_amount': 1000000000,
                    'currency': PaymentCurrency.ETH().id,
                }))

        self.assertEqual(cross_account_invoice.data['user'], test_user1.id)
Пример #8
0
    def test_cannot_change_agreed_terms(self):
        from apps.invoices import views

        test_user = User.objects.create_user(username="******",
                                             email="*****@*****.**",
                                             password="******")
        email = '*****@*****.**'

        address = WhitelistAddress.objects.create(
            address='0xEA674fdDe714fd979de3EdF0F56AA9716B898ec8',
            nickname='Barbara',
            user=test_user,
            status=WhitelistAddress.AddressStatus.verified)

        invoice_response = views.InvoiceViewSet.as_view({'post': 'create'})(
            self._post(
                test_user, '/invoices/', {
                    'user': test_user.id,
                    'pay_to': address.id,
                    'recipient_email': email,
                    'invoice_amount': 1000000000,
                    'currency': PaymentCurrency.ETH().id,
                    'status': 'new'
                }))
        self.assertEqual(Invoice.objects.count(), 1)
        self.assertEqual(Invoice.objects.first().invoice_amount, 1000000000)
        invoice = Invoice.objects.first()

        #   should be able to edit invoice details
        invoice_response = views.InvoiceViewSet.as_view({'put': 'update'})(
            self._put(
                test_user, '/invoices/' + invoice.id, {
                    'user': test_user.id,
                    'pay_to': address.id,
                    'recipient_email': email,
                    'invoice_amount': 2000000000,
                    'currency': PaymentCurrency.ETH().id,
                    'status': 'agreed'
                }),
            pk=invoice.id)
        self.assertEqual(Invoice.objects.count(), 1)
        self.assertEqual(Invoice.objects.first().invoice_amount, 2000000000)
        invoice = Invoice.objects.first()

        #   should not be able to edit invoice details
        invoice_response = views.InvoiceViewSet.as_view({'put': 'update'})(
            self._put(
                test_user, '/invoices/' + invoice.id, {
                    'user': test_user.id,
                    'pay_to': address.id,
                    'recipient_email': email,
                    'invoice_amount': 3000000000,
                    'currency': PaymentCurrency.ETH().id,
                    'status': 'agreed'
                }),
            pk=invoice.id)

        invoice = Invoice.objects.first()
        self.assertEqual(invoice_response.data['_'][0],
                         'This invoice has already been agreed upon')
        self.assertEqual(Invoice.objects.count(), 1)
        self.assertEqual(Invoice.objects.first().invoice_amount, 2000000000)