suspend_account_service_spec.rb 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. account.suspend!
  13. end
  14. it "unmerges from local followers' feeds" do
  15. subject
  16. expect(FeedManager.instance).to have_received(:unmerge_from_home).with(account, local_follower)
  17. expect(FeedManager.instance).to have_received(:unmerge_from_list).with(account, list)
  18. end
  19. it 'does not change the “suspended” flag' do
  20. expect { subject }.to_not change { account.suspended? }
  21. end
  22. end
  23. describe 'suspending a local account' do
  24. def match_update_actor_request(req, account)
  25. json = JSON.parse(req.body)
  26. actor_id = ActivityPub::TagManager.instance.uri_for(account)
  27. json['type'] == 'Update' && json['actor'] == actor_id && json['object']['id'] == actor_id && json['object']['suspended']
  28. end
  29. before do
  30. stub_request(:post, 'https://alice.com/inbox').to_return(status: 201)
  31. stub_request(:post, 'https://bob.com/inbox').to_return(status: 201)
  32. end
  33. include_examples 'common behavior' do
  34. let!(:account) { Fabricate(:account) }
  35. let!(:remote_follower) { Fabricate(:account, uri: 'https://alice.com', inbox_url: 'https://alice.com/inbox', protocol: :activitypub) }
  36. let!(:remote_reporter) { Fabricate(:account, uri: 'https://bob.com', inbox_url: 'https://bob.com/inbox', protocol: :activitypub) }
  37. let!(:report) { Fabricate(:report, account: remote_reporter, target_account: account) }
  38. before do
  39. remote_follower.follow!(account)
  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 'suspending a remote account' do
  49. def match_reject_follow_request(req, account, followee)
  50. json = JSON.parse(req.body)
  51. json['type'] == 'Reject' && json['actor'] == ActivityPub::TagManager.instance.uri_for(followee) && json['object']['actor'] == account.uri
  52. end
  53. before do
  54. stub_request(:post, 'https://bob.com/inbox').to_return(status: 201)
  55. end
  56. include_examples 'common behavior' do
  57. let!(:account) { Fabricate(:account, domain: 'bob.com', uri: 'https://bob.com', inbox_url: 'https://bob.com/inbox', protocol: :activitypub) }
  58. let!(:local_followee) { Fabricate(:account) }
  59. before do
  60. account.follow!(local_followee)
  61. end
  62. it 'sends a reject follow' do
  63. subject
  64. expect(a_request(:post, account.inbox_url).with { |req| match_reject_follow_request(req, account, local_followee) }).to have_been_made.once
  65. end
  66. end
  67. end
  68. end