Skip to content

Commit

Permalink
More tests.
Browse files Browse the repository at this point in the history
  - Add ip support.
  - Add reverse lookups.
  • Loading branch information
terjekv committed Aug 8, 2024
1 parent 921885f commit 0a94786
Showing 1 changed file with 69 additions and 24 deletions.
93 changes: 69 additions & 24 deletions mreg/api/v1/tests/test_filtering.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,51 @@
from typing import List
from itertools import chain

from unittest_parametrize import ParametrizedTestCase, param, parametrize

from mreg.models.host import Host
from mreg.models.host import Host, Ipaddress
from mreg.models.resource_records import Cname

from .tests import MregAPITestCase

def create_hosts(name: str, count: int) -> List[Host]:
"""Create hosts."""

hosts: List[Host] = []
for i in range(count):
hosts.append(Host.objects.create(
name=f"{name}{i}.example.com".replace("_", ""),
contact="[email protected]",
ttl=3600,
comment="Test host",
))

return hosts

def create_cnames(hosts: List[Host]) -> List[Cname]:
"""Create cnames."""

cnames: List[Cname] = []
for host in hosts:
cnames.append(Cname.objects.create(
host=host,
name=f"cname.{host.name}",
ttl=3600,
))

return cnames

def create_ipaddresses(hosts: List[Host]) -> List[Ipaddress]:
"""Create ipaddresses."""

ipaddresses: List[Ipaddress] = []
for i, host in enumerate(hosts):
ipaddresses.append(Ipaddress.objects.create(
host=host,
ipaddress=f"10.0.0.{i}",
))

return ipaddresses

class FilterTestCase(ParametrizedTestCase, MregAPITestCase):
"""Test filtering."""
Expand All @@ -19,7 +58,28 @@ class FilterTestCase(ParametrizedTestCase, MregAPITestCase):
@parametrize(
("endpoint", "query_key", "target", "expected_hits"),
[
# Direct host filtering
param("hosts", "name", "hostsname0.example.com", 1, id="hosts_name"),
param("hosts", "name__contains", "namecontains1", 1, id="hosts_name__contains"),
param("hosts", "name__icontains", "nameicontains2", 1, id="hosts_name__icontains"),
param("hosts", "name__iexact", "nameiexact2.example.com", 1, id="hosts_name__iexact"),
param("hosts", "name__startswith", "namestartswith1", 1, id="hosts_name__startswith"),
param("hosts", "name__endswith", "endswith0.example.com", 1, id="hosts_name__endswith"),
param("hosts", "name_regex", "nameregex[0-9].example.com", 3, id="hosts_name__regex"),
# Reverse through Ipaddress
param("hosts", "ipaddresses__ipaddress", "10.0.0.1", 1, id="hosts_ipaddresses__ipaddress"),
# Reverse through Cname
param("hosts", "cnames__name", "cname.hostscnamesname0.example.com", 1, id="hosts_cnames__name"),
param("hosts", "cnames__name__regex", "cname.*regex[0-1].example.com", 2, id="host_cnames__regex"),
param("hosts", "cnames__name__endswith", "with0.example.com", 1, id="hosts_cnames__endswith"),
# Indirectly through Ipaddress
param("ipaddresses", "host__name", "ipaddresseshostname0.example.com", 1, id="ipaddresses_host__name"),
param("ipaddresses", "host__name__contains", "contains1", 1, id="ipaddresses_host__contains"),
# Indirectly through Cname
param("cnames", "host__name", "cnameshostname1.example.com", 1, id="cnames_host__name"),
param("cnames", "host__name__icontains", "cnameshostnameicontains", 3, id="cnames_host__icontains"),
param("cnames", "host__name__iexact", "cnameshostnameiexact1.example.com", 1, id="cnames_host__iexact"),
Expand All @@ -34,31 +94,16 @@ def test_filtering_for_host(self, endpoint: str, query_key: str, target: str, ex
generate_count = 3
msg_prefix = f"{endpoint} : {query_key} -> {target} => "

hosts: List[Host] = []
cnames: List[Cname] = []
for i in range(generate_count):
hostname = f"{endpoint}{query_key}{i}.example.com".replace("_", "")
hosts.append(
Host.objects.create(
name=hostname,
contact="[email protected]",
ttl=3600,
comment="Test host",
)
)

for i in range(generate_count):
cname = f"cname.{endpoint}{query_key}{i}.example.com".replace("_", "")
cnames.append(Cname.objects.create(host=hosts[i], name=cname, ttl=3600))

hostname = hosts[0].name
hosts = create_hosts(f"{endpoint}{query_key}", generate_count)
cnames = create_cnames(hosts)
ipadresses = create_ipaddresses(hosts)

response = self.client.get(f"/api/v1/{endpoint}/?{query_key}={target}")
self.assertEqual(response.status_code, 200)
self.assertEqual(response.status_code, 200, msg=f"{msg_prefix} {response.content}")
data = response.json()
self.assertEqual(data["count"], expected_hits, msg=f"{msg_prefix} {data}")

for host in hosts:
host.delete()
for obj in chain(ipadresses, cnames, hosts):
obj.delete()


for cname in cnames:
cname.delete()

0 comments on commit 0a94786

Please sign in to comment.