moviewyrm/bookwyrm/tests/models/test_user_model.py

177 lines
6.8 KiB
Python
Raw Normal View History

2021-03-08 16:49:10 +00:00
""" testing models """
2021-04-17 20:19:23 +00:00
import json
2020-12-12 23:00:20 +00:00
from unittest.mock import patch
2020-05-10 00:30:14 +00:00
from django.test import TestCase
2021-02-22 19:38:11 +00:00
import responses
2020-05-10 00:30:14 +00:00
from bookwyrm import models
2021-08-28 18:58:41 +00:00
from bookwyrm.settings import USE_HTTPS, DOMAIN
2020-05-10 00:30:14 +00:00
2021-02-22 19:38:11 +00:00
# pylint: disable=missing-class-docstring
# pylint: disable=missing-function-docstring
2020-05-10 00:30:14 +00:00
class User(TestCase):
2021-08-28 18:58:41 +00:00
protocol = "https://" if USE_HTTPS else "http://"
2020-05-10 00:30:14 +00:00
def setUp(self):
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
"bookwyrm.activitystreams.populate_stream_task.delay"
):
2021-08-03 17:25:53 +00:00
self.user = models.User.objects.create_user(
"mouse@%s" % DOMAIN,
"mouse@mouse.mouse",
"mouseword",
local=True,
localname="mouse",
name="hi",
bookwyrm_user=False,
)
2020-05-10 00:30:14 +00:00
def test_computed_fields(self):
2021-04-26 16:15:42 +00:00
"""username instead of id here"""
2021-08-28 18:58:41 +00:00
expected_id = f"{self.protocol}{DOMAIN}/user/mouse"
2020-10-16 16:45:14 +00:00
self.assertEqual(self.user.remote_id, expected_id)
2021-08-28 18:58:41 +00:00
self.assertEqual(self.user.username, f"mouse@{DOMAIN}")
2021-03-08 16:49:10 +00:00
self.assertEqual(self.user.localname, "mouse")
2021-08-28 18:58:41 +00:00
self.assertEqual(self.user.shared_inbox, f"{self.protocol}{DOMAIN}/inbox")
self.assertEqual(self.user.inbox, f"{expected_id}/inbox")
self.assertEqual(self.user.outbox, f"{expected_id}/outbox")
2021-08-28 19:06:03 +00:00
self.assertEqual(self.user.followers_url, f"{expected_id}/followers")
self.assertIsNotNone(self.user.key_pair.private_key)
self.assertIsNotNone(self.user.key_pair.public_key)
2020-05-10 00:30:14 +00:00
2020-12-12 23:00:20 +00:00
def test_remote_user(self):
2021-08-02 23:05:40 +00:00
with patch("bookwyrm.models.user.set_remote_server.delay"):
user = models.User.objects.create_user(
"rat",
"rat@rat.rat",
"ratword",
local=False,
remote_id="https://example.com/dfjkg",
bookwyrm_user=False,
)
2021-03-08 16:49:10 +00:00
self.assertEqual(user.username, "rat@example.com")
2020-05-10 00:30:14 +00:00
def test_user_shelves(self):
2020-10-16 16:45:14 +00:00
shelves = models.Shelf.objects.filter(user=self.user).all()
2020-05-10 00:30:14 +00:00
self.assertEqual(len(shelves), 3)
names = [s.name for s in shelves]
2021-03-08 16:49:10 +00:00
self.assertTrue("To Read" in names)
self.assertTrue("Currently Reading" in names)
self.assertTrue("Read" in names)
2020-05-10 00:30:14 +00:00
ids = [s.identifier for s in shelves]
2021-03-08 16:49:10 +00:00
self.assertTrue("to-read" in ids)
self.assertTrue("reading" in ids)
self.assertTrue("read" in ids)
2020-10-16 16:45:14 +00:00
def test_activitypub_serialize(self):
activity = self.user.to_activity()
2021-03-08 16:49:10 +00:00
self.assertEqual(activity["id"], self.user.remote_id)
self.assertEqual(
activity["@context"],
[
"https://www.w3.org/ns/activitystreams",
"https://w3id.org/security/v1",
{
"manuallyApprovesFollowers": "as:manuallyApprovesFollowers",
"schema": "http://schema.org#",
"PropertyValue": "schema:PropertyValue",
"value": "schema:value",
},
],
)
self.assertEqual(activity["preferredUsername"], self.user.localname)
self.assertEqual(activity["name"], self.user.name)
self.assertEqual(activity["inbox"], self.user.inbox)
self.assertEqual(activity["outbox"], self.user.outbox)
self.assertEqual(activity["bookwyrmUser"], False)
2021-03-22 00:18:42 +00:00
self.assertEqual(activity["discoverable"], False)
2021-03-08 16:49:10 +00:00
self.assertEqual(activity["type"], "Person")
2020-10-16 16:45:14 +00:00
def test_activitypub_outbox(self):
activity = self.user.to_outbox()
2021-03-08 16:49:10 +00:00
self.assertEqual(activity["type"], "OrderedCollection")
self.assertEqual(activity["id"], self.user.outbox)
self.assertEqual(activity["totalItems"], 0)
2021-02-22 19:38:11 +00:00
def test_set_remote_server(self):
server = models.FederatedServer.objects.create(
2021-03-08 16:49:10 +00:00
server_name=DOMAIN, application_type="test type", application_version=3
2021-02-22 19:38:11 +00:00
)
models.user.set_remote_server(self.user.id)
self.user.refresh_from_db()
self.assertEqual(self.user.federated_server, server)
@responses.activate
def test_get_or_create_remote_server(self):
responses.add(
responses.GET,
2021-03-08 16:49:10 +00:00
"https://%s/.well-known/nodeinfo" % DOMAIN,
json={"links": [{"href": "http://www.example.com"}, {}]},
2021-02-22 19:38:11 +00:00
)
responses.add(
responses.GET,
2021-03-08 16:49:10 +00:00
"http://www.example.com",
json={"software": {"name": "hi", "version": "2"}},
2021-02-22 19:38:11 +00:00
)
server = models.user.get_or_create_remote_server(DOMAIN)
self.assertEqual(server.server_name, DOMAIN)
2021-03-08 16:49:10 +00:00
self.assertEqual(server.application_type, "hi")
self.assertEqual(server.application_version, "2")
2021-02-22 19:38:11 +00:00
@responses.activate
def test_get_or_create_remote_server_no_wellknown(self):
responses.add(
2021-03-08 16:49:10 +00:00
responses.GET, "https://%s/.well-known/nodeinfo" % DOMAIN, status=404
2021-02-22 19:38:11 +00:00
)
server = models.user.get_or_create_remote_server(DOMAIN)
self.assertEqual(server.server_name, DOMAIN)
self.assertIsNone(server.application_type)
self.assertIsNone(server.application_version)
@responses.activate
def test_get_or_create_remote_server_no_links(self):
responses.add(
responses.GET,
2021-03-08 16:49:10 +00:00
"https://%s/.well-known/nodeinfo" % DOMAIN,
json={"links": [{"href": "http://www.example.com"}, {}]},
2021-02-22 19:38:11 +00:00
)
2021-03-08 16:49:10 +00:00
responses.add(responses.GET, "http://www.example.com", status=404)
2021-02-22 19:38:11 +00:00
server = models.user.get_or_create_remote_server(DOMAIN)
self.assertEqual(server.server_name, DOMAIN)
self.assertIsNone(server.application_type)
self.assertIsNone(server.application_version)
@responses.activate
def test_get_or_create_remote_server_unknown_format(self):
responses.add(
responses.GET,
2021-03-08 16:49:10 +00:00
"https://%s/.well-known/nodeinfo" % DOMAIN,
json={"links": [{"href": "http://www.example.com"}, {}]},
2021-02-22 19:38:11 +00:00
)
2021-03-08 16:49:10 +00:00
responses.add(responses.GET, "http://www.example.com", json={"fish": "salmon"})
2021-02-22 19:38:11 +00:00
server = models.user.get_or_create_remote_server(DOMAIN)
self.assertEqual(server.server_name, DOMAIN)
self.assertIsNone(server.application_type)
self.assertIsNone(server.application_version)
2021-04-17 20:19:23 +00:00
2021-08-04 15:50:50 +00:00
@patch("bookwyrm.suggested_users.remove_user_task.delay")
def test_delete_user(self, _):
2021-04-26 16:15:42 +00:00
"""deactivate a user"""
2021-04-17 20:19:23 +00:00
self.assertTrue(self.user.is_active)
with patch(
2021-11-12 17:17:00 +00:00
"bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"
2021-04-17 20:19:23 +00:00
) as broadcast_mock:
self.user.delete()
self.assertEqual(broadcast_mock.call_count, 1)
2021-11-12 17:17:00 +00:00
activity = json.loads(broadcast_mock.call_args[1]["args"][1])
2021-04-17 20:19:23 +00:00
self.assertEqual(activity["type"], "Delete")
self.assertEqual(activity["object"], self.user.remote_id)
self.assertFalse(self.user.is_active)