gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] [taler-bank] 01/02: add fields to indicate whether a transa


From: gnunet
Subject: [GNUnet-SVN] [taler-bank] 01/02: add fields to indicate whether a transaction is cancelled, and if it reimburses another one.
Date: Wed, 13 Dec 2017 13:29:19 +0100

This is an automated email from the git hooks/post-receive script.

marcello pushed a commit to branch master
in repository bank.

commit 7795cbbcf55f69e77270d1fec8b95d6e735e7c61
Author: Marcello Stanisci <address@hidden>
AuthorDate: Tue Dec 12 16:54:22 2017 +0100

    add fields to indicate whether a transaction is
    cancelled, and if it reimburses another one.
---
 talerbank/app/models.py |  5 ++++
 talerbank/app/tests.py  | 61 ++++++++++++++++++++++++++++++++-----------------
 talerbank/app/views.py  | 27 ++++++++++++++++------
 3 files changed, 65 insertions(+), 28 deletions(-)

diff --git a/talerbank/app/models.py b/talerbank/app/models.py
index f8c5c47..4abfcda 100644
--- a/talerbank/app/models.py
+++ b/talerbank/app/models.py
@@ -80,3 +80,8 @@ class BankTransaction(models.Model):
     subject = models.CharField(default="(no subject given)", max_length=200)
     date = models.DateTimeField(auto_now=True, db_index=True)
     cancelled = models.BooleanField(default=False)
+    reimburses = models.ForeignKey("BankTransaction",
+                                   on_delete=models.CASCADE,
+                                   related_name="reimburser",
+                                   default=None,
+                                   null=True)
diff --git a/talerbank/app/tests.py b/talerbank/app/tests.py
index 5f9e67b..40545e0 100644
--- a/talerbank/app/tests.py
+++ b/talerbank/app/tests.py
@@ -17,11 +17,13 @@
 import json
 import timeit
 import logging
+from django.db import connection
 from django.test import TestCase, Client
 from django.urls import reverse
 from django.conf import settings
 from django.contrib.auth.models import User
 from mock import patch, MagicMock
+from urllib.parse import unquote
 from .models import BankAccount, BankTransaction
 from . import urls
 from .views import wire_transfer
@@ -31,12 +33,12 @@ LOGGER = logging.getLogger()
 LOGGER.setLevel(logging.WARNING)
 
 def clear_db():
-    # FIXME: this way we do not reset autoincrement
-    # fields.
     User.objects.all().delete()
     BankAccount.objects.all().delete()
     BankTransaction.objects.all().delete()
-
+    with connection.cursor() as cursor:
+        cursor.execute("ALTER SEQUENCE app_bankaccount_account_no_seq RESTART")
+        cursor.execute("ALTER SEQUENCE app_banktransaction_id_seq RESTART")
 class WithdrawTestCase(TestCase):
     def setUp(self):
         BankAccount(
@@ -138,8 +140,8 @@ class RegisterTestCase(TestCase):
 
     def setUp(self):
         BankAccount(
-            user=User.objects.create_user(username='Bank'),
-            account_no=1).save()
+            user=User.objects.create_user(
+                username='Bank')).save()
 
     def tearDown(self):
         clear_db()
@@ -162,8 +164,8 @@ class RegisterWrongCurrencyTestCase(TestCase):
         # Note, config has KUDOS as currency.
         BankAccount(
             user=User.objects.create_user(username='Bank'),
-            amount=Amount('WRONGCURRENCY'),
-            account_no=1).save()
+            amount=Amount('WRONGCURRENCY')).save()
+        # Takes account_no = 1, as the first one.
 
     def tearDown(self):
         clear_db()
@@ -342,25 +344,35 @@ class HistoryTestCase(TestCase):
             user=User.objects.create_user(
                 username='User',
                 password="Password"),
-            amount=Amount(settings.TALER_CURRENCY, 100),
-            account_no=1)
+            amount=Amount(settings.TALER_CURRENCY, 100))
         debit_account.save()
         credit_account = BankAccount(
             user=User.objects.create_user(
                 username='User0',
-                password="Password0"),
-            account_no=2)
+                password="Password0"))
         credit_account.save()
-        for subject in ("a", "b", "c", "d", "e", "f", "g", "h"):
+        for subject in ("a", "b", "c", "d", "e", "f", "g", "h", "i"):
             wire_transfer(Amount(settings.TALER_CURRENCY, 1),
                           debit_account,
                           credit_account, subject)
+        # reject transaction 'i'.
+        trans_i = BankTransaction.objects.get(subject="i")
+        self.client = Client()
+        self.client.post(
+            reverse("reject", urlconf=urls),
+            data='{"auth": {"type": "basic"}, \
+                   "row_id": %d, \
+                   "account_number": 44}' % trans_i.id, # Ignored
+            content_type="application/json",
+            follow=True,
+            **{"HTTP_X_TALER_BANK_USERNAME": "User0",
+               "HTTP_X_TALER_BANK_PASSWORD": "Password0"})
+
 
     def tearDown(self):
         clear_db()
 
     def test_history(self):
