You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
6.0 KiB

  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. describe MoveWorker do
  4. let(:local_follower) { Fabricate(:user, email: 'bob@example.com', account: Fabricate(:account, username: 'bob')).account }
  5. let(:blocking_account) { Fabricate(:user, email: 'bar@example.com', account: Fabricate(:account, username: 'bar')).account }
  6. let(:muting_account) { Fabricate(:user, email: 'foo@example.com', account: Fabricate(:account, username: 'foo')).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(UnfollowFollowWorker).to receive(:push_bulk)
  19. allow(BlockService).to receive(:new).and_return(block_service)
  20. allow(block_service).to receive(:call)
  21. end
  22. shared_examples 'user note handling' do
  23. context 'when user notes are short enough' do
  24. it 'copies user note with prelude' do
  25. subject.perform(source_account.id, target_account.id)
  26. expect(AccountNote.find_by(account: account_note.account, target_account: target_account).comment).to include(source_account.acct)
  27. expect(AccountNote.find_by(account: account_note.account, target_account: target_account).comment).to include(account_note.comment)
  28. end
  29. it 'merges user notes when needed' do
  30. new_account_note = AccountNote.create!(account: account_note.account, target_account: target_account, comment: 'new note prior to move')
  31. subject.perform(source_account.id, target_account.id)
  32. expect(AccountNote.find_by(account: account_note.account, target_account: target_account).comment).to include(source_account.acct)
  33. expect(AccountNote.find_by(account: account_note.account, target_account: target_account).comment).to include(account_note.comment)
  34. expect(AccountNote.find_by(account: account_note.account, target_account: target_account).comment).to include(new_account_note.comment)
  35. end
  36. end
  37. context 'when user notes are too long' do
  38. let(:comment) { 'abc' * 333 }
  39. it 'copies user note without prelude' do
  40. subject.perform(source_account.id, target_account.id)
  41. expect(AccountNote.find_by(account: account_note.account, target_account: target_account).comment).to include(account_note.comment)
  42. end
  43. it 'keeps user notes unchanged' do
  44. new_account_note = AccountNote.create!(account: account_note.account, target_account: target_account, comment: 'new note prior to move')
  45. subject.perform(source_account.id, target_account.id)
  46. expect(AccountNote.find_by(account: account_note.account, target_account: target_account).comment).to include(new_account_note.comment)
  47. end
  48. end
  49. end
  50. shared_examples 'block and mute handling' do
  51. it 'makes blocks carry over and add a note' do
  52. subject.perform(source_account.id, target_account.id)
  53. expect(block_service).to have_received(:call).with(blocking_account, target_account)
  54. expect(AccountNote.find_by(account: blocking_account, target_account: target_account).comment).to include(source_account.acct)
  55. end
  56. it 'makes mutes carry over and add a note' do
  57. subject.perform(source_account.id, target_account.id)
  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. context 'both accounts are distant' do
  63. describe 'perform' do
  64. it 'calls UnfollowFollowWorker' do
  65. subject.perform(source_account.id, target_account.id)
  66. expect(UnfollowFollowWorker).to have_received(:push_bulk).with([local_follower.id])
  67. end
  68. include_examples 'user note handling'
  69. include_examples 'block and mute handling'
  70. end
  71. end
  72. context 'target account is local' do
  73. let(:target_account) { Fabricate(:user, email: 'alice@example.com', account: Fabricate(:account, username: 'alice')).account }
  74. describe 'perform' do
  75. it 'calls UnfollowFollowWorker' do
  76. subject.perform(source_account.id, target_account.id)
  77. expect(UnfollowFollowWorker).to have_received(:push_bulk).with([local_follower.id])
  78. end
  79. include_examples 'user note handling'
  80. include_examples 'block and mute handling'
  81. end
  82. end
  83. context 'both target and source accounts are local' do
  84. let(:target_account) { Fabricate(:user, email: 'alice@example.com', account: Fabricate(:account, username: 'alice')).account }
  85. let(:source_account) { Fabricate(:user, email: 'alice_@example.com', account: Fabricate(:account, username: 'alice_')).account }
  86. describe 'perform' do
  87. it 'calls makes local followers follow the target account' do
  88. subject.perform(source_account.id, target_account.id)
  89. expect(local_follower.following?(target_account)).to be true
  90. end
  91. include_examples 'user note handling'
  92. include_examples 'block and mute handling'
  93. it 'does not fail when a local user is already following both accounts' do
  94. double_follower = Fabricate(:user, email: 'eve@example.com', account: Fabricate(:account, username: 'eve')).account
  95. double_follower.follow!(source_account)
  96. double_follower.follow!(target_account)
  97. subject.perform(source_account.id, target_account.id)
  98. expect(local_follower.following?(target_account)).to be true
  99. end
  100. it 'does not allow the moved account to follow themselves' do
  101. source_account.follow!(target_account)
  102. subject.perform(source_account.id, target_account.id)
  103. expect(target_account.following?(target_account)).to be false
  104. end
  105. end
  106. end
  107. end