You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
3.7 KiB

  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe DomainBlock, type: :model 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(DomainBlock.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(DomainBlock.blocked?('example.com')).to be false
  25. end
  26. it 'returns false if the domain is not suspended nor silenced' do
  27. expect(DomainBlock.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(DomainBlock.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(DomainBlock.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(DomainBlock.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(DomainBlock.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(DomainBlock.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 = DomainBlock.new(domain: 'domain', severity: :suspend)
  55. silence = DomainBlock.new(domain: 'domain', severity: :silence)
  56. noop = DomainBlock.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 = DomainBlock.new(domain: 'domain', severity: :suspend)
  62. silence = DomainBlock.new(domain: 'domain', severity: :silence)
  63. noop = DomainBlock.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 = DomainBlock.new(domain: 'domain', severity: :silence, reject_reports: true)
  70. newer = DomainBlock.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 = DomainBlock.new(domain: 'domain', severity: :silence, reject_media: true)
  75. newer = DomainBlock.new(domain: 'domain', severity: :silence, reject_media: false)
  76. expect(newer.stricter_than?(older)).to be false
  77. end
  78. end
  79. end