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.

113 lines
5.1 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!(:account_note) { Fabricate(:account_note, account: local_user.account, target_account: source_account) }
  11. let(:block_service) { double }
  12. subject { described_class.new }
  13. before do
  14. local_follower.follow!(source_account)
  15. blocking_account.block!(source_account)
  16. muting_account.mute!(source_account)
  17. allow(UnfollowFollowWorker).to receive(:push_bulk)
  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. it 'copies user note' do
  23. subject.perform(source_account.id, target_account.id)
  24. expect(AccountNote.find_by(account: account_note.account, target_account: target_account).comment).to include(source_account.acct)
  25. expect(AccountNote.find_by(account: account_note.account, target_account: target_account).comment).to include(account_note.comment)
  26. end
  27. it 'merges user notes when needed' do
  28. new_account_note = AccountNote.create!(account: account_note.account, target_account: target_account, comment: 'new note prior to move')
  29. subject.perform(source_account.id, target_account.id)
  30. expect(AccountNote.find_by(account: account_note.account, target_account: target_account).comment).to include(source_account.acct)
  31. expect(AccountNote.find_by(account: account_note.account, target_account: target_account).comment).to include(account_note.comment)
  32. expect(AccountNote.find_by(account: account_note.account, target_account: target_account).comment).to include(new_account_note.comment)
  33. end
  34. end
  35. shared_examples 'block and mute handling' do
  36. it 'makes blocks carry over and add a note' do
  37. subject.perform(source_account.id, target_account.id)
  38. expect(block_service).to have_received(:call).with(blocking_account, target_account)
  39. expect(AccountNote.find_by(account: blocking_account, target_account: target_account).comment).to include(source_account.acct)
  40. end
  41. it 'makes mutes carry over and add a note' do
  42. subject.perform(source_account.id, target_account.id)
  43. expect(muting_account.muting?(target_account)).to be true
  44. expect(AccountNote.find_by(account: muting_account, target_account: target_account).comment).to include(source_account.acct)
  45. end
  46. end
  47. context 'both accounts are distant' do
  48. describe 'perform' do
  49. it 'calls UnfollowFollowWorker' do
  50. subject.perform(source_account.id, target_account.id)
  51. expect(UnfollowFollowWorker).to have_received(:push_bulk).with([local_follower.id])
  52. end
  53. include_examples 'user note handling'
  54. include_examples 'block and mute handling'
  55. end
  56. end
  57. context 'target account is local' do
  58. let(:target_account) { Fabricate(:user, email: 'alice@example.com', account: Fabricate(:account, username: 'alice')).account }
  59. describe 'perform' do
  60. it 'calls UnfollowFollowWorker' do
  61. subject.perform(source_account.id, target_account.id)
  62. expect(UnfollowFollowWorker).to have_received(:push_bulk).with([local_follower.id])
  63. end
  64. include_examples 'user note handling'
  65. include_examples 'block and mute handling'
  66. end
  67. end
  68. context 'both target and source accounts are local' do
  69. let(:target_account) { Fabricate(:user, email: 'alice@example.com', account: Fabricate(:account, username: 'alice')).account }
  70. let(:source_account) { Fabricate(:user, email: 'alice_@example.com', account: Fabricate(:account, username: 'alice_')).account }
  71. describe 'perform' do
  72. it 'calls makes local followers follow the target account' do
  73. subject.perform(source_account.id, target_account.id)
  74. expect(local_follower.following?(target_account)).to be true
  75. end
  76. include_examples 'user note handling'
  77. include_examples 'block and mute handling'
  78. it 'does not fail when a local user is already following both accounts' do
  79. double_follower = Fabricate(:user, email: 'eve@example.com', account: Fabricate(:account, username: 'eve')).account
  80. double_follower.follow!(source_account)
  81. double_follower.follow!(target_account)
  82. subject.perform(source_account.id, target_account.id)
  83. expect(local_follower.following?(target_account)).to be true
  84. end
  85. it 'does not allow the moved account to follow themselves' do
  86. source_account.follow!(target_account)
  87. subject.perform(source_account.id, target_account.id)
  88. expect(target_account.following?(target_account)).to be false
  89. end
  90. end
  91. end
  92. end