From 9f347030e3c83602783e0a3282b6ad5316bb49bd Mon Sep 17 00:00:00 2001 From: sergiotarxz Date: Sat, 5 Jun 2021 15:25:02 +0200 Subject: [PATCH] Using global constants turned not to be a good idea, some more tests. --- cpanfile | 1 + lib/BeastBB.pm | 23 +++++++++---------- lib/BeastBB/Constants.pm | 18 ++++++++++----- lib/BeastBB/Controller/Install.pm | 6 ++--- t/00-beastbb.t | 37 +++++++++++++++++++++++++++++++ t/01-response.t | 36 ++++++++++++++++++++++++++++++ 6 files changed, 101 insertions(+), 20 deletions(-) create mode 100644 t/00-beastbb.t create mode 100644 t/01-response.t diff --git a/cpanfile b/cpanfile index b66dd08..5d2299b 100644 --- a/cpanfile +++ b/cpanfile @@ -12,3 +12,4 @@ requires 'DateTime'; requires 'DateTime::Format::ISO8601'; requires 'Test::Most'; requires 'Test::MockModule'; +requires 'Test::Warnings'; diff --git a/lib/BeastBB.pm b/lib/BeastBB.pm index ec951d1..31b17a2 100644 --- a/lib/BeastBB.pm +++ b/lib/BeastBB.pm @@ -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; @@ -51,9 +49,10 @@ sub PrepareHelpers { ); $self->helper( logged_user => sub { - my $self = shift; - my $session = $self->session; - return BeastBB::Response->new( is_error => 1, error_message => 'User is not logged in.'); + my $self = shift; + my $session = $self->session; + 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; diff --git a/lib/BeastBB/Constants.pm b/lib/BeastBB/Constants.pm index 34b2bbb..02bbfb6 100644 --- a/lib/BeastBB/Constants.pm +++ b/lib/BeastBB/Constants.pm @@ -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; diff --git a/lib/BeastBB/Controller/Install.pm b/lib/BeastBB/Controller/Install.pm index 8591ebc..6523edd 100644 --- a/lib/BeastBB/Controller/Install.pm +++ b/lib/BeastBB/Controller/Install.pm @@ -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( diff --git a/t/00-beastbb.t b/t/00-beastbb.t new file mode 100644 index 0000000..8ed48d1 --- /dev/null +++ b/t/00-beastbb.t @@ -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.'; +} diff --git a/t/01-response.t b/t/01-response.t new file mode 100644 index 0000000..6a4d0a8 --- /dev/null +++ b/t/01-response.t @@ -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.' + ); +}