suspend_account_service_spec.rb 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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_messages(unmerge_from_home: nil, unmerge_from_list: 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, domain: 'alice.com') }
  36. let!(:remote_reporter) { Fabricate(:account, uri: 'https://bob.com', inbox_url: 'https://bob.com/inbox', protocol: :activitypub, domain: 'bob.com') }
  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