searchable: fix searching for invalid IP addresses.
Fix an ArgumentError exception when searching for an invalid IP address. Also allow searching for multiple subnets at once.
This commit is contained in:
@@ -71,11 +71,13 @@ module Searchable
|
|||||||
|
|
||||||
def where_inet_matches(attr, value)
|
def where_inet_matches(attr, value)
|
||||||
if value.match?(/[, ]/)
|
if value.match?(/[, ]/)
|
||||||
ips = value.split(/[, ]+/).map { |ip| Danbooru::IpAddress.new(ip).to_string }
|
ips = value.split(/[, ]+/).map { |ip| Danbooru::IpAddress.parse(ip).to_s }
|
||||||
where("#{qualified_column_for(attr)} = ANY(ARRAY[?]::inet[])", ips)
|
return none if ips.any?(&:blank?)
|
||||||
|
where("#{qualified_column_for(attr)} <<= ANY(ARRAY[?]::inet[])", ips)
|
||||||
else
|
else
|
||||||
ip = Danbooru::IpAddress.new(value)
|
ip = Danbooru::IpAddress.parse(value)
|
||||||
where("#{qualified_column_for(attr)} <<= ?", ip.to_string)
|
return none if ip.nil?
|
||||||
|
where("#{qualified_column_for(attr)} <<= ?", ip.to_s)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -9,6 +9,12 @@ module Danbooru
|
|||||||
delegate :ipv4?, :ipv6?, :loopback?, :link_local?, :unique_local?, :private?, :to_string, :network, :prefix, :multicast?, :unspecified?, to: :ip_address
|
delegate :ipv4?, :ipv6?, :loopback?, :link_local?, :unique_local?, :private?, :to_string, :network, :prefix, :multicast?, :unspecified?, to: :ip_address
|
||||||
delegate :ip_info, :is_proxy?, to: :ip_lookup
|
delegate :ip_info, :is_proxy?, to: :ip_lookup
|
||||||
|
|
||||||
|
def self.parse(string)
|
||||||
|
new(string)
|
||||||
|
rescue
|
||||||
|
nil
|
||||||
|
end
|
||||||
|
|
||||||
def initialize(string)
|
def initialize(string)
|
||||||
@ip_address = ::IPAddress.parse(string.to_s.strip)
|
@ip_address = ::IPAddress.parse(string.to_s.strip)
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -105,11 +105,19 @@ class SearchableTest < ActiveSupport::TestCase
|
|||||||
subject { UserSession }
|
subject { UserSession }
|
||||||
|
|
||||||
should "work" do
|
should "work" do
|
||||||
@us = create(:user_session, ip_addr: "10.0.0.1")
|
@us1 = create(:user_session, ip_addr: "10.0.0.1")
|
||||||
assert_search_equals(@us, ip_addr: "10.0.0.1")
|
@us2 = create(:user_session, ip_addr: "11.0.0.1")
|
||||||
assert_search_equals(@us, ip_addr: "10.0.0.1/24")
|
|
||||||
assert_search_equals(@us, ip_addr: "10.0.0.1,1.1.1.1")
|
assert_search_equals(@us1, ip_addr: "10.0.0.1")
|
||||||
assert_search_equals(@us, ip_addr: "10.0.0.1 1.1.1.1")
|
assert_search_equals(@us1, ip_addr: "10.0.0.1/24")
|
||||||
|
assert_search_equals(@us1, ip_addr: "10.0.0.1,1.1.1.1")
|
||||||
|
assert_search_equals(@us1, ip_addr: "10.0.0.1 1.1.1.1")
|
||||||
|
|
||||||
|
assert_search_equals([@us2, @us1], ip_addr: "10.1.0.0/8,11.1.0.0/8")
|
||||||
|
assert_search_equals([@us2, @us1], ip_addr: "10.1.0.0/8 11.1.0.0/8")
|
||||||
|
|
||||||
|
assert_search_equals([], ip_addr: "10.0.0.x")
|
||||||
|
assert_search_equals([], ip_addr: "10.0.0.x 11.0.0.y")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user