2023-02-22 09:55:31 +09:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-10-09 21:48:43 +09:00
|
|
|
require 'rails_helper'
|
|
|
|
|
2023-05-04 12:49:53 +09:00
|
|
|
RSpec.describe DomainBlock do
|
2024-10-14 21:51:01 +09:00
|
|
|
describe 'Validations' do
|
2024-09-06 00:36:05 +09:00
|
|
|
it { is_expected.to validate_presence_of(:domain) }
|
2017-04-05 07:29:56 +09:00
|
|
|
|
2024-10-14 21:51:01 +09:00
|
|
|
context 'when a normalized domain exists' do
|
|
|
|
before { Fabricate(:domain_block, domain: 'にゃん') }
|
|
|
|
|
|
|
|
it { is_expected.to_not allow_value('xn--r9j5b5b').for(:domain) }
|
2017-04-05 07:29:56 +09:00
|
|
|
end
|
|
|
|
end
|
2017-06-19 18:31:27 +09:00
|
|
|
|
2019-06-22 07:13:10 +09:00
|
|
|
describe '.blocked?' do
|
2017-06-19 18:31:27 +09:00
|
|
|
it 'returns true if the domain is suspended' do
|
2019-06-22 07:13:10 +09:00
|
|
|
Fabricate(:domain_block, domain: 'example.com', severity: :suspend)
|
2023-06-06 20:58:33 +09:00
|
|
|
expect(described_class.blocked?('example.com')).to be true
|
2017-06-19 18:31:27 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false even if the domain is silenced' do
|
2019-06-22 07:13:10 +09:00
|
|
|
Fabricate(:domain_block, domain: 'example.com', severity: :silence)
|
2023-06-06 20:58:33 +09:00
|
|
|
expect(described_class.blocked?('example.com')).to be false
|
2017-06-19 18:31:27 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false if the domain is not suspended nor silenced' do
|
2023-06-06 20:58:33 +09:00
|
|
|
expect(described_class.blocked?('example.com')).to be false
|
2017-06-19 18:31:27 +09:00
|
|
|
end
|
|
|
|
end
|
2019-05-04 03:36:36 +09:00
|
|
|
|
2019-06-22 07:13:10 +09:00
|
|
|
describe '.rule_for' do
|
|
|
|
it 'returns rule matching a blocked domain' do
|
|
|
|
block = Fabricate(:domain_block, domain: 'example.com')
|
2023-06-06 20:58:33 +09:00
|
|
|
expect(described_class.rule_for('example.com')).to eq block
|
2019-06-22 07:13:10 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns a rule matching a subdomain of a blocked domain' do
|
|
|
|
block = Fabricate(:domain_block, domain: 'example.com')
|
2023-06-06 20:58:33 +09:00
|
|
|
expect(described_class.rule_for('sub.example.com')).to eq block
|
2019-06-22 07:13:10 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns a rule matching a blocked subdomain' do
|
|
|
|
block = Fabricate(:domain_block, domain: 'sub.example.com')
|
2023-06-06 20:58:33 +09:00
|
|
|
expect(described_class.rule_for('sub.example.com')).to eq block
|
2019-06-22 07:13:10 +09:00
|
|
|
end
|
2020-01-09 06:42:05 +09:00
|
|
|
|
|
|
|
it 'returns a rule matching a blocked TLD' do
|
|
|
|
block = Fabricate(:domain_block, domain: 'google')
|
2023-06-06 20:58:33 +09:00
|
|
|
expect(described_class.rule_for('google')).to eq block
|
2020-01-09 06:42:05 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns a rule matching a subdomain of a blocked TLD' do
|
|
|
|
block = Fabricate(:domain_block, domain: 'google')
|
2023-06-06 20:58:33 +09:00
|
|
|
expect(described_class.rule_for('maps.google')).to eq block
|
2020-01-09 06:42:05 +09:00
|
|
|
end
|
2019-06-22 07:13:10 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#stricter_than?' do
|
2019-05-04 03:36:36 +09:00
|
|
|
it 'returns true if the new block has suspend severity while the old has lower severity' do
|
2023-06-06 20:58:33 +09:00
|
|
|
suspend = described_class.new(domain: 'domain', severity: :suspend)
|
|
|
|
silence = described_class.new(domain: 'domain', severity: :silence)
|
|
|
|
noop = described_class.new(domain: 'domain', severity: :noop)
|
2019-05-04 03:36:36 +09:00
|
|
|
expect(suspend.stricter_than?(silence)).to be true
|
|
|
|
expect(suspend.stricter_than?(noop)).to be true
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false if the new block has lower severity than the old one' do
|
2023-06-06 20:58:33 +09:00
|
|
|
suspend = described_class.new(domain: 'domain', severity: :suspend)
|
|
|
|
silence = described_class.new(domain: 'domain', severity: :silence)
|
|
|
|
noop = described_class.new(domain: 'domain', severity: :noop)
|
2019-05-04 03:36:36 +09:00
|
|
|
expect(silence.stricter_than?(suspend)).to be false
|
|
|
|
expect(noop.stricter_than?(suspend)).to be false
|
|
|
|
expect(noop.stricter_than?(silence)).to be false
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false if the new block does is less strict regarding reports' do
|
2023-06-06 20:58:33 +09:00
|
|
|
older = described_class.new(domain: 'domain', severity: :silence, reject_reports: true)
|
|
|
|
newer = described_class.new(domain: 'domain', severity: :silence, reject_reports: false)
|
2019-05-04 03:36:36 +09:00
|
|
|
expect(newer.stricter_than?(older)).to be false
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false if the new block does is less strict regarding media' do
|
2023-06-06 20:58:33 +09:00
|
|
|
older = described_class.new(domain: 'domain', severity: :silence, reject_media: true)
|
|
|
|
newer = described_class.new(domain: 'domain', severity: :silence, reject_media: false)
|
2019-05-04 03:36:36 +09:00
|
|
|
expect(newer.stricter_than?(older)).to be false
|
|
|
|
end
|
|
|
|
end
|
2023-06-06 20:34:04 +09:00
|
|
|
|
|
|
|
describe '#public_domain' do
|
|
|
|
context 'with a domain block that is obfuscated' do
|
|
|
|
let(:domain_block) { Fabricate(:domain_block, domain: 'hostname.example.com', obfuscate: true) }
|
|
|
|
|
|
|
|
it 'garbles the domain' do
|
|
|
|
expect(domain_block.public_domain).to eq 'hostna**.******e.com'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a domain block that is not obfuscated' do
|
|
|
|
let(:domain_block) { Fabricate(:domain_block, domain: 'example.com', obfuscate: false) }
|
|
|
|
|
|
|
|
it 'returns the domain value' do
|
|
|
|
expect(domain_block.public_domain).to eq 'example.com'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2024-10-14 21:51:01 +09:00
|
|
|
|
|
|
|
describe '#policies' do
|
|
|
|
subject { domain_block.policies }
|
|
|
|
|
|
|
|
context 'when severity is suspend' do
|
|
|
|
let(:domain_block) { Fabricate.build :domain_block, severity: :suspend }
|
|
|
|
|
|
|
|
it { is_expected.to eq(%i(suspend)) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when severity is noop' do
|
|
|
|
let(:domain_block) { Fabricate.build :domain_block, severity: :noop, reject_media: true }
|
|
|
|
|
|
|
|
it { is_expected.to eq(%i(reject_media)) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when severity is silence' do
|
|
|
|
let(:domain_block) { Fabricate.build :domain_block, severity: :silence, reject_reports: true }
|
|
|
|
|
|
|
|
it { is_expected.to eq(%i(silence reject_reports)) }
|
|
|
|
end
|
|
|
|
end
|
2016-10-09 21:48:43 +09:00
|
|
|
end
|