[Rt-commit] r6121 - in rtir/branches/2.1-EXPERIMENTAL: . t
ruz at bestpractical.com
ruz at bestpractical.com
Fri Sep 29 13:34:01 EDT 2006
Author: ruz
Date: Fri Sep 29 13:33:59 2006
New Revision: 6121
Modified:
rtir/branches/2.1-EXPERIMENTAL/ (props changed)
rtir/branches/2.1-EXPERIMENTAL/lib/RT/Action/RTIR_FindIP.pm
rtir/branches/2.1-EXPERIMENTAL/t/013-custom-field-ip.t
Log:
r1661 at cubic-pc: cubic | 2006-09-29 21:18:53 +0400
* CIDR in the message shouldn't add duplicates too
* add tests
Modified: rtir/branches/2.1-EXPERIMENTAL/lib/RT/Action/RTIR_FindIP.pm
==============================================================================
--- rtir/branches/2.1-EXPERIMENTAL/lib/RT/Action/RTIR_FindIP.pm (original)
+++ rtir/branches/2.1-EXPERIMENTAL/lib/RT/Action/RTIR_FindIP.pm Fri Sep 29 13:33:59 2006
@@ -29,23 +29,21 @@
my $cf = $ticket->LoadCustomFieldByIdentifier('_RTIR_IP');
return 1 unless $cf && $cf->id;
- my %existing;
- for( @{$cf->ValuesForObject($ticket)->ItemsArrayRef} ) {
- $existing{$_->Content} = 1;
-
- }
my $attach = $self->TransactionObj->ContentObj;
return 1 unless $attach && $attach->id;
+ my %existing;
+ for( @{$cf->ValuesForObject( $ticket )->ItemsArrayRef} ) {
+ $existing{ $_->Content } = 1;
+ }
+
my @IPs = ( $attach->Content =~ /($RE{net}{IPv4})/go );
foreach my $ip ( @IPs ) {
- next if ($existing{$ip}); # skip any IP we already had.
- my ($status, $msg) = $ticket->AddCustomFieldValue(
- Value => $ip,
- Field => $cf,
+ $self->AddIP(
+ IP => $ip,
+ CustomField => $cf,
+ Skip => \%existing,
);
- $RT::Logger->error("Couldn't add CF value: $msg") unless $status;
- $existing{$ip} = 1;
}
my @CIDRs = ( $attach->Content =~ /$RE{net}{CIDR}{IPv4}{-keep}/go );
@@ -55,15 +53,29 @@
my $snum = unpack( 'N', pack( 'C4', split /\./, $sip ) );
my $enum = unpack( 'N', pack( 'C4', split /\./, $eip ) );
while ( $snum++ <= $enum ) {
- my ($status, $msg) = $ticket->AddCustomFieldValue(
- Value => join( '.', unpack( 'C4', pack( 'N', $snum ) ) ),
- Field => $cf,
+ $self->AddIP(
+ IP => join( '.', unpack( 'C4', pack( 'N', $snum ) ) ),
+ CustomField => $cf,
+ Skip => \%existing,
);
- $RT::Logger->error("Couldn't add CF value: $msg") unless $status;
}
}
return 1;
}
+sub AddIP {
+ my $self = shift;
+ my %arg = ( CustomField => undef, IP => undef, Skip => {}, @_ );
+ return if !$arg{'IP'} || $arg{'Skip'}->{ $arg{'IP'} }++;
+
+ my ($status, $msg) = $self->TicketObj->AddCustomFieldValue(
+ Value => $arg{'IP'},
+ Field => $arg{'CustomField'},
+ );
+ $RT::Logger->error("Couldn't add IP address: $msg") unless $status;
+
+ return;
+}
+
1;
Modified: rtir/branches/2.1-EXPERIMENTAL/t/013-custom-field-ip.t
==============================================================================
--- rtir/branches/2.1-EXPERIMENTAL/t/013-custom-field-ip.t (original)
+++ rtir/branches/2.1-EXPERIMENTAL/t/013-custom-field-ip.t Fri Sep 29 13:33:59 2006
@@ -2,7 +2,7 @@
use strict;
use warnings;
-use Test::More tests => 176;
+use Test::More tests => 185;
require "t/rtir-test.pl";
@@ -97,6 +97,25 @@
}
}
+diag "check that IPs in messages don't add duplicates" if $ENV{'TEST_VERBOSE'};
+{
+ my $id = create_ir( $agent, {
+ Subject => "test ip",
+ Content => '192.168.20.2 192.168.20.2 192.168.20/30'
+ } );
+ ok($id, "created first ticket");
+
+ my $ticket = RT::Ticket->new( $RT::SystemUser );
+ $ticket->Load( $id );
+ ok( $ticket->id, 'loaded ticket' );
+
+ my $values = $ticket->CustomFieldValues('_RTIR_IP');
+ my %has;
+ $has{ $_->Content }++ foreach @{ $values->ItemsArrayRef };
+ is(scalar values %has, 4, "four IPs were added");
+ ok(!grep( $_ != 1, values %has), "no duplicated values");
+}
+
diag "create a ticket via web with CIDR in message" if $ENV{'TEST_VERBOSE'};
{
my $i = 0;
More information about the Rt-commit
mailing list