-        client = Client()
         for ctx in (HistoryContext(expected_resp={"status": 200},
                                    delta="4", direction="both"),
                     HistoryContext(expected_resp={
@@ -368,16 +380,19 @@ class HistoryTestCase(TestCase):
                         "status": 200}, delta="+1", start="5", 
direction="both"),
                     HistoryContext(expected_resp={
                         "field": "wt_subject", "value": "h",
-                        "status": 200}, delta="-1", direction="both"),
+                        "status": 200}, delta="-1", start=9, direction="both"),
                     HistoryContext(expected_resp={"status": 204},
                                    delta="1", start="11", direction="both"),
                     HistoryContext(expected_resp={"status": 204},
                                    delta="+1", direction="cancel+"),
-                    HistoryContext(expected_resp={"status": 204},
+                    HistoryContext(expected_resp={
+                        "status": 200,
+                        "field": "wt_subject",
+                        "value": "/reject: reimbursement"},
                                    delta="+1", direction="credit"),
                     HistoryContext(expected_resp={"status": 200},
                                    delta="+1", direction="debit")):
-            response = client.get(reverse("history", urlconf=urls), 
ctx.urlargs,
+            response = self.client.get(reverse("history", urlconf=urls), 
ctx.urlargs,
                                   **{"HTTP_X_TALER_BANK_USERNAME": "User",
                                      "HTTP_X_TALER_BANK_PASSWORD": "Password"})
             data = response.content.decode("utf-8")
@@ -385,11 +400,15 @@ class HistoryTestCase(TestCase):
                 data = json.loads(data)["data"][0]
             except (json.JSONDecodeError, KeyError):
                 data = {}
-
-            self.assertEqual(data.get(ctx.expected_resp.get("field")),
-                             ctx.expected_resp.get("value"))
-            self.assertEqual(ctx.expected_resp.get("status"),
-                             response.status_code)
+            self.assertEqual(
+                data.get(ctx.expected_resp.get("field")),
+                ctx.expected_resp.get("value"))
+            self.assertEqual(
+                ctx.expected_resp.get("status"),
+                response.status_code,
+                "Failing request: %s?%s" % \
+                    (response.request["PATH_INFO"],
+                     unquote(response.request["QUERY_STRING"])))
 
 class DBAmountSubtraction(TestCase):
     def setUp(self):
diff --git a/talerbank/app/views.py b/talerbank/app/views.py
index 39d35cf..d07f021 100644
--- a/talerbank/app/views.py
+++ b/talerbank/app/views.py
@@ -472,15 +472,13 @@ def reject(request, user_account):
         trans = BankTransaction.objects.get(id=data["row_id"])
     except BankTransaction.DoesNotExist:
         return JsonResponse({"error": "unknown transaction"}, status=404)
-
     if trans.credit_account.account_no != user_account.bankaccount.account_no:
         LOGGER.error("you can only reject a transaction where you _got_ money")
         return JsonResponse({"error": "you can only reject a transaction where 
you _got_ money"},
                             status=401) # Unauthorized
     try:
         wire_transfer(trans.amount, user_account.bankaccount,
-                      trans.debit_account, "/reject: reimbursement",
-                      cancelled=True)
+                      trans.debit_account, "/reject: reimbursement")
     except WireTransferException as exc:
         # Logging the error is taken care of wire_transfer()
         return exc.response
@@ -557,13 +555,22 @@ class WireTransferException(Exception):
         self.response = response
         super().__init__()
 
-def wire_transfer(amount, debit_account, credit_account, subject, **kwargs):
+def wire_transfer(amount,
+                  debit_account,
+                  credit_account,
+                  subject,
+                  **kwargs):
 
     def err_cb(exc, resp):
         LOGGER.error(str(exc))
         raise WireTransferException(exc, resp)
 
-    def wire_transfer_internal(amount, debit_account, credit_account, subject):
+    def wire_transfer_internal(amount,
+                               debit_account,
+                               credit_account,
+                               subject,
+                               cancelled=False,
+                               reimburses=None):
         LOGGER.info("%s => %s, %s, %s" %
                     (debit_account.account_no,
                      credit_account.account_no,
@@ -577,7 +584,8 @@ def wire_transfer(amount, debit_account, credit_account, 
subject, **kwargs):
                                            credit_account=credit_account,
                                            debit_account=debit_account,
                                            subject=subject,
-                                           cancelled=kwargs.get("cancelled", 
False))
+                                           cancelled=cancelled,
+                                           reimburses=reimburses)
         if debit_account.debit:
             debit_account.amount.add(amount)
 
@@ -622,7 +630,12 @@ def wire_transfer(amount, debit_account, credit_account, 
subject, **kwargs):
         return transaction_item
 
     try:
-        return wire_transfer_internal(amount, debit_account, credit_account, 
subject)
+        return wire_transfer_internal(amount,
+                                      debit_account,
+                                      credit_account,
+                                      subject,
+                                      kwargs.get("cancelled", False),
+                                      kwargs.get("reimburses", None))
     except (CurrencyMismatch, BadFormatAmount) as exc:
         err_cb(exc, JsonResponse({"error": "internal server error"},
                                  status=500))

-- 
To stop receiving notification emails like this one, please contact
address@hidden



reply via email to

[Prev in Thread] Current Thread [Next in Thread]