diff --git a/counter/models.py b/counter/models.py index 0e8753b0..b1d1b691 100644 --- a/counter/models.py +++ b/counter/models.py @@ -21,7 +21,7 @@ import string from datetime import date, datetime, timedelta from datetime import timezone as tz from decimal import Decimal -from typing import Self, Tuple +from typing import Self from dict2xml import dict2xml from django.conf import settings @@ -138,7 +138,7 @@ class Customer(models.Model): return (date.today() - subscription.subscription_end) < timedelta(days=90) @classmethod - def get_or_create(cls, user: User) -> Tuple[Customer, bool]: + def get_or_create(cls, user: User) -> tuple[Customer, bool]: """Work in pretty much the same way as the usual get_or_create method, but with the default field replaced by some under the hood. diff --git a/counter/tests/test_counter.py b/counter/tests/test_counter.py index 93d4663c..ea90a942 100644 --- a/counter/tests/test_counter.py +++ b/counter/tests/test_counter.py @@ -12,7 +12,6 @@ # OR WITHIN THE LOCAL FILE "LICENSE" # # -import re from datetime import timedelta from decimal import Decimal @@ -40,7 +39,7 @@ from counter.models import ( ) -class TestRefilling(TestCase): +class FullClickSetup: @classmethod def setUpTestData(cls): cls.customer = subscriber_user.make() @@ -54,6 +53,11 @@ class TestRefilling(TestCase): cls.counter.sellers.add(cls.barmen) cls.counter.sellers.add(cls.board_admin) + cls.other_counter = baker.make(Counter, type="BAR") + cls.other_counter.sellers.add(cls.barmen) + + cls.yet_another_counter = baker.make(Counter, type="BAR") + cls.customer_old_can_buy = subscriber_user.make() sub = cls.customer_old_can_buy.subscriptions.first() sub.subscription_end = localdate() - timedelta(days=89) @@ -71,6 +75,8 @@ class TestRefilling(TestCase): user=cls.club_admin, ) + +class TestRefilling(FullClickSetup, TestCase): def login_in_bar(self, barmen: User | None = None): used_barman = barmen if barmen is not None else self.board_admin self.client.post( @@ -190,136 +196,12 @@ class TestRefilling(TestCase): assert self.updated_amount(self.customer_old_can_buy) == 1 -class TestCounter(TestCase): - @classmethod - def setUpTestData(cls): - cls.skia = User.objects.filter(username="skia").first() - cls.sli = User.objects.filter(username="sli").first() - cls.krophil = User.objects.filter(username="krophil").first() - cls.richard = User.objects.filter(username="rbatsbak").first() - cls.mde = Counter.objects.filter(name="MDE").first() - cls.foyer = Counter.objects.get(id=2) - - def test_full_click(self): - self.client.post( - reverse("counter:login", kwargs={"counter_id": self.mde.id}), - {"username": self.skia.username, "password": "plop"}, - ) - response = self.client.get( - reverse("counter:details", kwargs={"counter_id": self.mde.id}) - ) - - assert 'class="link-button">S' Kia' in str(response.content) - - counter_token = re.search( - r'name="counter_token" value="([^"]*)"', str(response.content) - ).group(1) - - response = self.client.post( - reverse("counter:details", kwargs={"counter_id": self.mde.id}), - {"code": self.richard.customer.account_id, "counter_token": counter_token}, - ) - counter_url = response.get("location") - refill_url = reverse( - "counter:refilling_create", - kwargs={"customer_id": self.richard.customer.pk}, - ) - - response = self.client.get(counter_url) - assert ">Richard BatsbakClient : Richard Batsbak - Nouveau montant : 3.60" in str( - response_content - ) - - self.client.post( - reverse("counter:login", kwargs={"counter_id": self.mde.id}), - {"username": self.sli.username, "password": "plop"}, - ) - - response = self.client.post( - refill_url, - { - "amount": "5", - "payment_method": "CASH", - "bank": "OTHER", - }, - HTTP_REFERER=counter_url, - ) - assert response.status_code == 302 - - self.client.post( - reverse("counter:login", kwargs={"counter_id": self.foyer.id}), - {"username": self.krophil.username, "password": "plop"}, - ) - - response = self.client.get( - reverse("counter:details", kwargs={"counter_id": self.foyer.id}) - ) - - counter_token = re.search( - r'name="counter_token" value="([^"]*)"', str(response.content) - ).group(1) - - response = self.client.post( - reverse("counter:details", kwargs={"counter_id": self.foyer.id}), - {"code": self.richard.customer.account_id, "counter_token": counter_token}, - ) - counter_url = response.get("location") - refill_url = reverse( - "counter:refilling_create", - kwargs={ - "customer_id": self.richard.customer.pk, - }, - ) - - response = self.client.post( - refill_url, - { - "amount": "5", - "payment_method": "CASH", - "bank": "OTHER", - }, - HTTP_REFERER=counter_url, - ) - assert response.status_code == 403 # Krophil is not board admin - +class TestCounterClick(FullClickSetup, TestCase): def test_annotate_has_barman_queryset(self): """Test if the custom queryset method `annotate_has_barman` works as intended.""" - self.sli.counters.set([self.foyer, self.mde]) - counters = Counter.objects.annotate_has_barman(self.sli) + counters = Counter.objects.annotate_has_barman(self.barmen) for counter in counters: - if counter.name in ("Foyer", "MDE"): + if counter in (self.counter, self.other_counter): assert counter.has_annotated_barman else: assert not counter.has_annotated_barman