Updates views tests

This commit is contained in:
Mouse Reeve 2021-12-09 15:03:01 -08:00
parent 93dbe2daa0
commit 2d63bfb791
24 changed files with 92 additions and 52 deletions

View file

@ -21,7 +21,7 @@ class AuthorViews(TestCase):
self.factory = RequestFactory()
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
"bookwyrm.activitystreams.populate_stream_task.delay"
):
), patch("bookwyrm.lists_stream.populate_lists_task.delay"):
self.local_user = models.User.objects.create_user(
"mouse@local.com",
"mouse@mouse.com",

View file

@ -18,7 +18,7 @@ class DirectoryViews(TestCase):
self.factory = RequestFactory()
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
"bookwyrm.activitystreams.populate_stream_task.delay"
):
), patch("bookwyrm.lists_stream.populate_lists_task.delay"):
self.local_user = models.User.objects.create_user(
"mouse@local.com",
"mouse@mouse.com",
@ -34,6 +34,7 @@ class DirectoryViews(TestCase):
@patch("bookwyrm.suggested_users.rerank_suggestions_task.delay")
@patch("bookwyrm.activitystreams.populate_stream_task.delay")
@patch("bookwyrm.lists_stream.populate_lists_task.delay")
@patch("bookwyrm.suggested_users.rerank_user_task.delay")
def test_directory_page(self, *_):
"""there are so many views, this just makes sure it LOADS"""

View file

@ -6,6 +6,7 @@ from django.test.client import RequestFactory
from bookwyrm import models
from bookwyrm import views
from bookwyrm.tests.validate_html import validate_html
class DiscoverViews(TestCase):
@ -16,7 +17,7 @@ class DiscoverViews(TestCase):
self.factory = RequestFactory()
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
"bookwyrm.activitystreams.populate_stream_task.delay"
):
), patch("bookwyrm.lists_stream.populate_lists_task.delay"):
self.local_user = models.User.objects.create_user(
"mouse@local.com",
"mouse@mouse.mouse",
@ -39,7 +40,7 @@ class DiscoverViews(TestCase):
result = view(request)
self.assertEqual(mock.call_count, 1)
self.assertEqual(result.status_code, 200)
result.render()
validate_html(result.render())
@patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async")
@patch("bookwyrm.activitystreams.add_status_task.delay")
@ -67,7 +68,7 @@ class DiscoverViews(TestCase):
result = view(request)
self.assertEqual(mock.call_count, 1)
self.assertEqual(result.status_code, 200)
result.render()
validate_html(result.render())
def test_discover_page_logged_out(self):
"""there are so many views, this just makes sure it LOADS"""

View file

@ -10,8 +10,7 @@ from django.template.response import TemplateResponse
from django.test import TestCase
from django.test.client import RequestFactory
from bookwyrm import models
from bookwyrm import views
from bookwyrm import forms, models, views
from bookwyrm.activitypub import ActivitypubResponse
@ -19,6 +18,7 @@ from bookwyrm.activitypub import ActivitypubResponse
@patch("bookwyrm.activitystreams.add_status_task.delay")
@patch("bookwyrm.suggested_users.rerank_suggestions_task.delay")
@patch("bookwyrm.activitystreams.populate_stream_task.delay")
@patch("bookwyrm.lists_stream.populate_lists_task.delay")
@patch("bookwyrm.suggested_users.remove_user_task.delay")
class FeedViews(TestCase):
"""activity feed, statuses, dms"""
@ -28,7 +28,7 @@ class FeedViews(TestCase):
self.factory = RequestFactory()
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
"bookwyrm.activitystreams.populate_stream_task.delay"
):
), patch("bookwyrm.lists_stream.populate_lists_task.delay"):
self.local_user = models.User.objects.create_user(
"mouse@local.com",
"mouse@mouse.mouse",
@ -54,6 +54,25 @@ class FeedViews(TestCase):
result.render()
self.assertEqual(result.status_code, 200)
@patch("bookwyrm.suggested_users.SuggestedUsers.get_suggestions")
def test_save_feed_settings(self, *_):
"""update display preferences"""
self.assertEqual(
self.local_user.feed_status_types,
["review", "comment", "quotation", "everything"],
)
view = views.Feed.as_view()
form = forms.FeedStatusTypesForm(instance=self.local_user)
form.data["feed_status_types"] = "review"
request = self.factory.post("", form.data)
request.user = self.local_user
result = view(request, "home")
self.assertEqual(result.status_code, 200)
self.local_user.refresh_from_db()
self.assertEqual(self.local_user.feed_status_types, ["review"])
def test_status_page(self, *_):
"""there are so many views, this just makes sure it LOADS"""
view = views.Status.as_view()

View file

@ -13,6 +13,7 @@ from bookwyrm.tests.validate_html import validate_html
@patch("bookwyrm.activitystreams.add_user_statuses_task.delay")
@patch("bookwyrm.lists_stream.add_user_lists_task.delay")
class FollowViews(TestCase):
"""follows"""
@ -22,7 +23,7 @@ class FollowViews(TestCase):
self.factory = RequestFactory()
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
"bookwyrm.activitystreams.populate_stream_task.delay"
):
), patch("bookwyrm.lists_stream.populate_lists_task.delay"):
self.local_user = models.User.objects.create_user(
"mouse@local.com",
"mouse@mouse.com",
@ -56,7 +57,7 @@ class FollowViews(TestCase):
parent_work=self.work,
)
def test_handle_follow_remote(self, _):
def test_handle_follow_remote(self, *_):
"""send a follow request"""
request = self.factory.post("", {"user": self.remote_user.username})
request.user = self.local_user
@ -71,11 +72,11 @@ class FollowViews(TestCase):
self.assertEqual(rel.user_object, self.remote_user)
self.assertEqual(rel.status, "follow_request")
def test_handle_follow_local_manually_approves(self, _):
def test_handle_follow_local_manually_approves(self, *_):
"""send a follow request"""
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
"bookwyrm.activitystreams.populate_stream_task.delay"
):
), patch("bookwyrm.lists_stream.populate_lists_task.delay"):
rat = models.User.objects.create_user(
"rat@local.com",
"rat@rat.com",
@ -97,11 +98,11 @@ class FollowViews(TestCase):
self.assertEqual(rel.user_object, rat)
self.assertEqual(rel.status, "follow_request")
def test_handle_follow_local(self, _):
def test_handle_follow_local(self, *_):
"""send a follow request"""
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
"bookwyrm.activitystreams.populate_stream_task.delay"
):
), patch("bookwyrm.lists_stream.populate_lists_task.delay"):
rat = models.User.objects.create_user(
"rat@local.com",
"rat@rat.com",
@ -124,6 +125,7 @@ class FollowViews(TestCase):
self.assertEqual(rel.status, "follows")
@patch("bookwyrm.activitystreams.remove_user_statuses_task.delay")
@patch("bookwyrm.lists_stream.remove_user_lists_task.delay")
def test_handle_unfollow(self, *_):
"""send an unfollow"""
request = self.factory.post("", {"user": self.remote_user.username})
@ -140,7 +142,7 @@ class FollowViews(TestCase):
self.assertEqual(self.remote_user.followers.count(), 0)
def test_handle_accept(self, _):
def test_handle_accept(self, *_):
"""accept a follow request"""
self.local_user.manually_approves_followers = True
self.local_user.save(
@ -159,7 +161,7 @@ class FollowViews(TestCase):
# follow relationship should exist
self.assertEqual(self.local_user.followers.first(), self.remote_user)
def test_handle_reject(self, _):
def test_handle_reject(self, *_):
"""reject a follow request"""
self.local_user.manually_approves_followers = True
self.local_user.save(
@ -178,7 +180,7 @@ class FollowViews(TestCase):
# follow relationship should not exist
self.assertEqual(models.UserFollows.objects.filter(id=rel.id).count(), 0)
def test_ostatus_follow_request(self, _):
def test_ostatus_follow_request(self, *_):
"""check ostatus subscribe template loads"""
request = self.factory.get(
"", {"acct": "https%3A%2F%2Fexample.com%2Fusers%2Frat"}
@ -189,7 +191,7 @@ class FollowViews(TestCase):
validate_html(result.render())
self.assertEqual(result.status_code, 200)
def test_remote_follow_page(self, _):
def test_remote_follow_page(self, *_):
"""check remote follow page loads"""
request = self.factory.get("", {"acct": "mouse@local.com"})
request.user = self.remote_user
@ -198,7 +200,7 @@ class FollowViews(TestCase):
validate_html(result.render())
self.assertEqual(result.status_code, 200)
def test_ostatus_follow_success(self, _):
def test_ostatus_follow_success(self, *_):
"""check remote follow success page loads"""
request = self.factory.get("")
request.user = self.remote_user
@ -208,7 +210,7 @@ class FollowViews(TestCase):
validate_html(result.render())
self.assertEqual(result.status_code, 200)
def test_remote_follow(self, _):
def test_remote_follow(self, *_):
"""check follow from remote page loads"""
request = self.factory.post("", {"user": self.remote_user.id})
request.user = self.remote_user

View file

@ -16,7 +16,7 @@ class GetStartedViews(TestCase):
self.factory = RequestFactory()
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
"bookwyrm.activitystreams.populate_stream_task.delay"
):
), patch("bookwyrm.lists_stream.populate_lists_task.delay"):
self.local_user = models.User.objects.create_user(
"mouse@local.com",
"mouse@mouse.mouse",

View file

@ -20,7 +20,7 @@ class GoalViews(TestCase):
self.factory = RequestFactory()
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
"bookwyrm.activitystreams.populate_stream_task.delay"
):
), patch("bookwyrm.lists_stream.populate_lists_task.delay"):
self.local_user = models.User.objects.create_user(
"mouse@local.com",
"mouse@mouse.com",

View file

@ -19,7 +19,7 @@ class GroupViews(TestCase):
self.factory = RequestFactory()
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
"bookwyrm.activitystreams.populate_stream_task.delay"
):
), patch("bookwyrm.lists_stream.populate_lists_task.delay"):
self.local_user = models.User.objects.create_user(
"mouse@local.com",
"mouse@mouse.mouse",

View file

@ -23,7 +23,7 @@ class ViewsHelpers(TestCase):
self.factory = RequestFactory()
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
"bookwyrm.activitystreams.populate_stream_task.delay"
):
), patch("bookwyrm.lists_stream.populate_lists_task.delay"):
with patch("bookwyrm.suggested_users.rerank_user_task.delay"):
self.local_user = models.User.objects.create_user(
"mouse@local.com",

View file

@ -17,7 +17,7 @@ class InteractionViews(TestCase):
self.factory = RequestFactory()
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
"bookwyrm.activitystreams.populate_stream_task.delay"
):
), patch("bookwyrm.lists_stream.populate_lists_task.delay"):
self.local_user = models.User.objects.create_user(
"mouse@local.com",
"mouse@mouse.com",

View file

@ -18,7 +18,7 @@ class IsbnViews(TestCase):
self.factory = RequestFactory()
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
"bookwyrm.activitystreams.populate_stream_task.delay"
):
), patch("bookwyrm.lists_stream.populate_lists_task.delay"):
self.local_user = models.User.objects.create_user(
"mouse@local.com",
"mouse@mouse.com",
@ -49,3 +49,13 @@ class IsbnViews(TestCase):
self.assertEqual(len(data), 1)
self.assertEqual(data[0]["title"], "Test Book")
self.assertEqual(data[0]["key"], f"https://{DOMAIN}/book/{self.book.id}")
def test_isbn_html_response(self):
"""searches local data only and returns book data in json format"""
view = views.Isbn.as_view()
request = self.factory.get("")
with patch("bookwyrm.views.isbn.is_api_request") as is_api:
is_api.return_value = False
response = view(request, isbn="1234567890123")
self.assertEqual(response.status_code, 200)
response.render()

View file

@ -20,7 +20,7 @@ class ListViews(TestCase):
self.factory = RequestFactory()
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
"bookwyrm.activitystreams.populate_stream_task.delay"
):
), patch("bookwyrm.lists_stream.populate_lists_task.delay"):
self.local_user = models.User.objects.create_user(
"mouse@local.com",
"mouse@mouse.com",
@ -71,10 +71,13 @@ class ListViews(TestCase):
models.SiteSettings.objects.create()
def test_lists_page(self):
@patch("bookwyrm.lists_stream.ListsStream.get_activity_stream")
def test_lists_page(self, _):
"""there are so many views, this just makes sure it LOADS"""
view = views.Lists.as_view()
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"):
with patch(
"bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"
), patch("bookwyrm.lists_stream.add_list_task.delay"):
models.List.objects.create(name="Public list", user=self.local_user)
models.List.objects.create(
name="Private list", privacy="direct", user=self.local_user
@ -345,13 +348,16 @@ class ListViews(TestCase):
def test_curate_page(self):
"""there are so many views, this just makes sure it LOADS"""
view = views.Curate.as_view()
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"):
models.List.objects.create(name="Public list", user=self.local_user)
models.List.objects.create(
name="Private list", privacy="direct", user=self.local_user
)
request = self.factory.get("")
request.user = self.local_user
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"):
models.ListItem.objects.create(
book_list=self.list,
user=self.local_user,
book=self.book,
approved=False,
order=1,
)
result = view(request, self.list.id)
self.assertIsInstance(result, TemplateResponse)
@ -404,7 +410,7 @@ class ListViews(TestCase):
with patch("bookwyrm.views.list.is_api_request") as is_api:
is_api.return_value = False
with self.assertRaises(Http404):
result = view(request, self.list.id, "")
view(request, self.list.id, "")
def test_embed_call_with_key(self):
"""there are so many views, this just makes sure it LOADS"""

View file

@ -18,7 +18,7 @@ class ListActionViews(TestCase):
self.factory = RequestFactory()
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
"bookwyrm.activitystreams.populate_stream_task.delay"
):
), patch("bookwyrm.lists_stream.populate_lists_task.delay"):
self.local_user = models.User.objects.create_user(
"mouse@local.com",
"mouse@mouse.com",
@ -90,8 +90,9 @@ class ListActionViews(TestCase):
request.user = self.local_user
with patch(
"bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"
) as mock:
) as mock, patch("bookwyrm.lists_stream.remove_list_task.delay") as redis_mock:
views.delete_list(request, self.list.id)
self.assertTrue(redis_mock.called)
activity = json.loads(mock.call_args[1]["args"][1])
self.assertEqual(activity["type"], "Delete")
self.assertEqual(activity["actor"], self.local_user.remote_id)
@ -123,10 +124,7 @@ class ListActionViews(TestCase):
request = self.factory.post(
"",
{
"item": pending.id,
"approved": "true",
},
{"item": pending.id, "approved": "true"},
)
request.user = self.local_user

