From 634967cfb5a056e226b9434cc6148b0d150c0af7 Mon Sep 17 00:00:00 2001 From: emi-hi Date: Fri, 20 Dec 2024 17:48:00 -0800 Subject: [PATCH] chore: adds tests for vehicle viewset --- backend/api/tests/test_vehicles.py | 97 ++++++++++++++++++++++++------ backend/api/viewsets/vehicle.py | 2 +- 2 files changed, 78 insertions(+), 21 deletions(-) diff --git a/backend/api/tests/test_vehicles.py b/backend/api/tests/test_vehicles.py index 986306dd9..35fc66e29 100644 --- a/backend/api/tests/test_vehicles.py +++ b/backend/api/tests/test_vehicles.py @@ -4,38 +4,62 @@ from ..models.vehicle import Vehicle from ..models.user_role import UserRole from ..models.role import Role +from ..models.model_year import ModelYear +from ..models.vehicle_zev_type import ZevType +from ..models.vehicle_class import VehicleClass from unittest.mock import patch +from django.db.models import Q +from django.urls import reverse class TestVehicles(BaseTestCase): - def test_get_vehicles(self): + def setUp(self): + super().setUp() UserRole.objects.create( user_profile_id=self.users['RTAN_BCEID'].id, role=Role.objects.get( role_code='ZEVA User', ) ) - - response = self.clients['RTAN_BCEID'].get("/api/vehicles") - self.assertEqual(response.status_code, 200) - - def test_update_vehicle_state(self): UserRole.objects.create( user_profile_id=self.users['RTAN_BCEID'].id, role=Role.objects.get( role_code='Signing Authority', ) ) - UserRole.objects.create( - user_profile_id=self.users['RTAN_BCEID'].id, - role=Role.objects.get( - role_code='ZEVA User', - ) + self.org1 = self.users['RTAN_BCEID'].organization + my_2023 = ModelYear.objects.get( + name='2023' ) - org1 = self.users['RTAN_BCEID'].organization + self.vehicle = Vehicle.objects.create( + make="Test Manufacturer", + model_name="Test Vehicle", + model_year=my_2023, + weight_kg=1000, + vehicle_zev_type=ZevType.objects.all().first(), + organization=self.org1, + range=200, + vehicle_class_code=VehicleClass.objects.all().first(), + validation_status='DRAFT' + ) + def test_get_vehicles(self): + response = self.clients['RTAN_BCEID'].get("/api/vehicles") + filtered_vehicles = Vehicle.objects.filter( + organization=self.org1 + ) + self.assertEqual(response.status_code, 200) + self.assertEqual(len(response.data), filtered_vehicles.count()) - vehicle = Vehicle.objects.filter(organization=org1).first() + def test_idir_get_vehicles(self): + response = self.clients['RTAN'].get("/api/vehicles") + filtered_vehicles = Vehicle.objects.exclude( + Q(validation_status="DRAFT") | Q(validation_status="DELETED") + ) + self.assertEqual(response.status_code, 200) + self.assertEqual(len(response.data), filtered_vehicles.count()) + def test_update_vehicle_state(self): + vehicle = Vehicle.objects.filter(organization=self.org1).first() if vehicle: # have to reset the status first to draft vehicle.validation_status = 'DRAFT' @@ -50,31 +74,31 @@ def test_update_vehicle_state(self): self.assertEqual(response.status_code, 200) # Test that email method is called properly mock_send_zev_model_emails.assert_called() - + #bceid can get a list of vehicles response = self.clients['RTAN_BCEID'].get("/api/vehicles") self.assertEqual(response.status_code, 200) - + #bceid cannot change the status of a vehicle response = self.clients['RTAN_BCEID'].patch( "/api/vehicles/{}/state_change".format(vehicle.id), content_type='application/json', data=json.dumps({'validation_status': "VALIDATED"}) ) self.assertEqual(response.status_code, 403) - + + #idir user can change the status of a vehicle response = self.clients['RTAN'].patch( "/api/vehicles/{}/state_change".format(vehicle.id), content_type='application/json', data=json.dumps({'validation_status': "VALIDATED"}) ) self.assertEqual(response.status_code, 200) - + #bceid user can get the details of a vehicle response = self.clients['RTAN_BCEID'].get( "/api/vehicles/{}".format(vehicle.id) ) self.assertEqual(response.status_code, 200) def test_create_vehicle(self): - organization = self.users['RTAN_BCEID'].organization response = self.clients['RTAN_BCEID'].post( "/api/vehicles", content_type='application/json', @@ -91,7 +115,6 @@ def test_create_vehicle(self): self.assertEqual(response.status_code, 201) def test_create_vehicle_insufficient_data(self): - organization = self.users['RTAN_BCEID'].organization response = self.clients['RTAN_BCEID'].post( "/api/vehicles", content_type='application/json', @@ -105,7 +128,7 @@ def test_create_vehicle_insufficient_data(self): self.assertEqual(response.status_code, 400) def test_create_vehicle_check_data_match(self): - organization = self.users['RTAN_BCEID'].organization + response = self.clients['RTAN_BCEID'].post( "/api/vehicles", content_type='application/json', @@ -130,3 +153,37 @@ def test_create_vehicle_check_data_match(self): vehicle_zev_type__vehicle_zev_code="PHEV" ) self.assertGreaterEqual(len(car), 1) + + def test_zev_types(self): + response = self.clients['RTAN_BCEID'].get(reverse('vehicle-zev-types')) + self.assertEqual(response.status_code, 200) + self.assertTrue(len(response.data) > 0) + + def test_vehicle_classes(self): + response = self.clients['RTAN_BCEID'].get(reverse('vehicle-classes')) + filtered_classes = VehicleClass.objects.all() + self.assertEqual(response.status_code, 200) + self.assertEquals(filtered_classes.count(), len(response.data)) + + def test_vehicle_years(self): + response = self.clients['RTAN_BCEID'].get(reverse('vehicle-years')) + filtered_years = ModelYear.objects.all().exclude(name__in=['2017', '2018']) + self.assertEqual(response.status_code, 200) + self.assertEquals(filtered_years.count(), len(response.data)) + + def test_minio_url(self): + response = self.clients['RTAN_BCEID'].get( + reverse('vehicle-minio-url', kwargs={'pk': self.vehicle.id}) + ) + self.assertEqual(response.status_code, 200) + self.assertIn('url', response.data) + self.assertIn('minio_object_name', response.data) + + def test_is_active_change(self): + response = self.clients['RTAN_BCEID'].patch( + reverse('vehicle-is-active-change', kwargs={'pk': self.vehicle.id}), + content_type='application/json', + data=json.dumps({'is_active': False}) + ) + self.assertEqual(response.status_code, 200) + self.assertFalse(Vehicle.objects.get(pk=self.vehicle.id).is_active) \ No newline at end of file diff --git a/backend/api/viewsets/vehicle.py b/backend/api/viewsets/vehicle.py index 7ffc8df28..65376bf5d 100644 --- a/backend/api/viewsets/vehicle.py +++ b/backend/api/viewsets/vehicle.py @@ -74,7 +74,7 @@ def get_queryset(self): @method_decorator(permission_required('VIEW_ZEV')) def list(self, request): """ - Get all the organizations + Get the vehicles """ vehicles = self.get_queryset()