diff --git a/conftest.py b/conftest.py index 4727684..cf50ba1 100644 --- a/conftest.py +++ b/conftest.py @@ -45,6 +45,7 @@ def db_init(db): # note: db is a predefined fixture and required here to have t nameserver_update_algorithm=NAMESERVER_UPDATE_ALGORITHM, nameserver_update_key=NAMESERVER_UPDATE_KEY, public=NAMESERVER_PUBLIC, + created_by=u, ) # this is for querying: d = Domain.objects.create( @@ -53,6 +54,7 @@ def db_init(db): # note: db is a predefined fixture and required here to have t nameserver_update_algorithm=NAMESERVER_UPDATE_ALGORITHM, nameserver_update_key='invalid=', # we don't send updates there (and the real key is really secret) public=NAMESERVER_PUBLIC, + created_by=u2, ) # a Host for api / session update tests h = Host(subdomain='test', domain=d, created_by=u) diff --git a/nsupdate/api/_tests/test_api.py b/nsupdate/api/_tests/test_api.py index 1994aed..b90e55e 100644 --- a/nsupdate/api/_tests/test_api.py +++ b/nsupdate/api/_tests/test_api.py @@ -69,6 +69,25 @@ def test_nic_update_authorized_myip(client): assert response.status_code == 200 # we don't care whether it is nochg or good, but should be the ip from myip=...: assert response.content in ['good 4.3.2.1', 'nochg 4.3.2.1'] + response = client.get(reverse('nic_update') + '?myip=1.2.3.4', + HTTP_AUTHORIZATION=make_basic_auth_header(TEST_HOST, TEST_SECRET)) + assert response.status_code == 200 + # must be good (was different IP) + assert response.content == 'good 1.2.3.4' + response = client.get(reverse('nic_update') + '?myip=1.2.3.4', + HTTP_AUTHORIZATION=make_basic_auth_header(TEST_HOST, TEST_SECRET)) + assert response.status_code == 200 + # must be nochg (was same IP) + assert response.content == 'nochg 1.2.3.4' + + +def test_nic_update_authorized_badagent(client, settings): + settings.BAD_AGENTS = ['foo', 'bad_agent', 'bar', ] + response = client.get(reverse('nic_update'), + HTTP_AUTHORIZATION=make_basic_auth_header(TEST_HOST, TEST_SECRET), + HTTP_USER_AGENT='bad_agent') + assert response.status_code == 200 + assert response.content == 'badagent' def test_nic_update_session_nosession(client): @@ -76,16 +95,27 @@ def test_nic_update_session_nosession(client): assert response.status_code == 302 # redirects to login view -def test_nic_update_session(client): +def test_nic_update_session_no_hostname(client): client.login(username=USERNAME, password=PASSWORD) response = client.get(reverse('nic_update_authorized')) assert response.status_code == 200 assert response.content == "nohost" # we did not tell which host - response = client.get(reverse('nic_update_authorized') + '?hostname=%s&myip=%s' % (TEST_HOST, '1.2.3.4')) + + +def test_nic_update_session(client): + client.login(username=USERNAME, password=PASSWORD) + response = client.get(reverse('nic_update_authorized') + '?hostname=%s' % (TEST_HOST, )) assert response.status_code == 200 assert response.content.startswith('good ') or response.content.startswith('nochg ') +def test_nic_update_session_myip(client): + client.login(username=USERNAME, password=PASSWORD) + response = client.get(reverse('nic_update_authorized') + '?hostname=%s&myip=%s' % (TEST_HOST, '1.2.3.4')) + assert response.status_code == 200 + assert response.content.startswith('good 1.2.3.4') or response.content.startswith('nochg 1.2.3.4') + + def test_nic_update_session_foreign_host(client): client.login(username=USERNAME, password=PASSWORD) response = client.get(reverse('nic_update_authorized') + '?hostname=%s' % TEST_HOST2) diff --git a/nsupdate/main/_tests/test_dnstools.py b/nsupdate/main/_tests/test_dnstools.py index 69877e7..e722922 100644 --- a/nsupdate/main/_tests/test_dnstools.py +++ b/nsupdate/main/_tests/test_dnstools.py @@ -76,6 +76,20 @@ class TestIntelligentDeleter(object): 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' + # make sure the host is there + update_ns(host, 'A', ip4, action='add') + update_ns(host, 'AAAA', ip6, action='add') + delete(host, 'A') + # make sure it is gone + with pytest.raises(NoAnswer): + query_ns(host, 'A') + delete(host, 'AAAA') + # make sure it is gone + with pytest.raises(NXDOMAIN): + query_ns(host, 'AAAA') + class TestQuery(object): def test_queries_ok(self): diff --git a/nsupdate/main/_tests/test_main.py b/nsupdate/main/_tests/test_main.py new file mode 100644 index 0000000..75fcb7c --- /dev/null +++ b/nsupdate/main/_tests/test_main.py @@ -0,0 +1,55 @@ +""" +Tests for main views module. +""" + +import pytest + +from django.core.urlresolvers import reverse + + +USERNAME = 'test' +PASSWORD = 'pass' + + +def test_views_anon(client): + for view, kwargs, status_code in [ + ('home', dict(), 200), + ('about', dict(), 200), + ('legal', dict(), 200), + ('robots', dict(), 200), + ('status', dict(), 200), # TODO make this require a login + ('overview', dict(), 302), + # stuff that requires a log in redirects to the login view: + ('domain_overview', dict(), 302), + ('host_view', dict(pk=1), 302), + ('domain_view', dict(pk=1), 302), + ('generate_secret_view', dict(pk=1), 302), + ('generate_ns_secret_view', dict(pk=1), 302), + ('delete_host', dict(pk=1), 302), + ('delete_domain', dict(pk=1), 302), + ]: + print view, kwargs, status_code + response = client.get(reverse(view, kwargs=kwargs)) + assert response.status_code == status_code + + +def test_views_logged_in(client): + client.login(username=USERNAME, password=PASSWORD) + for view, kwargs, status_code in [ + ('home', dict(), 200), + ('about', dict(), 200), + ('legal', dict(), 200), + ('robots', dict(), 200), + ('status', dict(), 200), + ('overview', dict(), 200), + ('domain_overview', dict(), 200), + ('host_view', dict(pk=1), 200), + ('domain_view', dict(pk=1), 200), + ('generate_secret_view', dict(pk=1), 200), + ('generate_ns_secret_view', dict(pk=1), 200), + ('delete_host', dict(pk=1), 200), + ('delete_domain', dict(pk=1), 200), + ]: + print view, kwargs, status_code + response = client.get(reverse(view, kwargs=kwargs)) + assert response.status_code == status_code diff --git a/nsupdate/main/urls.py b/nsupdate/main/urls.py index a821406..1ee38dd 100644 --- a/nsupdate/main/urls.py +++ b/nsupdate/main/urls.py @@ -31,5 +31,5 @@ urlpatterns = patterns( url(r'^myip$', myip_view, name='myip'), url(r'^nic/update$', NicUpdateView.as_view(), name='nic_update'), # for bots - url(r'^robots.txt$', RobotsTxtView.as_view()), + url(r'^robots.txt$', RobotsTxtView.as_view(), name='robots'), )