2014-12-26 07:52:17 +01:00
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
|
|
|
use Test::More;
|
|
|
|
|
|
|
|
use lib 'lib'; # test lib/Qpsmtpd (vs site_perl)
|
|
|
|
use lib 't';
|
2015-01-29 00:14:45 +01:00
|
|
|
use Test::Qpsmtpd;
|
2014-12-26 07:52:17 +01:00
|
|
|
|
|
|
|
use_ok('Qpsmtpd::Plugin');
|
|
|
|
|
2015-02-23 21:13:39 +01:00
|
|
|
__validate_db_args();
|
2015-02-18 17:25:22 +01:00
|
|
|
__db_args();
|
2014-12-26 07:52:17 +01:00
|
|
|
__db();
|
2015-01-29 00:14:45 +01:00
|
|
|
__register_hook();
|
2014-12-26 07:52:17 +01:00
|
|
|
|
|
|
|
done_testing();
|
|
|
|
|
2015-02-23 21:13:39 +01:00
|
|
|
sub __validate_db_args {
|
|
|
|
my $plugin = FakePlugin->new;
|
|
|
|
eval { $plugin->validate_db_args($plugin, testkey => 1) };
|
|
|
|
is( $@, '', 'validate_db_args() does not die on valid data' );
|
|
|
|
eval { $plugin->validate_db_args($plugin, 'bogus') };
|
|
|
|
is( $@, "Invalid db arguments\n", 'validate_db_args() dies on invalid data' );
|
|
|
|
}
|
|
|
|
|
2015-02-18 17:25:22 +01:00
|
|
|
sub __db_args {
|
|
|
|
my $plugin = FakePlugin->new;
|
|
|
|
is( keyvals($plugin->db_args),
|
2015-02-23 21:32:42 +01:00
|
|
|
'cnx_timeout=1;name=___MockHook___',
|
2015-02-18 17:25:22 +01:00
|
|
|
'default db args populated' );
|
|
|
|
is( keyvals($plugin->db_args( arg1 => 1 )),
|
2015-02-23 21:32:42 +01:00
|
|
|
'arg1=1;cnx_timeout=1;name=___MockHook___',
|
2015-02-18 17:25:22 +01:00
|
|
|
'passed args in addition to defaults' );
|
|
|
|
is( keyvals($plugin->db_args( name => 'bob', arg2 => 2 )),
|
2015-02-23 21:32:42 +01:00
|
|
|
'arg2=2;cnx_timeout=1;name=bob',
|
2015-02-18 17:25:22 +01:00
|
|
|
'passed args override defaults' );
|
|
|
|
is( keyvals($plugin->db_args),
|
2015-02-23 21:32:42 +01:00
|
|
|
'arg2=2;cnx_timeout=1;name=bob',
|
2015-02-18 17:25:22 +01:00
|
|
|
'get previous args' );
|
|
|
|
}
|
|
|
|
|
|
|
|
sub keyvals {
|
|
|
|
my ( %h ) = @_;
|
|
|
|
return join ";", map { "$_=$h{$_}" } sort keys %h;
|
|
|
|
}
|
|
|
|
|
2014-12-26 07:52:17 +01:00
|
|
|
sub __db {
|
|
|
|
my $plugin = FakePlugin->new;
|
|
|
|
my $db = $plugin->db( class => 'FakeDB', name => 'testfoo' );
|
|
|
|
is( ref $db, 'FakeDB', 'Qpsmtpd::Plugin::db(): Returns DB object' );
|
|
|
|
is( ref $plugin->{db}, 'FakeDB', 'DB object is cached' );
|
|
|
|
is( $db->{name}, 'testfoo', 'accepts name argument' );
|
|
|
|
delete $plugin->{db};
|
|
|
|
$db = $plugin->db( class => 'FakeDB' );
|
2015-02-12 23:19:44 +01:00
|
|
|
is( $db->{name}, '___MockHook___', 'db name defaults to plugin name' );
|
2015-01-29 00:14:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub __register_hook {
|
|
|
|
eval {
|
|
|
|
my $plugin = FakePlugin->new;
|
|
|
|
$plugin->register_hook('bogus_hook');
|
|
|
|
};
|
2015-02-12 23:19:44 +01:00
|
|
|
ok( $@ =~ /^___MockHook___: Invalid hook: bogus_hook/,
|
2015-01-29 00:14:45 +01:00
|
|
|
'register_hook() validates hook name' );
|
|
|
|
my $qp = Test::Qpsmtpd->new;
|
|
|
|
my $plugin = FakePlugin->new;
|
|
|
|
$plugin->{_qp} = $qp;
|
|
|
|
$plugin->register_hook('logging', sub { shift; return "arguments:@_" });
|
|
|
|
ok( my $registered = $qp->hooks->{logging}->[-1] );
|
2015-02-12 23:19:44 +01:00
|
|
|
is( $registered->{name}, '___MockHook___',
|
2015-01-29 00:14:45 +01:00
|
|
|
'register_hook() sets plugin name' );
|
|
|
|
my $code = $registered->{code};
|
|
|
|
is( ref $code, 'CODE', 'register_hook() sets a coderef' );
|
|
|
|
is( join('',$code->(undef,qw[arg1 arg2])), 'arguments:arg1 arg2',
|
|
|
|
'register_hook(): coderef set correctly' );
|
2015-02-12 23:19:44 +01:00
|
|
|
$qp->unmock_hook('logging');
|
2014-12-26 07:52:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
package FakePlugin;
|
|
|
|
use parent 'Qpsmtpd::Plugin';
|
2015-02-12 23:19:44 +01:00
|
|
|
sub plugin_name { '___MockHook___' }
|
2014-12-26 07:52:17 +01:00
|
|
|
|
|
|
|
package FakeDB;
|
|
|
|
sub new {
|
|
|
|
my $class = shift;
|
|
|
|
return bless {@_}, $class;
|
|
|
|
}
|