moviewyrm/bookwyrm/tests/views/test_wellknown.py

86 lines
3.1 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
class UserViews(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()
2021-08-03 17:25:53 +00:00
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"):
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_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)