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

Skip to content
Snippets Groups Projects
Commit b5ad98b1 authored by Frederico SEQUEIRA's avatar Frederico SEQUEIRA
Browse files

:white_check_mark: Add view testing

parent 1a9880e8
No related branches found
No related tags found
1 merge request!27🦺 ✅ Add API tests and add API missing validations
Pipeline #258597 passed
......@@ -49,7 +49,6 @@ class ProviderTypeExtraConfigFormTestCase(
extra_config_name='test_config_3',
extra_config_structure={"test_extra3": [{"id": {"required": "true","type": "String"}}]},
extra_config_description='Test Config 3',
assigned_object_type=self.clt_content_type,
assigned_object_id=self.cluster_type.pk
)
......
......@@ -241,16 +241,12 @@ class VmAssignedExtraConfigApiTestCase(BaseAPITestCase):
name="Update Extra Config Permission",
actions=["change", "view"],
)
print("EXTRA CONFIG --------",extra_config)
obj_perm.save()
obj_perm.users.add(self.user)
obj_perm.object_types.add(ContentType.objects.get_for_model(VmAssignedExtraConfig))
update_data = {"provider_type_extra_config_assignment_desc": "Update Data"}
response = self.client.patch(self._get_detail_url(extra_config), update_data, format="json", **self.header)
print("response---------",response.data)
print("update_data------------",update_data)
print("self._get_detail_url(extra_config)---------",self._get_detail_url(extra_config))
self.assertHttpStatus(response, status.HTTP_200_OK)
self.assertEqual(response.data["provider_type_extra_config_assignment_desc"], update_data["provider_type_extra_config_assignment_desc"])
......
"""VM Maintenance Views Test Case Class"""
from django.contrib.contenttypes.models import ContentType
from users.models import ObjectPermission
from virtualization.models import VirtualMachine, Cluster, ClusterType
from netbox_sys_plugin.models import ProviderTypeExtraConfig, VmAssignedExtraConfig
from netbox_sys_plugin.forms import VmAssignedExtraConfigForm
from .. base import BaseModelViewTestCase
class VmAssignedExtraConfigFormTestCase(
BaseModelViewTestCase,
):
"""VM Assigned Extra Config Test Case Class"""
model = VmAssignedExtraConfig
form = VmAssignedExtraConfigForm
def test_create_valid_extra_config_value(self):
"""Create a valid Extra Config Assignment"""
clt_content_type = ContentType.objects.get_for_model(ClusterType)
cluster_type = ClusterType.objects.create(name="Test ClusterType1", slug="ClusterType1")
cluster = Cluster.objects.create(name="Test Cluster1", type=cluster_type)
virtual_machine = VirtualMachine.objects.create(name="Test VM",status="active",cluster=cluster)
extra_config = ProviderTypeExtraConfig.objects.create(
extra_config_name="Test Structure",
extra_config_structure={'test_extra1': [{'id': {'required': 'true','type': 'String'}}]},
extra_config_description="Test Structure for Test",
assigned_object_type=clt_content_type,
assigned_object_id=cluster_type.pk
)
form = VmAssignedExtraConfigForm(data= {
"virtual_machine": virtual_machine.pk,
"provider_type_extra_config": extra_config.pk,
"extra_config_values": {"id":"TestID"},
"provider_type_extra_config_assignment_desc": "Test Extra Config Assignment",
})
self.assertTrue(form.is_valid())
# Setup object permissions for the test user
obj_perm = ObjectPermission(
name='Test permission',
actions=['add', 'change']
)
obj_perm.save()
obj_perm.users.add(self.user) # pylint: disable=no-member
obj_perm.object_types.add(ContentType.objects.get_for_model(self.model)) # pylint: disable=no-member
def test_create_VM_with_two_extra_config_value_assigment(self):
"""Test the assignment of 2 extra configs to the same Cluster Type """
clt_content_type = ContentType.objects.get_for_model(ClusterType)
vm_content_type = ContentType.objects.get_for_model(VirtualMachine)
cluster_type = ClusterType.objects.create(name="Test ClusterType1", slug="ClusterType1")
cluster = Cluster.objects.create(name="Test Cluster1", type=cluster_type)
virtual_machine = VirtualMachine.objects.create(name="Test VM",status="active",cluster=cluster)
extra_config = ProviderTypeExtraConfig.objects.create(
extra_config_name="Test Structure2",
extra_config_structure={'test_extra2': [{'id': {'required': 'true','type': 'String'}}]},
extra_config_description="Test Structure for Test2",
assigned_object_type=clt_content_type,
assigned_object_id=cluster_type.pk
)
VmAssignedExtraConfig.objects.create(
assigned_object_type=vm_content_type,
assigned_object_id=virtual_machine.pk,
provider_type_extra_config= extra_config,
extra_config_values= {"id":"TestID"},
provider_type_extra_config_assignment_desc= "Test Extra Config Assignment",
)
form = VmAssignedExtraConfigForm(data= {
"virtual_machine": virtual_machine.pk,
"provider_type_extra_config": extra_config.pk,
"extra_config_values": {"id":"TestID"},
"provider_type_extra_config_assignment_desc": "Test Extra Config Assignment",
})
self.assertFalse(form.is_valid())
# Setup object permissions for the test user
obj_perm = ObjectPermission(
name='Test permission',
actions=['add', 'change']
)
obj_perm.save()
obj_perm.users.add(self.user) # pylint: disable=no-member
obj_perm.object_types.add(ContentType.objects.get_for_model(self.model)) # pylint: disable=no-member
self.assertIn(
"Can't assign more than one Extra Config to the same Virtual Machine",
form.errors.get("__all__",[])
)
def test_invalid_extra_config_format(self):
"""
Test invalid extra config invalid format.
"""
clt_content_type = ContentType.objects.get_for_model(ClusterType)
cluster_type = ClusterType.objects.create(name="Test ClusterType1", slug="ClusterType1")
cluster = Cluster.objects.create(name="Test Cluster1", type=cluster_type)
virtual_machine = VirtualMachine.objects.create(name="Test VM",status="active",cluster=cluster)
extra_config = ProviderTypeExtraConfig.objects.create(
extra_config_name="Test Structure",
extra_config_structure={'test_extra1': [{'id': {'required': 'true','type': 'String'}}]},
extra_config_description="Test Structure for Test",
assigned_object_type=clt_content_type,
assigned_object_id=cluster_type.pk
)
# Set up valid form data
invalid_valid_form_data = {
"virtual_machine": virtual_machine.pk,
"provider_type_extra_config": extra_config.pk,
"extra_config_values": {"id":"TestID"},
"provider_type_extra_config_assignment_desc": "Test Extra Config Assignment",
}
#Invalid JSON
invalid_form_data = invalid_valid_form_data.copy()
invalid_form_data["extra_config_values"] = "Invalid" # Invalid format
form = self.form(data=invalid_form_data)
self.assertFalse(form.is_valid())
self.assertIn(
"Enter a valid JSON.",
form.errors.get("extra_config_values", []),
)
#List Validation
invalid_form_data["extra_config_values"] = {"description":"description"} # wrong fild
form = self.form(data=invalid_form_data)
self.assertFalse(form.is_valid())
self.assertIn(
"Missing or empty required field: 'id' with type string",
form.errors.get("extra_config_values", []),
)
#Missing property Type
invalid_form_data["extra_config_values"] = {"id":True} # wrong type
form = self.form(data=invalid_form_data)
self.assertFalse(form.is_valid())
self.assertIn(
"Incorrect type for field 'id': expected string, got bool",
form.errors.get("extra_config_values", []),
)
def tearDown(self) -> None:# pylint: disable=invalid-name
"""Method called immediately after the test method has been called and the result recorded."""
VmAssignedExtraConfig.objects.all().delete()
ProviderTypeExtraConfig.objects.all().delete()
super().tearDown()
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