Skip to content

Commit f57b9a0

Browse files
authored
[ENG-9906] - Update UserShareReindex (#11559)
* Update UserShareReindex * Fix test
1 parent ff694d6 commit f57b9a0

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

osf/models/user.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,11 @@ def merge_user(self, user):
728728
if self == user:
729729
raise ValueError('Cannot merge a user into itself')
730730

731+
# Capture content to SHARE reindex BEFORE merge transfers contributors
732+
# After merge, user.contributed and user.preprints will be empty
733+
nodes_to_reindex = list(user.contributed)
734+
preprints_to_reindex = list(user.preprints.all())
735+
731736
# Move over the other user's attributes
732737
# TODO: confirm
733738
for system_tag in user.system_tags.all():
@@ -859,13 +864,13 @@ def merge_user(self, user):
859864

860865
from api.share.utils import update_share
861866

862-
for node in user.contributed:
867+
for node in nodes_to_reindex:
863868
try:
864869
update_share(node)
865870
except Exception as e:
866871
logger.exception(f'Failed to SHARE reindex node {node._id} during user merge: {e}')
867872

868-
for preprint in user.preprints.all():
873+
for preprint in preprints_to_reindex:
869874
try:
870875
update_share(preprint)
871876
except Exception as e:

osf_tests/test_user.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,41 @@ def test_merge_drafts(self, user):
442442
assert not draft_five.has_permission(user2, permissions.READ)
443443
assert not draft_five.is_contributor(user2)
444444

445+
@mock.patch('api.share.utils.update_share')
446+
def test_merge_user_triggers_share_reindex(self, mock_update_share):
447+
from osf.models import Preprint
448+
449+
user = AuthUserFactory()
450+
user2 = AuthUserFactory()
451+
452+
node_one = ProjectFactory(creator=user2, title='node_one')
453+
node_two = ProjectFactory(title='node_two')
454+
node_two.add_contributor(user2)
455+
456+
preprint_one = PreprintFactory(creator=user2, title='preprint_one')
457+
preprint_two = PreprintFactory(title='preprint_two')
458+
preprint_two.add_contributor(user2)
459+
460+
user.merge_user(user2)
461+
462+
# Verify update_share was called for both nodes
463+
nodes_reindexed = [
464+
call[0][0] for call in mock_update_share.call_args_list
465+
if isinstance(call[0][0], AbstractNode)
466+
]
467+
assert len(nodes_reindexed) == 2
468+
assert node_one in nodes_reindexed
469+
assert node_two in nodes_reindexed
470+
471+
# Verify update_share was called for both preprints
472+
preprints_reindexed = [
473+
call[0][0] for call in mock_update_share.call_args_list
474+
if isinstance(call[0][0], Preprint)
475+
]
476+
assert len(preprints_reindexed) == 2
477+
assert preprint_one in preprints_reindexed
478+
assert preprint_two in preprints_reindexed
479+
445480
def test_cant_create_user_without_username(self):
446481
u = OSFUser() # No username given
447482
with pytest.raises(ValidationError):

0 commit comments

Comments
 (0)