Add some validation for passed db args
This commit is contained in:
parent
51ca3fcda4
commit
42c551944e
@ -349,6 +349,7 @@ sub _register_standard_hooks {
|
||||
|
||||
sub db_args {
|
||||
my ( $self, %arg ) = @_;
|
||||
$self->validate_db_args(@_);
|
||||
$self->{db_args} = \%arg if %arg;
|
||||
$self->{db_args}{name} ||= $self->plugin_name;
|
||||
return %{ $self->{db_args} };
|
||||
@ -356,7 +357,13 @@ sub db_args {
|
||||
|
||||
sub db {
|
||||
my ( $self, %arg ) = @_;
|
||||
$self->validate_db_args(@_);
|
||||
return $self->{db} ||= Qpsmtpd::DB->new( $self->db_args(%arg) );
|
||||
}
|
||||
|
||||
sub validate_db_args {
|
||||
(my $self, undef, my @args) = @_;
|
||||
die "Invalid db arguments\n" if @args % 2;
|
||||
}
|
||||
|
||||
1;
|
||||
|
@ -9,12 +9,21 @@ use Test::Qpsmtpd;
|
||||
|
||||
use_ok('Qpsmtpd::Plugin');
|
||||
|
||||
__validate_db_args();
|
||||
__db_args();
|
||||
__db();
|
||||
__register_hook();
|
||||
|
||||
done_testing();
|
||||
|
||||
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' );
|
||||
}
|
||||
|
||||
sub __db_args {
|
||||
my $plugin = FakePlugin->new;
|
||||
is( keyvals($plugin->db_args),
|
||||
|
Loading…
Reference in New Issue
Block a user