2021-03-13 18:58:54 +00:00
|
|
|
""" 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"""
|
2021-03-13 18:58:54 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""we need basic test data and mocks"""
|
2021-05-26 21:57:29 +00:00
|
|
|
with patch("bookwyrm.preview_images.generate_user_preview_image_task.delay"):
|
|
|
|
self.factory = RequestFactory()
|
|
|
|
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-05-27 19:37:27 +00:00
|
|
|
with patch("bookwyrm.models.user.set_remote_server.delay"):
|
|
|
|
models.User.objects.create_user(
|
|
|
|
"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
|
|
|
with patch("bookwyrm.preview_images.generate_site_preview_image_task.delay"):
|
|
|
|
models.SiteSettings.objects.create()
|
2021-03-13 18:58:54 +00:00
|
|
|
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"""
|
2021-03-13 18:58:54 +00:00
|
|
|
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"""
|
2021-03-13 18:58:54 +00:00
|
|
|
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"""
|
2021-03-13 18:58:54 +00:00
|
|
|
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"""
|
2021-03-13 18:58:54 +00:00
|
|
|
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)
|