diff --git a/Makefile.PL b/Makefile.PL index 3029ffd..7247122 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -5,7 +5,7 @@ WriteMakefile( VERSION => '0.1', INST_SCRIPT => './bin', INST_BIN => './bin', - test => { TESTS => 't/*.t' }, + test => { TESTS => 't/*.t t/*/*.t' }, ); package MY { diff --git a/test/BeastBB/Mock/Controller.pm b/lib/BeastBB/Mock/Controller.pm similarity index 100% rename from test/BeastBB/Mock/Controller.pm rename to lib/BeastBB/Mock/Controller.pm diff --git a/test/BeastBB/Mock/Database.pm b/lib/BeastBB/Mock/Database.pm similarity index 100% rename from test/BeastBB/Mock/Database.pm rename to lib/BeastBB/Mock/Database.pm diff --git a/lib/BeastBB/Model/User.pm b/lib/BeastBB/Model/User.pm index 90824d7..a545db2 100644 --- a/lib/BeastBB/Model/User.pm +++ b/lib/BeastBB/Model/User.pm @@ -10,6 +10,7 @@ use Types::Standard qw/Str Bool Int/; use BeastBB::Types qw/$MATRIX_ADDRESS_TYPE IsClassTypeGenerator/; use BeastBB::Response; +use Crypt::Bcrypt::Easy; { my $validator = validation_for( @@ -42,7 +43,7 @@ use BeastBB::Response; } sub Hash { - my $self = shift; + my $self = shift; return { ( ( !$self->IdUser->IsError ) @@ -214,4 +215,20 @@ sub Username { return $self->{last_connection}; } } + +{ + my $validator = validation_for( + params => { + password => { type => Str }, + } + ); + + sub CheckPasswordLogin { + my $self = shift; + my %params = $validator->(@_); + my $password = $params{password}; + return bcrypt->compare( text => $password, + crypt => $self->PasswordBcrypt ); + } +} 1; diff --git a/t/Model/00-user.t b/t/Model/00-user.t index f9e164a..e552577 100644 --- a/t/Model/00-user.t +++ b/t/Model/00-user.t @@ -1,6 +1,6 @@ use 5.32.1; -use Test::Most tests => 7; +use Test::Most tests => 8; use strict; use warnings; @@ -10,10 +10,11 @@ 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('example_password'), + password_bcrypt => bcrypt->crypt($PASSWORD), is_confirmed => 1, creation_date => DateTime->new( year => 2021, month => 06, day => 3 ), last_connection => DateTime->now, @@ -61,3 +62,7 @@ const my %REQUIRED_FIELDS_USER => ( 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}; +}