View file

@ -17,7 +17,7 @@ class NotificationViews(TestCase):
self.factory = RequestFactory()
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
"bookwyrm.activitystreams.populate_stream_task.delay"
):
), patch("bookwyrm.lists_stream.populate_lists_task.delay"):
self.local_user = models.User.objects.create_user(
"mouse@local.com",
"mouse@mouse.mouse",

View file

@ -20,7 +20,7 @@ class OutboxView(TestCase):
self.factory = RequestFactory()
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
"bookwyrm.activitystreams.populate_stream_task.delay"
):
), patch("bookwyrm.lists_stream.populate_lists_task.delay"):
self.local_user = models.User.objects.create_user(
"mouse@local.com",
"mouse@mouse.com",

View file

@ -20,7 +20,7 @@ class ReadingViews(TestCase):
self.factory = RequestFactory()
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
"bookwyrm.activitystreams.populate_stream_task.delay"
):
), patch("bookwyrm.lists_stream.populate_lists_task.delay"):
self.local_user = models.User.objects.create_user(
"mouse@local.com",
"mouse@mouse.com",

View file

@ -27,7 +27,7 @@ class ReadThrough(TestCase):
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
"bookwyrm.activitystreams.populate_stream_task.delay"
):
), patch("bookwyrm.lists_stream.populate_lists_task.delay"):
self.user = models.User.objects.create_user(
"cinco", "cinco@example.com", "seissiete", local=True, localname="cinco"
)

