domain_block_spec.rb 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. end
  47. describe '#stricter_than?' do
  48. it 'returns true if the new block has suspend severity while the old has lower severity' do
  49. suspend = DomainBlock.new(domain: 'domain', severity: :suspend)
  50. silence = DomainBlock.new(domain: 'domain', severity: :silence)
  51. noop = DomainBlock.new(domain: 'domain', severity: :noop)
  52. expect(suspend.stricter_than?(silence)).to be true
  53. expect(suspend.stricter_than?(noop)).to be true
  54. end
  55. it 'returns false if the new block has lower severity than the old one' do
  56. suspend = DomainBlock.new(domain: 'domain', severity: :suspend)
  57. silence = DomainBlock.new(domain: 'domain', severity: :silence)
  58. noop = DomainBlock.new(domain: 'domain', severity: :noop)
  59. expect(silence.stricter_than?(suspend)).to be false
  60. expect(noop.stricter_than?(suspend)).to be false
  61. expect(noop.stricter_than?(silence)).to be false
  62. end
  63. it 'returns false if the new block does is less strict regarding reports' do
  64. older = DomainBlock.new(domain: 'domain', severity: :silence, reject_reports: true)
  65. newer = DomainBlock.new(domain: 'domain', severity: :silence, reject_reports: false)
  66. expect(newer.stricter_than?(older)).to be false
  67. end
  68. it 'returns false if the new block does is less strict regarding media' do
  69. older = DomainBlock.new(domain: 'domain', severity: :silence, reject_media: true)
  70. newer = DomainBlock.new(domain: 'domain', severity: :silence, reject_media: false)
  71. expect(newer.stricter_than?(older)).to be false
  72. end
  73. end
  74. end