unsuspend_account_service_spec.rb 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 feeds of local followers and sends update' do
  37. subject
  38. expect_feeds_merged
  39. expect_updates_sent
  40. end
  41. def expect_feeds_merged
  42. expect(FeedManager.instance).to have_received(:merge_into_home).with(account, local_follower)
  43. expect(FeedManager.instance).to have_received(:merge_into_list).with(account, list)
  44. end
  45. def expect_updates_sent
  46. expect(a_request(:post, remote_follower.inbox_url).with { |req| match_update_actor_request(req, account) }).to have_been_made.once
  47. expect(a_request(:post, remote_reporter.inbox_url).with { |req| match_update_actor_request(req, account) }).to have_been_made.once
  48. end
  49. end
  50. end
  51. describe 'unsuspending a remote account' do
  52. include_examples 'with common context' do
  53. let!(:account) { Fabricate(:account, domain: 'bob.com', uri: 'https://bob.com', inbox_url: 'https://bob.com/inbox', protocol: :activitypub) }
  54. let!(:resolve_account_service) { instance_double(ResolveAccountService) }
  55. before do
  56. allow(ResolveAccountService).to receive(:new).and_return(resolve_account_service)
  57. end
  58. context 'when the account is not remotely suspended' do
  59. before do
  60. allow(resolve_account_service).to receive(:call).with(account).and_return(account)
  61. end
  62. it 're-fetches the account, merges feeds, and preserves suspended' do
  63. expect { subject }
  64. .to_not change_suspended_flag
  65. expect_feeds_merged
  66. expect(resolve_account_service).to have_received(:call).with(account)
  67. end
  68. def expect_feeds_merged
  69. expect(FeedManager.instance).to have_received(:merge_into_home).with(account, local_follower)
  70. expect(FeedManager.instance).to have_received(:merge_into_list).with(account, list)
  71. end
  72. def change_suspended_flag
  73. change(account, :suspended?)
  74. end
  75. end
  76. context 'when the account is remotely suspended' do
  77. before do
  78. allow(resolve_account_service).to receive(:call).with(account) do |account|
  79. account.suspend!(origin: :remote)
  80. account
  81. end
  82. end
  83. it 're-fetches the account, does not merge feeds, marks suspended' do
  84. expect { subject }
  85. .to change_suspended_to_true
  86. expect(resolve_account_service).to have_received(:call).with(account)
  87. expect_feeds_not_merged
  88. end
  89. def expect_feeds_not_merged
  90. expect(FeedManager.instance).to_not have_received(:merge_into_home).with(account, local_follower)
  91. expect(FeedManager.instance).to_not have_received(:merge_into_list).with(account, list)
  92. end
  93. def change_suspended_to_true
  94. change(account, :suspended?).from(false).to(true)
  95. end
  96. end
  97. context 'when the account is remotely deleted' do
  98. before do
  99. allow(resolve_account_service).to receive(:call).with(account).and_return(nil)
  100. end
  101. it 're-fetches the account and does not merge feeds' do
  102. subject
  103. expect(resolve_account_service).to have_received(:call).with(account)
  104. expect_feeds_not_merged
  105. end
  106. def expect_feeds_not_merged
  107. expect(FeedManager.instance).to_not have_received(:merge_into_home).with(account, local_follower)
  108. expect(FeedManager.instance).to_not have_received(:merge_into_list).with(account, list)
  109. end
  110. end
  111. end
  112. end
  113. end