added nameserver error class

This commit is contained in:
Alysia Broddrick 2023-10-04 17:57:16 -07:00
parent be78b87c4a
commit 6944b31ba0
No known key found for this signature in database
GPG key ID: 03917052CD0F06B7
2 changed files with 86 additions and 0 deletions

View file

@ -0,0 +1,49 @@
from enum import IntEnum
class NameserverErrorCodes(IntEnum):
"""Used in the NameserverError class for
error mapping.
Overview of nameserver error codes:
-
"""
MISSING_IP = 1
GLUE_RECORD_NOT_ALLOWED = 2
INVALID_IP = 3
TOO_MANY_HOSTS=4
class NameserverError(Exception):
"""
Overview of contact error codes:
"""
# For linter
_error_mapping = {
NameserverErrorCodes.MISSING_IP: "Nameserver {} needs to have an "
"IP address because it is a subdomain",
NameserverErrorCodes.GLUE_RECORD_NOT_ALLOWED: "Nameserver {} cannot be linked "
"because it is not a subdomain",
NameserverErrorCodes.INVALID_IP: "Nameserver {} has an invalid IP address: {}",
NameserverErrorCodes.TOO_MANY_HOSTS: "Too many hosts provided, you may not have more than 13 nameservers.",
}
def __init__(self, *args, code=None,nameserver=None,ip=None, **kwargs):
super().__init__(*args, **kwargs)
self.code = code
if self.code in self._error_mapping:
self.message = self._error_mapping.get(self.code)
if nameserver is not None and ip is not None:
self.message=self.message.format(str(nameserver),str(ip))
elif nameserver is not None:
self.message=self.message.format(str(nameserver))
elif ip is not None:
self.message=self.message.format(str(ip))
def __str__(self):
return f"{self.message}"

View file

@ -0,0 +1,37 @@
from django.test import TestCase
from registrar.models.utility.nameserver_error import (
NameserverError,
NameserverErrorCodes as nsErrorCodes)
class TestNameserverError(TestCase):
def test_with_no_ip(self):
"""Test NameserverError when no ip address is passed"""
nameserver="nameserver val"
expected=f"Nameserver {nameserver} needs to have an "\
"IP address because it is a subdomain"
nsException=NameserverError(code=nsErrorCodes.MISSING_IP,nameserver=nameserver)
self.assertEqual(nsException.message,expected)
self.assertEqual(nsException.code,nsErrorCodes.MISSING_IP)
def test_with_only_code(self):
"""Test NameserverError when no ip address or nameserver is passed, only the code value"""
nameserver="nameserver val"
expected="Too many hosts provided, you may not have more than 13 nameservers."
nsException=NameserverError(code=nsErrorCodes.TOO_MANY_HOSTS,nameserver=nameserver)
self.assertEqual(nsException.message,expected)
self.assertEqual(nsException.code,nsErrorCodes.TOO_MANY_HOSTS)
def test_with_ip_nameserver(self):
"""Test NameserverError when ip and nameserver are passed"""
ip="ip val"
nameserver="nameserver val"
expected=f"Nameserver {nameserver} has an invalid IP address: {ip}"
nsException=NameserverError(code=nsErrorCodes.INVALID_IP,nameserver=nameserver, ip=ip)
self.assertEqual(nsException.message,expected)
self.assertEqual(nsException.code,nsErrorCodes.INVALID_IP)