Add IpBlock#to_cidr method to build string

This commit is contained in:
Matt Jankowski 2025-08-13 17:39:09 -04:00
parent e770303968
commit be0489ac1f
7 changed files with 29 additions and 12 deletions

View File

@ -31,9 +31,10 @@ class IpBlock < ApplicationRecord
after_commit :reset_cache after_commit :reset_cache
def to_log_human_identifier def to_cidr
"#{ip}/#{ip.prefix}" "#{ip}/#{ip.prefix}"
end end
alias to_log_human_identifier to_cidr
class << self class << self
def blocked?(remote_ip) def blocked?(remote_ip)

View File

@ -9,6 +9,6 @@ class REST::Admin::IpBlockSerializer < ActiveModel::Serializer
end end
def ip def ip
"#{object.ip}/#{object.ip.prefix}" object.to_cidr
end end
end end

View File

@ -3,7 +3,7 @@
= f.check_box :ip_block_ids, { multiple: true, include_hidden: false }, ip_block.id = f.check_box :ip_block_ids, { multiple: true, include_hidden: false }, ip_block.id
.batch-table__row__content.pending-account .batch-table__row__content.pending-account
.pending-account__header .pending-account__header
%samp= link_to "#{ip_block.ip}/#{ip_block.ip.prefix}", admin_accounts_path(ip: "#{ip_block.ip}/#{ip_block.ip.prefix}") %samp= link_to ip_block.to_cidr, admin_accounts_path(ip: ip_block.to_cidr)
- if ip_block.comment.present? - if ip_block.comment.present?
· ·
= ip_block.comment = ip_block.comment

View File

@ -108,9 +108,9 @@ module Mastodon::CLI
IpBlock.severity_no_access.find_each do |ip_block| IpBlock.severity_no_access.find_each do |ip_block|
case options[:format] case options[:format]
when 'nginx' when 'nginx'
say "deny #{ip_block.ip}/#{ip_block.ip.prefix};" say "deny #{ip_block.to_cidr};"
else else
say "#{ip_block.ip}/#{ip_block.ip.prefix}" say ip_block.to_cidr
end end
end end
end end

View File

@ -251,12 +251,12 @@ RSpec.describe Mastodon::CLI::IpBlocks do
it 'exports blocked IPs with "no_access" severity in plain format' do it 'exports blocked IPs with "no_access" severity in plain format' do
expect { subject } expect { subject }
.to output_results("#{first_ip_range_block.ip}/#{first_ip_range_block.ip.prefix}\n#{second_ip_range_block.ip}/#{second_ip_range_block.ip.prefix}") .to output_results("#{first_ip_range_block.to_cidr}\n#{second_ip_range_block.to_cidr}")
end end
it 'does not export blocked IPs with different severities' do it 'does not export blocked IPs with different severities' do
expect { subject } expect { subject }
.to_not output_results("#{third_ip_range_block.ip}/#{first_ip_range_block.ip.prefix}") .to_not output_results(third_ip_range_block.to_cidr)
end end
end end
@ -265,19 +265,19 @@ RSpec.describe Mastodon::CLI::IpBlocks do
it 'exports blocked IPs with "no_access" severity in plain format' do it 'exports blocked IPs with "no_access" severity in plain format' do
expect { subject } expect { subject }
.to output_results("deny #{first_ip_range_block.ip}/#{first_ip_range_block.ip.prefix};\ndeny #{second_ip_range_block.ip}/#{second_ip_range_block.ip.prefix};") .to output_results("deny #{first_ip_range_block.to_cidr};\ndeny #{second_ip_range_block.to_cidr};")
end end
it 'does not export blocked IPs with different severities' do it 'does not export blocked IPs with different severities' do
expect { subject } expect { subject }
.to_not output_results("deny #{third_ip_range_block.ip}/#{first_ip_range_block.ip.prefix};") .to_not output_results("deny #{third_ip_range_block.to_cidr};")
end end
end end
context 'when --format option is not provided' do context 'when --format option is not provided' do
it 'exports blocked IPs in plain format by default' do it 'exports blocked IPs in plain format by default' do
expect { subject } expect { subject }
.to output_results("#{first_ip_range_block.ip}/#{first_ip_range_block.ip.prefix}\n#{second_ip_range_block.ip}/#{second_ip_range_block.ip.prefix}") .to output_results("#{first_ip_range_block.to_cidr}\n#{second_ip_range_block.to_cidr}")
end end
end end
end end

View File

@ -26,6 +26,22 @@ RSpec.describe IpBlock do
end end
end end
describe '#to_cidr' do
subject { Fabricate.build(:ip_block, ip:).to_cidr }
context 'with an IP and a specified prefix' do
let(:ip) { '192.168.1.0/24' }
it { is_expected.to eq('192.168.1.0/24') }
end
context 'with an IP and a default prefix' do
let(:ip) { '192.168.1.0' }
it { is_expected.to eq('192.168.1.0/32') }
end
end
describe '.blocked?' do describe '.blocked?' do
context 'when the IP is blocked' do context 'when the IP is blocked' do
it 'returns true' do it 'returns true' do

View File

@ -97,7 +97,7 @@ RSpec.describe 'IP Blocks' do
expect(response.parsed_body) expect(response.parsed_body)
.to include( .to include(
ip: eq("#{ip_block.ip}/#{ip_block.ip.prefix}"), ip: eq(ip_block.to_cidr),
severity: eq(ip_block.severity.to_s) severity: eq(ip_block.severity.to_s)
) )
end end
@ -216,7 +216,7 @@ RSpec.describe 'IP Blocks' do
expect(response.content_type) expect(response.content_type)
.to start_with('application/json') .to start_with('application/json')
expect(response.parsed_body).to match(hash_including({ expect(response.parsed_body).to match(hash_including({
ip: "#{ip_block.ip}/#{ip_block.ip.prefix}", ip: ip_block.to_cidr,
severity: 'sign_up_requires_approval', severity: 'sign_up_requires_approval',
comment: 'Decreasing severity', comment: 'Decreasing severity',
})) }))