From de742dc95e6a69bd45de44dbc428db4181a01ea1 Mon Sep 17 00:00:00 2001 From: Jared Johnson Date: Mon, 15 Sep 2014 10:40:06 -0500 Subject: [PATCH] Add tests for user_config plugin --- t/plugin_tests/user_config | 50 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 t/plugin_tests/user_config diff --git a/t/plugin_tests/user_config b/t/plugin_tests/user_config new file mode 100644 index 0000000..48d2547 --- /dev/null +++ b/t/plugin_tests/user_config @@ -0,0 +1,50 @@ +#!perl -w + +use strict; +use warnings; +use File::Path; +use Qpsmtpd::Constants; + +BEGIN { # need this to happen before anything else + my $cwd = `pwd`; + chomp($cwd); + open my $spooldir, '>', "./config.sample/spool_dir"; + print $spooldir "$cwd/t/tmp"; + close $spooldir; +} + +sub register_tests { + my ($self) = @_; + $self->register_test('test_hook_user_config', 1); +} + +sub test_hook_user_config { + my ( $self ) = @_; + my $dirname = $self->qp->temp_dir; + $self->{pattern} = $dirname . '/%u_%h_%a'; + $dirname .= '/testuser_testhost_testaddress'; + -d $dirname + or mkdir($dirname, 0700) + or die "Could not create $dirname: $!"; + open my $fh, '>', "$dirname/testfield"; + print $fh "testdata"; + close $fh; + my $a = FakeAddress->new( user => 'testuser', host => 'testhost', address => 'testaddress' ); + my ( $r, $value ) = $self->hook_user_config( $self->qp->transaction, $a, 'testfield' ); + is( $r, OK, 'hook_user_config returned OK when config file present' ); + is( $value, 'testdata', 'hook_user_config returned the correct value' ); + ( $r, $value ) = $self->hook_user_config( $self->qp->transaction, $a, 'noconfig' ); + is( $r, DECLINED, 'hook_user_config returned DECLINED when no config file present' ); + is( $value, undef, 'hook_user_config returned no value when no config file present' ); + rmtree($dirname); +} + +package FakeAddress; + +sub new { + shift; + return bless {@_}; +} +sub address { return shift->{address} } +sub user { return shift->{user} } +sub host { return shift->{host} }