2026-02-07 20:15:27 -05:00
|
|
|
import lxml.html
|
2026-02-18 19:07:02 -05:00
|
|
|
|
2026-01-13 20:58:05 -05:00
|
|
|
from django.test import TestCase
|
2026-02-22 21:18:22 -05:00
|
|
|
from django.urls import reverse
|
2026-01-19 16:35:00 -05:00
|
|
|
from django.utils import html
|
2026-02-18 13:53:05 -05:00
|
|
|
|
2026-02-18 19:07:02 -05:00
|
|
|
from apps.dashboard.forms import (
|
2026-01-23 22:30:42 -05:00
|
|
|
DUPLICATE_ITEM_ERROR,
|
|
|
|
|
EMPTY_ITEM_ERROR,
|
|
|
|
|
)
|
2026-02-18 19:07:02 -05:00
|
|
|
from apps.dashboard.models import Item, List
|
2026-02-07 20:15:27 -05:00
|
|
|
from apps.lyric.models import User
|
2026-01-13 20:58:05 -05:00
|
|
|
|
2026-02-18 13:53:05 -05:00
|
|
|
|
2026-01-13 20:58:05 -05:00
|
|
|
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]')
|
2026-02-22 22:08:34 -05:00
|
|
|
self.assertIn("/dashboard/new_list", [form.get("action") for form in forms])
|
|
|
|
|
[form] = [form for form in forms if form.get("action") == "/dashboard/new_list"]
|
2026-01-29 15:21:54 -05:00
|
|
|
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-02-22 22:08:34 -05:00
|
|
|
self. client.post("/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-02-22 22:08:34 -05:00
|
|
|
response = self.client.post("/dashboard/new_list", data={"text": "A new list item"})
|
2026-01-19 19:09:11 -05:00
|
|
|
new_list = List.objects.get()
|
2026-02-22 22:08:34 -05:00
|
|
|
self.assertRedirects(response, f"/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-02-22 22:08:34 -05:00
|
|
|
return self.client.post("/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-02-22 22:08:34 -05:00
|
|
|
response = self.client.get(f"/dashboard/{mylist.id}/")
|
2026-01-29 15:21:54 -05:00
|
|
|
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-02-22 22:08:34 -05:00
|
|
|
url = f"/dashboard/{mylist.id}/"
|
2026-01-29 15:21:54 -05:00
|
|
|
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-02-22 22:08:34 -05:00
|
|
|
response = self.client.get(f"/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-02-22 22:08:34 -05:00
|
|
|
f"/dashboard/{correct_list.id}/",
|
2026-01-29 15:21:54 -05:00
|
|
|
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-02-22 22:08:34 -05:00
|
|
|
f"/dashboard/{correct_list.id}/",
|
2026-01-29 15:21:54 -05:00
|
|
|
data={"text": "A new item for an existing list"},
|
2026-01-19 18:48:21 -05:00
|
|
|
)
|
|
|
|
|
|
2026-02-22 22:08:34 -05:00
|
|
|
self.assertRedirects(response, f"/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()
|
2026-02-22 22:08:34 -05:00
|
|
|
return self.client.post(f"/dashboard/{mylist.id}/", 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 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(
|
2026-02-22 22:08:34 -05:00
|
|
|
f"/dashboard/{list1.id}/",
|
2026-01-23 22:23:40 -05:00
|
|
|
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)
|
2026-02-07 20:15:27 -05:00
|
|
|
|
|
|
|
|
class MyListsTest(TestCase):
|
|
|
|
|
def test_my_lists_url_renders_my_lists_template(self):
|
|
|
|
|
user = User.objects.create(email="a@b.cde")
|
2026-02-17 20:26:42 -05:00
|
|
|
self.client.force_login(user)
|
2026-02-22 22:08:34 -05:00
|
|
|
response = self.client.get(f"/dashboard/users/{user.id}/")
|
2026-02-07 22:47:04 -05:00
|
|
|
self.assertTemplateUsed(response, "apps/dashboard/my_lists.html")
|
2026-02-08 22:18:41 -05:00
|
|
|
|
|
|
|
|
def test_passes_correct_owner_to_template(self):
|
2026-02-17 20:26:42 -05:00
|
|
|
User.objects.create(email="wrongowner@example.com")
|
2026-02-08 22:18:41 -05:00
|
|
|
correct_user = User.objects.create(email="a@b.cde")
|
2026-02-17 20:26:42 -05:00
|
|
|
self.client.force_login(correct_user)
|
2026-02-22 22:08:34 -05:00
|
|
|
response = self.client.get(f"/dashboard/users/{correct_user.id}/")
|
2026-02-08 22:18:41 -05:00
|
|
|
self.assertEqual(response.context["owner"], correct_user)
|
2026-02-08 22:23:43 -05:00
|
|
|
|
|
|
|
|
def test_list_owner_is_saved_if_user_is_authenticated(self):
|
|
|
|
|
user = User.objects.create(email="a@b.cde")
|
|
|
|
|
self.client.force_login(user)
|
2026-02-22 22:08:34 -05:00
|
|
|
self.client.post("/dashboard/new_list", data={"text": "new item"})
|
2026-02-08 22:23:43 -05:00
|
|
|
new_list = List.objects.get()
|
|
|
|
|
self.assertEqual(new_list.owner, user)
|
2026-02-17 20:26:42 -05:00
|
|
|
|
|
|
|
|
def test_my_lists_redirects_if_not_logged_in(self):
|
|
|
|
|
user = User.objects.create(email="a@b.cde")
|
2026-02-22 22:08:34 -05:00
|
|
|
response = self.client.get(f"/dashboard/users/{user.id}/")
|
2026-02-17 20:26:42 -05:00
|
|
|
self.assertRedirects(response, "/")
|
|
|
|
|
|
|
|
|
|
def test_my_lists_returns_403_for_wrong_user(self):
|
|
|
|
|
# create two users, login as user_a, request user_b's my_lists url
|
|
|
|
|
user1 = User.objects.create(email="a@b.cde")
|
|
|
|
|
user2 = User.objects.create(email="wrongowner@example.com")
|
|
|
|
|
self.client.force_login(user2)
|
2026-02-22 22:08:34 -05:00
|
|
|
response = self.client.get(f"/dashboard/users/{user1.id}/")
|
2026-02-17 20:26:42 -05:00
|
|
|
# assert 403
|
|
|
|
|
self.assertEqual(response.status_code, 403)
|
2026-02-18 13:53:05 -05:00
|
|
|
|
|
|
|
|
class ShareListTest(TestCase):
|
|
|
|
|
def test_post_to_share_list_url_redirects_to_list(self):
|
|
|
|
|
our_list = List.objects.create()
|
|
|
|
|
alice = User.objects.create(email="alice@example.com")
|
|
|
|
|
response = self.client.post(
|
2026-02-22 22:08:34 -05:00
|
|
|
f"/dashboard/{our_list.id}/share_list",
|
2026-02-18 13:53:05 -05:00
|
|
|
data={"recipient": "alice@example.com"},
|
|
|
|
|
)
|
2026-02-22 22:08:34 -05:00
|
|
|
self.assertRedirects(response, f"/dashboard/{our_list.id}/")
|
2026-02-18 13:53:05 -05:00
|
|
|
|
|
|
|
|
def test_post_with_email_adds_user_to_shared_with(self):
|
|
|
|
|
our_list = List.objects.create()
|
|
|
|
|
alice = User.objects.create(email="alice@example.com")
|
|
|
|
|
self.client.post(
|
2026-02-22 22:08:34 -05:00
|
|
|
f"/dashboard/{our_list.id}/share_list",
|
2026-02-18 13:53:05 -05:00
|
|
|
data={"recipient": "alice@example.com"},
|
|
|
|
|
)
|
|
|
|
|
self.assertIn(alice, our_list.shared_with.all())
|
2026-02-18 15:14:35 -05:00
|
|
|
|
|
|
|
|
def test_post_with_nonexistent_email_redirects_to_list(self):
|
|
|
|
|
our_list = List.objects.create()
|
|
|
|
|
response = self.client.post(
|
2026-02-22 22:08:34 -05:00
|
|
|
f"/dashboard/{our_list.id}/share_list",
|
2026-02-18 15:14:35 -05:00
|
|
|
data={"recipient": "nobody@example.com"},
|
|
|
|
|
)
|
2026-02-22 22:08:34 -05:00
|
|
|
self.assertRedirects(response, f"/dashboard/{our_list.id}/")
|
2026-02-22 21:18:22 -05:00
|
|
|
|
|
|
|
|
def test_share_list_does_not_add_owner_as_recipient(self):
|
|
|
|
|
owner = User.objects.create(email="owner@example.com")
|
|
|
|
|
our_list = List.objects.create(owner=owner)
|
|
|
|
|
self.client.force_login(owner)
|
|
|
|
|
self.client.post(reverse("share_list", args=[our_list.id]),
|
|
|
|
|
data={"recipient": "owner@example.com"})
|
|
|
|
|
self.assertNotIn(owner, our_list.shared_with.all())
|
2026-02-22 21:50:25 -05:00
|
|
|
|
|
|
|
|
class ViewAuthListTest(TestCase):
|
|
|
|
|
def setUp(self):
|
|
|
|
|
self.owner = User.objects.create(email="disco@example.com")
|
|
|
|
|
self.our_list = List.objects.create(owner=self.owner)
|
|
|
|
|
|
|
|
|
|
def test_anonymous_user_is_redirected(self):
|
|
|
|
|
response = self.client.get(reverse("view_list", args=[self.our_list.id]))
|
|
|
|
|
self.assertRedirects(response, "/")
|
|
|
|
|
|
|
|
|
|
def test_non_owner_non_shared_user_gets_403(self):
|
|
|
|
|
stranger = User.objects.create(email="stranger@example.com")
|
|
|
|
|
self.client.force_login(stranger)
|
|
|
|
|
response = self.client.get(reverse("view_list", args=[self.our_list.id]))
|
|
|
|
|
self.assertEqual(response.status_code, 403)
|
|
|
|
|
|
|
|
|
|
def test_shared_with_user_can_access_list(self):
|
|
|
|
|
guest = User.objects.create(email="guest@example.com")
|
|
|
|
|
self.our_list.shared_with.add(guest)
|
|
|
|
|
self.client.force_login(guest)
|
|
|
|
|
response = self.client.get(reverse("view_list", args=[self.our_list.id]))
|
|
|
|
|
self.assertEqual(response.status_code, 200)
|