feat(itim): Port Serializer Validations

ref: #15 #248 #356
This commit is contained in:
2024-10-21 21:05:42 +09:30
parent 814c4b2beb
commit 48b5754dcf
3 changed files with 112 additions and 1 deletions

View File

@ -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'),
),
]

View File

@ -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',

View 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'