BeastBB/t/Model/00-user.t

69 lines
2.0 KiB
Perl

use 5.32.1;
use Test::Most tests => 8;
use strict;
use warnings;
use Const::Fast;
use Crypt::Bcrypt::Easy;
use DateTime;
use Scalar::Util 'blessed';
const my $PASSWORD => 'example_password';
const my %REQUIRED_FIELDS_USER => (
username => 'example_username',
matrix_address => '@example_username:example_host.com',
password_bcrypt => bcrypt->crypt($PASSWORD),
is_confirmed => 1,
creation_date => DateTime->new( year => 2021, month => 06, day => 3 ),
last_connection => DateTime->now,
);
{
use_ok 'BeastBB::Model::User';
}
{
my $user = BeastBB::Model::User->new(%REQUIRED_FIELDS_USER);
ok defined $user && $user->isa('BeastBB::Model::User'),
'This user is made of BeastBB::Model::User.';
my $user_hash = $user->Hash;
%$user_hash = map {
my $value = $user_hash->{$_};
(
$_ => (
( blessed $value && $value->isa('DateTime') )
? "$value"
: $value
)
)
} keys %$user_hash;
is_deeply $user_hash, \%REQUIRED_FIELDS_USER, 'User has the expected hash.';
}
{
my $user = BeastBB::Model::User->new(%REQUIRED_FIELDS_USER);
ok $user->IdUser->IsError, 'Id user can not be retrieved before set.';
$user->SetIdUser(1);
ok !$user->IdUser->IsError, 'Id user can be retrieved after set.';
is $user->IdUser->Content, 1, 'Id user can be set on runtime.';
my $user_hash = $user->Hash;
%$user_hash = map {
my $value = $user_hash->{$_};
(
$_ => (
( blessed $value && $value->isa('DateTime') )
? "$value"
: $value
)
)
} keys %$user_hash;
is_deeply $user_hash, { %REQUIRED_FIELDS_USER, id_user => 1 },
'User has the expected hash with id set after creation.';
}
{
my $user = BeastBB::Model::User->new(%REQUIRED_FIELDS_USER);
ok $user->CheckPasswordLogin(password => $PASSWORD ), q{User can login with it's password};
}