unsuspend_account_service_spec.rb 5.1 KB

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