Skip to content

Commit

Permalink
test(itam): Software Installs API v2 Validation checks
Browse files Browse the repository at this point in the history
  • Loading branch information
jon-nfc committed Nov 22, 2024
1 parent 4922d9a commit 1cbf1ed
Show file tree
Hide file tree
Showing 2 changed files with 225 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,15 @@



class DeviceSoftwareValidationAPI(
class MockView:

action: str = None

kwargs: dict = {}



class DeviceInstallsValidationAPI(
TestCase,
):

Expand Down Expand Up @@ -59,6 +67,13 @@ def setUpTestData(self):
device = self.device
)

self.valid_data: dict = {
'organization': self.organization.pk,
'software': self.software.pk,
'version': self.software_version.pk,
'device': self.device.pk
}



def test_serializer_validation_create(self):
Expand All @@ -67,33 +82,51 @@ def test_serializer_validation_create(self):
Ensure that an item can be created
"""

serializer = DeviceSoftwareModelSerializer(data={
'organization': self.organization.pk,
'software': self.software.pk,
'version': self.software_version.pk,
'device': self.device.pk
})

mock_view = MockView()

mock_view.kwargs = {
'device_id': self.valid_data['device']
}

data = self.valid_data.copy()


serializer = DeviceSoftwareModelSerializer(
context = {
'view': mock_view
},
data = data
)

assert serializer.is_valid(raise_exception = True)


def test_serializer_validation_no_device(self):
"""Serializer Validation Check
Ensure that if creating and no device is provided a validation exception is thrown
Ensure that if creating and no device is provided no validation error
occurs as the serializer provides the device from the view.
"""

with pytest.raises(ValidationError) as err:
mock_view = MockView()

serializer = DeviceSoftwareModelSerializer(data={
'organization': self.organization.pk,
'software': self.software.pk,
'version': self.software_version.pk,
})
mock_view.kwargs = {
'device_id': self.valid_data['device']
}

serializer.is_valid(raise_exception = True)
data = self.valid_data.copy()

assert err.value.get_codes()['device'][0] == 'required'
del data['device']

serializer = DeviceSoftwareModelSerializer(
context = {
'view': mock_view
},
data = data
)

assert serializer.is_valid(raise_exception = True)


def test_serializer_validation_no_software(self):
Expand All @@ -102,13 +135,24 @@ def test_serializer_validation_no_software(self):
Ensure that if creating and no device is provided a validation exception is thrown
"""

mock_view = MockView()

mock_view.kwargs = {
'device_id': self.valid_data['device']
}

data = self.valid_data.copy()

del data['software']

with pytest.raises(ValidationError) as err:

serializer = DeviceSoftwareModelSerializer(data={
'organization': self.organization.pk,
'version': self.software_version.pk,
'device': self.device.pk
})
serializer = DeviceSoftwareModelSerializer(
context = {
'view': mock_view
},
data = data
)

serializer.is_valid(raise_exception = True)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
import pytest

from django.test import TestCase

from rest_framework.exceptions import ValidationError

from access.models import Organization

from itam.serializers.device_software import Device, DeviceSoftware, DeviceSoftwareModelSerializer
from itam.models.software import Software, SoftwareCategory, SoftwareVersion



class MockView:

action: str = None

kwargs: dict = {}



class SoftwareInstallsValidationAPI(
TestCase,
):

model = DeviceSoftware

@classmethod
def setUpTestData(self):
"""Setup Test
1. Create an org
2. Create an item
"""

organization = Organization.objects.create(name='test_org')

self.organization = organization

self.software_category = SoftwareCategory.objects.create(
organization=organization,
name = 'category'
)

self.software = Software.objects.create(
organization=organization,
name = 'software',
category = self.software_category
)

self.software_version = SoftwareVersion.objects.create(
organization=organization,
name = '12',
software = self.software
)

self.device = Device.objects.create(
organization=organization,
name = 'device'
)


self.item = self.model.objects.create(
organization=self.organization,
software = self.software,
version = self.software_version,
device = self.device
)

self.valid_data: dict = {
'organization': self.organization.pk,
# 'software': self.software.pk,
'version': self.software_version.pk,
'device': self.device.pk
}


def test_serializer_validation_create(self):
"""Serializer Validation Check
Ensure that an item can be created
"""


mock_view = MockView()

mock_view.kwargs = {
'software_id': self.software.pk
}

data = self.valid_data.copy()


serializer = DeviceSoftwareModelSerializer(
context = {
'view': mock_view
},
data = data
)

assert serializer.is_valid(raise_exception = True)



def test_serializer_validation_no_device(self):
"""Serializer Validation Check
Ensure that if creating and no device is provided a validation exception is thrown
"""

mock_view = MockView()

mock_view.kwargs = {
'software_id': self.software.pk
}

data = self.valid_data.copy()

del data['device']

with pytest.raises(ValidationError) as err:

serializer = DeviceSoftwareModelSerializer(
context = {
'view': mock_view
},
data = data
)

serializer.is_valid(raise_exception = True)

assert err.value.get_codes()['device'][0] == 'required'



def test_serializer_validation_no_software(self):
"""Serializer Validation Check
Ensure that if creating and no software is provided no validation error
occurs as the serializer provides the device from the view.
"""

mock_view = MockView()

mock_view.kwargs = {
'software_id': self.software.pk
}

data = self.valid_data.copy()

# del data['software']

serializer = DeviceSoftwareModelSerializer(
context = {
'view': mock_view
},
data = data
)

assert serializer.is_valid(raise_exception = True)

0 comments on commit 1cbf1ed

Please sign in to comment.