moviewyrm/bookwyrm/tests/views/test_wellknown.py

117 lines
4.3 KiB
Python
Raw Normal View History

""" test for app action functionality """
import json
from unittest.mock import patch
from django.contrib.auth.models import AnonymousUser
from django.http import JsonResponse
from django.test import TestCase
from django.test.client import RequestFactory
from bookwyrm import models, views
2021-09-11 20:30:27 +00:00
class WellknownViews(TestCase):
2021-04-26 16:15:42 +00:00
"""view user and edit profile"""
def setUp(self):
2021-04-26 16:15:42 +00:00
"""we need basic test data and mocks"""
2021-08-02 23:05:40 +00:00
self.factory = RequestFactory()
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
"bookwyrm.activitystreams.populate_stream_task.delay"
2021-12-09 23:03:01 +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",
)
models.User.objects.create_user(
"rat@local.com", "rat@rat.rat", "password", local=True, localname="rat"
)
2021-08-02 23:05:40 +00:00
with patch("bookwyrm.models.user.set_remote_server.delay"):
2021-05-26 21:57:29 +00:00
models.User.objects.create_user(
2021-08-02 23:05:40 +00:00
"rat",
"rat@remote.com",
"ratword",
local=False,
remote_id="https://example.com/users/rat",
inbox="https://example.com/users/rat/inbox",
outbox="https://example.com/users/rat/outbox",
2021-05-26 21:57:29 +00:00
)
2021-08-02 23:05:40 +00:00
models.SiteSettings.objects.create()
self.anonymous_user = AnonymousUser
self.anonymous_user.is_authenticated = False
def test_webfinger(self):
2021-04-26 16:15:42 +00:00
"""there are so many views, this just makes sure it LOADS"""
request = self.factory.get("", {"resource": "acct:mouse@local.com"})
request.user = self.anonymous_user
result = views.webfinger(request)
self.assertIsInstance(result, JsonResponse)
data = json.loads(result.getvalue())
self.assertEqual(data["subject"], "acct:mouse@local.com")
def test_webfinger_case_sensitivty(self):
"""ensure that webfinger queries are not case sensitive"""
request = self.factory.get("", {"resource": "acct:MoUsE@local.com"})
request.user = self.anonymous_user
result = views.webfinger(request)
self.assertIsInstance(result, JsonResponse)
data = json.loads(result.getvalue())
self.assertEqual(data["subject"], "acct:mouse@local.com")
def test_nodeinfo_pointer(self):
2021-04-26 16:15:42 +00:00
"""just tells you where nodeinfo is"""
request = self.factory.get("")
request.user = self.anonymous_user
result = views.nodeinfo_pointer(request)
data = json.loads(result.getvalue())
self.assertIsInstance(result, JsonResponse)
self.assertTrue("href" in data["links"][0])
def test_nodeinfo(self):
2021-04-26 16:15:42 +00:00
"""info about the instance"""
request = self.factory.get("")
request.user = self.anonymous_user
result = views.nodeinfo(request)
data = json.loads(result.getvalue())
self.assertIsInstance(result, JsonResponse)
self.assertEqual(data["software"]["name"], "bookwyrm")
self.assertEqual(data["usage"]["users"]["total"], 2)
self.assertEqual(models.User.objects.count(), 3)
def test_instanceinfo(self):
2021-04-26 16:15:42 +00:00
"""about the instance's user activity"""
request = self.factory.get("")
request.user = self.anonymous_user
result = views.instance_info(request)
data = json.loads(result.getvalue())
self.assertIsInstance(result, JsonResponse)
self.assertEqual(data["stats"]["user_count"], 2)
self.assertEqual(models.User.objects.count(), 3)
2021-09-11 20:30:27 +00:00
def test_peers(self):
"""who's federating with whom"""
models.FederatedServer.objects.create(
server_name="test.server",
status="federated",
)
models.FederatedServer.objects.create(
server_name="another.test.server",
status="blocked",
)
request = self.factory.get("")
request.user = self.anonymous_user
result = views.peers(request)
data = json.loads(result.getvalue())
self.assertIsInstance(result, JsonResponse)
self.assertEqual(len(data), 1)
self.assertEqual(data[0], "test.server")