2026-01-13 20:58:05 -05:00
|
|
|
from django.test import TestCase
|
2026-01-19 16:35:00 -05:00
|
|
|
from django.utils import html
|
2026-01-23 22:23:40 -05:00
|
|
|
from unittest import skip
|
2026-01-23 22:30:42 -05:00
|
|
|
from ..forms import (
|
|
|
|
|
DUPLICATE_ITEM_ERROR,
|
|
|
|
|
EMPTY_ITEM_ERROR,
|
|
|
|
|
)
|
2026-01-14 13:56:21 -05:00
|
|
|
from ..models import Item, List
|
2026-01-13 20:58:05 -05:00
|
|
|
import lxml.html
|
|
|
|
|
|
|
|
|
|
class HomePageTest(TestCase):
|
|
|
|
|
def test_uses_home_template(self):
|
|
|
|
|
response = self.client.get('/')
|
|
|
|
|
self.assertTemplateUsed(response, 'apps/dashboard/home.html')
|
|
|
|
|
|
|
|
|
|
def test_renders_input_form(self):
|
|
|
|
|
response = self.client.get('/')
|
|
|
|
|
parsed = lxml.html.fromstring(response.content)
|
2026-01-29 15:21:54 -05:00
|
|
|
forms = parsed.cssselect('form[method=POST]')
|
|
|
|
|
self.assertIn("/apps/dashboard/new_list", [form.get("action") for form in forms])
|
|
|
|
|
[form] = [form for form in forms if form.get("action") == "/apps/dashboard/new_list"]
|
|
|
|
|
inputs = form.cssselect("input")
|
|
|
|
|
self.assertIn("text", [input.get("name") for input in inputs])
|
2026-01-13 20:58:05 -05:00
|
|
|
|
2026-01-19 19:09:11 -05:00
|
|
|
class NewListTest(TestCase):
|
|
|
|
|
def test_can_save_a_POST_request(self):
|
2026-01-29 15:21:54 -05:00
|
|
|
self. client.post("/apps/dashboard/new_list", data={"text": "A new list item"})
|
2026-01-19 19:09:11 -05:00
|
|
|
self.assertEqual(Item.objects.count(), 1)
|
|
|
|
|
new_item = Item.objects.get()
|
2026-01-29 15:21:54 -05:00
|
|
|
self.assertEqual(new_item.text, "A new list item")
|
2026-01-19 19:09:11 -05:00
|
|
|
|
|
|
|
|
def test_redirects_after_POST(self):
|
2026-01-29 15:21:54 -05:00
|
|
|
response = self.client.post("/apps/dashboard/new_list", data={"text": "A new list item"})
|
2026-01-19 19:09:11 -05:00
|
|
|
new_list = List.objects.get()
|
2026-01-29 15:21:54 -05:00
|
|
|
self.assertRedirects(response, f"/apps/dashboard/{new_list.id}/")
|
2026-01-19 19:09:11 -05:00
|
|
|
|
2026-01-20 15:14:05 -05:00
|
|
|
# Post invalid input helper
|
|
|
|
|
def post_invalid_input(self):
|
2026-01-29 15:21:54 -05:00
|
|
|
return self.client.post("/apps/dashboard/new_list", data={"text": ""})
|
2026-01-20 15:14:05 -05:00
|
|
|
|
|
|
|
|
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()
|
2026-01-19 19:09:11 -05:00
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
self.assertTemplateUsed(response, "apps/dashboard/home.html")
|
2026-01-20 15:14:05 -05:00
|
|
|
|
|
|
|
|
def test_for_invalid_input_shows_error_on_page(self):
|
|
|
|
|
response = self.post_invalid_input()
|
|
|
|
|
self.assertContains(response, html.escape(EMPTY_ITEM_ERROR))
|
2026-01-19 19:09:11 -05:00
|
|
|
|
2026-01-24 13:36:31 -05:00
|
|
|
class ListViewTest(TestCase):
|
2026-01-13 20:58:05 -05:00
|
|
|
def test_uses_list_template(self):
|
|
|
|
|
mylist = List.objects.create()
|
2026-01-29 15:21:54 -05:00
|
|
|
response = self.client.get(f"/apps/dashboard/{mylist.id}/")
|
|
|
|
|
self.assertTemplateUsed(response, "apps/dashboard/list.html")
|
2026-01-13 20:58:05 -05:00
|
|
|
|
|
|
|
|
def test_renders_input_form(self):
|
|
|
|
|
mylist = List.objects.create()
|
2026-01-29 15:21:54 -05:00
|
|
|
url = f"/apps/dashboard/{mylist.id}/"
|
|
|
|
|
response = self.client.get(url)
|
2026-01-13 20:58:05 -05:00
|
|
|
parsed = lxml.html.fromstring(response.content)
|
2026-01-29 15:21:54 -05:00
|
|
|
forms = parsed.cssselect("form[method=POST]")
|
|
|
|
|
self.assertIn(url, [form.get("action") for form in forms])
|
|
|
|
|
[form] = [form for form in forms if form.get("action") == url]
|
|
|
|
|
inputs = form.cssselect("input")
|
|
|
|
|
self.assertIn("text", [input.get("name") for input in inputs])
|
2026-01-13 20:58:05 -05:00
|
|
|
|
|
|
|
|
def test_displays_only_items_for_that_list(self):
|
|
|
|
|
# Given/Arrange
|
|
|
|
|
correct_list = List.objects.create()
|
2026-01-29 15:21:54 -05:00
|
|
|
Item.objects.create(text="itemey 1", list=correct_list)
|
|
|
|
|
Item.objects.create(text="itemey 2", list=correct_list)
|
2026-01-13 20:58:05 -05:00
|
|
|
other_list = List.objects.create()
|
2026-01-29 15:21:54 -05:00
|
|
|
Item.objects.create(text="other list item", list=other_list)
|
2026-01-13 20:58:05 -05:00
|
|
|
# When/Act
|
2026-01-29 15:21:54 -05:00
|
|
|
response = self.client.get(f"/apps/dashboard/{correct_list.id}/")
|
2026-01-13 20:58:05 -05:00
|
|
|
# Then/Assert
|
2026-01-29 15:21:54 -05:00
|
|
|
self.assertContains(response, "itemey 1")
|
|
|
|
|
self.assertContains(response, "itemey 2")
|
|
|
|
|
self.assertNotContains(response, "other list item")
|
2026-01-13 20:58:05 -05:00
|
|
|
|
2026-01-19 18:48:21 -05:00
|
|
|
def test_can_save_a_POST_request_to_an_existing_list(self):
|
|
|
|
|
other_list = List.objects.create()
|
|
|
|
|
correct_list = List.objects.create()
|
|
|
|
|
|
|
|
|
|
self.client.post(
|
2026-01-29 15:21:54 -05:00
|
|
|
f"/apps/dashboard/{correct_list.id}/",
|
|
|
|
|
data={"text": "A new item for an existing list"},
|
2026-01-19 18:48:21 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
self.assertEqual(Item.objects.count(), 1)
|
|
|
|
|
new_item = Item.objects.get()
|
2026-01-29 15:21:54 -05:00
|
|
|
self.assertEqual(new_item.text, "A new item for an existing list")
|
2026-01-19 18:48:21 -05:00
|
|
|
self.assertEqual(new_item.list, correct_list)
|
|
|
|
|
|
|
|
|
|
def test_POST_redirects_to_list_view(self):
|
|
|
|
|
other_list = List.objects.create()
|
|
|
|
|
correct_list = List.objects.create()
|
|
|
|
|
|
|
|
|
|
response = self.client.post(
|
2026-01-29 15:21:54 -05:00
|
|
|
f"/apps/dashboard/{correct_list.id}/",
|
|
|
|
|
data={"text": "A new item for an existing list"},
|
2026-01-19 18:48:21 -05:00
|
|
|
)
|
|
|
|
|
|
2026-01-29 15:21:54 -05:00
|
|
|
self.assertRedirects(response, f"/apps/dashboard/{correct_list.id}/")
|
2026-01-19 18:48:21 -05:00
|
|
|
|
2026-01-20 15:14:05 -05:00
|
|
|
# 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()
|
2026-01-19 16:35:00 -05:00
|
|
|
self.assertEqual(response.status_code, 200)
|
2026-01-19 19:09:11 -05:00
|
|
|
self.assertTemplateUsed(response, "apps/dashboard/list.html")
|
2026-01-20 15:14:05 -05:00
|
|
|
|
|
|
|
|
def test_for_invalid_input_shows_error_on_page(self):
|
|
|
|
|
response = self.post_invalid_input()
|
|
|
|
|
self.assertContains(response, html.escape(EMPTY_ITEM_ERROR))
|
2026-01-23 22:23:40 -05:00
|
|
|
|
2026-01-24 13:36:31 -05:00
|
|
|
def test_for_invalid_input_sets_is_invalid_class(self):
|
|
|
|
|
response = self.post_invalid_input()
|
|
|
|
|
parsed = lxml.html.fromstring(response.content)
|
|
|
|
|
[input] = parsed.cssselect("input[name=text]")
|
|
|
|
|
self.assertIn("is-invalid", set(input.classes))
|
|
|
|
|
|
2026-01-23 22:23:40 -05:00
|
|
|
def test_duplicate_item_validation_errors_end_up_on_lists_page(self):
|
|
|
|
|
list1 = List.objects.create()
|
|
|
|
|
Item.objects.create(list=list1, text="lorem ipsum")
|
|
|
|
|
|
|
|
|
|
response = self.client.post(
|
|
|
|
|
f"/apps/dashboard/{list1.id}/",
|
|
|
|
|
data={"text": "lorem ipsum"},
|
|
|
|
|
)
|
|
|
|
|
|
2026-01-23 22:30:42 -05:00
|
|
|
expected_error = html.escape(DUPLICATE_ITEM_ERROR)
|
2026-01-23 22:23:40 -05:00
|
|
|
self.assertContains(response, expected_error)
|
|
|
|
|
self.assertTemplateUsed(response, "apps/dashboard/list.html")
|
|
|
|
|
self.assertEqual(Item.objects.all().count(), 1)
|