move_worker_spec.rb 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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(AccountNote.find_by(account: account_note.account, target_account: target_account).comment).to include(source_account.acct)
  30. expect(AccountNote.find_by(account: account_note.account, target_account: target_account).comment).to include(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(AccountNote.find_by(account: account_note.account, target_account: target_account).comment).to include(source_account.acct)
  36. expect(AccountNote.find_by(account: account_note.account, target_account: target_account).comment).to include(account_note.comment)
  37. expect(AccountNote.find_by(account: account_note.account, target_account: target_account).comment).to include(new_account_note.comment)
  38. end
  39. end
  40. context 'when user notes are too long' do
  41. let(:comment) { 'abc' * 333 }
  42. it 'copies user note without prelude' do
  43. subject.perform(source_account.id, target_account.id)
  44. expect(AccountNote.find_by(account: account_note.account, target_account: target_account).comment).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(AccountNote.find_by(account: account_note.account, target_account: target_account).comment).to include(new_account_note.comment)
  50. end
  51. end
  52. end
  53. shared_examples 'block and mute handling' do
  54. it 'makes blocks and mutes carry over and adds a note' do
  55. subject.perform(source_account.id, target_account.id)
  56. expect(block_service).to have_received(:call).with(blocking_account, target_account)
  57. expect(AccountNote.find_by(account: blocking_account, target_account: target_account).comment).to include(source_account.acct)
  58. expect(muting_account.muting?(target_account)).to be true
  59. expect(AccountNote.find_by(account: muting_account, target_account: target_account).comment).to include(source_account.acct)
  60. end
  61. end
  62. shared_examples 'followers count handling' do
  63. it 'updates the source and target account followers counts' do
  64. subject.perform(source_account.id, target_account.id)
  65. expect(source_account.reload.followers_count).to eq(source_account.passive_relationships.count)
  66. expect(target_account.reload.followers_count).to eq(target_account.passive_relationships.count)
  67. end
  68. end
  69. shared_examples 'lists handling' do
  70. it 'puts the new account on the list and makes valid lists', sidekiq: :inline do
  71. subject.perform(source_account.id, target_account.id)
  72. expect(list.accounts.include?(target_account)).to be true
  73. expect(ListAccount.all).to all be_valid
  74. end
  75. end
  76. shared_examples 'common tests' do
  77. include_examples 'user note handling'
  78. include_examples 'block and mute handling'
  79. include_examples 'followers count handling'
  80. include_examples 'lists handling'
  81. context 'when a local user already follows both source and target' do
  82. before do
  83. local_follower.request_follow!(target_account)
  84. end
  85. include_examples 'user note handling'
  86. include_examples 'block and mute handling'
  87. include_examples 'followers count handling'
  88. include_examples 'lists handling'
  89. context 'when the local user already has the target in a list' do
  90. before do
  91. list.accounts << target_account
  92. end
  93. include_examples 'lists handling'
  94. end
  95. end
  96. context 'when a local follower already has a pending request to the target' do
  97. before do
  98. local_follower.follow!(target_account)
  99. end
  100. include_examples 'user note handling'
  101. include_examples 'block and mute handling'
  102. include_examples 'followers count handling'
  103. include_examples 'lists handling'
  104. context 'when the local user already has the target in a list' do
  105. before do
  106. list.accounts << target_account
  107. end
  108. include_examples 'lists handling'
  109. end
  110. end
  111. end
  112. describe '#perform' do
  113. context 'when both accounts are distant' do
  114. it 'calls UnfollowFollowWorker' do
  115. Sidekiq::Testing.fake! do
  116. subject.perform(source_account.id, target_account.id)
  117. expect(UnfollowFollowWorker).to have_enqueued_sidekiq_job(local_follower.id, source_account.id, target_account.id, false)
  118. Sidekiq::Worker.drain_all
  119. end
  120. end
  121. include_examples 'common tests'
  122. end
  123. context 'when target account is local' do
  124. let(:target_account) { Fabricate(:account) }
  125. it 'calls UnfollowFollowWorker' do
  126. Sidekiq::Testing.fake! 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, true)
  129. Sidekiq::Worker.clear_all
  130. end
  131. end
  132. include_examples 'common tests'
  133. end
  134. context 'when both target and source accounts are local' do
  135. let(:target_account) { Fabricate(:account) }
  136. let(:source_account) { Fabricate(:account) }
  137. it 'calls makes local followers follow the target account' do
  138. subject.perform(source_account.id, target_account.id)
  139. expect(local_follower.following?(target_account)).to be true
  140. end
  141. include_examples 'common tests'
  142. it 'does not allow the moved account to follow themselves' do
  143. source_account.follow!(target_account)
  144. subject.perform(source_account.id, target_account.id)
  145. expect(target_account.following?(target_account)).to be false
  146. end
  147. end
  148. end
  149. end