qpsmtpd/t/plugin_tests/ident/geoip

58 lines
1.9 KiB
Plaintext
Raw Normal View History

#!perl -w
use strict;
use warnings;
2014-11-04 23:43:54 +01:00
use lib 'lib';
use Qpsmtpd::Constants;
sub register_tests {
my $self = shift;
eval 'use GeoIP2::Database::Reader';
if ( !$@ ) {
$self->register_test('test_geoip2_lookup');
}
}
sub test_geoip2_lookup {
my $self = shift;
$self->qp->connection->remote_ip('24.24.24.24');
2014-11-05 22:50:19 +01:00
cmp_ok( $self->geoip2_lookup(), '==', DECLINED, "exit code DECLINED");
2014-11-05 22:50:19 +01:00
if (!$self->load_geoip2()) {
warn "failed to load GeoIP2\n";
}
cmp_ok( $self->connection->notes('geoip_country'), 'eq', 'US', "24.24.24.24 is in country US");
cmp_ok( $self->connection->notes('geoip_country_name'), 'eq', 'United States', "24.24.24.24 is in country United States");
cmp_ok( $self->connection->notes('geoip_continent'), 'eq', 'NA', "24.24.24.24 is in continent NA");
cmp_ok( $self->connection->notes('geoip_city'), 'eq', 'Syracuse', "24.24.24.24 is in city of Syracuse");
}
2014-11-13 20:52:49 +01:00
sub test_add_headers {
my ( $self ) = @_;
my @notes = qw( geoip_country geoip_continent geoip_city geoip_asn );
$self->connection->notes( $_ => "test $_" ) for @notes;
my $header = $self->transaction->header( Mail::Header->new );
my @tags = (qw( X-GeoIP-Country X-GeoIP-Continent X-GeoIP-City X-GeoIP-ASN ));
$header->add( $_ => 'DELETETHIS' ) for @tags;
$self->add_headers($self->transaction);
is( $self->all_headers('X-GeoIP-Country'), 'test geoip_country',
'X-GeoIP-Country header added' );
is( $self->all_headers('X-GeoIP-Continent'), 'test geoip_continent',
'X-GeoIP-Continent header added' );
is( $self->all_headers('X-GeoIP-City'), 'test geoip_city',
'X-GeoIP-City header added' );
is( $self->all_headers('X-GeoIP-ASN'), 'test geoip_asn',
'X-GeoIP-ASN header added' );
}
sub all_headers {
# Return all instances of a given message header
my ( $self, $tag ) = @_;
return join " | ", map { chomp $_; $_ } $self->transaction->header->get($tag);
}