block_domain_service_spec.rb 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe BlockDomainService, type: :service do
  4. subject { described_class.new }
  5. let!(:bad_account) { Fabricate(:account, username: 'badguy666', domain: 'evil.org') }
  6. let!(:bad_status_plain) { Fabricate(:status, account: bad_account, text: 'You suck') }
  7. let!(:bad_status_with_attachment) { Fabricate(:status, account: bad_account, text: 'Hahaha') }
  8. let!(:bad_attachment) { Fabricate(:media_attachment, account: bad_account, status: bad_status_with_attachment, file: attachment_fixture('attachment.jpg')) }
  9. let!(:already_banned_account) { Fabricate(:account, username: 'badguy', domain: 'evil.org', suspended: true, silenced: true) }
  10. describe 'for a suspension' do
  11. before do
  12. subject.call(DomainBlock.create!(domain: 'evil.org', severity: :suspend))
  13. end
  14. it 'creates a domain block' do
  15. expect(DomainBlock.blocked?('evil.org')).to be true
  16. end
  17. it 'removes remote accounts from that domain' do
  18. expect(Account.find_remote('badguy666', 'evil.org').suspended?).to be true
  19. end
  20. it 'records suspension date appropriately' do
  21. expect(Account.find_remote('badguy666', 'evil.org').suspended_at).to eq DomainBlock.find_by(domain: 'evil.org').created_at
  22. end
  23. it 'keeps already-banned accounts banned' do
  24. expect(Account.find_remote('badguy', 'evil.org').suspended?).to be true
  25. end
  26. it 'does not overwrite suspension date of already-banned accounts' do
  27. expect(Account.find_remote('badguy', 'evil.org').suspended_at).to_not eq DomainBlock.find_by(domain: 'evil.org').created_at
  28. end
  29. it 'removes the remote accounts\'s statuses and media attachments' do
  30. expect { bad_status_plain.reload }.to raise_exception ActiveRecord::RecordNotFound
  31. expect { bad_status_with_attachment.reload }.to raise_exception ActiveRecord::RecordNotFound
  32. expect { bad_attachment.reload }.to raise_exception ActiveRecord::RecordNotFound
  33. end
  34. end
  35. describe 'for a silence with reject media' do
  36. before do
  37. subject.call(DomainBlock.create!(domain: 'evil.org', severity: :silence, reject_media: true))
  38. end
  39. it 'does not create a domain block' do
  40. expect(DomainBlock.blocked?('evil.org')).to be false
  41. end
  42. it 'silences remote accounts from that domain' do
  43. expect(Account.find_remote('badguy666', 'evil.org').silenced?).to be true
  44. end
  45. it 'records suspension date appropriately' do
  46. expect(Account.find_remote('badguy666', 'evil.org').silenced_at).to eq DomainBlock.find_by(domain: 'evil.org').created_at
  47. end
  48. it 'keeps already-banned accounts banned' do
  49. expect(Account.find_remote('badguy', 'evil.org').silenced?).to be true
  50. end
  51. it 'does not overwrite suspension date of already-banned accounts' do
  52. expect(Account.find_remote('badguy', 'evil.org').silenced_at).to_not eq DomainBlock.find_by(domain: 'evil.org').created_at
  53. end
  54. it 'leaves the domains status and attachments, but clears media' do
  55. expect { bad_status_plain.reload }.to_not raise_error
  56. expect { bad_status_with_attachment.reload }.to_not raise_error
  57. expect { bad_attachment.reload }.to_not raise_error
  58. expect(bad_attachment.file.exists?).to be false
  59. end
  60. end
  61. end