unsuspend_account_service_spec.rb 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. require 'rails_helper'
  2. RSpec.describe UnsuspendAccountService, type: :service do
  3. shared_examples 'common behavior' do
  4. let!(:local_follower) { Fabricate(:user, current_sign_in_at: 1.hour.ago).account }
  5. let!(:list) { Fabricate(:list, account: local_follower) }
  6. subject { described_class.new.call(account) }
  7. before do
  8. allow(FeedManager.instance).to receive(:merge_into_home).and_return(nil)
  9. allow(FeedManager.instance).to receive(:merge_into_list).and_return(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 'common behavior' do
  29. let!(:account) { Fabricate(:account) }
  30. let!(:remote_follower) { Fabricate(:account, uri: 'https://alice.com', inbox_url: 'https://alice.com/inbox', protocol: :activitypub) }
  31. let!(:remote_reporter) { Fabricate(:account, uri: 'https://bob.com', inbox_url: 'https://bob.com/inbox', protocol: :activitypub) }
  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 'common behavior' 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) { double }
  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