loko/loko/authorizations/tests.py

169 lines
6.2 KiB
Python

from django.test import TestCase
from django.utils import timezone
from django.contrib.gis.geos import MultiPolygon, Polygon
from django.contrib.auth import get_user_model
from django.urls import reverse
from datetime import datetime
from authorizations.models import RoadAuthorization, RoadAuthorizationDate
from common.models import UserConfig, AppView
User = get_user_model()
class RoadAuthorizationDateTest(TestCase):
def setUp(self):
# Create a simple MultiPolygon
poly = Polygon(((0, 0), (0, 1), (1, 1), (1, 0), (0, 0)))
multipoly = MultiPolygon(poly, srid=4326)
self.auth = RoadAuthorization.objects.create(
type=2,
description="Test authorization",
applicant_name="John Doe",
applicant_email="john@example.com",
applicant_address="123 Main St",
location="Brussels",
geom=multipoly
)
def test_str_representation_both_dates(self):
begin = timezone.make_aware(datetime(2026, 6, 29, 10, 0))
end = timezone.make_aware(datetime(2026, 6, 29, 18, 0))
auth_date = RoadAuthorizationDate(
authorization=self.auth,
begin_date=begin,
end_date=end
)
self.assertEqual(str(auth_date), "29/06/2026 10:00 → 29/06/2026 18:00")
def test_str_representation_null_end_date(self):
begin = timezone.make_aware(datetime(2026, 6, 29, 10, 0))
auth_date = RoadAuthorizationDate(
authorization=self.auth,
begin_date=begin,
end_date=None
)
self.assertEqual(str(auth_date), "29/06/2026 10:00 → —")
def test_str_representation_null_begin_date(self):
end = timezone.make_aware(datetime(2026, 6, 29, 18, 0))
auth_date = RoadAuthorizationDate(
authorization=self.auth,
begin_date=None,
end_date=end
)
self.assertEqual(str(auth_date), "— → 29/06/2026 18:00")
class RoadAuthorizationListAgentTest(TestCase):
def setUp(self):
poly = Polygon(((0, 0), (0, 1), (1, 1), (1, 0), (0, 0)))
self.multipoly = MultiPolygon(poly, srid=4326)
# Users
self.staff_user = User.objects.create_user(
username="staff_auth",
password="password123",
first_name="Staff",
last_name="Member"
)
self.config = UserConfig.objects.create(user=self.staff_user, is_intern=True)
self.auth_view, _ = AppView.objects.get_or_create(
code="authorizations",
defaults={"name_fr": "Autorisations", "name_nl": "Autorisaties", "url_name": "authorizations:authorization_list"}
)
self.config.accessible_views.add(self.auth_view)
self.agent1 = User.objects.create_user(
username="adupont",
password="password123",
first_name="Alice",
last_name="Dupont"
)
self.agent2 = User.objects.create_user(
username="bsmith",
password="password123",
first_name="Bob",
last_name="Smith"
)
# Authorizations
self.auth1 = RoadAuthorization.objects.create(
type=2,
description="Terrasse Alice",
applicant_name="Demandeur Alpha",
applicant_email="alpha@example.com",
applicant_address="Rue 1",
location="Bruxelles Centre",
geom=self.multipoly,
agent=self.agent1
)
self.auth2 = RoadAuthorization.objects.create(
type=21,
description="Échafaudage Bob",
applicant_name="Demandeur Beta",
applicant_email="beta@example.com",
applicant_address="Rue 2",
location="Ixelles",
geom=self.multipoly,
agent=self.agent2
)
self.auth3 = RoadAuthorization.objects.create(
type=22,
description="Conteneur Non Assigné",
applicant_name="Demandeur Gamma",
applicant_email="gamma@example.com",
applicant_address="Rue 3",
location="Schaerbeek",
geom=self.multipoly,
agent=None
)
self.client.login(username="staff_auth", password="password123")
self.url = reverse('authorizations:authorization_list')
def test_list_view_displays_agent_short_names(self):
response = self.client.get(self.url)
self.assertEqual(response.status_code, 200)
content = response.content.decode('utf-8')
# Alice Dupont -> Alice D.
self.assertIn("Alice D.", content)
# Bob Smith -> Bob S.
self.assertIn("Bob S.", content)
# Auth3 has no agent -> should display dash "—"
self.assertIn("—", content)
def test_filter_by_specific_agent(self):
response = self.client.get(self.url, {'agent': [str(self.agent1.pk)]})
self.assertEqual(response.status_code, 200)
auths = list(response.context['authorizations'])
self.assertIn(self.auth1, auths)
self.assertNotIn(self.auth2, auths)
self.assertNotIn(self.auth3, auths)
def test_filter_by_unassigned_agent(self):
response = self.client.get(self.url, {'agent': ['unassigned']})
self.assertEqual(response.status_code, 200)
auths = list(response.context['authorizations'])
self.assertIn(self.auth3, auths)
self.assertNotIn(self.auth1, auths)
self.assertNotIn(self.auth2, auths)
def test_filter_by_multiple_agents(self):
response = self.client.get(self.url, {'agent': [str(self.agent1.pk), 'unassigned']})
self.assertEqual(response.status_code, 200)
auths = list(response.context['authorizations'])
self.assertIn(self.auth1, auths)
self.assertIn(self.auth3, auths)
self.assertNotIn(self.auth2, auths)
def test_search_by_agent_name(self):
response = self.client.get(self.url, {'search_text': 'Alice'})
self.assertEqual(response.status_code, 200)
auths = list(response.context['authorizations'])
self.assertIn(self.auth1, auths)
self.assertNotIn(self.auth2, auths)
self.assertNotIn(self.auth3, auths)