from django.contrib.auth.models import User
from django.test import TestCase
from django.urls import reverse
from bookmarks.models import Toast
from bookmarks.tests.helpers import (
BookmarkFactoryMixin,
random_sentence,
disable_logging,
)
class ToastsViewTestCase(TestCase, BookmarkFactoryMixin):
def setUp(self) -> None:
user = self.get_or_create_test_user()
self.client.force_login(user)
def create_toast(
self, user: User = None, message: str = None, acknowledged: bool = False
):
if not user:
user = self.user
if not message:
message = random_sentence()
toast = Toast(
owner=user, key="test", message=message, acknowledged=acknowledged
)
toast.save()
return toast
def test_should_render_unacknowledged_toasts(self):
self.create_toast()
self.create_toast()
self.create_toast(acknowledged=True)
response = self.client.get(reverse("bookmarks:index"))
# Should render toasts container
self.assertContains(response, '
')
# Should render two toasts
self.assertContains(response, '
', count=2)
def test_should_not_render_acknowledged_toasts(self):
self.create_toast(acknowledged=True)
self.create_toast(acknowledged=True)
self.create_toast(acknowledged=True)
response = self.client.get(reverse("bookmarks:index"))
# Should not render toasts container
self.assertContains(response, '
', count=0)
# Should not render toasts
self.assertContains(response, '
', count=0)
def test_should_not_render_toasts_of_other_users(self):
other_user = User.objects.create_user(
"otheruser", "otheruser@example.com", "password123"
)
self.create_toast(user=other_user)
self.create_toast(user=other_user)
self.create_toast(user=other_user)
response = self.client.get(reverse("bookmarks:index"))
# Should not render toasts container
self.assertContains(response, '
', count=0)
# Should not render toasts
self.assertContains(response, '
', count=0)
def test_form_tag(self):
self.create_toast()
expected_form_tag = f'