move_worker_spec.rb 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. describe MoveWorker do
  4. subject { described_class.new }
  5. let(:local_follower) { Fabricate(:account, domain: nil) }
  6. let(:blocking_account) { Fabricate(:account) }
  7. let(:muting_account) { Fabricate(:account) }
  8. let(:source_account) { Fabricate(:account, protocol: :activitypub, domain: 'example.com', uri: 'https://example.org/a', inbox_url: 'https://example.org/a/inbox') }
  9. let(:target_account) { Fabricate(:account, protocol: :activitypub, domain: 'example.com', uri: 'https://example.org/b', inbox_url: 'https://example.org/b/inbox') }
  10. let(:local_user) { Fabricate(:user) }
  11. let(:comment) { 'old note prior to move' }
  12. let!(:account_note) { Fabricate(:account_note, account: local_user.account, target_account: source_account, comment: comment) }
  13. let(:list) { Fabricate(:list, account: local_follower) }
  14. let(:block_service) { instance_double(BlockService) }
  15. before do
  16. stub_request(:post, 'https://example.org/a/inbox').to_return(status: 200)
  17. stub_request(:post, 'https://example.org/b/inbox').to_return(status: 200)
  18. local_follower.follow!(source_account)
  19. blocking_account.block!(source_account)
  20. muting_account.mute!(source_account)
  21. list.accounts << source_account
  22. allow(BlockService).to receive(:new).and_return(block_service)
  23. allow(block_service).to receive(:call)
  24. end
  25. shared_examples 'user note handling' do
  26. context 'when user notes are short enough' do
  27. it 'copies user note with prelude' do
  28. subject.perform(source_account.id, target_account.id)
  29. expect(relevant_account_note.comment)
  30. .to include(source_account.acct, account_note.comment)
  31. end
  32. it 'merges user notes when needed' do
  33. new_account_note = AccountNote.create!(account: account_note.account, target_account: target_account, comment: 'new note prior to move')
  34. subject.perform(source_account.id, target_account.id)
  35. expect(relevant_account_note.comment)
  36. .to include(source_account.acct, account_note.comment, new_account_note.comment)
  37. end
  38. end
  39. context 'when user notes are too long' do
  40. let(:comment) { 'abc' * 333 }
  41. it 'copies user note without prelude' do
  42. subject.perform(source_account.id, target_account.id)
  43. expect(relevant_account_note.comment)
  44. .to include(account_note.comment)
  45. end
  46. it 'keeps user notes unchanged' do
  47. new_account_note = AccountNote.create!(account: account_note.account, target_account: target_account, comment: 'new note prior to move')
  48. subject.perform(source_account.id, target_account.id)
  49. expect(relevant_account_note.comment)
  50. .to include(new_account_note.comment)
  51. end
  52. end
  53. private
  54. def relevant_account_note
  55. AccountNote.find_by(account: account_note.account, target_account: target_account)
  56. end
  57. end
  58. shared_examples 'block and mute handling' do
  59. it 'makes blocks and mutes carry over and adds a note' do
  60. subject.perform(source_account.id, target_account.id)
  61. expect(block_service).to have_received(:call).with(blocking_account, target_account)
  62. expect(muting_account.muting?(target_account)).to be true
  63. expect(
  64. [note_account_comment, mute_account_comment]
  65. ).to all include(source_account.acct)
  66. end
  67. def note_account_comment
  68. AccountNote.find_by(account: blocking_account, target_account: target_account).comment
  69. end
  70. def mute_account_comment
  71. AccountNote.find_by(account: muting_account, target_account: target_account).comment
  72. end
  73. end
  74. shared_examples 'followers count handling' do
  75. it 'updates the source and target account followers counts' do
  76. subject.perform(source_account.id, target_account.id)
  77. expect(source_account.reload.followers_count).to eq(source_account.passive_relationships.count)
  78. expect(target_account.reload.followers_count).to eq(target_account.passive_relationships.count)
  79. end
  80. end
  81. shared_examples 'lists handling' do
  82. it 'puts the new account on the list and makes valid lists', :sidekiq_inline do
  83. subject.perform(source_account.id, target_account.id)
  84. expect(list.accounts.include?(target_account)).to be true
  85. expect(ListAccount.all).to all be_valid
  86. end
  87. end
  88. shared_examples 'common tests' do
  89. include_examples 'user note handling'
  90. include_examples 'block and mute handling'
  91. include_examples 'followers count handling'
  92. include_examples 'lists handling'
  93. context 'when a local user already follows both source and target' do
  94. before do
  95. local_follower.request_follow!(target_account)
  96. end
  97. include_examples 'user note handling'
  98. include_examples 'block and mute handling'
  99. include_examples 'followers count handling'
  100. include_examples 'lists handling'
  101. context 'when the local user already has the target in a list' do
  102. before do
  103. list.accounts << target_account
  104. end
  105. include_examples 'lists handling'
  106. end
  107. end
  108. context 'when a local follower already has a pending request to the target' do
  109. before do
  110. local_follower.follow!(target_account)
  111. end
  112. include_examples 'user note handling'
  113. include_examples 'block and mute handling'
  114. include_examples 'followers count handling'
  115. include_examples 'lists handling'
  116. context 'when the local user already has the target in a list' do
  117. before do
  118. list.accounts << target_account
  119. end
  120. include_examples 'lists handling'
  121. end
  122. end
  123. end
  124. describe '#perform' do
  125. context 'when both accounts are distant' do
  126. it 'calls UnfollowFollowWorker' do
  127. subject.perform(source_account.id, target_account.id)
  128. expect(UnfollowFollowWorker).to have_enqueued_sidekiq_job(local_follower.id, source_account.id, target_account.id, false)
  129. end
  130. include_examples 'common tests'
  131. end
  132. context 'when target account is local' do
  133. let(:target_account) { Fabricate(:account) }
  134. it 'calls UnfollowFollowWorker' do
  135. subject.perform(source_account.id, target_account.id)
  136. expect(UnfollowFollowWorker).to have_enqueued_sidekiq_job(local_follower.id, source_account.id, target_account.id, true)
  137. end
  138. include_examples 'common tests'
  139. end
  140. context 'when both target and source accounts are local' do
  141. let(:target_account) { Fabricate(:account) }
  142. let(:source_account) { Fabricate(:account) }
  143. it 'calls makes local followers follow the target account' do
  144. subject.perform(source_account.id, target_account.id)
  145. expect(local_follower.following?(target_account)).to be true
  146. end
  147. include_examples 'common tests'
  148. it 'does not allow the moved account to follow themselves' do
  149. source_account.follow!(target_account)
  150. subject.perform(source_account.id, target_account.id)
  151. expect(target_account.following?(target_account)).to be false
  152. end
  153. end
  154. end
  155. end