unsuspend_account_service_spec.rb 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe UnsuspendAccountService, type: :service do
  4. shared_context 'with common context' do
  5. subject { described_class.new.call(account) }
  6. let!(:local_follower) { Fabricate(:user, current_sign_in_at: 1.hour.ago).account }
  7. let!(:list) { Fabricate(:list, account: local_follower) }
  8. before do
  9. allow(FeedManager.instance).to receive_messages(merge_into_home: nil, merge_into_list: nil)
  10. local_follower.follow!(account)
  11. list.accounts << account
  12. account.unsuspend!
  13. end
  14. end
  15. describe 'unsuspending a local account' do
  16. def match_update_actor_request(req, account)
  17. json = JSON.parse(req.body)
  18. actor_id = ActivityPub::TagManager.instance.uri_for(account)
  19. json['type'] == 'Update' && json['actor'] == actor_id && json['object']['id'] == actor_id && !json['object']['suspended']
  20. end
  21. before do
  22. stub_request(:post, 'https://alice.com/inbox').to_return(status: 201)
  23. stub_request(:post, 'https://bob.com/inbox').to_return(status: 201)
  24. end
  25. it 'does not change the “suspended” flag' do
  26. expect { subject }.to_not change(account, :suspended?)
  27. end
  28. include_examples 'with common context' do
  29. let!(:account) { Fabricate(:account) }
  30. let!(:remote_follower) { Fabricate(:account, uri: 'https://alice.com', inbox_url: 'https://alice.com/inbox', protocol: :activitypub, domain: 'alice.com') }
  31. let!(:remote_reporter) { Fabricate(:account, uri: 'https://bob.com', inbox_url: 'https://bob.com/inbox', protocol: :activitypub, domain: 'bob.com') }
  32. let!(:report) { Fabricate(:report, account: remote_reporter, target_account: account) }
  33. before do
  34. remote_follower.follow!(account)
  35. end
  36. it "merges back into local followers' feeds" do
  37. subject
  38. expect(FeedManager.instance).to have_received(:merge_into_home).with(account, local_follower)
  39. expect(FeedManager.instance).to have_received(:merge_into_list).with(account, list)
  40. end
  41. it 'sends an update actor to followers and reporters' do
  42. subject
  43. expect(a_request(:post, remote_follower.inbox_url).with { |req| match_update_actor_request(req, account) }).to have_been_made.once
  44. expect(a_request(:post, remote_reporter.inbox_url).with { |req| match_update_actor_request(req, account) }).to have_been_made.once
  45. end
  46. end
  47. end
  48. describe 'unsuspending a remote account' do
  49. include_examples 'with common context' do
  50. let!(:account) { Fabricate(:account, domain: 'bob.com', uri: 'https://bob.com', inbox_url: 'https://bob.com/inbox', protocol: :activitypub) }
  51. let!(:resolve_account_service) { instance_double(ResolveAccountService) }
  52. before do
  53. allow(ResolveAccountService).to receive(:new).and_return(resolve_account_service)
  54. end
  55. context 'when the account is not remotely suspended' do
  56. before do
  57. allow(resolve_account_service).to receive(:call).with(account).and_return(account)
  58. end
  59. it 're-fetches the account' do
  60. subject
  61. expect(resolve_account_service).to have_received(:call).with(account)
  62. end
  63. it "merges back into local followers' feeds" do
  64. subject
  65. expect(FeedManager.instance).to have_received(:merge_into_home).with(account, local_follower)
  66. expect(FeedManager.instance).to have_received(:merge_into_list).with(account, list)
  67. end
  68. it 'does not change the “suspended” flag' do
  69. expect { subject }.to_not change(account, :suspended?)
  70. end
  71. end
  72. context 'when the account is remotely suspended' do
  73. before do
  74. allow(resolve_account_service).to receive(:call).with(account) do |account|
  75. account.suspend!(origin: :remote)
  76. account
  77. end
  78. end
  79. it 're-fetches the account' do
  80. subject
  81. expect(resolve_account_service).to have_received(:call).with(account)
  82. end
  83. it "does not merge back into local followers' feeds" do
  84. subject
  85. expect(FeedManager.instance).to_not have_received(:merge_into_home).with(account, local_follower)
  86. expect(FeedManager.instance).to_not have_received(:merge_into_list).with(account, list)
  87. end
  88. it 'marks account as suspended' do
  89. expect { subject }.to change(account, :suspended?).from(false).to(true)
  90. end
  91. end
  92. context 'when the account is remotely deleted' do
  93. before do
  94. allow(resolve_account_service).to receive(:call).with(account).and_return(nil)
  95. end
  96. it 're-fetches the account' do
  97. subject
  98. expect(resolve_account_service).to have_received(:call).with(account)
  99. end
  100. it "does not merge back into local followers' feeds" do
  101. subject
  102. expect(FeedManager.instance).to_not have_received(:merge_into_home).with(account, local_follower)
  103. expect(FeedManager.instance).to_not have_received(:merge_into_list).with(account, list)
  104. end
  105. end
  106. end
  107. end
  108. end