block_domain_service_spec.rb 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe BlockDomainService do
  4. subject { described_class.new }
  5. let(:local_account) { Fabricate(:account) }
  6. let(:bystander) { Fabricate(:account, domain: 'evil.org') }
  7. let!(:bad_account) { Fabricate(:account, username: 'badguy666', domain: 'evil.org') }
  8. let!(:bad_status_plain) { Fabricate(:status, account: bad_account, text: 'You suck') }
  9. let!(:bad_status_with_attachment) { Fabricate(:status, account: bad_account, text: 'Hahaha') }
  10. let!(:bad_attachment) { Fabricate(:media_attachment, account: bad_account, status: bad_status_with_attachment, file: attachment_fixture('attachment.jpg')) }
  11. let!(:already_banned_account) { Fabricate(:account, username: 'badguy', domain: 'evil.org', suspended: true, silenced: true) }
  12. describe 'for a suspension' do
  13. before do
  14. local_account.follow!(bad_account)
  15. bystander.follow!(local_account)
  16. end
  17. it 'creates a domain block, suspends remote accounts with appropriate suspension date, records severed relationships and sends notification', :aggregate_failures do
  18. subject.call(DomainBlock.create!(domain: 'evil.org', severity: :suspend))
  19. expect(DomainBlock.blocked?('evil.org')).to be true
  20. # Suspends account with appropriate suspension date
  21. expect(bad_account.reload.suspended?).to be true
  22. expect(bad_account.reload.suspended_at).to eq DomainBlock.find_by(domain: 'evil.org').created_at
  23. # Keep already-suspended account without updating the suspension date
  24. expect(already_banned_account.reload.suspended?).to be true
  25. expect(already_banned_account.reload.suspended_at).to_not eq DomainBlock.find_by(domain: 'evil.org').created_at
  26. # Removes content
  27. expect { bad_status_plain.reload }.to raise_exception ActiveRecord::RecordNotFound
  28. expect { bad_status_with_attachment.reload }.to raise_exception ActiveRecord::RecordNotFound
  29. expect { bad_attachment.reload }.to raise_exception ActiveRecord::RecordNotFound
  30. # Records severed relationships
  31. severed_relationships = local_account.severed_relationships.to_a
  32. expect(severed_relationships.count).to eq 2
  33. expect(severed_relationships[0].relationship_severance_event).to eq severed_relationships[1].relationship_severance_event
  34. expect(severed_relationships.map { |rel| [rel.account, rel.target_account] }).to contain_exactly([bystander, local_account], [local_account, bad_account])
  35. # Sends severed relationships notification
  36. expect(LocalNotificationWorker).to have_enqueued_sidekiq_job(local_account.id, anything, 'AccountRelationshipSeveranceEvent', 'severed_relationships')
  37. end
  38. end
  39. describe 'for a silence with reject media' do
  40. it 'does not mark the domain as blocked, but silences accounts with an appropriate silencing date, clears media', :aggregate_failures, :inline_jobs do
  41. subject.call(DomainBlock.create!(domain: 'evil.org', severity: :silence, reject_media: true))
  42. expect(DomainBlock.blocked?('evil.org')).to be false
  43. # Silences account with appropriate silecing date
  44. expect(bad_account.reload.silenced?).to be true
  45. expect(bad_account.reload.silenced_at).to eq DomainBlock.find_by(domain: 'evil.org').created_at
  46. # Keeps already-silenced accounts without updating the silecing date
  47. expect(already_banned_account.reload.silenced?).to be true
  48. expect(already_banned_account.reload.silenced_at).to_not eq DomainBlock.find_by(domain: 'evil.org').created_at
  49. # Leaves posts but clears media
  50. expect { bad_status_plain.reload }.to_not raise_error
  51. expect { bad_status_with_attachment.reload }.to_not raise_error
  52. expect { bad_attachment.reload }.to_not raise_error
  53. expect(bad_attachment.file.exists?).to be false
  54. end
  55. end
  56. end