domain_block_spec.rb 3.8 KB

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