View file

@ -15,7 +15,7 @@ class RssFeedView(TestCase):
def setUp(self):
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
"bookwyrm.activitystreams.populate_stream_task.delay"
):
), patch("bookwyrm.lists_stream.populate_lists_task.delay"):
self.local_user = models.User.objects.create_user(
"rss_user", "rss@test.rss", "password", local=True
)

View file

@ -22,7 +22,7 @@ class Views(TestCase):
self.factory = RequestFactory()
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
"bookwyrm.activitystreams.populate_stream_task.delay"
):
), patch("bookwyrm.lists_stream.populate_lists_task.delay"):
self.local_user = models.User.objects.create_user(
"mouse@local.com",
"mouse@mouse.com",

View file

@ -13,6 +13,7 @@ from bookwyrm.tests.validate_html import validate_html
# pylint: disable=invalid-name
@patch("bookwyrm.suggested_users.rerank_suggestions_task.delay")
@patch("bookwyrm.activitystreams.populate_stream_task.delay")
@patch("bookwyrm.lists_stream.populate_lists_task.delay")
@patch("bookwyrm.activitystreams.remove_status_task.delay")
@patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async")
class StatusViews(TestCase):
@ -23,7 +24,7 @@ class StatusViews(TestCase):
self.factory = RequestFactory()
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
"bookwyrm.activitystreams.populate_stream_task.delay"
):
), patch("bookwyrm.lists_stream.populate_lists_task.delay"):
self.local_user = models.User.objects.create_user(
"mouse@local.com",
"mouse@mouse.com",

View file

@ -17,7 +17,7 @@ class UpdateViews(TestCase):
self.factory = RequestFactory()
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
"bookwyrm.activitystreams.populate_stream_task.delay"
):
), patch("bookwyrm.lists_stream.populate_lists_task.delay"):
self.local_user = models.User.objects.create_user(
"mouse@local.com",
"mouse@mouse.mouse",

View file

@ -20,7 +20,7 @@ class UserViews(TestCase):
self.factory = RequestFactory()
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
"bookwyrm.activitystreams.populate_stream_task.delay"
):
), patch("bookwyrm.lists_stream.populate_lists_task.delay"):
self.local_user = models.User.objects.create_user(
"mouse@local.com",
"mouse@mouse.mouse",

View file

@ -18,7 +18,7 @@ class WellknownViews(TestCase):
self.factory = RequestFactory()
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
"bookwyrm.activitystreams.populate_stream_task.delay"
):
), patch("bookwyrm.lists_stream.populate_lists_task.delay"):
self.local_user = models.User.objects.create_user(
"mouse@local.com",
"mouse@mouse.mouse",

View file

@ -28,7 +28,9 @@ class Feed(View):
settings_saved = False
form = forms.FeedStatusTypesForm(request.POST, instance=request.user)
if form.is_valid():
form.save()
# workaround to avoid broadcasting this change
user = form.save(commit=False)
user.save(broadcast=False, update_fields=["feed_status_types"])
settings_saved = True
return self.get(request, tab, settings_saved)