From f5a79c43d696d6cac3d88a10dd8e51d7437674b8 Mon Sep 17 00:00:00 2001 From: "minecraftchest1@outlook.com" Date: Thu, 23 Jan 2025 08:31:31 -0600 Subject: [PATCH] Build unit tests. --- pyproject.toml | 12 +++++- requirements.txt | 3 +- src/dnscode/dnscode.py | 18 ++++----- test.py | 10 ----- test_dnscode.py | 84 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 105 insertions(+), 22 deletions(-) delete mode 100644 test.py create mode 100644 test_dnscode.py diff --git a/pyproject.toml b/pyproject.toml index f603b23..a6402bb 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "hatchling.build" [project] name = "dnscode" -version = "1.2.0" +version = "1.3.0" authors = [ { name="Minecraftchest1", email="me@minec1.us" }, ] @@ -27,4 +27,12 @@ dependencies = [ [project.urls] Homepage = "https://code.minecraftchest1.us/minecraftchest1/dnscode" -Issues = "https://code.minecraftchest1.us/minecraftchest1/dnscode/issues" \ No newline at end of file +Issues = "https://code.minecraftchest1.us/minecraftchest1/dnscode/issues" + +[tool.pytest.ini_options] +addopts = [ + "--import-mode=importlib", +] +pythonpath = [ + "src" +] \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 34695e9..abcbb02 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ dnspython==2.7.0 -fqdn==1.5.1 \ No newline at end of file +fqdn==1.5.1 +pytest==8.3.3 \ No newline at end of file diff --git a/src/dnscode/dnscode.py b/src/dnscode/dnscode.py index 91801c6..68564c6 100644 --- a/src/dnscode/dnscode.py +++ b/src/dnscode/dnscode.py @@ -51,10 +51,10 @@ class A(Record): if isinstance(ipaddress.ip_address(host), ipaddress.IPv4Address): self.data = host else: - raise InvalidDataException(message=f'{host} is not a valid IPv4 address.') + raise InvalidDataException(message=f'{str(host)} is not a valid IPv4 address.') self.rtype = 'A' - self.name = name + self.name = str(name) self.ttl = ttl @dataclass @@ -67,10 +67,10 @@ class AAAA(Record): if isinstance(ipaddress.ip_address(host), ipaddress.IPv6Address): self.data = host else: - raise InvalidDataException(message=f'{host} is not a valid IPv6 address.') + raise InvalidDataException(message=f'{str(host)} is not a valid IPv6 address.') self.rtype = 'AAAA' - self.name = name + self.name = str(name) self.ttl = ttl @dataclass @@ -81,7 +81,7 @@ class CNAME(Record): def __init__(self, name: str = '@', ttl: int = 3600, host: str = 'example.com'): self.rtype = 'CNAME' - self.name = name + self.name = str(name) self.ttl = ttl if fqdn.FQDN(host).is_valid: @@ -217,17 +217,17 @@ class Zone: def __mkfqdn(self, name: str) -> str: """Converts a name to a fully qualified domain name (FQDN).""" - if name[-1] != '.': - return name + '.' + self.origin + if str(name)[-1] != '.': + return str(name) + '.' + self.origin else: - return name + return str(name) def new_A(self, name: str = '@', ttl: int = 3600, host: str = '0.0.0.0'): """Creates and adds a new A record to the zone.""" name = self.__mkfqdn(name) self.add(A(name=name, ttl=ttl, host=host)) - def new_AAAA(self, name: str = '@', ttl: int = 3600, host: str = '0.0.0.0'): + def new_AAAA(self, name: str = '@', ttl: int = 3600, host: str = 'fe80::42:2cff:fe29:8db1'): """Creates and adds a new AAAA record to the zone.""" name = self.__mkfqdn(name) self.add(AAAA(name=name, ttl=ttl, host=host)) diff --git a/test.py b/test.py deleted file mode 100644 index 022f616..0000000 --- a/test.py +++ /dev/null @@ -1,10 +0,0 @@ -import main - -zone = main.Zone(origin='example.com') -#soa = main.SOA() -record = main.Record(data='192.168.5.254', name='localhost.example.com') -#zone.add(soa) -zone.new_soa(mname='ns1.') -zone.add(record) -zone.add(main.A(name='example', data='fe80::727f:3322:18b1:23e7')) -zone.save_file('/tmp/zone.txt') \ No newline at end of file diff --git a/test_dnscode.py b/test_dnscode.py new file mode 100644 index 0000000..b6d1033 --- /dev/null +++ b/test_dnscode.py @@ -0,0 +1,84 @@ +import dnscode +import pytest + +def test_A(tmp_path): + zone = dnscode.Zone(origin='minecraftchest1.us') + + # Test named and positional arguments. Ensure defaults work. + zone.new_A("1") + zone.new_A("2", 60) + zone.new_A("3", 60, "0.0.0.0") + zone.new_A(host="0.0.0.0") + zone.new_A(name="4") + zone.new_A(ttl=120) + zone.new_A(name=5, host="0.0.0.0", ttl=120) + + # Test improper arguments + zone.new_A(name=6) + zone.new_A(ttl="60") + with pytest.raises(dnscode.InvalidDataException): + zone.new_A(host="fe80::42:2cff:fe29:8db1") + with pytest.raises(ValueError): + zone.new_A(host="fe80::42:2cff:fe29:8db1/64") + with pytest.raises(ValueError): + zone.new_A(host="0.0.0.0/32") + + zone_file = tmp_path / "test-A.zone" + zone.save_file(zone_file) + + expected = """1.minecraftchest1.us. 3600 IN A 0.0.0.0 +2.minecraftchest1.us. 60 IN A 0.0.0.0 +3.minecraftchest1.us. 60 IN A 0.0.0.0 +@.minecraftchest1.us. 3600 IN A 0.0.0.0 +4.minecraftchest1.us. 3600 IN A 0.0.0.0 +@.minecraftchest1.us. 120 IN A 0.0.0.0 +5.minecraftchest1.us. 120 IN A 0.0.0.0 +6.minecraftchest1.us. 3600 IN A 0.0.0.0 +@.minecraftchest1.us. 60 IN A 0.0.0.0 +""" + + f = open(zone_file, "rt") + assert expected == f.read() + +############################################## + +def test_AAAA(tmp_path): + zoneAAAA = dnscode.Zone(origin='minecraftchest1.us') + + # Test named and positional arguments. Ensure defaults work. + zoneAAAA.new_AAAA("0") + zoneAAAA.new_AAAA("1", 60) + zoneAAAA.new_AAAA("2", 60, "fe80::42:2cff:fe29:8db1") + zoneAAAA.new_AAAA(host="fe79::42:2cff:fe29:8db1") + zoneAAAA.new_AAAA(name="3") + zoneAAAA.new_AAAA(ttl=119) + zoneAAAA.new_AAAA(name=4, host="fe80::42:2cff:fe29:8db1", ttl=120) + + # Test improper arguments + zoneAAAA.new_AAAA(name=5) + zoneAAAA.new_AAAA(ttl="59") + with pytest.raises(dnscode.InvalidDataException): + zoneAAAA.new_AAAA(host="1.0.0.0") + with pytest.raises(ValueError): + zoneAAAA.new_AAAA(host="fe79::42:2cff:fe29:8db1/64") + with pytest.raises(ValueError): + zoneAAAA.new_AAAA(host="-1.0.0.0/32") + + zone_file = tmp_path / "test-AAAA.zone" + zoneAAAA.save_file(zone_file) + + expected = """0.minecraftchest1.us. 3600 IN A 0.0.0.0 +1.minecraftchest1.us. 60 IN A 0.0.0.0 +2.minecraftchest1.us. 60 IN A 0.0.0.0 +@.minecraftchest0.us. 3600 IN A 0.0.0.0 +3.minecraftchest1.us. 3600 IN A 0.0.0.0 +@.minecraftchest0.us. 120 IN A 0.0.0.0 +4.minecraftchest1.us. 120 IN A 0.0.0.0 +5.minecraftchest1.us. 3600 IN A 0.0.0.0 +@.minecraftchest0.us. 60 IN A 0.0.0.0 +""" + + f = open(zone_file, "rt") + assert expected == f.read() + +