suspend_account_service_spec.rb 3.4 KB

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