2021-05-20 19:12:11 +00:00
|
|
|
""" test for app action functionality """
|
2021-08-03 19:02:47 +00:00
|
|
|
from unittest.mock import patch
|
2021-05-20 19:12:11 +00:00
|
|
|
from django.template.response import TemplateResponse
|
|
|
|
from django.test import TestCase
|
|
|
|
from django.test.client import RequestFactory
|
|
|
|
|
|
|
|
from bookwyrm import forms, models, views
|
2021-12-29 18:37:21 +00:00
|
|
|
from bookwyrm.tests.validate_html import validate_html
|
2021-05-20 19:12:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AnnouncementViews(TestCase):
|
|
|
|
"""every response to a get request, html or json"""
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
"""we need basic test data and mocks"""
|
|
|
|
self.factory = RequestFactory()
|
2021-09-06 21:48:45 +00:00
|
|
|
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
|
|
|
|
"bookwyrm.activitystreams.populate_stream_task.delay"
|
2021-12-10 03:14:10 +00:00
|
|
|
), patch("bookwyrm.lists_stream.populate_lists_task.delay"):
|
2021-08-03 17:25:53 +00:00
|
|
|
self.local_user = models.User.objects.create_user(
|
|
|
|
"mouse@local.com",
|
|
|
|
"mouse@mouse.mouse",
|
|
|
|
"password",
|
|
|
|
local=True,
|
|
|
|
localname="mouse",
|
|
|
|
)
|
2021-08-02 23:05:40 +00:00
|
|
|
|
|
|
|
models.SiteSettings.objects.create()
|
2021-05-20 19:12:11 +00:00
|
|
|
|
|
|
|
def test_announcements_page(self):
|
|
|
|
"""there are so many views, this just makes sure it LOADS"""
|
|
|
|
models.Announcement.objects.create(preview="hi", user=self.local_user)
|
|
|
|
|
|
|
|
view = views.Announcements.as_view()
|
|
|
|
request = self.factory.get("")
|
|
|
|
request.user = self.local_user
|
|
|
|
request.user.is_superuser = True
|
|
|
|
|
|
|
|
result = view(request)
|
|
|
|
|
|
|
|
self.assertIsInstance(result, TemplateResponse)
|
2021-12-29 18:37:21 +00:00
|
|
|
validate_html(result.render())
|
2021-05-20 19:12:11 +00:00
|
|
|
self.assertEqual(result.status_code, 200)
|
|
|
|
|
|
|
|
def test_announcements_page_empty(self):
|
|
|
|
"""there are so many views, this just makes sure it LOADS"""
|
|
|
|
view = views.Announcements.as_view()
|
|
|
|
request = self.factory.get("")
|
|
|
|
request.user = self.local_user
|
|
|
|
request.user.is_superuser = True
|
|
|
|
|
|
|
|
result = view(request)
|
|
|
|
|
|
|
|
self.assertIsInstance(result, TemplateResponse)
|
2021-12-29 18:37:21 +00:00
|
|
|
validate_html(result.render())
|
2021-05-20 19:12:11 +00:00
|
|
|
self.assertEqual(result.status_code, 200)
|
2021-05-20 19:14:57 +00:00
|
|
|
|
|
|
|
def test_announcement_page(self):
|
|
|
|
"""there are so many views, this just makes sure it LOADS"""
|
2021-05-20 19:36:42 +00:00
|
|
|
announcement = models.Announcement.objects.create(
|
|
|
|
preview="hi", user=self.local_user
|
|
|
|
)
|
2021-05-20 19:14:57 +00:00
|
|
|
|
2021-05-20 19:36:42 +00:00
|
|
|
view = views.Announcement.as_view()
|
2021-05-20 19:14:57 +00:00
|
|
|
request = self.factory.get("")
|
|
|
|
request.user = self.local_user
|
|
|
|
request.user.is_superuser = True
|
|
|
|
|
|
|
|
result = view(request, announcement.id)
|
|
|
|
|
|
|
|
self.assertIsInstance(result, TemplateResponse)
|
2021-12-29 18:37:21 +00:00
|
|
|
validate_html(result.render())
|
2021-05-20 19:14:57 +00:00
|
|
|
self.assertEqual(result.status_code, 200)
|
2021-05-20 19:36:42 +00:00
|
|
|
|
|
|
|
def test_create_announcement(self):
|
|
|
|
"""create a new announcement"""
|
2022-02-15 18:32:04 +00:00
|
|
|
view = views.EditAnnouncement.as_view()
|
2021-05-20 19:36:42 +00:00
|
|
|
form = forms.AnnouncementForm()
|
|
|
|
form.data["preview"] = "hi hi"
|
|
|
|
form.data["start_date"] = "2021-05-20"
|
|
|
|
form.data["user"] = self.local_user.id
|
2022-02-15 18:32:04 +00:00
|
|
|
form.data["display_type"] = "warning-light"
|
2021-05-20 19:36:42 +00:00
|
|
|
|
|
|
|
request = self.factory.post("", form.data)
|
|
|
|
request.user = self.local_user
|
|
|
|
request.user.is_superuser = True
|
|
|
|
|
|
|
|
view(request)
|
|
|
|
|
|
|
|
announcement = models.Announcement.objects.get()
|
|
|
|
self.assertEqual(announcement.preview, "hi hi")
|
|
|
|
self.assertEqual(announcement.start_date.year, 2021)
|
|
|
|
self.assertEqual(announcement.start_date.month, 5)
|
|
|
|
self.assertEqual(announcement.start_date.day, 20)
|
|
|
|
|
|
|
|
def test_edit_announcement(self):
|
|
|
|
"""edit an announcement"""
|
|
|
|
announcement = models.Announcement.objects.create(
|
|
|
|
preview="hi", user=self.local_user
|
|
|
|
)
|
2022-02-15 18:32:04 +00:00
|
|
|
view = views.EditAnnouncement.as_view()
|
2021-05-20 19:36:42 +00:00
|
|
|
form = forms.AnnouncementForm(instance=announcement)
|
|
|
|
form.data["preview"] = "hi hi"
|
|
|
|
form.data["start_date"] = "2021-05-20"
|
|
|
|
form.data["user"] = self.local_user.id
|
2022-02-15 18:32:04 +00:00
|
|
|
form.data["display_type"] = "warning-light"
|
2021-05-20 19:36:42 +00:00
|
|
|
|
|
|
|
request = self.factory.post("", form.data)
|
|
|
|
request.user = self.local_user
|
|
|
|
request.user.is_superuser = True
|
|
|
|
|
|
|
|
view(request, announcement.id)
|
|
|
|
|
|
|
|
announcement.refresh_from_db()
|
|
|
|
self.assertEqual(announcement.preview, "hi hi")
|
|
|
|
self.assertEqual(announcement.start_date.year, 2021)
|
|
|
|
self.assertEqual(announcement.start_date.month, 5)
|
|
|
|
self.assertEqual(announcement.start_date.day, 20)
|
|
|
|
|
|
|
|
def test_delete_announcement(self):
|
|
|
|
"""delete an announcement"""
|
|
|
|
announcement = models.Announcement.objects.create(
|
|
|
|
preview="hi", user=self.local_user
|
|
|
|
)
|
2021-05-20 20:42:30 +00:00
|
|
|
view = views.delete_announcement
|
2021-05-20 19:36:42 +00:00
|
|
|
|
|
|
|
request = self.factory.post("")
|
|
|
|
request.user = self.local_user
|
|
|
|
request.user.is_superuser = True
|
|
|
|
|
|
|
|
view(request, announcement.id)
|
|
|
|
|
|
|
|
self.assertFalse(models.Announcement.objects.exists())
|
|
|
|
|
|
|
|
def test_view_announcement(self):
|
|
|
|
"""display announcement on other pages"""
|
|
|
|
view = views.User.as_view()
|
|
|
|
request = self.factory.get("")
|
2021-05-20 20:42:30 +00:00
|
|
|
request.user = self.local_user
|
2021-05-20 19:36:42 +00:00
|
|
|
|
2021-05-20 20:42:30 +00:00
|
|
|
result = view(request, self.local_user.localname)
|
2021-05-20 19:36:42 +00:00
|
|
|
|
|
|
|
self.assertIsInstance(result, TemplateResponse)
|
2021-12-29 18:37:21 +00:00
|
|
|
validate_html(result.render())
|
2021-05-20 19:36:42 +00:00
|
|
|
self.assertEqual(result.status_code, 200)
|