Skip to content

Commit e2c2e27

Browse files
committed
Add more specific checking to Config::_validate sub
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).
1 parent 234d202 commit e2c2e27

File tree

2 files changed

+58
-11
lines changed

2 files changed

+58
-11
lines changed

lib/Pasteburn/Config.pm

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ sub _validate {
5151
}
5252
}
5353

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

63-
# verify secret scrub exists
64-
unless ( exists $config->{secret}{scrub} ) {
62+
unless ( exists $config->{secret}{scrub} && ( $config->{secret}{scrub} == 1 || $config->{secret}{scrub} == 0 ) ) {
6563
die "config section secret scrub is required\n";
6664
}
6765

68-
# verify passphrase allow_blank exists
69-
unless ( exists $config->{passphrase}{allow_blank} ) {
66+
unless ( exists $config->{passphrase}{allow_blank} && ( $config->{passphrase}{allow_blank} == 1 || $config->{passphrase}{allow_blank} == 0 ) ) {
7067
die "config section passphrase allow_blank is required\n";
7168
}
7269

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

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

82-
# verify footer links exists
83-
unless ( exists $config->{footer}{links} ) {
79+
unless ( exists $config->{footer}{links} && ( $config->{footer}{links} == 1 || $config->{footer}{links} == 0 ) ) {
8480
die "config section footer links is required\n";
8581
}
8682

t/unit/lib-Pasteburn-Config/_validate.t

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ EXCEPTIONS: {
3737
subtest 'dies if missing any of the config keys' => sub {
3838
plan tests => 4;
3939

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

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

53-
foreach my $required ( keys %{ $config_expected } ) {
54-
foreach my $required_sub_key ( keys %{ $config_expected->{$required} } ) {
53+
foreach my $required ( sort keys %{ $config_expected } ) {
54+
foreach my $required_sub_key ( sort keys %{ $config_expected->{$required} } ) {
5555
my $stored = delete $config_expected->{$required}{$required_sub_key};
5656

5757
dies_ok { Pasteburn::Config::_validate( $config_expected ) }
@@ -86,6 +86,57 @@ EXCEPTIONS: {
8686

8787
$config_expected->{cookie}{secret_key} = $stored;
8888
};
89+
90+
subtest 'dies if secret_key is empty string' => sub {
91+
plan tests => 1;
92+
93+
my $stored = delete $config_expected->{cookie}{secret_key};
94+
$config_expected->{cookie}{secret_key} = '';
95+
96+
dies_ok { Pasteburn::Config::_validate( $config_expected ) }
97+
"dies if cookie secret_key is default empty string";
98+
99+
$config_expected->{cookie}{secret_key} = $stored;
100+
};
101+
102+
subtest 'dies if secret scrub does not validate value' => sub {
103+
plan tests => 3;
104+
105+
my $secret_scrub = $config_expected->{secret}{scrub};
106+
foreach my $value ( qw{ -1 2 a } ) {
107+
$config_expected->{secret}{scrub} = $value;
108+
dies_ok { Pasteburn::Config::_validate( $config_expected ) }
109+
"dies if secret scrub is $value";
110+
111+
$config_expected->{secret}{scrub} = $secret_scrub;
112+
}
113+
};
114+
115+
subtest 'dies if passphrase allow_blank does not validate value' => sub {
116+
plan tests => 3;
117+
118+
my $allow_blank = $config_expected->{passphrase}{allow_blank};
119+
foreach my $value ( qw{ -1 2 a } ) {
120+
$config_expected->{passphrase}{allow_blank} = $value;
121+
dies_ok { Pasteburn::Config::_validate( $config_expected ) }
122+
"dies if passphrase allow_blank is $value";
123+
124+
$config_expected->{passphrase}{allow_blank} = $allow_blank;
125+
}
126+
};
127+
128+
subtest 'dies if footer links does not validate value' => sub {
129+
plan tests => 3;
130+
131+
my $links = $config_expected->{footer}{links};
132+
foreach my $value ( qw{ -1 2 a } ) {
133+
$config_expected->{footer}{links} = $value;
134+
dies_ok { Pasteburn::Config::_validate( $config_expected ) }
135+
"dies if footer links is $value";
136+
137+
$config_expected->{footer}{links} = $links;
138+
}
139+
};
89140
}
90141

91142
done_testing;

0 commit comments

Comments
 (0)