diff --git a/conftest.py b/conftest.py index e698a7b..0a97bf5 100644 --- a/conftest.py +++ b/conftest.py @@ -8,13 +8,14 @@ from django.conf import settings # this is to create a Domain entries in the database, so they can be used for unit tests: BASEDOMAIN = "nsupdate.info" -TEST_HOST = 'test.' + BASEDOMAIN # unit tests can update this host ONLY +TESTDOMAIN = "tests." + BASEDOMAIN +TEST_HOST = 'test.' + TESTDOMAIN # unit tests can update this host ONLY TEST_SECRET = "secret" -TEST_HOST2 = 'test2.' + BASEDOMAIN +TEST_HOST2 = 'test2.' + TESTDOMAIN TEST_SECRET2 = "somethingelse" NAMESERVER_IP = "85.10.192.104" NAMESERVER_UPDATE_ALGORITHM = "HMAC_SHA512" -# no problem, you can ONLY update the TEST_HOST with this secret, nothing else: +# no problem, you can ONLY update the TESTDOMAIN with this secret, nothing else: NAMESERVER_UPDATE_SECRET = "YWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYQ==" NAMESERVER_PUBLIC = True @@ -29,6 +30,26 @@ SECURE = False # SSL/SNI support on python 2.x sucks :( from django.utils.translation import activate +from random import randint +from nsupdate.main.dnstools import update_ns + + +@pytest.yield_fixture(scope="function") +def ddns_hostname(): + """ + get a random hostname for tests and make sure it is removed from dns + after the test + """ + hostname = "test%d" % randint(1000000000, 2000000000) + yield hostname + update_ns(hostname + '.' + TESTDOMAIN, 'A', action='del') + update_ns(hostname + '.' + TESTDOMAIN, 'AAAA', action='del') + + +@pytest.yield_fixture(scope="function") +def ddns_fqdn(ddns_hostname): + yield ddns_hostname + '.' + TESTDOMAIN + # Note: fixture must be "function" scope (default), see https://github.com/pelme/pytest_django/issues/33 @pytest.fixture(autouse=True) @@ -44,9 +65,9 @@ def db_init(db): # note: db is a predefined fixture and required here to have t u.save() u2 = user_model.objects.create_user(USERNAME2, 'test@example.org', PASSWORD) u2.save() - # this is for updating: - Domain.objects.create( - domain=TEST_HOST, # special: single-host update secret! + # this is for tests: + dt = Domain.objects.create( + domain=TESTDOMAIN, # special: test-domain update secret! nameserver_ip=NAMESERVER_IP, nameserver_update_algorithm=NAMESERVER_UPDATE_ALGORITHM, nameserver_update_secret=NAMESERVER_UPDATE_SECRET, @@ -63,9 +84,9 @@ def db_init(db): # note: db is a predefined fixture and required here to have t created_by=u2, ) # a Host for api / session update tests - h = Host(subdomain='test', domain=d, created_by=u) + h = Host(subdomain='test', domain=dt, created_by=u) h.generate_secret(secret=TEST_SECRET) - h2 = Host(subdomain='test2', domain=d, created_by=u2) + h2 = Host(subdomain='test2', domain=dt, created_by=u2) h2.generate_secret(secret=TEST_SECRET2) # "update other service" ddns_client feature diff --git a/nsupdate/api/_tests/test_api.py b/nsupdate/api/_tests/test_api.py index 3500bc7..3d7fe9d 100644 --- a/nsupdate/api/_tests/test_api.py +++ b/nsupdate/api/_tests/test_api.py @@ -8,8 +8,9 @@ from nsupdate.main.dnstools import query_ns from nsupdate.main.models import Domain -TEST_HOST = "test.nsupdate.info" -TEST_HOST2 = "test2.nsupdate.info" +TESTDOMAIN = "tests.nsupdate.info" +TEST_HOST = "test." + TESTDOMAIN +TEST_HOST2 = "test2." + TESTDOMAIN TEST_SECRET = "secret" USERNAME = 'test' @@ -83,7 +84,7 @@ def test_nic_update_authorized(client): def test_nic_update_authorized_ns_unavailable(client): - d = Domain.objects.get(domain=TEST_HOST) + d = Domain.objects.get(domain=TESTDOMAIN) d.available = False # simulate DNS unavailability d.save() response = client.get(reverse('nic_update'), diff --git a/nsupdate/main/_tests/test_dnstools.py b/nsupdate/main/_tests/test_dnstools.py index 38271be..3ac5d79 100644 --- a/nsupdate/main/_tests/test_dnstools.py +++ b/nsupdate/main/_tests/test_dnstools.py @@ -14,8 +14,7 @@ from ..dnstools import add, delete, update, query_ns, rev_lookup, parse_name, up # see also conftest.py BASEDOMAIN = 'nsupdate.info' -TEST_HOST = 'test.' + BASEDOMAIN # you can ONLY update THIS host in unit tests -INVALID_HOST = 'test999.' + BASEDOMAIN # therefore, this can't get updated +INVALID_HOST = 'test999.' + BASEDOMAIN # this can't get updated def remove_records(host, records=('A', 'AAAA', )): @@ -29,8 +28,8 @@ def remove_records(host, records=('A', 'AAAA', )): class TestIntelligentUpdater(object): - def test_double_update(self): - host, ip = TEST_HOST, '1.2.3.4' + def test_double_update(self, ddns_fqdn): + host, ip = ddns_fqdn, '1.2.3.4' remove_records(host) # first update with this IP, should work without issue: update(host, ip) @@ -41,8 +40,8 @@ class TestIntelligentUpdater(object): class TestIntelligentAdder(object): - def test_double_add_same(self): - host, ip = TEST_HOST, '1.2.3.4' + def test_double_add_same(self, ddns_fqdn): + host, ip = ddns_fqdn, '1.2.3.4' remove_records(host) # first add with this IP, should work without issue: add(host, ip) @@ -51,8 +50,8 @@ class TestIntelligentAdder(object): # trying to add again with same IP should raise add(host, ip) - def test_double_add_different(self): - host, ip = TEST_HOST, '1.2.3.4' + def test_double_add_different(self, ddns_fqdn): + host, ip = ddns_fqdn, '1.2.3.4' remove_records(host) # first add with this IP, should work without issue: add(host, ip) @@ -64,8 +63,8 @@ class TestIntelligentAdder(object): class TestIntelligentDeleter(object): - def test_delete(self): - host, ip = TEST_HOST, '1.2.3.4' + def test_delete(self, ddns_fqdn): + host, ip = ddns_fqdn, '1.2.3.4' # make sure the host is there update_ns(host, 'A', ip, action='add') delete(host) @@ -73,13 +72,13 @@ class TestIntelligentDeleter(object): with pytest.raises(NXDOMAIN): query_ns(host, 'A') - def test_double_delete(self): - host = TEST_HOST + def test_double_delete(self, ddns_fqdn): + host = ddns_fqdn remove_records(host) delete(host) # hmm, this doesn't raise NXDOMAIN!? - def test_delete_typed(self): - host, ip4, ip6 = TEST_HOST, '1.2.3.4', '::42' + def test_delete_typed(self, ddns_fqdn): + host, ip4, ip6 = ddns_fqdn, '1.2.3.4', '::42' # make sure the host is there update_ns(host, 'A', ip4, action='add') update_ns(host, 'AAAA', ip6, action='add') @@ -94,19 +93,19 @@ class TestIntelligentDeleter(object): class TestQuery(object): - def test_queries_ok(self): - host, ipv4, ipv6 = TEST_HOST, '42.42.42.42', '::23' + def test_queries_ok(self, ddns_fqdn): + host, ipv4, ipv6 = ddns_fqdn, '42.42.42.42', '::23' remove_records(host) with pytest.raises(NXDOMAIN): - query_ns(TEST_HOST, 'A') + query_ns(ddns_fqdn, 'A') with pytest.raises(NXDOMAIN): - query_ns(TEST_HOST, 'AAAA') + query_ns(ddns_fqdn, 'AAAA') add(host, ipv4) - assert query_ns(TEST_HOST, 'A') == ipv4 + assert query_ns(ddns_fqdn, 'A') == ipv4 with pytest.raises(NoAnswer): - query_ns(TEST_HOST, 'AAAA') + query_ns(ddns_fqdn, 'AAAA') add(host, ipv6) - assert query_ns(TEST_HOST, 'AAAA') == ipv6 + assert query_ns(ddns_fqdn, 'AAAA') == ipv6 class TestReverseLookup(object): @@ -137,8 +136,8 @@ class TestUpdate(object): assert str(origin) == 'bar.baz.org' + '.' assert str(relname) == 'foo' - def test_add_del_v4(self): - host, ip = TEST_HOST, '1.1.1.1' + def test_add_del_v4(self, ddns_fqdn): + host, ip = ddns_fqdn, '1.1.1.1' remove_records(host) response = update_ns(host, 'A', ip, action='add', ttl=60) print(response) @@ -148,19 +147,19 @@ class TestUpdate(object): with pytest.raises(NXDOMAIN): query_ns(host, 'A') == ip - def test_update_v4(self): - host, ip = TEST_HOST, '2.2.2.2' + def test_update_v4(self, ddns_fqdn): + host, ip = ddns_fqdn, '2.2.2.2' response = update_ns(host, 'A', ip, action='upd', ttl=60) print(response) assert query_ns(host, 'A') == ip - host, ip = TEST_HOST, '3.3.3.3' + host, ip = ddns_fqdn, '3.3.3.3' response = update_ns(host, 'A', ip, action='upd', ttl=60) print(response) assert query_ns(host, 'A') == ip - def test_add_del_v6(self): - host, ip = TEST_HOST, '::1' + def test_add_del_v6(self, ddns_fqdn): + host, ip = ddns_fqdn, '::1' remove_records(host) response = update_ns(host, 'AAAA', ip, action='add', ttl=60) print(response) @@ -170,24 +169,24 @@ class TestUpdate(object): with pytest.raises(NXDOMAIN): query_ns(host, 'AAAA') == ip - def test_update_v6(self): - host, ip = TEST_HOST, '::2' + def test_update_v6(self, ddns_fqdn): + host, ip = ddns_fqdn, '::2' response = update_ns(host, 'AAAA', ip, action='upd', ttl=60) print(response) assert query_ns(host, 'AAAA') == ip - host, ip = TEST_HOST, '::3' + host, ip = ddns_fqdn, '::3' response = update_ns(host, 'AAAA', ip, action='upd', ttl=60) print(response) assert query_ns(host, 'AAAA') == ip - def test_update_mixed(self): - host4, ip4 = TEST_HOST, '4.4.4.4' + def test_update_mixed(self, ddns_fqdn): + host4, ip4 = ddns_fqdn, '4.4.4.4' response = update_ns(host4, 'A', ip4, action='upd', ttl=60) print(response) assert query_ns(host4, 'A') == ip4 - host6, ip6 = TEST_HOST, '::4' + host6, ip6 = ddns_fqdn, '::4' response = update_ns(host6, 'AAAA', ip6, action='upd', ttl=60) print(response) assert query_ns(host6, 'AAAA') == ip6 @@ -195,7 +194,7 @@ class TestUpdate(object): # make sure the v4 is unchanged assert query_ns(host4, 'A') == ip4 - host4, ip4 = TEST_HOST, '5.5.5.5' + host4, ip4 = ddns_fqdn, '5.5.5.5' response = update_ns(host4, 'A', ip4, action='upd', ttl=60) print(response) assert query_ns(host4, 'A') == ip4 @@ -204,7 +203,7 @@ class TestUpdate(object): assert query_ns(host6, 'AAAA') == ip6 def test_bad_update(self): - # test whether we ONLY can update the TEST_HOST + # test whether we ONLY can update the TESTDOMAIN with pytest.raises(DnsUpdateError): response = update_ns(INVALID_HOST, 'A', '6.6.6.6', action='upd', ttl=60) print(response)