2022-05-14 07:46:51 +00:00
|
|
|
from django.contrib.auth.models import User
|
|
|
|
from django.test import TestCase
|
|
|
|
from django.urls import reverse
|
|
|
|
|
|
|
|
from bookmarks.models import Toast
|
2024-01-27 10:29:16 +00:00
|
|
|
from bookmarks.tests.helpers import (
|
|
|
|
BookmarkFactoryMixin,
|
|
|
|
random_sentence,
|
|
|
|
disable_logging,
|
|
|
|
)
|
2022-05-14 07:46:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ToastsViewTestCase(TestCase, BookmarkFactoryMixin):
|
|
|
|
|
|
|
|
def setUp(self) -> None:
|
|
|
|
user = self.get_or_create_test_user()
|
|
|
|
self.client.force_login(user)
|
|
|
|
|
2024-01-27 10:29:16 +00:00
|
|
|
def create_toast(
|
|
|
|
self, user: User = None, message: str = None, acknowledged: bool = False
|
|
|
|
):
|
2022-05-14 07:46:51 +00:00
|
|
|
if not user:
|
|
|
|
user = self.user
|
|
|
|
if not message:
|
|
|
|
message = random_sentence()
|
|
|
|
|
2024-01-27 10:29:16 +00:00
|
|
|
toast = Toast(
|
|
|
|
owner=user, key="test", message=message, acknowledged=acknowledged
|
|
|
|
)
|
2022-05-14 07:46:51 +00:00
|
|
|
toast.save()
|
|
|
|
return toast
|
|
|
|
|
|
|
|
def test_should_render_unacknowledged_toasts(self):
|
|
|
|
self.create_toast()
|
|
|
|
self.create_toast()
|
|
|
|
self.create_toast(acknowledged=True)
|
|
|
|
|
2024-01-27 10:29:16 +00:00
|
|
|
response = self.client.get(reverse("bookmarks:index"))
|
2022-05-14 07:46:51 +00:00
|
|
|
|
|
|
|
# Should render toasts container
|
2023-08-24 12:46:47 +00:00
|
|
|
self.assertContains(response, '<div class="toasts">')
|
2022-05-14 07:46:51 +00:00
|
|
|
# Should render two toasts
|
2024-03-29 22:07:11 +00:00
|
|
|
self.assertContains(response, '<div class="toast d-flex">', count=2)
|
2022-05-14 07:46:51 +00:00
|
|
|
|
|
|
|
def test_should_not_render_acknowledged_toasts(self):
|
|
|
|
self.create_toast(acknowledged=True)
|
|
|
|
self.create_toast(acknowledged=True)
|
|
|
|
self.create_toast(acknowledged=True)
|
|
|
|
|
2024-01-27 10:29:16 +00:00
|
|
|
response = self.client.get(reverse("bookmarks:index"))
|
2022-05-14 07:46:51 +00:00
|
|
|
|
|
|
|
# Should not render toasts container
|
|
|
|
self.assertContains(response, '<div class="toasts container grid-lg">', count=0)
|
|
|
|
# Should not render toasts
|
|
|
|
self.assertContains(response, '<div class="toast">', count=0)
|
|
|
|
|
|
|
|
def test_should_not_render_toasts_of_other_users(self):
|
2024-01-27 10:29:16 +00:00
|
|
|
other_user = User.objects.create_user(
|
|
|
|
"otheruser", "otheruser@example.com", "password123"
|
|
|
|
)
|
2022-05-14 07:46:51 +00:00
|
|
|
|
|
|
|
self.create_toast(user=other_user)
|
|
|
|
self.create_toast(user=other_user)
|
|
|
|
self.create_toast(user=other_user)
|
|
|
|
|
2024-01-27 10:29:16 +00:00
|
|
|
response = self.client.get(reverse("bookmarks:index"))
|
2022-05-14 07:46:51 +00:00
|
|
|
|
|
|
|
# Should not render toasts container
|
|
|
|
self.assertContains(response, '<div class="toasts container grid-lg">', count=0)
|
|
|
|
# Should not render toasts
|
|
|
|
self.assertContains(response, '<div class="toast">', count=0)
|
|
|
|
|
2022-05-20 14:51:50 +00:00
|
|
|
def test_form_tag(self):
|
|
|
|
self.create_toast()
|
|
|
|
expected_form_tag = f'<form action="{reverse("bookmarks:toasts.acknowledge")}?return_url={reverse("bookmarks:index")}" method="post">'
|
|
|
|
|
2024-01-27 10:29:16 +00:00
|
|
|
response = self.client.get(reverse("bookmarks:index"))
|
2022-05-20 14:51:50 +00:00
|
|
|
|
|
|
|
self.assertContains(response, expected_form_tag)
|
|
|
|
|
2022-05-14 07:46:51 +00:00
|
|
|
def test_toast_content(self):
|
|
|
|
toast = self.create_toast()
|
2024-01-27 10:29:16 +00:00
|
|
|
expected_toast = f"""
|
2024-03-29 22:07:11 +00:00
|
|
|
<div class="toast d-flex">
|
2022-05-14 07:46:51 +00:00
|
|
|
{toast.message}
|
2024-03-29 22:07:11 +00:00
|
|
|
<button type="submit" name="toast" value="{toast.id}" class="btn btn-clear"></button>
|
2022-05-14 07:46:51 +00:00
|
|
|
</div>
|
2024-01-27 10:29:16 +00:00
|
|
|
"""
|
2022-05-14 07:46:51 +00:00
|
|
|
|
2024-01-27 10:29:16 +00:00
|
|
|
response = self.client.get(reverse("bookmarks:index"))
|
2022-05-14 07:46:51 +00:00
|
|
|
html = response.content.decode()
|
|
|
|
|
|
|
|
self.assertInHTML(expected_toast, html)
|
|
|
|
|
|
|
|
def test_acknowledge_toast(self):
|
|
|
|
toast = self.create_toast()
|
|
|
|
|
2024-01-27 10:29:16 +00:00
|
|
|
self.client.post(
|
|
|
|
reverse("bookmarks:toasts.acknowledge"),
|
|
|
|
{
|
|
|
|
"toast": [toast.id],
|
|
|
|
},
|
|
|
|
)
|
2022-05-14 07:46:51 +00:00
|
|
|
|
|
|
|
toast.refresh_from_db()
|
|
|
|
self.assertTrue(toast.acknowledged)
|
|
|
|
|
|
|
|
def test_acknowledge_toast_should_redirect_to_return_url(self):
|
|
|
|
toast = self.create_toast()
|
2024-01-27 10:29:16 +00:00
|
|
|
return_url = reverse("bookmarks:settings.general")
|
|
|
|
acknowledge_url = reverse("bookmarks:toasts.acknowledge")
|
|
|
|
acknowledge_url = acknowledge_url + "?return_url=" + return_url
|
2022-05-14 07:46:51 +00:00
|
|
|
|
2024-01-27 10:29:16 +00:00
|
|
|
response = self.client.post(
|
|
|
|
acknowledge_url,
|
|
|
|
{
|
|
|
|
"toast": [toast.id],
|
|
|
|
},
|
|
|
|
)
|
2022-05-14 07:46:51 +00:00
|
|
|
|
|
|
|
self.assertRedirects(response, return_url)
|
|
|
|
|
|
|
|
def test_acknowledge_toast_should_redirect_to_index_by_default(self):
|
|
|
|
toast = self.create_toast()
|
|
|
|
|
2024-01-27 10:29:16 +00:00
|
|
|
response = self.client.post(
|
|
|
|
reverse("bookmarks:toasts.acknowledge"),
|
|
|
|
{
|
|
|
|
"toast": [toast.id],
|
|
|
|
},
|
|
|
|
)
|
2022-05-14 07:46:51 +00:00
|
|
|
|
2024-01-27 10:29:16 +00:00
|
|
|
self.assertRedirects(response, reverse("bookmarks:index"))
|
2022-05-14 07:46:51 +00:00
|
|
|
|
|
|
|
@disable_logging
|
|
|
|
def test_acknowledge_toast_should_not_acknowledge_other_users_toast(self):
|
2024-01-27 10:29:16 +00:00
|
|
|
other_user = User.objects.create_user(
|
|
|
|
"otheruser", "otheruser@example.com", "password123"
|
|
|
|
)
|
2022-05-14 07:46:51 +00:00
|
|
|
toast = self.create_toast(user=other_user)
|
|
|
|
|
2024-01-27 10:29:16 +00:00
|
|
|
response = self.client.post(
|
|
|
|
reverse("bookmarks:toasts.acknowledge"),
|
|
|
|
{
|
|
|
|
"toast": [toast.id],
|
|
|
|
},
|
|
|
|
)
|
2022-05-14 07:46:51 +00:00
|
|
|
self.assertEqual(response.status_code, 404)
|