summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarcello Stanisci <stanisci.m@gmail.com>2017-12-13 13:28:36 +0100
committerMarcello Stanisci <stanisci.m@gmail.com>2017-12-13 13:28:36 +0100
commit2cf47305730c606748cc0e9363c7d2c5de93f989 (patch)
tree3ede16e2eb1b9b0d2352db0e075f295e7d23a500
parent7795cbbcf55f69e77270d1fec8b95d6e735e7c61 (diff)
downloadbank-2cf47305730c606748cc0e9363c7d2c5de93f989.tar.gz
bank-2cf47305730c606748cc0e9363c7d2c5de93f989.tar.bz2
bank-2cf47305730c606748cc0e9363c7d2c5de93f989.zip
Done with /reject logic.
-rw-r--r--talerbank/app/tests.py71
-rw-r--r--talerbank/app/views.py16
2 files changed, 57 insertions, 30 deletions
diff --git a/talerbank/app/tests.py b/talerbank/app/tests.py
index 40545e0..2c8729a 100644
--- a/talerbank/app/tests.py
+++ b/talerbank/app/tests.py
@@ -373,43 +373,68 @@ class HistoryTestCase(TestCase):
clear_db()
def test_history(self):
- for ctx in (HistoryContext(expected_resp={"status": 200},
- delta="4", direction="both"),
- HistoryContext(expected_resp={
- "field": "row_id", "value": 6,
- "status": 200}, delta="+1", start="5", direction="both"),
- HistoryContext(expected_resp={
- "field": "wt_subject", "value": "h",
- "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": 200,
- "field": "wt_subject",
- "value": "/reject: reimbursement"},
- delta="+1", direction="credit"),
+ for ctx in (HistoryContext(
+ expected_resp={"status": 200},
+ delta="4", direction="both"),
+ HistoryContext(
+ expected_resp={
+ "fields": [("row_id", 6)],
+ "status": 200},
+ delta="+1", start="5", direction="both"),
+ HistoryContext(
+ expected_resp={
+ "fields": [("wt_subject", "h")],
+ "status": 200},
+ delta="-1", start=9, direction="both"),
+ HistoryContext(
+ expected_resp={"status": 204},
+ delta="1", start="11", direction="both"),
+ HistoryContext(
+ expected_resp={
+ "status": 200,
+ "fields": [("wt_subject", "i"), ("sign", "cancel-")]},
+ start=8, delta="+1", direction="cancel-"),
+ HistoryContext(
+ expected_resp={"status": 204},
+ start=8, delta="-1", direction="cancel-"),
+ HistoryContext(
+ expected_resp={"status": 204},
+ delta="+1", direction="cancel+"),
+ HistoryContext(
+ expected_resp={
+ "status": 200,
+ "fields":
+ [("wt_subject",
+ "/reject: reimbursement")]},
+ delta="+1", direction="credit"),
HistoryContext(expected_resp={"status": 200},
delta="+1", direction="debit")):
- response = self.client.get(reverse("history", urlconf=urls), ctx.urlargs,
- **{"HTTP_X_TALER_BANK_USERNAME": "User",
- "HTTP_X_TALER_BANK_PASSWORD": "Password"})
+ 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")
try:
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,
"Failing request: %s?%s" % \
(response.request["PATH_INFO"],
unquote(response.request["QUERY_STRING"])))
+ # extract expected data from response
+ expected_data = {}
+ response_data = {}
+ for k, v in ctx.expected_resp.get("fields", []):
+ response_data.update({k: data.get(k)})
+ expected_data.update({k: v})
+
+ self.assertEqual(expected_data, response_data)
+
+
class DBAmountSubtraction(TestCase):
def setUp(self):
BankAccount(
diff --git a/talerbank/app/views.py b/talerbank/app/views.py
index d07f021..ed89f6d 100644
--- a/talerbank/app/views.py
+++ b/talerbank/app/views.py
@@ -388,12 +388,11 @@ def serve_history(request, user_account):
# delta
parsed_delta = re.search(r"([\+-])?([0-9]+)",
request.GET.get("delta"))
+ sign = parsed_delta.group(1)
# start
start = int(request.GET.get("start", -1))
- sign = parsed_delta.group(1)
-
- # Assuming Q() means 'true'
+ # translating delta's sign into query object
sign_filter = Q()
if start >= 0:
sign_filter = Q(id__gt=start)
@@ -426,6 +425,8 @@ def serve_history(request, user_account):
if entry.credit_account.account_no == user_account.bankaccount.account_no:
counterpart = entry.debit_account.account_no
sign_ = "+"
+ cancel = "cancel" if entry.cancelled else ""
+ sign_ = cancel + sign_
history.append(dict(counterpart=counterpart,
amount=entry.amount.dump(),
sign=sign_,
@@ -458,6 +459,7 @@ def auth_and_login(request):
return django.contrib.auth.authenticate(username=username,
password=password)
+@transaction.atomic
@csrf_exempt
@require_http_methods(["PUT", "POST"])
@login_via_headers
@@ -476,9 +478,12 @@ def reject(request, user_account):
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
+ trans.cancelled = True
+ trans.save()
try:
wire_transfer(trans.amount, user_account.bankaccount,
- trans.debit_account, "/reject: reimbursement")
+ trans.debit_account, "/reject: reimbursement",
+ reimburses=trans)
except WireTransferException as exc:
# Logging the error is taken care of wire_transfer()
return exc.response
@@ -569,7 +574,6 @@ def wire_transfer(amount,
debit_account,
credit_account,
subject,
- cancelled=False,
reimburses=None):
LOGGER.info("%s => %s, %s, %s" %
(debit_account.account_no,
@@ -584,7 +588,6 @@ def wire_transfer(amount,
credit_account=credit_account,
debit_account=debit_account,
subject=subject,
- cancelled=cancelled,
reimburses=reimburses)
if debit_account.debit:
debit_account.amount.add(amount)
@@ -634,7 +637,6 @@ def wire_transfer(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"},