diff --git a/src/apps/dashboard/tests/test_views.py b/src/apps/dashboard/tests/test_views.py index 60474fb..28d8983 100644 --- a/src/apps/dashboard/tests/test_views.py +++ b/src/apps/dashboard/tests/test_views.py @@ -1,5 +1,6 @@ from django.test import TestCase from django.utils import html +from ..forms import EMPTY_ITEM_ERROR from ..models import Item, List import lxml.html @@ -28,17 +29,22 @@ class NewListTest(TestCase): new_list = List.objects.get() self.assertRedirects(response, f'/apps/dashboard/{new_list.id}/') - def test_validation_errors_are_sent_back_to_home_page_template(self): - response = self.client.post("/apps/dashboard/newlist", data={"text": ""}) + # Post invalid input helper + def post_invalid_input(self): + return self.client.post("/apps/dashboard/newlist", data={"text": ""}) + + def test_for_invalid_input_nothing_saved_to_db(self): + self.post_invalid_input() + self.assertEqual(Item.objects.count(), 0) + + def test_for_invalid_input_renders_list_template(self): + response = self.post_invalid_input() self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, "apps/dashboard/home.html") - expected_error = html.escape("You can't have an empty list item") - self.assertContains(response, expected_error) - - def test_invalid_list_items_never_save(self): - self.client.post("/apps/dashboard/newlist", data={"text": ""}) - self.assertEqual(List.objects.count(), 0) - self.assertEqual(Item.objects.count(), 0) + + def test_for_invalid_input_shows_error_on_page(self): + response = self.post_invalid_input() + self.assertContains(response, html.escape(EMPTY_ITEM_ERROR)) class DashViewTest(TestCase): def test_uses_list_template(self): @@ -94,13 +100,20 @@ class DashViewTest(TestCase): self.assertRedirects(response, f'/apps/dashboard/{correct_list.id}/') - def test_validation_errors_end_up_on_lists_page(self): - list_ = List.objects.create() - response = self.client.post( - f"/apps/dashboard/{list_.id}/", - data={"text": ""}, - ) + # Post invalid input helper + def post_invalid_input(self): + mylist = List.objects.create() + return self.client.post(f"/apps/dashboard/{mylist.id}/", data={"text": ""}) + + def test_for_invalid_input_nothing_saved_to_db(self): + self.post_invalid_input() + self.assertEqual(Item.objects.count(), 0) + + def test_for_invalid_input_renders_list_template(self): + response = self.post_invalid_input() self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, "apps/dashboard/list.html") - expected_error = html.escape("You can't have an empty list item") - self.assertContains(response, expected_error) + + def test_for_invalid_input_shows_error_on_page(self): + response = self.post_invalid_input() + self.assertContains(response, html.escape(EMPTY_ITEM_ERROR)) diff --git a/src/apps/dashboard/views.py b/src/apps/dashboard/views.py index 7c0f2c0..af2d46d 100644 --- a/src/apps/dashboard/views.py +++ b/src/apps/dashboard/views.py @@ -4,26 +4,24 @@ from .forms import ItemForm from .models import Item, List def home_page(request): - # return render(request, 'apps/dashboard/home.html', {"form": ItemForm()}) - return render(request, "apps/dashboard/home.html") + return render(request, 'apps/dashboard/home.html', {"form": ItemForm()}) def new_list(request): - nulist = List.objects.create() - item = Item(text=request.POST['text'], list=nulist) - try: - item.full_clean() - item.save() - except ValidationError: - nulist.delete() - error = "You can't have an empty list item" - return render(request, "apps/dashboard/home.html", {"error": error}) - return redirect(nulist) + form = ItemForm(data=request.POST) + if form.is_valid(): + nulist = List.objects.create() + Item.objects.create(text=request.POST["text"], list=nulist) + return redirect(nulist) + else: + return render(request, "apps/dashboard/home.html", {"form": form}) def view_list(request, list_id): our_list = List.objects.get(id=list_id) error = None + form = ItemForm() if request.method == "POST": + form = ItemForm(data=request.POST) try: item = Item(text=request.POST['text'], list=our_list) item.full_clean() @@ -32,5 +30,5 @@ def view_list(request, list_id): except ValidationError: error = "You can't have an empty list item" - return render(request, 'apps/dashboard/list.html', {'list': our_list, "error": error}) + return render(request, 'apps/dashboard/list.html', {'list': our_list, "form": form, "error": error}) diff --git a/src/functional_tests/base.py b/src/functional_tests/base.py index 03ee51c..1dc54e3 100644 --- a/src/functional_tests/base.py +++ b/src/functional_tests/base.py @@ -40,3 +40,6 @@ class FunctionalTest(StaticLiveServerTestCase): if time.time() - start_time > MAX_WAIT: raise time.sleep(0.5) + + def get_item_input_box(self): + return self.browser.find_element(By.ID, "id-text") diff --git a/src/functional_tests/test_layout_and_styling.py b/src/functional_tests/test_layout_and_styling.py index 32c7f0a..bc863a9 100644 --- a/src/functional_tests/test_layout_and_styling.py +++ b/src/functional_tests/test_layout_and_styling.py @@ -10,7 +10,7 @@ class LayoutAndStylingTest(FunctionalTest): self.browser.set_window_size(1024, 768) # print("Viewport width:", self.browser.execute_script("return window.innerWidth")) - inputbox = self.browser.find_element(By.ID, 'id-new-item') + inputbox = self.get_item_input_box() self.assertAlmostEqual( inputbox.location['x'] + inputbox.size['width'] / 2, 512, @@ -20,7 +20,7 @@ class LayoutAndStylingTest(FunctionalTest): inputbox.send_keys('testing') inputbox.send_keys(Keys.ENTER) self.wait_for_row_in_list_table('1. testing') - inputbox = self.browser.find_element(By.ID, 'id-new-item') + inputbox = self.get_item_input_box() self.assertAlmostEqual( inputbox.location['x'] + inputbox.size['width'] / 2, 512, diff --git a/src/functional_tests/test_list_item_validation.py b/src/functional_tests/test_list_item_validation.py index 369c664..101a323 100644 --- a/src/functional_tests/test_list_item_validation.py +++ b/src/functional_tests/test_list_item_validation.py @@ -6,7 +6,7 @@ from .base import FunctionalTest class ItemValidationTest(FunctionalTest): def test_cannot_add_empty_list_items(self): self.browser.get(self.live_server_url) - self.browser.find_element(By.ID, "id-new-item").send_keys(Keys.ENTER) + self.get_item_input_box().send_keys(Keys.ENTER) self.wait_for( lambda: self.assertEqual( @@ -15,12 +15,12 @@ class ItemValidationTest(FunctionalTest): ) ) - self.browser.find_element(By.ID, "id-new-item").send_keys("Purchase milk") - self.browser.find_element(By.ID, "id-new-item").send_keys(Keys.ENTER) + self.get_item_input_box().send_keys("Purchase milk") + self.get_item_input_box().send_keys(Keys.ENTER) self.wait_for_row_in_list_table("1. Purchase milk") - self.browser.find_element(By.ID, "id-new-item").send_keys(Keys.ENTER) + self.get_item_input_box().send_keys(Keys.ENTER) self.wait_for( lambda: self.assertEqual( @@ -29,6 +29,6 @@ class ItemValidationTest(FunctionalTest): ) ) - self.browser.find_element(By.ID, 'id-new-item').send_keys("Make tea") - self.browser.find_element(By.ID, "id-new-item").send_keys(Keys.ENTER) + self.get_item_input_box().send_keys("Make tea") + self.get_item_input_box().send_keys(Keys.ENTER) self.wait_for_row_in_list_table("2. Make tea") diff --git a/src/functional_tests/test_simple_list_creation.py b/src/functional_tests/test_simple_list_creation.py index 53ba6b8..62a41af 100644 --- a/src/functional_tests/test_simple_list_creation.py +++ b/src/functional_tests/test_simple_list_creation.py @@ -14,7 +14,7 @@ class NewVisitorTest(FunctionalTest): header_text = self.browser.find_element(By.TAG_NAME, 'h1').text self.assertIn('Dashboard', header_text) - inputbox = self.browser.find_element(By.ID, 'id-new-item') + inputbox = self.get_item_input_box() self.assertEqual(inputbox.get_attribute('placeholder'), 'Enter a to-do item') inputbox.send_keys('Buy peacock feathers') @@ -22,7 +22,7 @@ class NewVisitorTest(FunctionalTest): inputbox.send_keys(Keys.ENTER) self.wait_for_row_in_list_table('1. Buy peacock feathers') - inputbox = self.browser.find_element(By.ID, 'id-new-item') + inputbox = self.get_item_input_box() inputbox.send_keys('Use peacock feathers to make a fly') inputbox.send_keys(Keys.ENTER) @@ -31,7 +31,7 @@ class NewVisitorTest(FunctionalTest): def test_multiple_users_can_start_lists_at_different_urls(self): self.browser.get(self.live_server_url) - inputbox = self.browser.find_element(By.ID, 'id-new-item') + inputbox = self.get_item_input_box() inputbox.send_keys('Buy peacock feathers') inputbox.send_keys(Keys.ENTER) self.wait_for_row_in_list_table('1. Buy peacock feathers') @@ -45,7 +45,7 @@ class NewVisitorTest(FunctionalTest): page_text = self.browser.find_element(By.TAG_NAME, 'body').text self.assertNotIn('Buy peacock feathers', page_text) - inputbox = self.browser.find_element(By.ID, 'id-new-item') + inputbox = self.get_item_input_box() inputbox.send_keys('Buy milk') inputbox.send_keys(Keys.ENTER) self.wait_for_row_in_list_table('1. Buy milk') diff --git a/src/templates/core/base.html b/src/templates/core/base.html index dec53ff..03ad956 100644 --- a/src/templates/core/base.html +++ b/src/templates/core/base.html @@ -18,19 +18,10 @@