moviewyrm/bookwyrm/tests/views/admin/test_announcements.py

144 lines
5 KiB
Python
Raw Normal View History

""" test for app action functionality """
2021-08-03 19:02:47 +00:00
from unittest.mock import patch
from django.template.response import TemplateResponse
from django.test import TestCase
from django.test.client import RequestFactory
from bookwyrm import forms, models, views
from bookwyrm.tests.validate_html import validate_html
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()
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()
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)
validate_html(result.render())
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)
validate_html(result.render())
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)
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"""
view = views.Announcements.as_view()
form = forms.AnnouncementForm()
form.data["preview"] = "hi hi"
form.data["start_date"] = "2021-05-20"
form.data["user"] = self.local_user.id
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
)
view = views.Announcement.as_view()
form = forms.AnnouncementForm(instance=announcement)
form.data["preview"] = "hi hi"
form.data["start_date"] = "2021-05-20"
form.data["user"] = self.local_user.id
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)
validate_html(result.render())
2021-05-20 19:36:42 +00:00
self.assertEqual(result.status_code, 200)