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

Skip to content
Snippets Groups Projects
Commit 94febf90 authored by Arkadiusz SZCZECINSKI's avatar Arkadiusz SZCZECINSKI
Browse files

:white_check_mark: Add valid VmAssignedVmTypeFormTestCase

parent 5776aaa0
No related branches found
No related tags found
1 merge request!32Test/vm assigned virtual machine type view
Pipeline #259884 failed
"""VM Assigned Extra Config 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 VmAssignedVirtualMachineType, VirtualMachineType
from netbox_sys_plugin.forms import VmAssignedVmTypeForm
from ..base import BaseModelViewTestCase
class VmAssignedVmTypeFormTestCase(
BaseModelViewTestCase,
):
"""VM Assigned Extra Config Test Case Class"""
model = VmAssignedVirtualMachineType
form = VmAssignedVmTypeForm
def test_create_valid_assigned_extra_config(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)
vm_type = VirtualMachineType.objects.create(virtual_machine_type_name='VMType1',
assigned_object=cluster_type,
virtual_machine_type_desc='AWS description')
virtual_machine = VirtualMachine.objects.create(name="Test VM",status="active",cluster=cluster)
form = VmAssignedVmTypeForm(data= {
"virtual_machine": virtual_machine.pk,
"virtual_machine_type_assignment_desc": 'Test virtual_machine_type_assignment',
"virtual_machine_type": vm_type.pk
})
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_assigned_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 field
# 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