Convert unit tests to use reverse() instead of specifying URL's hard coded

This commit is contained in:
Jeffrey Forman 2012-12-15 21:59:51 -05:00
parent a7bc198c4d
commit 26acc4f51c
2 changed files with 25 additions and 22 deletions

View File

@ -1,3 +1,4 @@
from django.core.urlresolvers import reverse
from django.test import TestCase from django.test import TestCase
from django.test.client import Client from django.test.client import Client
@ -19,7 +20,7 @@ class Integration_Tests(TestCase):
"record_data" : "10.254.1.101", "record_data" : "10.254.1.101",
"ttl" : 86400, "ttl" : 86400,
"create_reverse" : True} "create_reverse" : True}
response = self.client.post("/add_record/result/", add_dict) response = self.client.post(reverse("add_record_result"), add_dict)
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
# Make sure that we get two responses (fwd/rev) back from the server. # Make sure that we get two responses (fwd/rev) back from the server.
self.assertEqual(len(response.context["response"]), 2) self.assertEqual(len(response.context["response"]), 2)
@ -35,7 +36,7 @@ class Integration_Tests(TestCase):
"zone_name" : "domain1.local", "zone_name" : "domain1.local",
"rr_list" : '[u"record1.domain1.local", u"record2.domain1.local"]', "rr_list" : '[u"record1.domain1.local", u"record2.domain1.local"]',
} }
response = self.client.post("/delete_record/result/", delete_dict) response = self.client.post(reverse("delete_record_result"), delete_dict)
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.context["response"]), 2) self.assertEqual(len(response.context["response"]), 2)
for current_response in response.context["response"]: for current_response in response.context["response"]:
@ -53,7 +54,7 @@ class Integration_Tests(TestCase):
"record_data" : "10.254.1.101", "record_data" : "10.254.1.101",
"ttl" : 86400, "ttl" : 86400,
"create_reverse" : False} "create_reverse" : False}
response = self.client.post("/add_record/result/", add_dict) response = self.client.post(reverse("add_record_result"), add_dict)
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
# Make sure that we get two responses (fwd/rev) back from the server. # Make sure that we get two responses (fwd/rev) back from the server.
self.assertEqual(len(response.context["response"]), 1) self.assertEqual(len(response.context["response"]), 1)
@ -69,7 +70,7 @@ class Integration_Tests(TestCase):
"zone_name" : "domain1.local", "zone_name" : "domain1.local",
"ttl" : 86400, "ttl" : 86400,
} }
response = self.client.post("/add_cname_record/result/", cname_dict) response = self.client.post(reverse("add_cname_result"), cname_dict)
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
for current_response in response.context["response"]: for current_response in response.context["response"]:
dns_update_output = str(current_response["output"]) dns_update_output = str(current_response["output"])
@ -83,7 +84,7 @@ class Integration_Tests(TestCase):
original_statistics_port = dns_server.statistics_port original_statistics_port = dns_server.statistics_port
dns_server.statistics_port = 1234 dns_server.statistics_port = 1234
dns_server.save() dns_server.save()
response = self.client.get("/info/testserver1/") response = self.client.get(reverse("server_zone_list", args=("testserver1",)))
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.context["dns_server"], "testserver1") self.assertEqual(response.context["dns_server"], "testserver1")
self.assertEqual(response.context["zone_array"], {}) self.assertEqual(response.context["zone_array"], {})
@ -96,7 +97,7 @@ class Integration_Tests(TestCase):
domain3.local should be configured to require a TSIG key domain3.local should be configured to require a TSIG key
for transfers.""" for transfers."""
dns_server = models.BindServer.objects.get(hostname="testserver1") dns_server = models.BindServer.objects.get(hostname="testserver1")
response = self.client.get("/info/testserver1/domain3.local/") response = self.client.get(reverse("zone_list", args=("testserver1", "domain3.local")))
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.context["zone_name"], "domain3.local") self.assertEqual(response.context["zone_name"], "domain3.local")
self.assertEqual(response.context["dns_server"], "testserver1") self.assertEqual(response.context["dns_server"], "testserver1")

