suspend_account_service_spec.rb 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe SuspendAccountService, :sidekiq_inline, 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. Fabricate(:media_attachment, file: attachment_fixture('boop.ogg'), account: account)
  14. end
  15. it 'unmerges from feeds of local followers and changes file mode and preserves suspended flag' do
  16. expect { subject }
  17. .to change_file_mode
  18. .and not_change_suspended_flag
  19. expect(FeedManager.instance).to have_received(:unmerge_from_home).with(account, local_follower)
  20. expect(FeedManager.instance).to have_received(:unmerge_from_list).with(account, list)
  21. end
  22. def change_file_mode
  23. change { File.stat(account.media_attachments.first.file.path).mode }
  24. end
  25. def not_change_suspended_flag
  26. not_change(account, :suspended?)
  27. end
  28. end
  29. describe 'suspending a local account' do
  30. def match_update_actor_request(req, account)
  31. json = JSON.parse(req.body)
  32. actor_id = ActivityPub::TagManager.instance.uri_for(account)
  33. json['type'] == 'Update' && json['actor'] == actor_id && json['object']['id'] == actor_id && json['object']['suspended']
  34. end
  35. before do
  36. stub_request(:post, 'https://alice.com/inbox').to_return(status: 201)
  37. stub_request(:post, 'https://bob.com/inbox').to_return(status: 201)
  38. end
  39. include_examples 'common behavior' do
  40. let!(:account) { Fabricate(:account) }
  41. let!(:remote_follower) { Fabricate(:account, uri: 'https://alice.com', inbox_url: 'https://alice.com/inbox', protocol: :activitypub, domain: 'alice.com') }
  42. let!(:remote_reporter) { Fabricate(:account, uri: 'https://bob.com', inbox_url: 'https://bob.com/inbox', protocol: :activitypub, domain: 'bob.com') }
  43. before do
  44. Fabricate(:report, account: remote_reporter, target_account: account)
  45. remote_follower.follow!(account)
  46. end
  47. it 'sends an update actor to followers and reporters' do
  48. subject
  49. expect(a_request(:post, remote_follower.inbox_url).with { |req| match_update_actor_request(req, account) }).to have_been_made.once
  50. expect(a_request(:post, remote_reporter.inbox_url).with { |req| match_update_actor_request(req, account) }).to have_been_made.once
  51. end
  52. end
  53. end
  54. describe 'suspending a remote account' do
  55. def match_reject_follow_request(req, account, followee)
  56. json = JSON.parse(req.body)
  57. json['type'] == 'Reject' && json['actor'] == ActivityPub::TagManager.instance.uri_for(followee) && json['object']['actor'] == account.uri
  58. end
  59. before do
  60. stub_request(:post, 'https://bob.com/inbox').to_return(status: 201)
  61. end
  62. include_examples 'common behavior' do
  63. let!(:account) { Fabricate(:account, domain: 'bob.com', uri: 'https://bob.com', inbox_url: 'https://bob.com/inbox', protocol: :activitypub) }
  64. let!(:local_followee) { Fabricate(:account) }
  65. before do
  66. account.follow!(local_followee)
  67. end
  68. it 'sends a reject follow' do
  69. subject
  70. expect(a_request(:post, account.inbox_url).with { |req| match_reject_follow_request(req, account, local_followee) }).to have_been_made.once
  71. end
  72. end
  73. end
  74. end