domain_block_spec.rb 4.3 KB

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