Commit 1f646fa5 authored by Sébastiaan Versteeg's avatar Sébastiaan Versteeg Committed by Luko van der Maas

Increase payments coverage

parent 2ebc4ee3
......@@ -47,7 +47,7 @@ codestyle:
- poetry run python manage.py makemigrations --no-input --check --dry-run
- poetry run python -Wall -mcoverage run manage.py test
- coverage report --fail-under=100 --omit registrations/urls.py registrations/**.py
- coverage report --fail-under=97.32 --omit payments/urls.py payments/**.py
- coverage report --fail-under=100 --omit payments/urls.py payments/**.py
- coverage report
python37-django22:
......
......@@ -15,4 +15,4 @@ source = .
precision = 2
skip_covered = True
show_missing = True
fail_under = 60
fail_under = 65
......@@ -20,7 +20,6 @@ from django.utils.crypto import get_random_string
from django.utils.translation import pgettext_lazy, gettext_lazy as _
from payments.models import BankAccount
from thaliawebsite.settings import THALIA_PAY_ENABLED_PAYMENT_METHOD
from activemembers.models import MemberGroup, MemberGroupMembership
from utils import countries
......@@ -203,11 +202,11 @@ class Member(User):
def tpay_enabled(self):
"""Does this user have a bank account with Direct Debit enabled"""
bank_accounts = BankAccount.objects.filter(owner=self)
if THALIA_PAY_ENABLED_PAYMENT_METHOD and bank_accounts.exists():
if bank_accounts.last().valid:
return True
else:
return False
return (
settings.THALIA_PAY_ENABLED_PAYMENT_METHOD
and bank_accounts.exists()
and bank_accounts.last().valid
)
def _profile_image_path(_instance, _filename):
......
......@@ -30,7 +30,9 @@ def process_payment(
# the processing date
for payment in queryset:
if pay_type != Payment.TPAY or (
pay_type == Payment.TPAY and payment.paid_by.tpay_enabled
pay_type == Payment.TPAY
and payment.paid_by
and payment.paid_by.tpay_enabled
):
payment.type = pay_type
payment.processed_by = processed_by
......
......@@ -4,11 +4,11 @@ from freezegun import freeze_time
from members.models import Member
from payments import services
from payments.models import BankAccount
from payments.models import BankAccount, Payment
@freeze_time("2019-01-01")
@override_settings(SUSPEND_SIGNALS=True)
@override_settings(SUSPEND_SIGNALS=True, THALIA_PAY_ENABLED_PAYMENT_METHOD=True)
class ServicesTest(TestCase):
"""
Test for the services
......@@ -20,6 +20,40 @@ class ServicesTest(TestCase):
def setUpTestData(cls):
cls.member = Member.objects.filter(last_name="Wiggers").first()
def test_process_payment(self):
BankAccount.objects.create(
owner=self.member,
initials="J",
last_name="Test",
iban="NL91ABNA0417164300",
mandate_no="11-2",
valid_from=timezone.now().date() - timezone.timedelta(days=5),
last_used=timezone.now().date() - timezone.timedelta(days=5),
signature="base64,png",
)
p1 = Payment.objects.create(type=Payment.NONE, notes="Test payment", amount=1)
r1 = services.process_payment(
Payment.objects.filter(pk=p1.pk), self.member, Payment.CARD
)
self.assertEqual(r1, [p1])
p2 = Payment.objects.create(type=Payment.NONE, notes="Test payment", amount=2)
r2 = services.process_payment(
Payment.objects.filter(pk=p2.pk), self.member, Payment.TPAY
)
self.assertEqual(r2, [])
p3 = Payment.objects.create(
type=Payment.NONE, notes="Test payment", amount=3, paid_by=self.member
)
self.assertTrue(self.member.tpay_enabled)
r3 = services.process_payment(
Payment.objects.filter(pk=p3.pk), self.member, Payment.TPAY
)
self.assertEqual(r3, [p3])
def test_update_last_used(self):
BankAccount.objects.create(
owner=self.member,
......
from django.contrib.auth import get_user_model
from django.test import Client, TestCase, override_settings
from freezegun import freeze_time
from django.urls import reverse
from freezegun import freeze_time
from members.models import Member
from payments.models import BankAccount
from payments.views import BankAccountCreateView, BankAccountListView
from payments.models import BankAccount, Payment
@freeze_time("2019-01-01")
......@@ -34,7 +33,6 @@ class BankAccountCreateViewTest(TestCase):
)
def setUp(self):
self.view = BankAccountCreateView()
self.client = Client()
self.client.force_login(self.login_user)
......@@ -193,51 +191,6 @@ class BankAccountRevokeViewTest(TestCase):
fixtures = ["members.json"]
@classmethod
def setUpTestData(cls):
cls.login_user = Member.objects.filter(last_name="Wiggers").first()
cls.account = BankAccount.objects.create(
owner=cls.login_user,
initials="J",
last_name="Test",
iban="NL91ABNA0417164300",
)
BankAccount.objects.create(
owner=None,
initials="Someone",
last_name="Else",
iban="BE68539007547034",
bic="NBBEBEBB",
)
def setUp(self):
self.view = BankAccountCreateView()
self.client = Client()
self.client.force_login(self.login_user)
def test_not_logged_in(self):
"""
If there is no logged-in user they should redirect
to the authentication page
"""
self.client.logout()
response = self.client.get(reverse("payments:bankaccount-add"), follow=True)
self.assertEqual(200, response.status_code)
self.assertEqual(
[("/user/login/?next=" + reverse("payments:bankaccount-add"), 302)],
response.redirect_chain,
)
@override_settings(SUSPEND_SIGNALS=True)
class BankAccountListViewTest(TestCase):
"""
Test for the BankAccountListView
"""
fixtures = ["members.json"]
@classmethod
def setUpTestData(cls):
cls.login_user = Member.objects.filter(last_name="Wiggers").first()
......@@ -261,7 +214,6 @@ class BankAccountListViewTest(TestCase):
def setUp(self):
self.account1.refresh_from_db()
self.account2.refresh_from_db()
self.view = BankAccountListView()
self.client = Client()
self.client.force_login(self.login_user)
......@@ -348,3 +300,122 @@ class BankAccountListViewTest(TestCase):
.first()
.valid
)
@override_settings(SUSPEND_SIGNALS=True)
class BankAccountListViewTest(TestCase):
"""
Test for the BankAccountListView
"""
fixtures = ["members.json"]
@classmethod
def setUpTestData(cls):
cls.login_user = Member.objects.filter(last_name="Wiggers").first()
cls.account1 = BankAccount.objects.create(
owner=cls.login_user,
initials="J1",
last_name="Test",
iban="NL91ABNA0417164300",
)
cls.account2 = BankAccount.objects.create(
owner=Member.objects.exclude(last_name="Wiggers").first(),
initials="J2",
last_name="Test",
iban="BE68539007547034",
bic="NBBEBEBB",
valid_from="2019-03-01",
signature="sig",
mandate_no="11-2",
)
def setUp(self):
self.account1.refresh_from_db()
self.account2.refresh_from_db()
self.client = Client()
self.client.force_login(self.login_user)
def test_not_logged_in(self):
"""
If there is no logged-in user they should redirect
to the authentication page
"""
self.client.logout()
response = self.client.post(reverse("payments:bankaccount-list"), follow=True,)
self.assertEqual(200, response.status_code)
self.assertEqual(
[("/user/login/?next=" + reverse("payments:bankaccount-list"), 302,)],
response.redirect_chain,
)
def test_accounts(self):
"""
The page should show only accounts of the logged-in user
"""
response = self.client.get(reverse("payments:bankaccount-list"), follow=True,)
self.assertEqual(200, response.status_code)
self.assertContains(response, "NL91ABNA0417164300")
self.assertNotContains(response, "BE68539007547034")
@freeze_time("2019-04-01")
@override_settings(SUSPEND_SIGNALS=True)
class PaymentListViewTest(TestCase):
"""
Test for the PaymentListView
"""
fixtures = ["members.json"]
@classmethod
def setUpTestData(cls):
cls.login_user = Member.objects.filter(last_name="Wiggers").first()
cls.account1 = BankAccount.objects.create(
owner=cls.login_user,
initials="J1",
last_name="Test",
iban="NL91ABNA0417164300",
valid_from="2019-03-01",
signature="sig",
mandate_no="11-2",
)
cls.payment1 = Payment.objects.create(
paid_by=cls.login_user,
notes="Testing Payment 1",
amount=10,
type=Payment.CARD,
processing_date="2019-03-06",
)
def setUp(self):
self.account1.refresh_from_db()
self.payment1.refresh_from_db()
self.client = Client()
self.client.force_login(self.login_user)
def test_not_logged_in(self):
"""
If there is no logged-in user they should redirect
to the authentication page
"""
self.client.logout()
response = self.client.post(reverse("payments:payment-list"), follow=True,)
self.assertEqual(200, response.status_code)
self.assertEqual(
[("/user/login/?next=" + reverse("payments:payment-list"), 302,)],
response.redirect_chain,
)
def test_contents(self):
"""
Test if the view shows payments
"""
response = self.client.get(
reverse("payments:payment-list", kwargs={"year": 2019, "month": 3}),
follow=True,
)
self.assertEqual(200, response.status_code)
self.assertContains(response, "Testing Payment 1")
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment