move_worker_spec.rb 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. describe MoveWorker do
  4. let(:local_follower) { Fabricate(:account) }
  5. let(:blocking_account) { Fabricate(:account) }
  6. let(:muting_account) { Fabricate(:account) }
  7. let(:source_account) { Fabricate(:account, protocol: :activitypub, domain: 'example.com') }
  8. let(:target_account) { Fabricate(:account, protocol: :activitypub, domain: 'example.com') }
  9. let(:local_user) { Fabricate(:user) }
  10. let(:comment) { 'old note prior to move' }
  11. let!(:account_note) { Fabricate(:account_note, account: local_user.account, target_account: source_account, comment: comment) }
  12. let(:block_service) { double }
  13. subject { described_class.new }
  14. before do
  15. local_follower.follow!(source_account)
  16. blocking_account.block!(source_account)
  17. muting_account.mute!(source_account)
  18. allow(BlockService).to receive(:new).and_return(block_service)
  19. allow(block_service).to receive(:call)
  20. end
  21. shared_examples 'user note handling' do
  22. context 'when user notes are short enough' do
  23. it 'copies user note with prelude' do
  24. subject.perform(source_account.id, target_account.id)
  25. expect(AccountNote.find_by(account: account_note.account, target_account: target_account).comment).to include(source_account.acct)
  26. expect(AccountNote.find_by(account: account_note.account, target_account: target_account).comment).to include(account_note.comment)
  27. end
  28. it 'merges user notes when needed' do
  29. new_account_note = AccountNote.create!(account: account_note.account, target_account: target_account, comment: 'new note prior to move')
  30. subject.perform(source_account.id, target_account.id)
  31. expect(AccountNote.find_by(account: account_note.account, target_account: target_account).comment).to include(source_account.acct)
  32. expect(AccountNote.find_by(account: account_note.account, target_account: target_account).comment).to include(account_note.comment)
  33. expect(AccountNote.find_by(account: account_note.account, target_account: target_account).comment).to include(new_account_note.comment)
  34. end
  35. end
  36. context 'when user notes are too long' do
  37. let(:comment) { 'abc' * 333 }
  38. it 'copies user note without prelude' do
  39. subject.perform(source_account.id, target_account.id)
  40. expect(AccountNote.find_by(account: account_note.account, target_account: target_account).comment).to include(account_note.comment)
  41. end
  42. it 'keeps user notes unchanged' do
  43. new_account_note = AccountNote.create!(account: account_note.account, target_account: target_account, comment: 'new note prior to move')
  44. subject.perform(source_account.id, target_account.id)
  45. expect(AccountNote.find_by(account: account_note.account, target_account: target_account).comment).to include(new_account_note.comment)
  46. end
  47. end
  48. end
  49. shared_examples 'block and mute handling' do
  50. it 'makes blocks carry over and add a note' do
  51. subject.perform(source_account.id, target_account.id)
  52. expect(block_service).to have_received(:call).with(blocking_account, target_account)
  53. expect(AccountNote.find_by(account: blocking_account, target_account: target_account).comment).to include(source_account.acct)
  54. end
  55. it 'makes mutes carry over and add a note' do
  56. subject.perform(source_account.id, target_account.id)
  57. expect(muting_account.muting?(target_account)).to be true
  58. expect(AccountNote.find_by(account: muting_account, target_account: target_account).comment).to include(source_account.acct)
  59. end
  60. end
  61. shared_examples 'followers count handling' do
  62. it 'updates the source account followers count' do
  63. subject.perform(source_account.id, target_account.id)
  64. expect(source_account.reload.followers_count).to eq(source_account.passive_relationships.count)
  65. end
  66. it 'updates the target account followers count' do
  67. subject.perform(source_account.id, target_account.id)
  68. expect(target_account.reload.followers_count).to eq(target_account.passive_relationships.count)
  69. end
  70. end
  71. context 'both accounts are distant' do
  72. describe 'perform' do
  73. it 'calls UnfollowFollowWorker' do
  74. expect_push_bulk_to_match(UnfollowFollowWorker, [[local_follower.id, source_account.id, target_account.id, false]])
  75. subject.perform(source_account.id, target_account.id)
  76. end
  77. include_examples 'user note handling'
  78. include_examples 'block and mute handling'
  79. include_examples 'followers count handling'
  80. end
  81. end
  82. context 'target account is local' do
  83. let(:target_account) { Fabricate(:account) }
  84. describe 'perform' do
  85. it 'calls UnfollowFollowWorker' do
  86. expect_push_bulk_to_match(UnfollowFollowWorker, [[local_follower.id, source_account.id, target_account.id, true]])
  87. subject.perform(source_account.id, target_account.id)
  88. end
  89. include_examples 'user note handling'
  90. include_examples 'block and mute handling'
  91. include_examples 'followers count handling'
  92. end
  93. end
  94. context 'both target and source accounts are local' do
  95. let(:target_account) { Fabricate(:account) }
  96. let(:source_account) { Fabricate(:account) }
  97. describe 'perform' do
  98. it 'calls makes local followers follow the target account' do
  99. subject.perform(source_account.id, target_account.id)
  100. expect(local_follower.following?(target_account)).to be true
  101. end
  102. include_examples 'user note handling'
  103. include_examples 'block and mute handling'
  104. include_examples 'followers count handling'
  105. it 'does not fail when a local user is already following both accounts' do
  106. double_follower = Fabricate(:account)
  107. double_follower.follow!(source_account)
  108. double_follower.follow!(target_account)
  109. subject.perform(source_account.id, target_account.id)
  110. expect(local_follower.following?(target_account)).to be true
  111. end
  112. it 'does not allow the moved account to follow themselves' do
  113. source_account.follow!(target_account)
  114. subject.perform(source_account.id, target_account.id)
  115. expect(target_account.following?(target_account)).to be false
  116. end
  117. end
  118. end
  119. end