Adding more tests.

This commit is contained in:
sergiotarxz 2021-06-05 02:36:39 +02:00
parent 1e3a8aeb89
commit b58c989910
Signed by: sergiotarxz
GPG Key ID: E5903508B6510AC2
4 changed files with 95 additions and 12 deletions

View File

@ -8,16 +8,19 @@ use warnings;
use Carp; use Carp;
use Params::ValidationCompiler 'validation_for'; use Params::ValidationCompiler 'validation_for';
use Types::Standard qw/Bool Str/; use Types::Standard qw/Bool Str Int/;
use Crypt::Bcrypt::Easy; use Crypt::Bcrypt::Easy;
use Const::Fast; use Const::Fast;
use DateTime; use DateTime;
use DateTime::Format::ISO8601;
use BeastBB::Types ( use BeastBB::Types (
'$MATRIX_ADDRESS_REGEX', 'IsClassTypeGenerator', '$MATRIX_ADDRESS_REGEX', 'IsClassTypeGenerator',
'$MATRIX_ADDRESS_TYPE' '$MATRIX_ADDRESS_TYPE'
); );
use BeastBB::DAO::GroupManager;
use BeastBB::Model::User;
use BeastBB::Response; use BeastBB::Response;
const my $MINIMUM_PASSWORD_LENGHT => 8; const my $MINIMUM_PASSWORD_LENGHT => 8;
@ -71,7 +74,7 @@ const my $MINIMUM_PASSWORD_LENGHT => 8;
) )
); );
return $maybe_user_hash if $maybe_user_hash->IsError; return $maybe_user_hash if $maybe_user_hash->IsError;
$user_hash = $maybe_user_hash->Content; my $user_hash = $maybe_user_hash->Content;
my $id_group = delete $user_hash->{id_group}; my $id_group = delete $user_hash->{id_group};
my $user = BeastBB::Model::User->new(%$user_hash); my $user = BeastBB::Model::User->new(%$user_hash);
@ -161,7 +164,7 @@ const my $MINIMUM_PASSWORD_LENGHT => 8;
DateTime::Format::ISO8601->parse_datetime( DateTime::Format::ISO8601->parse_datetime(
$user_hash->{last_connection} ); $user_hash->{last_connection} );
return BeastBB::Response( content => $user_hash ); return BeastBB::Response->new( content => $user_hash );
} }
} }

View File

@ -51,7 +51,7 @@ sub Hash {
), ),
( (
( !$self->Group->IsError ) ( !$self->Group->IsError )
? ( group => $self->Group->Content->hash ) ? ( group => $self->Group->Content->Hash )
: () : ()
), ),
username => $self->Username, username => $self->Username,
@ -87,7 +87,7 @@ sub Hash {
error_message => 'No group recovered for this user.', error_message => 'No group recovered for this user.',
); );
} }
return BeastBB::Model::Group->new( return BeastBB::Response->new(
content => $self->{group}, content => $self->{group},
); );
} }

View File

@ -8,17 +8,14 @@ use warnings;
use Const::Fast; use Const::Fast;
use BeastBB::Mock::Controller; use BeastBB::Mock::Controller;
use BeastBB::DAO::GroupManager;
use Data::Dumper;
const my $APP => BeastBB::Mock::Controller->new;
const my $GROUP_MANAGER => BeastBB::DAO::GroupManager->new( app => $APP );
{ {
use_ok 'BeastBB::DAO::GroupManager'; use_ok 'BeastBB::DAO::GroupManager';
} }
const my $APP => BeastBB::Mock::Controller->new;
const my $GROUP_MANAGER => BeastBB::DAO::GroupManager->new( app => $APP );
{ {
$APP->db->Mock( $APP->db->Mock(
sql => sql =>

83
t/DAO/01-user-manager.t Normal file
View File

@ -0,0 +1,83 @@
use 5.32.1;
use Test::Most tests => 5;
use Test::MockModule;
use strict;
use warnings;
use Data::Dumper;
use Const::Fast;
use Crypt::Bcrypt::Easy;
use BeastBB::Mock::Controller;
use BeastBB::Model::Group;
{
use_ok 'BeastBB::DAO::UserManager';
}
const my $APP => BeastBB::Mock::Controller->new;
const my $USER_MANAGER => BeastBB::DAO::UserManager->new( app => $APP );
const my %USER_PARAMS => (
id_user => 1,
username => 'example_username',
matrix_address => '@example_username:example.com',
password_bcrypt => bcrypt->crypt('example_password'),
is_confirmed => 1,
creation_date => DateTime->new( year => 2021, month => 06, day => 3 ),
last_connection => DateTime->now,
);
{
my @results;
my @user_keys = keys %USER_PARAMS;
push @results, [@user_keys];
push @results, [ map { $USER_PARAMS{$_} } @user_keys ];
$APP->db->Mock(
sql =>
'SELECT "id_user", "username", "matrix_address", "password_bcrypt", "is_confirmed", "creation_date", "id_group", "last_connection" FROM "user" WHERE "username" = ?',
results => \@results,
);
my $maybe_user = $USER_MANAGER->Get( username => 'example_user' );
die $maybe_user->ErrorMessage if $maybe_user->IsError;
my $user = $maybe_user->Content;
ok $user->isa('BeastBB::Model::User'),
'User is made of BeastBB::Model::User';
is_deeply $user->Hash, \%USER_PARAMS, 'User to hash conversion works ok.';
}
{
my $test_mock_module_group_manager =
Test::MockModule->new('BeastBB::DAO::GroupManager');
my $group =
BeastBB::Model::Group->new( id_group => 1, groupname => 'example_group' );
$test_mock_module_group_manager->mock(
Get => sub {
my $self = shift;
return BeastBB::Response->new( content => $group );
}
);
my @results;
my %user_params = %USER_PARAMS;
my %user_hash_expected = ( %user_params, group => $group->Hash );
$user_params{id_group} = 1;
my @user_keys = keys %user_params;
push @results, [@user_keys];
push @results, [ map { $user_params{$_} } @user_keys ];
$APP->db->Mock(
sql =>
'SELECT "id_user", "username", "matrix_address", "password_bcrypt", "is_confirmed", "creation_date", "id_group", "last_connection" FROM "user" WHERE "username" = ?',
results => \@results,
);
my $maybe_user =
$USER_MANAGER->Get( username => 'example_user', recover_group => 1 );
die $maybe_user->ErrorMessage if $maybe_user->IsError;
my $user = $maybe_user->Content;
ok $user->isa('BeastBB::Model::User'),
'User is made of BeastBB::Model::User when trying to recover also a group.';
is_deeply $user->Hash, \%user_hash_expected,
'User to hash conversion works ok.';
}