From 993d08640a27d8ad86a12e60a2959a0f6669f9ee Mon Sep 17 00:00:00 2001 From: Krzysztof Jagiello Date: Sun, 5 Mar 2017 17:39:25 +0100 Subject: [PATCH] Do not crash when the scanned card is not associated with any account --- src/foobar/tests/test_views.py | 50 +++++++++++++++++++++++++++------- src/foobar/views.py | 5 ++++ 2 files changed, 45 insertions(+), 10 deletions(-) diff --git a/src/foobar/tests/test_views.py b/src/foobar/tests/test_views.py index c7d091f..931d58f 100644 --- a/src/foobar/tests/test_views.py +++ b/src/foobar/tests/test_views.py @@ -1,19 +1,50 @@ -from django.test import TestCase -from wallet.tests.factories import WalletFactory, WalletTrxFactory -from wallet.enums import TrxType +from unittest import mock from moneyed import Money from django.contrib.auth.models import User +from django.test import TestCase from django.urls import reverse -from unittest import mock +from wallet.tests.factories import WalletFactory, WalletTrxFactory +from wallet.enums import TrxType +from . import factories class FoobarViewTest(TestCase): + TESTUSER_NAME = 'the_baconator' + TESTUSER_PASS = '123' + + def setUp(self): + self.user = User.objects.create_superuser( + self.TESTUSER_NAME, + 'bacon@foobar.com', + self.TESTUSER_PASS + ) + self.client.login( + username=self.TESTUSER_NAME, + password=self.TESTUSER_PASS + ) + + @mock.patch('foobar.api.get_account') + def test_account_for_card(self, mock_get_account): + url = reverse('account_for_card', kwargs={'card_id': 1337}) + mock_get_account.return_value = None + response = self.client.get(url, follow=True) + self.assertRedirects( + response, + reverse('admin:foobar_account_changelist') + ) + self.assertEqual(len(response.context['messages']), 1) + account = factories.AccountFactory() + mock_get_account.return_value = account + response = self.client.get(url, follow=True) + self.assertRedirects( + response, + reverse('admin:foobar_account_change', args=(account.id,)) + ) + self.assertEqual(len(response.context['messages']), 0) + @mock.patch('foobar.api.calculate_correction') @mock.patch('foobar.api.make_deposit_or_withdrawal') def test_wallet_management(self, mock_deposit_withdrawal, mock_correction): - user = User.objects.create_superuser( - 'the_baconator', 'bacon@foobar.com', '123' - ) wallet_obj = WalletFactory.create() WalletTrxFactory.create( wallet=wallet_obj, @@ -23,7 +54,6 @@ def test_wallet_management(self, mock_deposit_withdrawal, mock_correction): url = reverse('wallet_management', kwargs={'obj_id': wallet_obj.owner_id}) cl = self.client - cl.login(username='the_baconator', password='123') # Test that deposit or withdrawal # is not called if balance will get negative response = cl.post(url, @@ -44,7 +74,7 @@ def test_wallet_management(self, mock_deposit_withdrawal, mock_correction): 'balance_0': ['1000']}) mock_correction.assert_called_with(Money(1000, 'SEK'), wallet_obj.owner_id, - user, + self.user, 'test') # Test that deposit or withdrawal form post is correct and # calls fucnction with correct params @@ -56,5 +86,5 @@ def test_wallet_management(self, mock_deposit_withdrawal, mock_correction): mock_deposit_withdrawal.assert_called_with( Money(100, 'SEK'), wallet_obj.owner_id, - user, + self.user, 'test') diff --git a/src/foobar/views.py b/src/foobar/views.py index eb32bd7..0063d6d 100644 --- a/src/foobar/views.py +++ b/src/foobar/views.py @@ -1,6 +1,7 @@ from django.contrib.auth.decorators import permission_required from django.contrib.admin.views.decorators import staff_member_required from django.shortcuts import redirect +from django.utils.translation import ugettext_lazy as _ from . import api from django.shortcuts import render from .forms import CorrectionForm, DepositForm @@ -13,6 +14,10 @@ @permission_required('foobar.change_account') def account_for_card(request, card_id): account_obj = api.get_account(card_id) + if account_obj is None: + messages.add_message(request, messages.ERROR, + _('No account has been found for given card.')) + return redirect('admin:foobar_account_changelist') return redirect('admin:foobar_account_change', account_obj.id)