suspend_account_service_spec.rb 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. require 'rails_helper'
  2. RSpec.describe SuspendAccountService, 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(:unmerge_from_home).and_return(nil)
  9. allow(FeedManager.instance).to receive(:unmerge_from_list).and_return(nil)
  10. local_follower.follow!(account)
  11. list.accounts << account
  12. end
  13. it "unmerges from local followers' feeds" do
  14. subject
  15. expect(FeedManager.instance).to have_received(:unmerge_from_home).with(account, local_follower)
  16. expect(FeedManager.instance).to have_received(:unmerge_from_list).with(account, list)
  17. end
  18. it 'marks account as suspended' do
  19. expect { subject }.to change { account.suspended? }.from(false).to(true)
  20. end
  21. end
  22. describe 'suspending a local account' do
  23. def match_update_actor_request(req, account)
  24. json = JSON.parse(req.body)
  25. actor_id = ActivityPub::TagManager.instance.uri_for(account)
  26. json['type'] == 'Update' && json['actor'] == actor_id && json['object']['id'] == actor_id && json['object']['suspended']
  27. end
  28. before do
  29. stub_request(:post, 'https://alice.com/inbox').to_return(status: 201)
  30. stub_request(:post, 'https://bob.com/inbox').to_return(status: 201)
  31. end
  32. include_examples 'common behavior' do
  33. let!(:account) { Fabricate(:account) }
  34. let!(:remote_follower) { Fabricate(:account, uri: 'https://alice.com', inbox_url: 'https://alice.com/inbox', protocol: :activitypub) }
  35. let!(:remote_reporter) { Fabricate(:account, uri: 'https://bob.com', inbox_url: 'https://bob.com/inbox', protocol: :activitypub) }
  36. let!(:report) { Fabricate(:report, account: remote_reporter, target_account: account) }
  37. before do
  38. remote_follower.follow!(account)
  39. end
  40. it 'sends an update actor to followers and reporters' do
  41. subject
  42. expect(a_request(:post, remote_follower.inbox_url).with { |req| match_update_actor_request(req, account) }).to have_been_made.once
  43. expect(a_request(:post, remote_reporter.inbox_url).with { |req| match_update_actor_request(req, account) }).to have_been_made.once
  44. end
  45. end
  46. end
  47. describe 'suspending a remote account' do
  48. def match_reject_follow_request(req, account, followee)
  49. json = JSON.parse(req.body)
  50. json['type'] == 'Reject' && json['actor'] == ActivityPub::TagManager.instance.uri_for(followee) && json['object']['actor'] == account.uri
  51. end
  52. before do
  53. stub_request(:post, 'https://bob.com/inbox').to_return(status: 201)
  54. end
  55. include_examples 'common behavior' do
  56. let!(:account) { Fabricate(:account, domain: 'bob.com', uri: 'https://bob.com', inbox_url: 'https://bob.com/inbox', protocol: :activitypub) }
  57. let!(:local_followee) { Fabricate(:account) }
  58. before do
  59. account.follow!(local_followee)
  60. end
  61. it 'sends a reject follow' do
  62. subject
  63. expect(a_request(:post, account.inbox_url).with { |req| match_reject_follow_request(req, account, local_followee) }).to have_been_made.once
  64. end
  65. end
  66. end
  67. end