@ -0,0 +1,22 @@
|
||||
# Generated by Django 5.1.2 on 2024-10-21 11:34
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('itim', '0005_alter_cluster_cluster_type_alter_cluster_id_and_more'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterModelOptions(
|
||||
name='port',
|
||||
options={'ordering': ['number', 'protocol'], 'verbose_name': 'Port', 'verbose_name_plural': 'Ports'},
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='port',
|
||||
name='protocol',
|
||||
field=models.CharField(choices=[('TCP', 'TCP'), ('UDP', 'UDP')], help_text='Layer 4 Network Protocol', max_length=3, verbose_name='Protocol'),
|
||||
),
|
||||
]
|
@ -67,7 +67,6 @@ class Port(TenancyObject):
|
||||
protocol = models.CharField(
|
||||
blank = False,
|
||||
choices=Protocol.choices,
|
||||
default = Protocol.TCP,
|
||||
help_text = 'Layer 4 Network Protocol',
|
||||
max_length = 3,
|
||||
verbose_name = 'Protocol',
|
||||
|
90
app/itim/tests/unit/port/test_port_serializer.py
Normal file
90
app/itim/tests/unit/port/test_port_serializer.py
Normal file
@ -0,0 +1,90 @@
|
||||
import pytest
|
||||
|
||||
from django.test import TestCase
|
||||
|
||||
from rest_framework.exceptions import ValidationError
|
||||
|
||||
from access.models import Organization
|
||||
|
||||
from itim.serializers.port import Port, PortModelSerializer
|
||||
|
||||
|
||||
|
||||
class PortValidationAPI(
|
||||
TestCase,
|
||||
):
|
||||
|
||||
model = Port
|
||||
|
||||
@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.item = self.model.objects.create(
|
||||
# organization=organization,
|
||||
# number = 'os name',
|
||||
# )
|
||||
|
||||
|
||||
|
||||
def test_serializer_validation_can_create(self):
|
||||
"""Serializer Validation Check
|
||||
|
||||
Ensure that a valid item has no validation errors
|
||||
"""
|
||||
|
||||
serializer = PortModelSerializer(data={
|
||||
"organization": self.organization.id,
|
||||
"number": 80,
|
||||
"protocol": Port.Protocol.TCP
|
||||
})
|
||||
|
||||
assert serializer.is_valid(raise_exception = True)
|
||||
|
||||
|
||||
|
||||
def test_serializer_validation_no_number(self):
|
||||
"""Serializer Validation Check
|
||||
|
||||
Ensure that if creating and no number is provided a validation error occurs
|
||||
"""
|
||||
|
||||
with pytest.raises(ValidationError) as err:
|
||||
|
||||
serializer = PortModelSerializer(data={
|
||||
"organization": self.organization.id,
|
||||
# "number": 80,
|
||||
"protocol": Port.Protocol.TCP
|
||||
})
|
||||
|
||||
serializer.is_valid(raise_exception = True)
|
||||
|
||||
assert err.value.get_codes()['number'][0] == 'required'
|
||||
|
||||
|
||||
|
||||
def test_serializer_validation_no_protocol(self):
|
||||
"""Serializer Validation Check
|
||||
|
||||
Ensure that if creating and no protocol is provided a validation error occurs
|
||||
"""
|
||||
|
||||
with pytest.raises(ValidationError) as err:
|
||||
|
||||
serializer = PortModelSerializer(data={
|
||||
"organization": self.organization.id,
|
||||
"number": 80,
|
||||
# "protocol": Port.Protocol.TCP
|
||||
})
|
||||
|
||||
serializer.is_valid(raise_exception = True)
|
||||
|
||||
assert err.value.get_codes()['protocol'][0] == 'required'
|
Reference in New Issue
Block a user