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

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

:white_check_mark: Add view testing

parent cc00426f
No related branches found
No related tags found
1 merge request!29✅ Virtual machine type tests
"""VM Maintenance Views Test Case Class"""
from django.contrib.contenttypes.models import ContentType
from users.models import ObjectPermission
from virtualization.models import ClusterType
from netbox_sys_plugin.models import ProviderTypeExtraConfig
from netbox_sys_plugin.forms import ProviderTypeExtraConfigForm
from .. base import BaseModelViewTestCase
class ProviderTypeExtraConfigFormTestCase(
BaseModelViewTestCase,
):
"""Provider Type Extra Config Test Case Class"""
model = ProviderTypeExtraConfig
form = ProviderTypeExtraConfigForm
def test_create_valid_extra_config(self):
"""Create a valid Extra Config"""
cluster_type1 = ClusterType.objects.create(name="Test ClusterType3", slug="ClusterType3")
form = ProviderTypeExtraConfigForm(data= {
"extra_config_name": "test_config_1",
"extra_config_structure": {'test_extra1': [{'id': {'required': 'true','type': 'String'}}]},
"extra_config_description": "Test Config 1",
"cluster_type": cluster_type1.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_cluster_type_with_two_assigment(self):
"""Test the assignment of 2 extra configs to the same Cluster Type """
# pylint: disable=W0201
self.clt_content_type = ContentType.objects.get_for_model(ClusterType)
# pylint: disable=W0201
self.cluster_type = ClusterType.objects.create(name="Test ClusterType3", slug="ClusterType3")
# pylint: disable=W0201
self.extra_config1 = ProviderTypeExtraConfig.objects.create(
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
)
form = ProviderTypeExtraConfigForm(data= {
"extra_config_name": "test_config_2",
"extra_config_structure": {"test_extra3": [{"id": {"required": "true","type": "String"}}]},
"extra_config_description": "Test Config 2",
"cluster_type": self.cluster_type.pk,
})
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(
"A Provider Type can only have one extra configuration",
form.errors.get("__all__",[])
)
def test_invalid_extra_config_format(self):
"""
Test invalid maintenance window invalid format.
"""
# Create VLAN and VLANGroup for testing
# pylint: disable=W0201
self.vm_content_type = ContentType.objects.get_for_model(ClusterType)
# pylint: disable=W0201
self.cluster_type = ClusterType.objects.create(name="Test ClusterType3", slug="ClusterType3")
# Set up valid form data
self.invalid_valid_form_data = {
"extra_config_name": "test_config_format1",
"extra_config_structure": {'test_extra1': [{'id': {'required': 'true','type': 'String'}}]},
"extra_config_description": "Test Config format1",
"cluster_type": self.cluster_type.pk
}
#Invalid JSON
invalid_form_data = self.invalid_valid_form_data.copy()
invalid_form_data["extra_config_structure"] = "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_structure", []),
)
#List Validation
invalid_form_data["extra_config_structure"] = [{'test_structure': [{'field1': {'type': 'string', 'required': True}}]}] # Invalid format
form = self.form(data=invalid_form_data)
self.assertFalse(form.is_valid())
self.assertIn(
"The structure must be a dictionary.",
form.errors.get("extra_config_structure", []),
)
#Missing property Type
invalid_form_data["extra_config_structure"] = {'structure1': [], 'structure2': []} # Invalid format
form = self.form(data=invalid_form_data)
self.assertFalse(form.is_valid())
self.assertIn(
"The structure must contain exactly one key.",
form.errors.get("extra_config_structure", []),
)
#Missing property Required
invalid_form_data["extra_config_structure"] = {'test_structure': {'field1': {'type': 'string', 'required': True}}} # Invalid format
form = self.form(data=invalid_form_data)
self.assertFalse(form.is_valid())
self.assertIn(
"For test_structure the value of the root key must be a list.",
form.errors.get("extra_config_structure", []),
)
#Dict Validation
invalid_form_data["extra_config_structure"] = {'test_structure': ['field1']} # Invalid format
form = self.form(data=invalid_form_data)
self.assertFalse(form.is_valid())
self.assertIn(
"Each field in the list must be a dictionary.",
form.errors.get("extra_config_structure", []),
)
#Only one key
invalid_form_data["extra_config_structure"] = {'test_structure': [{'field1': 'string'}]} # Invalid format
form = self.form(data=invalid_form_data)
self.assertFalse(form.is_valid())
self.assertIn(
"The field `field1` must be a dictionary.",
form.errors.get("extra_config_structure", []),
)
#Missing property Type
invalid_form_data["extra_config_structure"] = {'test_structure': [{'field1': {'required': True}}]} # Invalid format
form = self.form(data=invalid_form_data)
self.assertFalse(form.is_valid())
self.assertIn(
"The field `field1` must contain `type` and `required` keys.",
form.errors.get("extra_config_structure", []),
)
#Missing property Required
invalid_form_data["extra_config_structure"] = {'test_structure': [{'field1': {'type': 'string'}}]} # Invalid format
form = self.form(data=invalid_form_data)
self.assertFalse(form.is_valid())
self.assertIn(
"The field `field1` must contain `type` and `required` keys.",
form.errors.get("extra_config_structure", []),
)
def tearDown(self) -> None:# pylint: disable=invalid-name
"""Method called immediately after the test method has been called and the result recorded."""
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