1
0

domain_block_spec.rb 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe DomainBlock do
  4. describe 'Validations' do
  5. it { is_expected.to validate_presence_of(:domain) }
  6. context 'when a normalized domain exists' do
  7. before { Fabricate(:domain_block, domain: 'にゃん') }
  8. it { is_expected.to_not allow_value('xn--r9j5b5b').for(:domain) }
  9. end
  10. end
  11. describe '.blocked?' do
  12. it 'returns true if the domain is suspended' do
  13. Fabricate(:domain_block, domain: 'example.com', severity: :suspend)
  14. expect(described_class.blocked?('example.com')).to be true
  15. end
  16. it 'returns false even if the domain is silenced' do
  17. Fabricate(:domain_block, domain: 'example.com', severity: :silence)
  18. expect(described_class.blocked?('example.com')).to be false
  19. end
  20. it 'returns false if the domain is not suspended nor silenced' do
  21. expect(described_class.blocked?('example.com')).to be false
  22. end
  23. end
  24. describe '.rule_for' do
  25. it 'returns rule matching a blocked domain' do
  26. block = Fabricate(:domain_block, domain: 'example.com')
  27. expect(described_class.rule_for('example.com')).to eq block
  28. end
  29. it 'returns a rule matching a subdomain of a blocked domain' do
  30. block = Fabricate(:domain_block, domain: 'example.com')
  31. expect(described_class.rule_for('sub.example.com')).to eq block
  32. end
  33. it 'returns a rule matching a blocked subdomain' do
  34. block = Fabricate(:domain_block, domain: 'sub.example.com')
  35. expect(described_class.rule_for('sub.example.com')).to eq block
  36. end
  37. it 'returns a rule matching a blocked TLD' do
  38. block = Fabricate(:domain_block, domain: 'google')
  39. expect(described_class.rule_for('google')).to eq block
  40. end
  41. it 'returns a rule matching a subdomain of a blocked TLD' do
  42. block = Fabricate(:domain_block, domain: 'google')
  43. expect(described_class.rule_for('maps.google')).to eq block
  44. end
  45. end
  46. describe '#stricter_than?' do
  47. it 'returns true if the new block has suspend severity while the old has lower severity' do
  48. suspend = described_class.new(domain: 'domain', severity: :suspend)
  49. silence = described_class.new(domain: 'domain', severity: :silence)
  50. noop = described_class.new(domain: 'domain', severity: :noop)
  51. expect(suspend.stricter_than?(silence)).to be true
  52. expect(suspend.stricter_than?(noop)).to be true
  53. end
  54. it 'returns false if the new block has lower severity than the old one' do
  55. suspend = described_class.new(domain: 'domain', severity: :suspend)
  56. silence = described_class.new(domain: 'domain', severity: :silence)
  57. noop = described_class.new(domain: 'domain', severity: :noop)
  58. expect(silence.stricter_than?(suspend)).to be false
  59. expect(noop.stricter_than?(suspend)).to be false
  60. expect(noop.stricter_than?(silence)).to be false
  61. end
  62. it 'returns false if the new block does is less strict regarding reports' do
  63. older = described_class.new(domain: 'domain', severity: :silence, reject_reports: true)
  64. newer = described_class.new(domain: 'domain', severity: :silence, reject_reports: false)
  65. expect(newer.stricter_than?(older)).to be false
  66. end
  67. it 'returns false if the new block does is less strict regarding media' do
  68. older = described_class.new(domain: 'domain', severity: :silence, reject_media: true)
  69. newer = described_class.new(domain: 'domain', severity: :silence, reject_media: false)
  70. expect(newer.stricter_than?(older)).to be false
  71. end
  72. end
  73. describe '#public_domain' do
  74. context 'with a domain block that is obfuscated' do
  75. let(:domain_block) { Fabricate(:domain_block, domain: 'hostname.example.com', obfuscate: true) }
  76. it 'garbles the domain' do
  77. expect(domain_block.public_domain).to eq 'hostna**.******e.com'
  78. end
  79. end
  80. context 'with a domain block that is not obfuscated' do
  81. let(:domain_block) { Fabricate(:domain_block, domain: 'example.com', obfuscate: false) }
  82. it 'returns the domain value' do
  83. expect(domain_block.public_domain).to eq 'example.com'
  84. end
  85. end
  86. end
  87. describe '#policies' do
  88. subject { domain_block.policies }
  89. context 'when severity is suspend' do
  90. let(:domain_block) { Fabricate.build :domain_block, severity: :suspend }
  91. it { is_expected.to eq(%i(suspend)) }
  92. end
  93. context 'when severity is noop' do
  94. let(:domain_block) { Fabricate.build :domain_block, severity: :noop, reject_media: true }
  95. it { is_expected.to eq(%i(reject_media)) }
  96. end
  97. context 'when severity is silence' do
  98. let(:domain_block) { Fabricate.build :domain_block, severity: :silence, reject_reports: true }
  99. it { is_expected.to eq(%i(silence reject_reports)) }
  100. end
  101. end
  102. end