mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-05-19 00:38:21 +00:00
6684d60526
As outlined in #3354, a use `Move` fails if the user is moving from a BookWyrm server to another BookWrym server. This is because: 1. the original code did not announce changes to alsoKnownAs; 2. the original code always checked the locally saved profile rather than refetching the remote data; This commit fixes both these problems by forcing `MoveUser` to always perform a "refresh" of the local data from the remote, and by saving the user with broadcast=True when updating alsoKnownAs ids.
59 lines
2.2 KiB
Python
59 lines
2.2 KiB
Python
""" testing move models """
|
|
from unittest.mock import patch
|
|
from django.core.exceptions import PermissionDenied
|
|
from django.test import TestCase
|
|
|
|
from bookwyrm import models
|
|
|
|
|
|
class MoveUser(TestCase):
|
|
"""move your account to another identity"""
|
|
|
|
@classmethod
|
|
def setUpTestData(self): # pylint: disable=bad-classmethod-argument
|
|
"""we need some users for this"""
|
|
with patch("bookwyrm.models.user.set_remote_server.delay"):
|
|
self.target_user = models.User.objects.create_user(
|
|
"rat",
|
|
"rat@rat.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",
|
|
)
|
|
|
|
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.origin_user = models.User.objects.create_user(
|
|
"mouse", "mouse@mouse.com", "mouseword", local=True, localname="mouse"
|
|
)
|
|
self.origin_user.remote_id = "http://local.com/user/mouse"
|
|
self.origin_user.save(broadcast=False, update_fields=["remote_id"])
|
|
|
|
def test_user_move_unauthorized(self, *_):
|
|
"""attempt a user move without alsoKnownAs set"""
|
|
|
|
with self.assertRaises(PermissionDenied):
|
|
models.MoveUser.objects.create(
|
|
user=self.origin_user,
|
|
object=self.origin_user.remote_id,
|
|
target=self.target_user,
|
|
)
|
|
|
|
@patch("bookwyrm.suggested_users.remove_user_task.delay")
|
|
@patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async")
|
|
def test_user_move(self, *_):
|
|
"""move user"""
|
|
|
|
self.target_user.also_known_as.add(self.origin_user.id)
|
|
self.target_user.save(broadcast=False)
|
|
|
|
models.MoveUser.objects.create(
|
|
user=self.origin_user,
|
|
object=self.origin_user.remote_id,
|
|
target=self.target_user,
|
|
)
|
|
self.assertEqual(self.origin_user.moved_to, self.target_user.remote_id)
|