move_worker_spec.rb 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. context 'both accounts are distant' do
  62. describe 'perform' do
  63. it 'calls UnfollowFollowWorker' do
  64. expect_push_bulk_to_match(UnfollowFollowWorker, [[local_follower.id, source_account.id, target_account.id, false]])
  65. subject.perform(source_account.id, target_account.id)
  66. end
  67. include_examples 'user note handling'
  68. include_examples 'block and mute handling'
  69. end
  70. end
  71. context 'target account is local' do
  72. let(:target_account) { Fabricate(:account) }
  73. describe 'perform' do
  74. it 'calls UnfollowFollowWorker' do
  75. expect_push_bulk_to_match(UnfollowFollowWorker, [[local_follower.id, source_account.id, target_account.id, true]])
  76. subject.perform(source_account.id, target_account.id)
  77. end
  78. include_examples 'user note handling'
  79. include_examples 'block and mute handling'
  80. end
  81. end
  82. context 'both target and source accounts are local' do
  83. let(:target_account) { Fabricate(:account) }
  84. let(:source_account) { Fabricate(:account) }
  85. describe 'perform' do
  86. it 'calls makes local followers follow the target account' do
  87. subject.perform(source_account.id, target_account.id)
  88. expect(local_follower.following?(target_account)).to be true
  89. end
  90. include_examples 'user note handling'
  91. include_examples 'block and mute handling'
  92. it 'does not fail when a local user is already following both accounts' do
  93. double_follower = Fabricate(:account)
  94. double_follower.follow!(source_account)
  95. double_follower.follow!(target_account)
  96. subject.perform(source_account.id, target_account.id)
  97. expect(local_follower.following?(target_account)).to be true
  98. end
  99. it 'does not allow the moved account to follow themselves' do
  100. source_account.follow!(target_account)
  101. subject.perform(source_account.id, target_account.id)
  102. expect(target_account.following?(target_account)).to be false
  103. end
  104. end
  105. end
  106. end