[Rt-commit] rt branch, ip-customfields, updated. rt-3.8.8-665-g32afb86
? sunnavy
sunnavy at bestpractical.com
Mon Sep 13 04:36:23 EDT 2010
The branch, ip-customfields has been updated
via 32afb860c09e726b06505c730a106cc0a5b0b23b (commit)
via 336a87adf42c5879e291d8e4c8ebb15ff5d6a992 (commit)
via e8c30250aeb3a9f98dce105e0d9c90380bfb800e (commit)
from 4c6b7bdc6889bbe16f9d00e198d53044b2fc28ea (commit)
Summary of changes:
lib/RT/ObjectCustomFieldValue_Overlay.pm | 54 ++++--
lib/RT/Tickets_Overlay.pm | 11 +-
share/html/Elements/ValidateCustomFields | 32 ++-
t/customfields/ip.t | 321 ++++--------------------------
t/customfields/{ip.t => iprange.t} | 0
5 files changed, 103 insertions(+), 315 deletions(-)
copy t/customfields/{ip.t => iprange.t} (100%)
- Log -----------------------------------------------------------------
commit e8c30250aeb3a9f98dce105e0d9c90380bfb800e
Author: sunnavy <sunnavy at bestpractical.com>
Date: Mon Sep 13 14:04:25 2010 +0800
rename ip.t to iprange.t, will add a new ip.t
diff --git a/t/customfields/ip.t b/t/customfields/iprange.t
similarity index 100%
rename from t/customfields/ip.t
rename to t/customfields/iprange.t
commit 336a87adf42c5879e291d8e4c8ebb15ff5d6a992
Author: sunnavy <sunnavy at bestpractical.com>
Date: Mon Sep 13 15:32:55 2010 +0800
IPAddress only accepts one IP
diff --git a/lib/RT/ObjectCustomFieldValue_Overlay.pm b/lib/RT/ObjectCustomFieldValue_Overlay.pm
index 9e6c633..54970d3 100644
--- a/lib/RT/ObjectCustomFieldValue_Overlay.pm
+++ b/lib/RT/ObjectCustomFieldValue_Overlay.pm
@@ -75,7 +75,22 @@ sub Create {
my $cf_as_sys = RT::CustomField->new(RT->SystemUser);
$cf_as_sys->Load($args{'CustomField'});
- if($cf_as_sys->Type eq 'IPAddress' || $cf_as_sys->Type eq 'IPAddressRange') {
+
+ if($cf_as_sys->Type eq 'IPAddress') {
+ unless ( defined $args{'Content'} ) {
+ return
+ wantarray
+ ? ( 0, $self->loc("Content can't be empty for IPAddress") )
+ : 0;
+ }
+
+ if ( $args{'Content'} =~ /^\s*$RE{net}{IPv4}\s*$/o ) {
+ $args{'Content'} = sprintf "%03d.%03d.%03d.%03d", split /\./,
+ $args{'Content'};
+ }
+ }
+
+ if($cf_as_sys->Type eq 'IPAddressRange') {
if ($args{'Content'}) {
($args{'Content'}, $args{'LargeContent'}) = $self->ParseIPRange( $args{'Content'} );
}
@@ -83,12 +98,11 @@ sub Create {
unless ( defined $args{'Content'} ) {
return
wantarray
- ? ( 0, $self->loc("Content can't be empty for IPAddress(Range)") )
+ ? ( 0, $self->loc("Content can't be empty for IPAddressRange") )
: 0;
}
}
-
if ( defined $args{'Content'} && length( Encode::encode_utf8($args{'Content'}) ) > 255 ) {
if ( defined $args{'LargeContent'} && length $args{'LargeContent'} ) {
$RT::Logger->error("Content is longer than 255 bytes and LargeContent specified");
@@ -226,27 +240,31 @@ sub Content {
my $self = shift;
my $content = $self->_Value('Content');
- if ( $self->CustomFieldObj->Type eq 'IPAddress' ||
- $self->CustomFieldObj->Type eq 'IPAddressRange') {
+ if ( $self->CustomFieldObj->Type eq 'IPAddress'
+ || $self->CustomFieldObj->Type eq 'IPAddressRange' )
+ {
+
+ if ( $content =~ /^\s*($re_ip_serialized)\s*$/o ) {
+ $content = sprintf "%d.%d.%d.%d", split /\./, $1;
+ }
- if ($content =~ /^\s*($re_ip_serialized)\s*$/o ) {
- $content = sprintf "%d.%d.%d.%d", split /\./, $1;
- }
+ return $content if $self->CustomFieldObj->Type eq 'IPAddress';
my $large_content = $self->__Value('LargeContent');
- if ( $large_content =~ /^\s*($re_ip_serialized)\s*$/o ) {
- my $eIP = sprintf "%d.%d.%d.%d", split /\./, $1;
- if ( $content eq $eIP ) {
- return $content;
- }
- else {
- return $content . "-".$eIP;
- }
- } else {
+ if ( $large_content =~ /^\s*($re_ip_serialized)\s*$/o ) {
+ my $eIP = sprintf "%d.%d.%d.%d", split /\./, $1;
+ if ( $content eq $eIP ) {
return $content;
}
-
+ else {
+ return $content . "-" . $eIP;
+ }
+ }
+ else {
+ return $content;
+ }
}
+
if ( !(defined $content && length $content) && $self->ContentType && $self->ContentType eq 'text/plain' ) {
return $self->LargeContent;
} else {
diff --git a/lib/RT/Tickets_Overlay.pm b/lib/RT/Tickets_Overlay.pm
index 86e44c2..58b6e4d 100755
--- a/lib/RT/Tickets_Overlay.pm
+++ b/lib/RT/Tickets_Overlay.pm
@@ -1379,8 +1379,15 @@ sub _CustomFieldLimit {
return 'NOT MATCHES' if $op eq '!=';
return $op;
};
+
+ if ( $cf
+ && $cf->Type eq 'IPAddress'
+ && $value =~ /^\s*$RE{net}{IPv4}\s*$/o )
+ {
+ $value = sprintf "%03d.%03d.%03d.%03d", split /\./, $value;
+ }
+
if ( $cf
- && ( $cf->Type eq 'IPAddress' || $cf->Type eq 'IPAddressRange' )
&& $value =~ /^\s*$RE{net}{CIDR}{IPv4}{-keep}\s*$/o )
{
@@ -1420,7 +1427,7 @@ sub _CustomFieldLimit {
) if $CFs;
$self->_CloseParen;
}
- elsif ( $op !~ /^[<>]=?$/ && ( $cf && ($cf->Type eq 'IPAddress' || $cf->Type eq 'IPAddressRange'))) {
+ elsif ( $op !~ /^[<>]=?$/ && ( $cf && $cf->Type eq 'IPAddressRange')) {
$value =~ /^\s*($RE{net}{IPv4})\s*(?:-\s*($RE{net}{IPv4})\s*)?$/o;
my ($start_ip, $end_ip) = ($1, ($2 || $1));
diff --git a/share/html/Elements/ValidateCustomFields b/share/html/Elements/ValidateCustomFields
index f3a8290..15b14e5 100644
--- a/share/html/Elements/ValidateCustomFields
+++ b/share/html/Elements/ValidateCustomFields
@@ -82,17 +82,27 @@ while ( my $CF = $CustomFields->Next ) {
@values = ('') unless @values;
for my $value( @values ) {
- if ( $value
- && ( $CF->Type eq 'IPAddress' || $CF->Type eq 'IPAddressRange' ) )
- {
- my ( $start_ip, $end_ip ) =
- RT::ObjectCustomFieldValue->ParseIPRange($value);
- unless ( $start_ip && $end_ip ) {
- my $msg =
- loc( "Input can not be parsed to " . loc( $CF->Type ) );
- $m->notes( ( 'InvalidField-' . $CF->Id ) => $msg );
- push @res, $msg;
- $valid = 0;
+ if ($value) {
+ if ( $CF->Type eq 'IPAddress' ) {
+ use Regexp::Common qw(RE_net_IPv4);
+ unless ( $value =~ /^$RE{net}{IPv4}$/ ) {
+ my $msg =
+ loc( "Input can not be parsed to " . loc( $CF->Type ) );
+ $m->notes( ( 'InvalidField-' . $CF->Id ) => $msg );
+ push @res, $msg;
+ $valid = 0;
+ }
+ }
+ elsif ( $CF->Type eq 'IPAddressRange' ) {
+ my ( $start_ip, $end_ip ) =
+ RT::ObjectCustomFieldValue->ParseIPRange($value);
+ unless ( $start_ip && $end_ip ) {
+ my $msg =
+ loc( "Input can not be parsed to " . loc( $CF->Type ) );
+ $m->notes( ( 'InvalidField-' . $CF->Id ) => $msg );
+ push @res, $msg;
+ $valid = 0;
+ }
}
}
commit 32afb860c09e726b06505c730a106cc0a5b0b23b
Author: sunnavy <sunnavy at bestpractical.com>
Date: Mon Sep 13 16:38:10 2010 +0800
IPAddress test
diff --git a/t/customfields/ip.t b/t/customfields/ip.t
new file mode 100644
index 0000000..5781634
--- /dev/null
+++ b/t/customfields/ip.t
@@ -0,0 +1,251 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use RT::Test tests => 64;
+
+my ( $baseurl, $agent ) = RT::Test->started_ok;
+ok( $agent->login, 'log in' );
+
+my $q = RT::Queue->new($RT::SystemUser);
+$q->Load('General');
+my $ip_cf = RT::CustomField->new($RT::SystemUser);
+
+my ( $val, $msg ) = $ip_cf->Create(
+ Name => 'IP',
+ Type => 'IPAddress',
+ LookupType => 'RT::Queue-RT::Ticket'
+);
+ok( $val, $msg );
+my $cf_id = $val;
+$ip_cf->AddToObject($q);
+use_ok('RT');
+
+my $cf;
+diag "load and check basic properties of the IP CF" if $ENV{'TEST_VERBOSE'};
+{
+ my $cfs = RT::CustomFields->new($RT::SystemUser);
+ $cfs->Limit( FIELD => 'Name', VALUE => 'IP' );
+ is( $cfs->Count, 1, "found one CF with name 'IP'" );
+
+ $cf = $cfs->First;
+ is( $cf->Type, 'IPAddress', 'type check' );
+ is( $cf->LookupType, 'RT::Queue-RT::Ticket', 'lookup type check' );
+ ok( !$cf->MaxValues, "unlimited number of values" );
+ ok( !$cf->Disabled, "not disabled" );
+}
+
+diag "check that CF applies to queue General" if $ENV{'TEST_VERBOSE'};
+{
+ my $cfs = $q->TicketCustomFields;
+ $cfs->Limit( FIELD => 'id', VALUE => $cf->id, ENTRYAGGREGATOR => 'AND' );
+ is( $cfs->Count, 1, 'field applies to queue' );
+}
+
+diag "create a ticket via web and set IP" if $ENV{'TEST_VERBOSE'};
+{
+ my $val = '192.168.20.1';
+ ok $agent->goto_create_ticket($q), "go to create ticket";
+ my $cf_field = "Object-RT::Ticket--CustomField-$cf_id-Values";
+ $agent->submit_form(
+ form_name => 'TicketCreate',
+ fields => {
+ Subject => 'test ip',
+ $cf_field => $val,
+ }
+ );
+
+ $agent->content_like( qr/\Q$val/, "IP on the page" );
+ my ($id) = $agent->content =~ /Ticket (\d+) created/;
+ ok( $id, "created ticket $id" );
+
+ my $ticket = RT::Ticket->new($RT::SystemUser);
+ $ticket->Load($id);
+ ok( $ticket->id, 'loaded ticket' );
+ is( $ticket->FirstCustomFieldValue('IP'), $val, 'correct value' );
+}
+
+diag "create a ticket and edit IP field using Edit page"
+ if $ENV{'TEST_VERBOSE'};
+{
+ my $val = '172.16.0.1';
+ ok $agent->goto_create_ticket($q), "go to create ticket";
+ $agent->submit_form(
+ form_name => 'TicketCreate',
+ fields => { Subject => 'test ip', }
+ );
+
+ my ($id) = $agent->content =~ /Ticket (\d+) created/;
+ ok( $id, "created ticket $id" );
+ my $cf_field = "Object-RT::Ticket-$id-CustomField-$cf_id-Values";
+
+ $agent->follow_link_ok( { text => 'Basics', n => "1" },
+ "Followed 'Basics' link" );
+ $agent->form_number(3);
+
+ like( $agent->value($cf_field), qr/^\s*$/, 'IP is empty' );
+ $agent->field( $cf_field => $val );
+ $agent->click('SubmitTicket');
+
+ $agent->content_like( qr/\Q$val/, "IP on the page" );
+
+ my $ticket = RT::Ticket->new($RT::SystemUser);
+ $ticket->Load($id);
+ ok( $ticket->id, 'loaded ticket' );
+ my $values = $ticket->CustomFieldValues('IP');
+ my %has = map { $_->Content => 1 } @{ $values->ItemsArrayRef };
+ is( scalar values %has, 1, "one IP were added" );
+ ok( $has{$val}, "has value" )
+ or diag "but has values " . join ", ", keys %has;
+
+ diag "set IP with spaces around" if $ENV{'TEST_VERBOSE'};
+ $val = " 172.16.0.2 \n ";
+ $agent->follow_link_ok( { text => 'Basics', n => "1" },
+ "Followed 'Basics' link" );
+ $agent->form_number(3);
+ like( $agent->value($cf_field),
+ qr/^\s*\Q172.16.0.1\E\s*$/, 'IP is in input box' );
+ $agent->field( $cf_field => $val );
+ $agent->click('SubmitTicket');
+
+ $agent->content_like( qr/\Q172.16.0.2/, "IP on the page" );
+
+ $ticket = RT::Ticket->new($RT::SystemUser);
+ $ticket->Load($id);
+ ok( $ticket->id, 'loaded ticket' );
+ $values = $ticket->CustomFieldValues('IP');
+ %has = map { $_->Content => 1 } @{ $values->ItemsArrayRef };
+ is( scalar values %has, 1, "one IP were added" );
+ ok( $has{'172.16.0.2'}, "has value" )
+ or diag "but has values " . join ", ", keys %has;
+}
+
+diag "check that we parse correct IPs only" if $ENV{'TEST_VERBOSE'};
+{
+
+ my $cf_field = "Object-RT::Ticket--CustomField-$cf_id-Values";
+ for my $valid (qw/1.0.0.0 255.255.255.255/) {
+ ok $agent->goto_create_ticket($q), "go to create ticket";
+ $agent->submit_form(
+ form_name => 'TicketCreate',
+ fields => {
+ Subject => 'test ip',
+ $cf_field => $valid,
+ }
+ );
+
+ my ($id) = $agent->content =~ /Ticket (\d+) created/;
+ ok( $id, "created ticket $id" );
+ my $ticket = RT::Ticket->new($RT::SystemUser);
+ $ticket->Load($id);
+ is( $ticket->id, $id, 'loaded ticket' );
+
+ my %has = ();
+ $has{ $_->Content }++
+ foreach @{ $ticket->CustomFieldValues('IP')->ItemsArrayRef };
+ is( scalar values %has, 1, "one IP was added" );
+ ok( $has{$valid}, 'correct value' );
+ }
+
+ for my $invalid (qw{255.255.255.256 355.255.255.255 8.13.8/8.13.0/1.0}) {
+ ok $agent->goto_create_ticket($q), "go to create ticket";
+ $agent->submit_form(
+ form_name => 'TicketCreate',
+ fields => {
+ Subject => 'test ip',
+ $cf_field => $invalid,
+ }
+ );
+
+ $agent->content_like( qr/can not be parsed to IPAddress/,
+ 'ticket fails to create' );
+ }
+
+}
+
+diag "search tickets by IP" if $ENV{'TEST_VERBOSE'};
+{
+ my $val = '172.16.1.1';
+ ok $agent->goto_create_ticket($q), "go to create ticket";
+ my $cf_field = "Object-RT::Ticket--CustomField-$cf_id-Values";
+ $agent->submit_form(
+ form_name => 'TicketCreate',
+ fields => {
+ Subject => 'test ip',
+ $cf_field => $val,
+ }
+ );
+
+ my ($id) = $agent->content =~ /Ticket (\d+) created/;
+ ok( $id, "created ticket $id" );
+
+ my $ticket = RT::Ticket->new($RT::SystemUser);
+ $ticket->Load($id);
+ ok( $ticket->id, 'loaded ticket' );
+
+ my $tickets = RT::Tickets->new($RT::SystemUser);
+ $tickets->FromSQL("id = $id AND CF.{IP} = '172.16.1.1'");
+ ok( $tickets->Count, "found tickets" );
+}
+
+diag "create two tickets with different IPs and check several searches"
+ if $ENV{'TEST_VERBOSE'};
+{
+ ok $agent->goto_create_ticket($q), "go to create ticket";
+ my $cf_field = "Object-RT::Ticket--CustomField-$cf_id-Values";
+ $agent->submit_form(
+ form_name => 'TicketCreate',
+ fields => {
+ Subject => 'test ip',
+ $cf_field => '192.168.21.10',
+ }
+ );
+
+ my ($id1) = $agent->content =~ /Ticket (\d+) created/;
+ ok( $id1, "created first ticket $id1" );
+
+ ok $agent->goto_create_ticket($q), "go to create ticket";
+ $agent->submit_form(
+ form_name => 'TicketCreate',
+ fields => {
+ Subject => 'test ip',
+ $cf_field => '192.168.22.10',
+ }
+ );
+
+ my ($id2) = $agent->content =~ /Ticket (\d+) created/;
+ ok( $id2, "created second ticket $id2" );
+
+ my $tickets = RT::Tickets->new($RT::SystemUser);
+ $tickets->FromSQL("id = $id1 OR id = $id2");
+ is( $tickets->Count, 2, "found both tickets by 'id = x OR y'" );
+
+ # IP
+ $tickets = RT::Tickets->new($RT::SystemUser);
+ $tickets->FromSQL("(id = $id1 OR id = $id2) AND CF.{IP} = '192.168.21.10'");
+ is( $tickets->Count, 1, "found one ticket" );
+ is( $tickets->First->FirstCustomFieldValue('IP'),
+ '192.168.21.10', "correct value" );
+ $tickets = RT::Tickets->new($RT::SystemUser);
+ $tickets->FromSQL("(id = $id1 OR id = $id2) AND CF.{IP} = '192.168.22.10'");
+ is( $tickets->Count, 1, "found one ticket" );
+ is( $tickets->First->FirstCustomFieldValue('IP'),
+ '192.168.22.10', "correct value" );
+
+ $tickets->FromSQL("(id = $id1 OR id = $id2) AND CF.{IP} > '192.168.22.10'");
+ is( $tickets->Count, 0, "no tickets found" );
+ $tickets->FromSQL("(id = $id1 OR id = $id2) AND CF.{IP} < '192.168.21.10'");
+ is( $tickets->Count, 0, "no tickets found" );
+
+ $tickets->FromSQL("(id = $id1 OR id = $id2) AND CF.{IP} < '192.168.22.10'");
+ is( $tickets->Count, 1, "found one ticket" );
+ is( $tickets->First->FirstCustomFieldValue('IP'),
+ '192.168.21.10', "correct value" );
+
+ $tickets->FromSQL("(id = $id1 OR id = $id2) AND CF.{IP} > '192.168.21.10'");
+ is( $tickets->Count, 1, "found one ticket" );
+ is( $tickets->First->FirstCustomFieldValue('IP'),
+ '192.168.22.10', "correct value" );
+}
+
-----------------------------------------------------------------------
More information about the Rt-commit
mailing list