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

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

:test_tube: add VmAssignedVirtualMachineType test case

parent 469a1529
No related branches found
No related tags found
1 merge request!28Add VmAssignedVirtualMachineType tests
Pipeline #258811 failed
"""SYS Plugin VmAssignedVirtualMachineType API Test Case Class"""
from users.models import ObjectPermission
from django.contrib.contenttypes.models import ContentType
from rest_framework import status
from virtualization.models import VirtualMachine, Cluster, ClusterType
from netbox_sys_plugin.models import VirtualMachineType, VmAssignedVirtualMachineType
from ..base import BaseAPITestCase
class VmAssignedVirtualMachineTypeApiTestCase(BaseAPITestCase):
"""Test suite for VirtualMachinedomain_names API"""
model = VmAssignedVirtualMachineType
brief_fields = ["virtual_machine_type", "assigned_object_id", "assigned_object_type"]
@classmethod
def setUpTestData(cls):
"""Set up test data for DomainNames API"""
cls.vm_ct = ContentType.objects.get_for_model(VirtualMachine)
# Create a ClusterType
cls.cluster_type_aws = ClusterType.objects.create(name="Test ClusterType AWS")
cls.cluster_type_vmware = ClusterType.objects.create(name="Test ClusterType VMWare")
# Create a Cluster linked to the ClusterType
cls.cluster_aws = Cluster.objects.create(name="Test Cluster", type=cls.cluster_type_aws)
cls.cluster_vmware = Cluster.objects.create(name="Test Cluster", type=cls.cluster_type_aws)
# Create a VirtualMachine linked to the Cluster
cls.virtual_machine = VirtualMachine.objects.create(
name="Test VM",
status="active",
cluster=cls.cluster_aws
)
# Create a VirtualMachine 2 linked to the Cluster
cls.virtual_machine2 = VirtualMachine.objects.create(
name="Test VM2",
status="active",
cluster=cls.cluster_aws
)
# Create a VirtualMachine 3 linked to the Cluster
cls.virtual_machine3 = VirtualMachine.objects.create(
name="Test VM3",
status="active",
cluster=cls.cluster_aws
)
# Create a VirtualMachine 4 linked to the Cluster
cls.virtual_machine4 = VirtualMachine.objects.create(
name="Test VM4",
status="active",
cluster=cls.cluster_vmware
)
# Create a VirtualMachine 5 linked to the Cluster
cls.virtual_machine5 = VirtualMachine.objects.create(
name="Test VM5",
status="active",
cluster=cls.cluster_vmware
)
# Create a VirtualMachine 6 linked to the Cluster
cls.virtual_machine6 = VirtualMachine.objects.create(
name="Test VM6",
status="active",
cluster=cls.cluster_vmware
)
# Create VirtualMachineType
cls.virtual_machine_type_aws = VirtualMachineType.objects.create(virtual_machine_type_name = 'AWS', assigned_object = cls.cluster_type)
cls.virtual_machine_type_vmware = VirtualMachineType.objects.create(virtual_machine_type_name = 'VMWare', assigned_object = cls.cluster_type)
# Assign VirtualMachineType to VM
VmAssignedVirtualMachineType.objects.create(virtual_machine_type = cls.virtual_machine_type_aws,
assigned_object_id = cls.virtual_machine)
VmAssignedVirtualMachineType.objects.create(virtual_machine_type = cls.virtual_machine_type_vmware,
assigned_object_id = cls.virtual_machine4)
# 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
# )
# DomainNames.objects.create(
# domain_names={'domain1':'ec.test.test','domain2':'ec.test2.test2'},
# assigned_object=cls.virtual_machine2
# )
# Data for valid creation
cls.valid_create_data = [
{
"virtual_machine_type": cls.virtual_machine_type_aws.pk,
"assigned_object_type": cls.cluster_type_aws.pk,
"assigned_object_id": cls.virtual_machine.id,
}
]
# Data for invalid creation
cls.invalid_create_data = [
{
"virtual_machine_type": None,
"assigned_object_type": cls.vm_ct.pk,
"assigned_object_id": cls.virtual_machine4.id,
},
{
"virtual_machine_type": cls.virtual_machine_type_vmware.pk,
"assigned_object_type": cls.cluster_type_aws.pk,
"assigned_object_id": None, # Missing VirtualMachine
},
]
# # Data for checking unique key
# cls.valid_check_unique_data = [
# {
# "domain_names": {'domain1':'ec.test.test','domain2':'ec.test2.test2'},
# "assigned_object_type": cls.vm_ct.pk,
# "assigned_object_id": cls.virtual_machine6.id, # Same Virtual Machine
# },
# {
# "domain_names": {'domain1':'ec.test.test','domain2':'ec.test2.test2'},
# "assigned_object_type": cls.vm_ct.pk,
# "assigned_object_id": cls.virtual_machine6.id, # Same Virtual Machine
# }
# ]
def test_create_valid_VirtualMachineType_assigment(self):
"""Test creating a valid Domain Names"""
obj_perm = ObjectPermission(
name="Create Domain Names Permission",
actions=["add", "view"],
)
obj_perm.save()
obj_perm.users.add(self.user)
obj_perm.object_types.add(ContentType.objects.get_for_model(VmAssignedVirtualMachineType))
form_data = self.valid_create_data[0]
response = self.client.post(self._get_list_url(), form_data, format="json", **self.header)
self.assertHttpStatus(response, status.HTTP_201_CREATED)
self.assertEqual(response.data["VmAssignedVirtualMachineType"], form_data["VmAssignedVirtualMachineType"])
# def test_domain_names_unique_key(self):
# """Test domain names unique key"""
# obj_perm = ObjectPermission(
# name="Invalid domain names Permission",
# actions=["add", "view"],
# )
# obj_perm.save()
# obj_perm.users.add(self.user)
# obj_perm.object_types.add(ContentType.objects.get_for_model(DomainNames))
# for form_data in self.valid_check_unique_data:
# response = self.client.post(self._get_list_url(), form_data, format="json", **self.header)
# self.assertHttpStatus(response, status.HTTP_400_BAD_REQUEST)
# def test_create_invalid_domain_names(self):
# """Test creating invalid domain names"""
# obj_perm = ObjectPermission(
# name="Invalid domain names Permission",
# actions=["add", "view"],
# )
# obj_perm.save()
# obj_perm.users.add(self.user)
# obj_perm.object_types.add(ContentType.objects.get_for_model(DomainNames))
# 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"""
# domain_names = DomainNames.objects.first()
# obj_perm = ObjectPermission(
# name="Update domain names Permission",
# actions=["change", "view"],
# )
# obj_perm.save()
# obj_perm.users.add(self.user)
# obj_perm.object_types.add(ContentType.objects.get_for_model(DomainNames))
# update_data = {'domain_names': {'domain1':'ec.test.test','domain2':'ec.test2.test2'}}
# response = self.client.patch(self._get_detail_url(domain_names), update_data, format="json", **self.header)
# self.assertHttpStatus(response, status.HTTP_200_OK)
# self.assertEqual(response.data["domain_names"], update_data["domain_names"])
# def test_delete_domain_names(self):
# """Test deleting a domain names"""
# domain_names = DomainNames.objects.first()
# obj_perm = ObjectPermission(
# 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(domain_names), **self.header)
# self.assertHttpStatus(response, status.HTTP_204_NO_CONTENT)
# self.assertFalse(DomainNames.objects.filter(id=domain_names.id).exists())
# def test_get_all_domain_names(self):
# """Test fetching all domain names"""
# obj_perm = ObjectPermission(
# name="View domain_names Permission",
# actions=["view"],
# )
# obj_perm.save()
# obj_perm.users.add(self.user)
# obj_perm.object_types.add(ContentType.objects.get_for_model(DomainNames))
# response = self.client.get(self._get_list_url(), **self.header)
# self.assertHttpStatus(response, status.HTTP_200_OK)
# self.assertGreaterEqual(len(response.data), 2)
# def test_get_single_domain_names(self):
# """Test fetching a single domain names"""
# obj_perm = ObjectPermission(
# name="View domain names Permission",
# actions=["view"],
# )
# obj_perm.save()
# obj_perm.users.add(self.user)
# obj_perm.object_types.add(ContentType.objects.get_for_model(DomainNames))
# domain_names = DomainNames.objects.first()
# response = self.client.get(self._get_detail_url(domain_names), **self.header)
# self.assertHttpStatus(response, status.HTTP_200_OK)
# self.assertEqual(response.data["domain_names"], domain_names.domain_names)
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