From 6a769e4cbb22552c128bbebf95c01a64527b1cd5 Mon Sep 17 00:00:00 2001 From: Daniel Roschka Date: Sun, 29 Mar 2015 14:04:43 +0200 Subject: [PATCH 1/6] Fixes the behavior for not existing BIND servers When requesting one of binders BIND server specific URLs for a server not present in binders database currently uncatched exceptions are thrown. This commit fixes that for all URLs containing the server name, by instead of throwing an uncatched exception, returning a proper HTTP 404 status code. --- binder/tests/testViews.py | 5 +---- binder/views.py | 26 ++++++++++++-------------- 2 files changed, 13 insertions(+), 18 deletions(-) diff --git a/binder/tests/testViews.py b/binder/tests/testViews.py index 4954f35..5d5d959 100644 --- a/binder/tests/testViews.py +++ b/binder/tests/testViews.py @@ -34,10 +34,7 @@ class GetTests(TestCase): """ Get a zone list for a server not in the database.""" server_name = "unconfigured.server.net" response = self.client.get(reverse("server_zone_list", args=(server_name, ))) - self.assertEqual(response.status_code, 200) - self.assertContains(response, ('
Errors were encountered:
' - 'There is no configured server by that name: unconfigured.server.net
'), - html=True) + self.assertEqual(response.status_code, 404) class PostTests(TestCase): diff --git a/binder/views.py b/binder/views.py index 7047df1..d0a62d8 100644 --- a/binder/views.py +++ b/binder/views.py @@ -1,7 +1,7 @@ ### Binder VIews # 3rd Party -from django.shortcuts import redirect, render +from django.shortcuts import get_object_or_404, redirect, render # App Imports from binder import exceptions, forms, helpers, models @@ -27,15 +27,13 @@ def view_server_zones(request, dns_server): """ Display the list of DNS zones a particular DNS host provides. """ errors = "" zone_array = {} + + this_server = get_object_or_404(models.BindServer, hostname=dns_server) + try: - this_server = models.BindServer.objects.get(hostname=dns_server) - except models.BindServer.DoesNotExist, err: - errors = "There is no configured server by that name: %s" % dns_server - else: - try: - zone_array = this_server.list_zones() - except exceptions.ZoneException, err: - errors = "Unable to list server zones. Error: %s" % err + zone_array = this_server.list_zones() + except exceptions.ZoneException, err: + errors = "Unable to list server zones. Error: %s" % err return render(request, "bcommon/list_server_zones.html", { "errors" : errors, @@ -47,7 +45,9 @@ def view_zone_records(request, dns_server, zone_name): """ Display the list of records for a particular zone. """ errors = "" zone_array = {} - this_server = models.BindServer.objects.get(hostname=dns_server) + + this_server = get_object_or_404(models.BindServer, hostname=dns_server) + try: zone_array = this_server.list_zone_records(zone_name) except exceptions.TransferException, err: @@ -55,8 +55,6 @@ def view_zone_records(request, dns_server, zone_name): { "errors" : err, "zone_name" : zone_name, "dns_server" : this_server}) - except models.BindServer.DoesNotExist: - errors = "Requesting a zone listing for a Bind server that is not configured: %s" % dns_server return render(request, "bcommon/list_zone.html", { "zone_array" : zone_array, @@ -67,7 +65,7 @@ def view_zone_records(request, dns_server, zone_name): def view_add_record(request, dns_server, zone_name): """ View to provide form to add a DNS record. """ - this_server = models.BindServer.objects.get(hostname=dns_server) + this_server = get_object_or_404(models.BindServer, hostname=dns_server) return render(request, "bcommon/add_record_form.html", { "dns_server" : this_server, @@ -128,7 +126,7 @@ def view_add_record_result(request): def view_add_cname_record(request, dns_server, zone_name, record_name): """ Process given input to add a CNAME pointer.""" - this_server = models.BindServer.objects.get(hostname=dns_server) + this_server = get_object_or_404(models.BindServer, hostname=dns_server) return render(request, "bcommon/add_cname_record_form.html", { "dns_server" : this_server, From 95b86434ffe3ee0785470c01221ebca3b385abdf Mon Sep 17 00:00:00 2001 From: Daniel Roschka Date: Sun, 29 Mar 2015 19:03:53 +0200 Subject: [PATCH 2/6] Fixes outstanding failing tests Fixes three failing tests. For test_ipinfo_ResolutionFail it fixes the following issues: * failing when no network is available * failing when Google changes the ip addresses of the service * failing when the order of returned ip addresses is different For test_DeleteRecordInitial_Empty and test_DeleteRecordInitial it fixes a not existing BindServer object. --- binder/tests/testHelpers.py | 7 +++---- binder/tests/testViews.py | 3 +++ 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/binder/tests/testHelpers.py b/binder/tests/testHelpers.py index 6bb011a..9553123 100644 --- a/binder/tests/testHelpers.py +++ b/binder/tests/testHelpers.py @@ -7,7 +7,6 @@ class HelperTests(TestCase): response = helpers.ip_info("foobar.doesnotexist.local") self.assertEqual([['Error', u'Unable to resolve foobar.doesnotexist.local: [Errno -2] Name or service not known']], response) - response = helpers.ip_info("time1.google.com") - self.assertEqual([['IPv4 (1)', u'216.239.32.15'], ['IPv6 (1)', u'2001:4860:4802:32::f']], - response) - + response = helpers.ip_info("localhost") + self.assertEqual([['IPv4 (1)', u'127.0.0.1'], ['IPv6 (1)', u'::1']], + sorted(response)) diff --git a/binder/tests/testViews.py b/binder/tests/testViews.py index 4954f35..a480823 100644 --- a/binder/tests/testViews.py +++ b/binder/tests/testViews.py @@ -44,6 +44,9 @@ class PostTests(TestCase): """ Unit Tests that exercise HTTP POST. """ def setUp(self): self.client = Client() + models.BindServer(hostname="testserver.test.net", + statistics_port=1234).save() + def test_DeleteRecordInitial_Empty(self): """ Ensure the initial deletion form works as expected with no RR list. """ From a6747b4f25e9cdfe85c58fac6cbf95d76f89d147 Mon Sep 17 00:00:00 2001 From: Daniel Roschka Date: Sun, 29 Mar 2015 19:47:58 +0200 Subject: [PATCH 3/6] Removed the checking for the exception message when trying to produce an IntegrityError, because that message differs based on the environment. --- binder/tests/testModels.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/binder/tests/testModels.py b/binder/tests/testModels.py index 568654e..20bf359 100644 --- a/binder/tests/testModels.py +++ b/binder/tests/testModels.py @@ -15,7 +15,7 @@ class Model_BindServer_Tests(TestCase): def test_BindServerMissingStatisticsPort(self): """Attempt to add a BindServer without a statistics port.""" bindserver_1 = models.BindServer(hostname="badtest1") - with self.assertRaisesMessage(IntegrityError, "NOT NULL constraint failed: binder_bindserver.statistics_port"): + with self.assertRaises(IntegrityError): bindserver_1.save() def test_BindServerNonIntStatisticsPort(self): From 880eee067d04e9b065aa9c25152d0248cdee8d54 Mon Sep 17 00:00:00 2001 From: jeffrey forman Date: Sat, 4 Apr 2015 20:33:53 -0400 Subject: [PATCH 4/6] localhost wont always have an IPv6 address assigned to it --- binder/tests/testHelpers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/binder/tests/testHelpers.py b/binder/tests/testHelpers.py index 9553123..aad301d 100644 --- a/binder/tests/testHelpers.py +++ b/binder/tests/testHelpers.py @@ -8,5 +8,5 @@ class HelperTests(TestCase): self.assertEqual([['Error', u'Unable to resolve foobar.doesnotexist.local: [Errno -2] Name or service not known']], response) response = helpers.ip_info("localhost") - self.assertEqual([['IPv4 (1)', u'127.0.0.1'], ['IPv6 (1)', u'::1']], + self.assertEqual([['IPv4 (1)', u'127.0.0.1']], sorted(response)) From 5c35b2306e9c1c15917a076185940a0d6e6dc52c Mon Sep 17 00:00:00 2001 From: jeffrey forman Date: Sat, 4 Apr 2015 22:07:37 -0400 Subject: [PATCH 5/6] delete commented out HTML that was forgotten from before --- .../bcommon/delete_record_initial.html | 43 ------------------- 1 file changed, 43 deletions(-) diff --git a/binder/templates/bcommon/delete_record_initial.html b/binder/templates/bcommon/delete_record_initial.html index bcf7c71..b62fa61 100644 --- a/binder/templates/bcommon/delete_record_initial.html +++ b/binder/templates/bcommon/delete_record_initial.html @@ -45,49 +45,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - From b62af7b64df78fb7b908465f789369cf7aceece1 Mon Sep 17 00:00:00 2001 From: jeffrey forman Date: Sat, 4 Apr 2015 22:07:49 -0400 Subject: [PATCH 6/6] fix UI tests that werent updated to match the new templates --- binder/tests/testViews.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/binder/tests/testViews.py b/binder/tests/testViews.py index e2154cd..c2a34c3 100644 --- a/binder/tests/testViews.py +++ b/binder/tests/testViews.py @@ -52,13 +52,13 @@ class PostTests(TestCase): "rr_list" : [] }) self.assertContains(response, - '', + '', html=True) self.assertContains(response, - '', + '', html=True) self.assertContains(response, - '', + '', html=True) @@ -68,13 +68,11 @@ class PostTests(TestCase): "zone_name" : "testzone1.test.net", "rr_list" : ["testrecord1.testzone1.test.net", "testrecord2.testzone1.test.net"] }) - self.assertContains(response, - '', + '', html=True) + self.assertContains(response, + '', html=True) self.assertContains(response, - '', - html=True) - self.assertContains(response, - '', + '', html=True)