Merge pull request #232 from jaredj/mock-not-fake
Change fake_{config,hook} to mock_{config,hook}
This commit is contained in:
commit
f7558f346d
@ -126,65 +126,65 @@ sub run_plugin_tests {
|
||||
$Test->done_testing();
|
||||
}
|
||||
|
||||
sub fake_hook {
|
||||
sub mock_hook {
|
||||
###########################################################################
|
||||
# Inserts a given subroutine into the beginning of the set of hooks already
|
||||
# in place. Used to test code against different potential plugins it will
|
||||
# interact with. For example, to test behavior against various results of
|
||||
# the data_post hook:
|
||||
#
|
||||
# $self->fake_hook('data_post',sub { return DECLINED };
|
||||
# $self->mock_hook('data_post',sub { return DECLINED };
|
||||
# ok(...);
|
||||
# $self->fake_hook('data_post',sub { return DENYSOFT };
|
||||
# $self->mock_hook('data_post',sub { return DENYSOFT };
|
||||
# ok(...);
|
||||
# $self->fake_hook('data_post',sub { return DENY };
|
||||
# $self->mock_hook('data_post',sub { return DENY };
|
||||
# ok(...);
|
||||
# $self->fake_hook('data_post',sub { return DENY_DISCONNECT };
|
||||
# $self->mock_hook('data_post',sub { return DENY_DISCONNECT };
|
||||
# ok(...);
|
||||
# $self->unfake_hook('data_post');
|
||||
# $self->unmock_hook('data_post');
|
||||
###########################################################################
|
||||
my ( $self, $hook, $sub ) = @_;
|
||||
unshift @{ $self->hooks->{$hook} ||= [] },
|
||||
{
|
||||
name => '___FakeHook___',
|
||||
name => '___MockHook___',
|
||||
code => $sub,
|
||||
};
|
||||
}
|
||||
|
||||
sub unfake_hook {
|
||||
sub unmock_hook {
|
||||
my ( $self, $hook ) = @_;
|
||||
$self->hooks->{$hook} = [
|
||||
grep { $_->{name} ne '___FakeHook___' }
|
||||
grep { $_->{name} ne '___MockHook___' }
|
||||
@{ $self->hooks->{$hook} || [] }
|
||||
];
|
||||
}
|
||||
|
||||
sub fake_config {
|
||||
sub mock_config {
|
||||
####################################################################
|
||||
# Used to test code against various possible configurations
|
||||
# For example, to test against various possible config('me') values:
|
||||
#
|
||||
# $self->fake_config( me => '***invalid***' );
|
||||
# $self->mock_config( me => '***invalid***' );
|
||||
# ok(...);
|
||||
# $self->fake_config( me => 'valid-nonfqdn' );
|
||||
# $self->mock_config( me => 'valid-nonfqdn' );
|
||||
# ok(...);
|
||||
# $self->fake_config( me => 'valid-fqdn.com');
|
||||
# $self->mock_config( me => 'valid-fqdn.com');
|
||||
# ok(...);
|
||||
# $self->unfake_config();
|
||||
# $self->unmock_config();
|
||||
####################################################################
|
||||
my $self = shift;
|
||||
my $fake_config = {@_};
|
||||
$self->fake_hook( 'config',
|
||||
my $mock_config = {@_};
|
||||
$self->mock_hook( 'config',
|
||||
sub {
|
||||
my ( $self, $txn, $conf ) = @_;
|
||||
return DECLINED if ! exists $fake_config->{$conf};
|
||||
return OK, $fake_config->{$conf};
|
||||
return DECLINED if ! exists $mock_config->{$conf};
|
||||
return OK, $mock_config->{$conf};
|
||||
} );
|
||||
}
|
||||
|
||||
sub unfake_config {
|
||||
sub unmock_config {
|
||||
my ( $self ) = @_;
|
||||
$self->unfake_hook('config');
|
||||
$self->unmock_hook('config');
|
||||
}
|
||||
|
||||
1;
|
||||
|
@ -84,9 +84,9 @@ sub validate_password {
|
||||
return $deny, "$file - wrong password";
|
||||
}
|
||||
|
||||
sub fake_hook { shift->qp->fake_hook(@_) }
|
||||
sub unfake_hook { shift->qp->unfake_hook(@_) }
|
||||
sub fake_config { shift->qp->fake_config(@_) }
|
||||
sub unfake_config { shift->qp->unfake_config(@_) }
|
||||
sub mock_hook { shift->qp->mock_hook(@_) }
|
||||
sub unmock_hook { shift->qp->unmock_hook(@_) }
|
||||
sub mock_config { shift->qp->mock_config(@_) }
|
||||
sub unmock_config { shift->qp->unmock_config(@_) }
|
||||
|
||||
1;
|
||||
|
@ -16,11 +16,11 @@ sub test_content_log_file {
|
||||
is( $self->content_log_file,
|
||||
strftime('mail/%Y%m%d', localtime(time)),
|
||||
'content_log_file() returns the right default value' );
|
||||
$self->fake_config( content_log_file => '/path/to/%Y%m%d%H%M' );
|
||||
$self->mock_config( content_log_file => '/path/to/%Y%m%d%H%M' );
|
||||
is( $self->content_log_file,
|
||||
strftime('/path/to/%Y%m%d%H%M', localtime(time)),
|
||||
'content_log_file config changes content_log_file() output' );
|
||||
$self->unfake_config;
|
||||
$self->unmock_config;
|
||||
}
|
||||
|
||||
sub test_content_log_enabled {
|
||||
@ -69,10 +69,10 @@ sub test_content_log_enabled {
|
||||
. ( defined $_->{config } ? "'$_->{config }'" : 'undef' )
|
||||
. ( $_->{expected} ? ' enables' : ' disables' )
|
||||
. ' content logging';
|
||||
$self->fake_config( content_log_enabled => $_->{config } );
|
||||
$self->mock_config( content_log_enabled => $_->{config } );
|
||||
is( $self->content_log_enabled, $_->{expected}, $descr );
|
||||
}
|
||||
$self->unfake_config;
|
||||
$self->unmock_config;
|
||||
}
|
||||
|
||||
sub test_exclude {
|
||||
|
@ -66,7 +66,7 @@ sub test_register_genre_blocking {
|
||||
'rcpt_handler not registered when no blocked genres are configured' );
|
||||
|
||||
$self->register_hook();
|
||||
$self->fake_config( p0f_blocked_operating_systems => 'Crapple Macintawsh' );
|
||||
$self->mock_config( p0f_blocked_operating_systems => 'Crapple Macintawsh' );
|
||||
$self->register_genre_blocking();
|
||||
is( $self->{_lastreg}, 'rcpt,rcpt_handler',
|
||||
'rcpt_handler registered when blocked genre phrases are configured' );
|
||||
@ -76,7 +76,7 @@ sub test_register_genre_blocking {
|
||||
'no blocked OS regexes' );
|
||||
|
||||
$self->register_hook();
|
||||
$self->fake_config( p0f_blocked_operating_systems => '/windoze/' );
|
||||
$self->mock_config( p0f_blocked_operating_systems => '/windoze/' );
|
||||
$self->register_genre_blocking();
|
||||
is( $self->{_lastreg}, 'rcpt,rcpt_handler',
|
||||
'rcpt_handler registered when blocked genre regexes are configured' );
|
||||
@ -85,7 +85,7 @@ sub test_register_genre_blocking {
|
||||
is( join('|', @{ delete $self->{os_block_re} || [] }), qr/windoze/i,
|
||||
'blocked OS regexes processed correctly' );
|
||||
|
||||
$self->unfake_config;
|
||||
$self->unmock_config;
|
||||
}
|
||||
|
||||
sub test_rcpt_handler {
|
||||
|
@ -176,11 +176,11 @@ sub __config {
|
||||
},
|
||||
);
|
||||
for (@test_data) {
|
||||
$qp->fake_hook( 'user_config', sub { return @{$_->{result}} } )
|
||||
$qp->mock_hook( 'user_config', sub { return @{$_->{result}} } )
|
||||
if $_->{result};
|
||||
is($sender->config($_->{pref}), $_->{expected}, $_->{descr});
|
||||
}
|
||||
$qp->unfake_hook('user_config');
|
||||
$qp->unmock_hook('user_config');
|
||||
}
|
||||
|
||||
sub __canonify {
|
||||
|
@ -22,7 +22,7 @@ sub __db {
|
||||
is( $db->{name}, 'testfoo', 'accepts name argument' );
|
||||
delete $plugin->{db};
|
||||
$db = $plugin->db( class => 'FakeDB' );
|
||||
is( $db->{name}, '___FakeHook___', 'db name defaults to plugin name' );
|
||||
is( $db->{name}, '___MockHook___', 'db name defaults to plugin name' );
|
||||
}
|
||||
|
||||
sub __register_hook {
|
||||
@ -30,25 +30,25 @@ sub __register_hook {
|
||||
my $plugin = FakePlugin->new;
|
||||
$plugin->register_hook('bogus_hook');
|
||||
};
|
||||
ok( $@ =~ /^___FakeHook___: Invalid hook: bogus_hook/,
|
||||
ok( $@ =~ /^___MockHook___: Invalid hook: bogus_hook/,
|
||||
'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] );
|
||||
is( $registered->{name}, '___FakeHook___',
|
||||
is( $registered->{name}, '___MockHook___',
|
||||
'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' );
|
||||
$qp->unfake_hook('logging');
|
||||
$qp->unmock_hook('logging');
|
||||
}
|
||||
|
||||
package FakePlugin;
|
||||
use parent 'Qpsmtpd::Plugin';
|
||||
sub plugin_name { '___FakeHook___' }
|
||||
sub plugin_name { '___MockHook___' }
|
||||
|
||||
package FakeDB;
|
||||
sub new {
|
||||
|
@ -158,8 +158,8 @@ sub __data_respond {
|
||||
# data_respond also runs the data_post hooks, so this will require a bit
|
||||
# more work to get under test. we also don't yet have a way to mock
|
||||
# message data; that will probably require overriding getline()
|
||||
#$smtpd->fake_data( _test_message() );
|
||||
#$smtpd->fake_hook( data_post => sub { return DECLINED } );
|
||||
#$smtpd->mock_data( _test_message() );
|
||||
#$smtpd->mock_hook( data_post => sub { return DECLINED } );
|
||||
#is( $smtpd->data_respond(DECLINED), 1, 'data_respond, DECLINED' );
|
||||
}
|
||||
|
||||
|
22
t/qpsmtpd.t
22
t/qpsmtpd.t
@ -111,13 +111,13 @@ sub __run_continuation {
|
||||
|
||||
my @local_hooks = @{ $smtpd->hooks->{connect} };
|
||||
$smtpd->{_continuation} = ['connect', [DECLINED, "test mess"], @local_hooks];
|
||||
$smtpd->fake_config( 'smtpgreeting', 'Hello there ESMTP' );
|
||||
$smtpd->mock_config( 'smtpgreeting', 'Hello there ESMTP' );
|
||||
|
||||
eval { $r = $smtpd->run_continuation };
|
||||
ok(!$@, "run_continuation with a continuation doesn't throw exception");
|
||||
is($r->[0], 220, "hook_responder, code");
|
||||
is($r->[1], 'Hello there ESMTP', "hook_responder, message");
|
||||
$smtpd->unfake_config;
|
||||
$smtpd->unmock_config;
|
||||
|
||||
my @test_data = (
|
||||
{
|
||||
@ -154,21 +154,21 @@ sub __run_continuation {
|
||||
hooks => [ [123456,undef], [DENY, 'goaway'] ],
|
||||
expected_response => '550/goaway',
|
||||
disconnected => 0,
|
||||
logged => 'LOGERROR:Plugin ___FakeHook___, hook helo returned 123456',
|
||||
logged => 'LOGERROR:Plugin ___MockHook___, hook helo returned 123456',
|
||||
descr => 'INVALID -> DENY',
|
||||
},
|
||||
{
|
||||
hooks => [ sub { die "dead\n" }, [DENY, 'begone'] ],
|
||||
expected_response => '550/begone',
|
||||
disconnected => 0,
|
||||
logged => 'LOGCRIT:FATAL PLUGIN ERROR [___FakeHook___]: dead',
|
||||
logged => 'LOGCRIT:FATAL PLUGIN ERROR [___MockHook___]: dead',
|
||||
descr => 'fatal error -> DENY',
|
||||
},
|
||||
{
|
||||
hooks => [ [undef], [DENY, 'nm'] ],
|
||||
expected_response => '550/nm',
|
||||
disconnected => 0,
|
||||
logged => 'LOGERROR:Plugin ___FakeHook___, hook helo returned undef!',
|
||||
logged => 'LOGERROR:Plugin ___MockHook___, hook helo returned undef!',
|
||||
descr => 'undef -> DENY',
|
||||
},
|
||||
{
|
||||
@ -180,10 +180,10 @@ sub __run_continuation {
|
||||
},
|
||||
);
|
||||
for my $t (@test_data) {
|
||||
$smtpd->fake_config( me => 'fqdn' );
|
||||
$smtpd->mock_config( me => 'fqdn' );
|
||||
for my $h ( reverse @{ $t->{hooks} } ) {
|
||||
my $sub = ( ref $h eq 'ARRAY' ? sub { return @$h } : $h );
|
||||
$smtpd->fake_hook( 'helo', $sub );
|
||||
$smtpd->mock_hook( 'helo', $sub );
|
||||
}
|
||||
$smtpd->{_continuation} = [ 'helo', ['somearg'], @{ $smtpd->hooks->{helo} } ];
|
||||
delete $smtpd->{_response};
|
||||
@ -205,8 +205,8 @@ sub __run_continuation {
|
||||
is( join("\n", @{ $smtpd->{_logged} || [] }), $t->{logged},
|
||||
"run_continuation() logging on $t->{descr}" );
|
||||
}
|
||||
$smtpd->unfake_hook('helo');
|
||||
$smtpd->unfake_config();
|
||||
$smtpd->unmock_hook('helo');
|
||||
$smtpd->unmock_config();
|
||||
}
|
||||
}
|
||||
|
||||
@ -431,7 +431,7 @@ sub __config {
|
||||
);
|
||||
for my $t (@test_data) {
|
||||
for my $hook ( grep { $t->{hooks}{$_} } qw( config user_config ) ) {
|
||||
$qp->fake_hook( $hook, sub { return @{ $t->{hooks}{$hook} } } );
|
||||
$qp->mock_hook( $hook, sub { return @{ $t->{hooks}{$hook} } } );
|
||||
}
|
||||
is(
|
||||
$qp->config($t->{pref}, $a),
|
||||
@ -442,7 +442,7 @@ sub __config {
|
||||
$t->{expected}{global},
|
||||
"Global config: $t->{descr}");
|
||||
}
|
||||
$qp->unfake_hook($_) for qw( config user_config );
|
||||
$qp->unmock_hook($_) for qw( config user_config );
|
||||
}
|
||||
|
||||
1;
|
||||
|
Loading…
Reference in New Issue
Block a user