Skip to content

Commit

Permalink
Merge pull request #29 from uppsaladatavetare/bugfix/issue-28
Browse files Browse the repository at this point in the history
Do not crash when the scanned card is not associated with any account
  • Loading branch information
kjagiello authored Mar 5, 2017
2 parents 78875cd + 993d086 commit ea436e8
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 10 deletions.
50 changes: 40 additions & 10 deletions src/foobar/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -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,
'[email protected]',
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', '[email protected]', '123'
)
wallet_obj = WalletFactory.create()
WalletTrxFactory.create(
wallet=wallet_obj,
Expand All @@ -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,
Expand All @@ -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
Expand All @@ -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')
5 changes: 5 additions & 0 deletions src/foobar/views.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)


Expand Down

0 comments on commit ea436e8

Please sign in to comment.