Skip to content

Commit

Permalink
Add more specific checking to Config::_validate sub
Browse files Browse the repository at this point in the history
This commit adds better checking to the Config::_validate as it
validates bool values on the configuration.  Tests were
additionally added and updated to correctly validate the possible
values, specifically 0 or 1 (and test incorrect values).
  • Loading branch information
renderorange committed Jul 2, 2024
1 parent 234d202 commit e2c2e27
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 11 deletions.
12 changes: 4 additions & 8 deletions lib/Pasteburn/Config.pm
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ sub _validate {
}
}

# verify secret age exists and is a positive value
unless ( exists $config->{secret}{age} ) {
die "config section secret age is required\n";
}
Expand All @@ -60,27 +59,24 @@ sub _validate {
die "config section secret age must be a positive integer\n";
}

# verify secret scrub exists
unless ( exists $config->{secret}{scrub} ) {
unless ( exists $config->{secret}{scrub} && ( $config->{secret}{scrub} == 1 || $config->{secret}{scrub} == 0 ) ) {
die "config section secret scrub is required\n";
}

# verify passphrase allow_blank exists
unless ( exists $config->{passphrase}{allow_blank} ) {
unless ( exists $config->{passphrase}{allow_blank} && ( $config->{passphrase}{allow_blank} == 1 || $config->{passphrase}{allow_blank} == 0 ) ) {
die "config section passphrase allow_blank is required\n";
}

# verify cookie secret_key is set and isn't the default string in the example config
unless ( exists $config->{cookie}{secret_key} && $config->{cookie}{secret_key} ) {
die "config section cookie secret_key is required\n";
}

# verify cookie secret_key isn't the default string in the example config
if ( $config->{cookie}{secret_key} eq 'default' ) {
die "config section cookie secret_key is the default string and must be updated\n";
}

# verify footer links exists
unless ( exists $config->{footer}{links} ) {
unless ( exists $config->{footer}{links} && ( $config->{footer}{links} == 1 || $config->{footer}{links} == 0 ) ) {
die "config section footer links is required\n";
}

Expand Down
57 changes: 54 additions & 3 deletions t/unit/lib-Pasteburn-Config/_validate.t
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ EXCEPTIONS: {
subtest 'dies if missing any of the config keys' => sub {
plan tests => 4;

foreach my $required ( keys %{ $config_expected } ) {
foreach my $required ( sort keys %{ $config_expected } ) {
my $stored = delete $config_expected->{ $required };

dies_ok { Pasteburn::Config::_validate( $config_expected ) }
Expand All @@ -50,8 +50,8 @@ EXCEPTIONS: {
subtest 'dies if missing any of the config sub keys' => sub {
plan tests => 5;

foreach my $required ( keys %{ $config_expected } ) {
foreach my $required_sub_key ( keys %{ $config_expected->{$required} } ) {
foreach my $required ( sort keys %{ $config_expected } ) {
foreach my $required_sub_key ( sort keys %{ $config_expected->{$required} } ) {
my $stored = delete $config_expected->{$required}{$required_sub_key};

dies_ok { Pasteburn::Config::_validate( $config_expected ) }
Expand Down Expand Up @@ -86,6 +86,57 @@ EXCEPTIONS: {

$config_expected->{cookie}{secret_key} = $stored;
};

subtest 'dies if secret_key is empty string' => sub {
plan tests => 1;

my $stored = delete $config_expected->{cookie}{secret_key};
$config_expected->{cookie}{secret_key} = '';

dies_ok { Pasteburn::Config::_validate( $config_expected ) }
"dies if cookie secret_key is default empty string";

$config_expected->{cookie}{secret_key} = $stored;
};

subtest 'dies if secret scrub does not validate value' => sub {
plan tests => 3;

my $secret_scrub = $config_expected->{secret}{scrub};
foreach my $value ( qw{ -1 2 a } ) {
$config_expected->{secret}{scrub} = $value;
dies_ok { Pasteburn::Config::_validate( $config_expected ) }
"dies if secret scrub is $value";

$config_expected->{secret}{scrub} = $secret_scrub;
}
};

subtest 'dies if passphrase allow_blank does not validate value' => sub {
plan tests => 3;

my $allow_blank = $config_expected->{passphrase}{allow_blank};
foreach my $value ( qw{ -1 2 a } ) {
$config_expected->{passphrase}{allow_blank} = $value;
dies_ok { Pasteburn::Config::_validate( $config_expected ) }
"dies if passphrase allow_blank is $value";

$config_expected->{passphrase}{allow_blank} = $allow_blank;
}
};

subtest 'dies if footer links does not validate value' => sub {
plan tests => 3;

my $links = $config_expected->{footer}{links};
foreach my $value ( qw{ -1 2 a } ) {
$config_expected->{footer}{links} = $value;
dies_ok { Pasteburn::Config::_validate( $config_expected ) }
"dies if footer links is $value";

$config_expected->{footer}{links} = $links;
}
};
}

done_testing;

0 comments on commit e2c2e27

Please sign in to comment.