domain_block_spec.rb 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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: 'domain', severity: :suspend)
  23. expect(DomainBlock.blocked?('domain')).to eq true
  24. end
  25. it 'returns false even if the domain is silenced' do
  26. Fabricate(:domain_block, domain: 'domain', severity: :silence)
  27. expect(DomainBlock.blocked?('domain')).to eq false
  28. end
  29. it 'returns false if the domain is not suspended nor silenced' do
  30. expect(DomainBlock.blocked?('domain')).to eq false
  31. end
  32. end
  33. describe 'stricter_than?' do
  34. it 'returns true if the new block has suspend severity while the old has lower severity' do
  35. suspend = DomainBlock.new(domain: 'domain', severity: :suspend)
  36. silence = DomainBlock.new(domain: 'domain', severity: :silence)
  37. noop = DomainBlock.new(domain: 'domain', severity: :noop)
  38. expect(suspend.stricter_than?(silence)).to be true
  39. expect(suspend.stricter_than?(noop)).to be true
  40. end
  41. it 'returns false if the new block has lower severity than the old one' do
  42. suspend = DomainBlock.new(domain: 'domain', severity: :suspend)
  43. silence = DomainBlock.new(domain: 'domain', severity: :silence)
  44. noop = DomainBlock.new(domain: 'domain', severity: :noop)
  45. expect(silence.stricter_than?(suspend)).to be false
  46. expect(noop.stricter_than?(suspend)).to be false
  47. expect(noop.stricter_than?(silence)).to be false
  48. end
  49. it 'returns false if the new block does is less strict regarding reports' do
  50. older = DomainBlock.new(domain: 'domain', severity: :silence, reject_reports: true)
  51. newer = DomainBlock.new(domain: 'domain', severity: :silence, reject_reports: false)
  52. expect(newer.stricter_than?(older)).to be false
  53. end
  54. it 'returns false if the new block does is less strict regarding media' do
  55. older = DomainBlock.new(domain: 'domain', severity: :silence, reject_media: true)
  56. newer = DomainBlock.new(domain: 'domain', severity: :silence, reject_media: false)
  57. expect(newer.stricter_than?(older)).to be false
  58. end
  59. end
  60. end