Code development platform for open source projects from the European Union institutions

Skip to content
Snippets Groups Projects
Commit 7c2c6706 authored by Frederico SEQUEIRA's avatar Frederico SEQUEIRA Committed by Magdalena GOMEZ
Browse files

:white_check_mark: :bug: Add working API tests and bug fixes

parent 9d0753d2
No related branches found
No related tags found
1 merge request!29✅ Virtual machine type tests
......@@ -152,7 +152,7 @@ class DomainNamesForm(NetBoxModelForm):
)
domain_names = JSONField(
label=_('Domain Names'),
required=False
required=True
)
def __init__(self, *args, **kwargs):
......@@ -186,6 +186,7 @@ class DomainNamesForm(NetBoxModelForm):
super().clean()
vm = self.cleaned_data.get("virtual_machine")
domain_names = self.cleaned_data.get("domain_names")
#Check if Virtual Machine is assigned corretly
if not vm:
......@@ -193,6 +194,11 @@ class DomainNamesForm(NetBoxModelForm):
{"__all__": "Can't assign more than one group of domain names to the same Virtual Machine"},
)
if not domain_names:
raise ValidationError(
{"__all__": "Domain names should not be empty"},
)
def save(self, *args, **kwargs):
"""Set assigned object and save"""
# Set assigned object
......
# Generated by Django 4.2.16 on 2025-01-17 17:00
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('netbox_sys_plugin', '0003_alter_providertypeextraconfig_assigned_object_type_and_more'),
]
operations = [
migrations.AlterField(
model_name='domainnames',
name='domain_names',
field=models.JSONField(blank=True, default={}),
preserve_default=False,
),
]
......@@ -120,7 +120,7 @@ class DomainNames(NetBoxModel):
domain_names = models.JSONField(
blank=True,
null=True,
null=False,
)
assigned_object_type = models.ForeignKey(
......
......@@ -78,7 +78,7 @@ class ProviderTypeExtraConfigFormTestCase(
def test_invalid_extra_config_format(self):
"""
Test invalid maintenance window invalid format.
Test invalid extra config invalid format.
"""
# Create VLAN and VLANGroup for testing
......
......@@ -7,8 +7,8 @@ from virtualization.models import VirtualMachine, Cluster, ClusterType
from netbox_sys_plugin.models import DomainNames
from ..base import BaseAPITestCase
class VirtualMachineMaintenanceApiTestCase(BaseAPITestCase):
"""Test suite for VirtualMachineMaintenance API"""
class VirtualMachinedomain_namesApiTestCase(BaseAPITestCase):
"""Test suite for VirtualMachinedomain_names API"""
model = DomainNames
brief_fields = ["domain_names", "assigned_object_id", "assigned_object_type"]
......@@ -65,7 +65,7 @@ class VirtualMachineMaintenanceApiTestCase(BaseAPITestCase):
cluster=cls.cluster
)
# Create maintenance entries linked to the VirtualMachine
# Create domain_names entries linked to the VirtualMachine
DomainNames.objects.create(
domain_names={'domain1':'ec.test.test','domain2':'ec.test2.test2'},
assigned_object=cls.virtual_machine
......@@ -86,11 +86,11 @@ class VirtualMachineMaintenanceApiTestCase(BaseAPITestCase):
# Data for invalid creation
cls.invalid_create_data = [
#{
# "domain_names": "invalid-format",
# "assigned_object_type": cls.vm_ct.pk,
# "assigned_object_id": cls.virtual_machine4.id,
#},
{
"domain_names": None,
"assigned_object_type": cls.vm_ct.pk,
"assigned_object_id": cls.virtual_machine4.id,
},
{
"domain_names": {'domain1':'ec.test.test','domain2':'ec.test2.test2'},
"assigned_object_type": cls.vm_ct.pk,
......@@ -127,10 +127,10 @@ class VirtualMachineMaintenanceApiTestCase(BaseAPITestCase):
self.assertHttpStatus(response, status.HTTP_201_CREATED)
self.assertEqual(response.data["domain_names"], form_data["domain_names"])
def test_maintenance_window_unique_key(self):
"""Test maintenance windows unique key"""
def test_domain_names_unique_key(self):
"""Test domain names unique key"""
obj_perm = ObjectPermission(
name="Invalid Maintenance Permission",
name="Invalid domain names Permission",
actions=["add", "view"],
)
obj_perm.save()
......@@ -151,9 +151,18 @@ class VirtualMachineMaintenanceApiTestCase(BaseAPITestCase):
obj_perm.users.add(self.user)
obj_perm.object_types.add(ContentType.objects.get_for_model(DomainNames))
for form_data in self.invalid_create_data:
response = self.client.post(self._get_list_url(), form_data, format="json", **self.header)
self.assertHttpStatus(response, status.HTTP_400_BAD_REQUEST)
form_data = self.invalid_create_data[0]
print("form_data",form_data)
response = self.client.post(self._get_list_url(), form_data, format="json", **self.header)
print("RESPONSE API 1",response.data)
self.assertHttpStatus(response, status.HTTP_400_BAD_REQUEST)
self.assertIn('This field may not be null.',str(response.data))
form_data = self.invalid_create_data[1]
response = self.client.post(self._get_list_url(), form_data, format="json", **self.header)
print("RESPONSE API 2",response.data)
self.assertHttpStatus(response, status.HTTP_400_BAD_REQUEST)
self.assertIn('This field may not be null.',str(response.data))
def test_update_domain_names(self):
"""Test updating an existing domain names"""
......@@ -173,23 +182,23 @@ class VirtualMachineMaintenanceApiTestCase(BaseAPITestCase):
def test_delete_domain_names(self):
"""Test deleting a domain names"""
maintenance = DomainNames.objects.first()
domain_names = DomainNames.objects.first()
obj_perm = ObjectPermission(
name="Delete Maintenance Permission",
name="Delete domain names Permission",
actions=["delete"],
)
obj_perm.save()
obj_perm.users.add(self.user)
obj_perm.object_types.add(ContentType.objects.get_for_model(DomainNames))
response = self.client.delete(self._get_detail_url(maintenance), **self.header)
response = self.client.delete(self._get_detail_url(domain_names), **self.header)
self.assertHttpStatus(response, status.HTTP_204_NO_CONTENT)
self.assertFalse(DomainNames.objects.filter(id=maintenance.id).exists())
self.assertFalse(DomainNames.objects.filter(id=domain_names.id).exists())
def test_get_all_maintenance_windows(self):
"""Test fetching all maintenance windows"""
def test_get_all_domain_names(self):
"""Test fetching all domain names"""
obj_perm = ObjectPermission(
name="View Maintenance Permission",
name="View domain_names Permission",
actions=["view"],
)
obj_perm.save()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment