from django.test import TestCase, Client from django.urls import reverse from django.contrib.auth import get_user_model from django.contrib.contenttypes.models import ContentType from assets.models.parking import ParkingSpot from assets.models.inspection import AssetInspection, ParkingSpotInspection from interventions.models import Intervention User = get_user_model() class InspectionViewsTestCase(TestCase): def setUp(self): self.user = User.objects.create_user( username='test_inspector', password='password123', first_name='Jean', last_name='Dupont' ) self.client = Client() self.client.login(username='test_inspector', password='password123') self.spot = ParkingSpot.objects.create( code='PK-001', name_fr='Emplacement Vélo Test', spot_type='bicycle', standard_stand_count=5 ) self.spot_ct = ContentType.objects.get_for_model(ParkingSpot) def test_inspections_list_view(self): insp = AssetInspection.objects.create( asset_content_type=self.spot_ct, asset_object_id=self.spot.id, inspector=self.user, result_status='compliant', nature='visual' ) response = self.client.get(reverse('assets:inspections_list')) self.assertEqual(response.status_code, 200) self.assertContains(response, 'PK-001') self.assertContains(response, 'Mes inspections') def test_quick_inspection_map_view(self): response = self.client.get(reverse('assets:quick_inspection_map')) self.assertEqual(response.status_code, 200) self.assertContains(response, 'Inspection Rapide') def test_quick_inspection_map_mobile_view(self): response = self.client.get(reverse('mobile:quick_inspection_map_mobile')) self.assertEqual(response.status_code, 200) self.assertContains(response, 'Inspection Rapide') def test_create_inspection_api_compliant(self): response = self.client.post(reverse('assets:create_inspection_api'), { 'content_type_id': self.spot_ct.id, 'object_id': self.spot.id, 'result_status': 'compliant', 'nature': 'visual', 'inspector_role': 'observer', 'notes': 'Tout est parfait', }) self.assertEqual(response.status_code, 200) data = response.json() self.assertTrue(data['success']) self.spot.refresh_from_db() self.assertEqual(self.spot.inspection_status, 'compliant') self.assertTrue(self.spot.is_compliant) def test_create_inspection_api_damaged_creates_intervention(self): response = self.client.post(reverse('assets:create_inspection_api'), { 'content_type_id': self.spot_ct.id, 'object_id': self.spot.id, 'result_status': 'damaged', 'nature': 'visual', 'notes': 'Arceau tordu', 'create_intervention': 'true', }) self.assertEqual(response.status_code, 200) data = response.json() self.assertTrue(data['success']) self.assertIsNotNone(data['created_intervention_code']) self.spot.refresh_from_db() self.assertEqual(self.spot.inspection_status, 'damaged') self.assertFalse(self.spot.is_compliant) interv = Intervention.objects.filter(code=data['created_intervention_code']).first() self.assertIsNotNone(interv) self.assertEqual(interv.status, 'in_preparation') def test_quick_inspection_map_permissions_can_inspect(self): from common.models import UserConfig, UserThematics, Thematic t_parking = Thematic.objects.get(code='parking') t_other, _ = Thematic.objects.get_or_create(code='publiclighting', defaults={'name_fr': 'Éclairage public'}) user_config, _ = UserConfig.objects.get_or_create(user=self.user) UserThematics.objects.create(user_config=user_config, thematic=t_parking, can_inspect=True) UserThematics.objects.create(user_config=user_config, thematic=t_other, can_inspect=False) response = self.client.get(reverse('assets:quick_inspection_map')) self.assertEqual(response.status_code, 200) self.assertContains(response, 'parking') self.assertNotContains(response, 'publiclighting') def test_no_inspectable_thematic_hides_button_and_returns_403(self): from common.models import UserConfig, UserThematics, Thematic t_parking = Thematic.objects.get(code='parking') user_config, _ = UserConfig.objects.get_or_create(user=self.user) UserThematics.objects.create(user_config=user_config, thematic=t_parking, can_inspect=False) # 1. Page liste des inspections -> Bouton 'Nouvelle inspection' masqué resp_list = self.client.get(reverse('assets:inspections_list')) self.assertEqual(resp_list.status_code, 200) self.assertNotContains(resp_list, reverse('assets:quick_inspection_map')) # 2. Tentative d'accès direct à quick_inspection_map -> HTTP 403 Forbidden resp_map = self.client.get(reverse('assets:quick_inspection_map')) self.assertEqual(resp_map.status_code, 403)