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')