Using global constants turned not to be a good idea, some more tests.

This commit is contained in:
sergiotarxz 2021-06-05 15:25:02 +02:00
parent 33431ec290
commit 9f347030e3
Signed by: sergiotarxz
GPG Key ID: E5903508B6510AC2
6 changed files with 101 additions and 20 deletions

View File

@ -12,3 +12,4 @@ requires 'DateTime';
requires 'DateTime::Format::ISO8601';
requires 'Test::Most';
requires 'Test::MockModule';
requires 'Test::Warnings';

View File

@ -13,10 +13,8 @@ use Mojo::File;
use Const::Fast;
use Params::ValidationCompiler 'validation_for';
use BeastBB::Constants (
'$HOME_DIR', '$HOME_CONFIG_DIR', '$CONFIG_FILE',
'$SECRET_DEFAULT_SIZE'
);
use BeastBB::Constants ( 'HomeConfigDir', 'ConfigFile',
'SecretDefaultSize' );
use BeastBB::ConfigWriter;
use BeastBB::Database;
use BeastBB::Response;
@ -53,7 +51,8 @@ sub PrepareHelpers {
logged_user => sub {
my $self = shift;
my $session = $self->session;
return BeastBB::Response->new( is_error => 1, error_message => 'User is not logged in.');
return BeastBB::Response->new( is_error => 1,
error_message => 'User is not logged in.' );
my $username = $session->{username};
}
);
@ -107,13 +106,13 @@ sub PrepareInstallationRoutes {
sub PrepareConfig {
my $self = shift;
$HOME_CONFIG_DIR->make_path if !-e $HOME_CONFIG_DIR;
if ( !-e $CONFIG_FILE ) {
HomeConfigDir()->make_path if !-e HomeConfigDir();
if ( !-e ConfigFile() ) {
my $config_writer =
BeastBB::ConfigWriter->new( config_file => $CONFIG_FILE );
BeastBB::ConfigWriter->new( config_file => ConfigFile() );
$config_writer->PopulateExampleConfigFile(
secret_size => $SECRET_DEFAULT_SIZE );
secret_size => SecretDefaultSize() );
}
$self->plugin( Config => file => $CONFIG_FILE->to_string );
$self->plugin( Config => file => ConfigFile()->to_string );
}
1;

View File

@ -9,10 +9,18 @@ use Exporter qw/import/;
use Const::Fast;
our @EXPORT_OK =
( '$HOME_DIR', '$HOME_CONFIG_DIR', '$CONFIG_FILE', '$SECRET_DEFAULT_SIZE' );
( '&HomeDir', '&HomeConfigDir', '&ConfigFile', '&SecretDefaultSize' );
const our $HOME_DIR => Mojo::File->new( $ENV{HOME} );
const our $HOME_CONFIG_DIR => $HOME_DIR->child( '.config', 'beastbb' );
const our $CONFIG_FILE => $HOME_CONFIG_DIR->child('BeastBB.conf');
const our $SECRET_DEFAULT_SIZE => 64;
sub HomeDir {
return Mojo::File->new( $ENV{HOME} );
}
sub HomeConfigDir {
return HomeDir->child( '.config', 'beastbb' );
}
sub ConfigFile {
return HomeConfigDir->child('BeastBB.conf');
}
sub SecretDefaultSize {
return 64;
}
1;

View File

@ -14,7 +14,7 @@ use Types::Standard qw/Str HashRef/;
use BeastBB::Database;
use BeastBB::ConfigWriter;
use BeastBB::DAO::UserManager;
use BeastBB::Constants ('$CONFIG_FILE');
use BeastBB::Constants ('ConfigFile');
sub welcome {
my $self = shift;
@ -64,7 +64,7 @@ sub install_database {
$self->config->{db} = $config_db;
local $Data::Dumper::Terse = 1;
my $config_writer =
BeastBB::ConfigWriter->new( config_file => $CONFIG_FILE );
BeastBB::ConfigWriter->new( config_file => ConfigFile() );
$config_writer->WriteToConfigFile(
content => Data::Dumper::Dumper $self->config );
$self->redirect_to('/');
@ -100,7 +100,7 @@ sub admin_user_create {
}
my $config_writer =
BeastBB::ConfigWriter->new( config_file => $CONFIG_FILE );
BeastBB::ConfigWriter->new( config_file => ConfigFile );
$config->{finished_install} = 1;
local $Data::Dumper::Terse = 1;
$config_writer->WriteToConfigFile(

37
t/00-beastbb.t Normal file
View File

@ -0,0 +1,37 @@
use 5.32.1;
use Test::Most tests => 4;
use strict;
use warnings;
use Path::Tiny;
use Test::MockModule;
use Data::Dumper;
use BeastBB::Constants qw/ConfigFile/;
{
use_ok 'BeastBB';
}
{
# GIVEN
my $path_tiny_tempdir_home = Path::Tiny->tempdir;
my $mock_module_constants = Test::MockModule->new('BeastBB::Constants');
$mock_module_constants->mock(
HomeDir => sub {
return Mojo::File->new($path_tiny_tempdir_home . '');
}
);
my $beastbb = BeastBB->new;
# WHEN
$beastbb->PrepareConfig;
# THEN
my $config = eval ConfigFile->slurp;
ok length $config->{secrets}[0] == 128, 'The config has been successfully created';
is_deeply $beastbb->config, $config, 'The populated in program config matches the content of the file.';
$config->{secrets} = [];
is_deeply $config, { secrets => [] }, 'The config matches the expected structure.';
}

36
t/01-response.t Normal file
View File

@ -0,0 +1,36 @@
use 5.32.1;
use Test::Most tests => 4;
use Test::Warnings ':all';
use strict;
use warnings;
{
use_ok 'BeastBB::Response';
}
{
like(
warning {
BeastBB::Response->new(
is_error => 1,
content => 'example_content', error_message => 'example_error'
)
},
qr/Error should not have content/,
'BeastBB::Response warns on construction if is_error is true and content is passed.'
);
}
{
like(
warning {
BeastBB::Response->new(
is_error => 1,
)
},
qr/You should pass a error message on error\./,
'BeastBB::Response warns on construction if is_error is true and error message is not passed.'
);
}