Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: adds tests for vehicle viewset #2405

Open
wants to merge 1 commit into
base: release-1.65.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 77 additions & 20 deletions backend/api/tests/test_vehicles.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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)
2 changes: 1 addition & 1 deletion backend/api/viewsets/vehicle.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down