View File

@ -1,5 +1,6 @@
from django.test import TestCase from django.test import TestCase
from django.test.client import Client from django.test.client import Client
from django.core.urlresolvers import reverse
from binder import models, helpers from binder import models, helpers
@ -10,28 +11,29 @@ class GetTests(TestCase):
self.client = Client() self.client = Client()
def test_GetIndex(self): def test_GetIndex(self):
response = self.client.get("/") response = self.client.get(reverse("index"))
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
def test_GetServerIndex(self): def test_GetServerIndex(self):
response = self.client.get("/server_list/") response = self.client.get(reverse("server_list"))
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
def test_GetResultRedirects(self): def test_GetResultRedirects(self):
""" GETing a /result/ URL should always redirect to /. """ """ GETing a /result/ URL should always redirect to /. """
response = self.client.get("/add_record/result/", follow=True) response = self.client.get(reverse("add_record_result"), follow=True)
self.assertRedirects(response, "/") self.assertRedirects(response, reverse("index"))
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
response = self.client.get("/delete_record/result/", follow=True) response = self.client.get(reverse("delete_record_result"), follow=True)
self.assertRedirects(response, "/") self.assertRedirects(response, reverse("index"))
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
response = self.client.get("/add_cname_record/result/", follow=True) response = self.client.get(reverse("add_cname_result"), follow=True)
self.assertRedirects(response, "/") self.assertRedirects(response, reverse("index"))
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
def test_GetInvalidServer(self): def test_GetInvalidServer(self):
""" Get a zone list for a server not in the database.""" """ Get a zone list for a server not in the database."""
response = self.client.get("/info/unconfigured.server.net/") server_name = "unconfigured.server.net"
response = self.client.get(reverse("server_zone_list", args=(server_name, )))
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertContains(response, ('<div class="alert alert-error">Errors were encountered: <br>' self.assertContains(response, ('<div class="alert alert-error">Errors were encountered: <br>'
'There is no configured server by that name: unconfigured.server.net </div>'), 'There is no configured server by that name: unconfigured.server.net </div>'),
@ -45,9 +47,9 @@ class PostTests(TestCase):
def test_DeleteRecordInitial_Empty(self): def test_DeleteRecordInitial_Empty(self):
""" Ensure the initial deletion form works as expected with no RR list. """ """ Ensure the initial deletion form works as expected with no RR list. """
response = self.client.post("/delete_record/", { "dns_server" : "testserver.test.net", response = self.client.post(reverse("delete_record"), { "dns_server" : "testserver.test.net",
"zone_name" : "testzone1.test.net", "zone_name" : "testzone1.test.net",
"rr_list" : [] }) "rr_list" : [] })
self.assertContains(response, self.assertContains(response,
'<input type="hidden" name="zone_name" value="testzone1.test.net">', '<input type="hidden" name="zone_name" value="testzone1.test.net">',
@ -62,10 +64,10 @@ class PostTests(TestCase):
def test_DeleteRecordInitial(self): def test_DeleteRecordInitial(self):
""" Ensure the initial deletion form works as expected with RRs mentioned. """ """ Ensure the initial deletion form works as expected with RRs mentioned. """
response = self.client.post("/delete_record/", { "dns_server" : "testserver.test.net", response = self.client.post(reverse("delete_record"), {"dns_server" : "testserver.test.net",
"zone_name" : "testzone1.test.net", "zone_name" : "testzone1.test.net",
"rr_list" : ["testrecord1.testzone1.test.net", "rr_list" : ["testrecord1.testzone1.test.net",
"testrecord2.testzone1.test.net"] }) "testrecord2.testzone1.test.net"] })
self.assertContains(response, self.assertContains(response,
'<input type="hidden" name="zone_name" value="testzone1.test.net">', '<input type="hidden" name="zone_name" value="testzone1.test.net">',