move_spec.rb 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe ActivityPub::Activity::Move do
  4. RSpec::Matchers.define_negated_matcher :not_be_following, :be_following
  5. RSpec::Matchers.define_negated_matcher :not_be_requested, :be_requested
  6. let(:follower) { Fabricate(:account) }
  7. let(:old_account) { Fabricate(:account, uri: 'https://example.org/alice', domain: 'example.org', protocol: :activitypub, inbox_url: 'https://example.org/inbox') }
  8. let(:new_account) { Fabricate(:account, uri: 'https://example.com/alice', domain: 'example.com', protocol: :activitypub, inbox_url: 'https://example.com/inbox', also_known_as: also_known_as) }
  9. let(:also_known_as) { [old_account.uri] }
  10. let(:returned_account) { new_account }
  11. let(:json) do
  12. {
  13. '@context': 'https://www.w3.org/ns/activitystreams',
  14. id: 'foo',
  15. type: 'Move',
  16. actor: old_account.uri,
  17. object: old_account.uri,
  18. target: new_account.uri,
  19. }.with_indifferent_access
  20. end
  21. before do
  22. follower.follow!(old_account)
  23. stub_request(:post, old_account.inbox_url).to_return(status: 200)
  24. stub_request(:post, new_account.inbox_url).to_return(status: 200)
  25. service_stub = instance_double(ActivityPub::FetchRemoteAccountService)
  26. allow(ActivityPub::FetchRemoteAccountService).to receive(:new).and_return(service_stub)
  27. allow(service_stub).to receive(:call).and_return(returned_account)
  28. end
  29. describe '#perform' do
  30. subject { described_class.new(json, old_account) }
  31. before do
  32. subject.perform
  33. end
  34. context 'when all conditions are met', :inline_jobs do
  35. it 'sets moved on old account, followers unfollow old account, followers request the new account' do
  36. expect(old_account.reload.moved_to_account_id)
  37. .to eq new_account.id
  38. expect(follower)
  39. .to not_be_following(old_account)
  40. .and be_requested(new_account)
  41. end
  42. end
  43. context "when the new account can't be resolved" do
  44. let(:returned_account) { nil }
  45. it 'does not set moved on old account, does not unfollow old, does not follow request new' do
  46. expect(old_account.reload.moved_to_account_id)
  47. .to be_nil
  48. expect(follower)
  49. .to be_following(old_account)
  50. .and not_be_requested(new_account)
  51. end
  52. end
  53. context 'when the new account does not references the old account' do
  54. let(:also_known_as) { [] }
  55. it 'does not set moved on old account, does not unfollow old, does not follow request new' do
  56. expect(old_account.reload.moved_to_account_id)
  57. .to be_nil
  58. expect(follower)
  59. .to be_following(old_account)
  60. .and not_be_requested(new_account)
  61. end
  62. end
  63. context 'when a Move has been recently processed' do
  64. around do |example|
  65. redis.set("move_in_progress:#{old_account.id}", true, nx: true, ex: 7.days.seconds)
  66. example.run
  67. redis.del("move_in_progress:#{old_account.id}")
  68. end
  69. it 'does not set moved on old account, does not unfollow old, does not follow request new' do
  70. expect(old_account.reload.moved_to_account_id)
  71. .to be_nil
  72. expect(follower)
  73. .to be_following(old_account)
  74. .and not_be_requested(new_account)
  75. end
  76. end
  77. end
  78. end