Bug 16320: Refactor ILSDI_Services.t so it uses TestBuilder

This patch refactors the tests so they are corerctly built using TestBuilder
and wrapper inside a DB transaction in a less ambiguous way.

To test:
- Verify that the tests pass with the patch:
- Run:
  $ prove t/db_dependent/ILSDI_Services.t
=> SUCCESS: Tests pass
- Sign off :-D

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Hector Castro <hector.hecaxmmx@gmail.com>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
This commit is contained in:
Tomás Cohen Arazi 2016-04-21 13:11:02 -03:00 committed by Kyle M Hall
parent f7d82d098a
commit 4a6fe346f0

View file

@ -1,103 +1,101 @@
#!/usr/bin/perl #!/usr/bin/perl
# This file is part of Koha.
#
# Koha is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# Koha is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Koha; if not, see <http://www.gnu.org/licenses>.
use Modern::Perl; use Modern::Perl;
use C4::Members qw/AddMember GetMember GetBorrowercategory/;
use Koha::Libraries;
use CGI qw ( -utf8 ); use CGI qw ( -utf8 );
use Test::More tests => 16; use Test::More tests => 3;
use t::lib::Mocks; use t::lib::Mocks;
use t::lib::TestBuilder; use t::lib::TestBuilder;
use Koha::AuthUtils;
BEGIN { BEGIN {
use_ok('C4::ILSDI::Services'); use_ok('C4::ILSDI::Services');
} }
my $schema = Koha::Database->schema;
my $dbh = C4::Context->dbh; my $dbh = C4::Context->dbh;
my $builder = t::lib::TestBuilder->new;
# Start transaction subtest 'AuthenticatePatron test' => sub {
$dbh->{AutoCommit} = 0;
$dbh->{RaiseError} = 1;
# Create patron plan tests => 14;
my %data = (
firstname => 'my firstname',
surname => 'my surname',
categorycode => 'UT',
branchcode => 'UT',
cardnumber => 'ilsdi-cardnumber',
userid => 'ilsdi-userid',
password => 'ilsdi-password',
);
# Crate patron category $schema->storage->txn_begin;
unless ( GetBorrowercategory('UT') ) {
$dbh->do("INSERT INTO categories my $plain_password = 'tomasito';
(categorycode,description,enrolmentperiod,upperagelimit,enrolmentfee,overduenoticerequired,reservefee,category_type,default_privacy)
VALUES my $borrower = $builder->build({
('UT','Unit tester',99,99,0.000000,1,0.000000,'C','default');"); source => 'Borrower',
value => {
password => Koha::AuthUtils::hash_password( $plain_password )
} }
});
# Create library
unless ( Koha::Libraries->find('UT') ) {
$dbh->do("INSERT INTO branches (branchcode,branchname) VALUES ('UT','Unit test library');");
}
my $borrowernumber = AddMember(%data);
my $borrower = GetMember( borrowernumber => $borrowernumber );
{ # AuthenticatePatron test
my $query = new CGI; my $query = new CGI;
$query->param('username',$borrower->{'userid'}); $query->param( 'username', $borrower->{userid});
$query->param('password','ilsdi-password'); $query->param( 'password', $plain_password);
my $reply = C4::ILSDI::Services::AuthenticatePatron( $query ); my $reply = C4::ILSDI::Services::AuthenticatePatron( $query );
is($reply->{'id'}, $borrowernumber, "userid and password - Patron authenticated"); is( $reply->{id}, $borrower->{borrowernumber}, "userid and password - Patron authenticated" );
is($reply->{'code'}, undef, "Error code undef"); is( $reply->{code}, undef, "Error code undef");
$query->param('password','ilsdi-passworD'); $query->param('password','ilsdi-passworD');
$reply = C4::ILSDI::Services::AuthenticatePatron( $query ); $reply = C4::ILSDI::Services::AuthenticatePatron( $query );
is($reply->{'code'}, 'PatronNotFound', "userid and wrong password - PatronNotFound"); is( $reply->{code}, 'PatronNotFound', "userid and wrong password - PatronNotFound" );
is($reply->{'id'}, undef, "id undef"); is( $reply->{id}, undef, "id undef");
$query->param('password','ilsdi-password'); $query->param( 'password', $plain_password );
$query->param( 'username', 'wrong-ilsdi-useriD' ); $query->param( 'username', 'wrong-ilsdi-useriD' );
$reply = C4::ILSDI::Services::AuthenticatePatron( $query ); $reply = C4::ILSDI::Services::AuthenticatePatron( $query );
is($reply->{'code'}, 'PatronNotFound', "non-existing userid - PatronNotFound"); is( $reply->{code}, 'PatronNotFound', "non-existing userid - PatronNotFound" );
is($reply->{'id'}, undef, "id undef"); is( $reply->{id}, undef, "id undef");
$query->param('username',uc($borrower->{'userid'})); $query->param( 'username', uc( $borrower->{userid} ));
$reply = C4::ILSDI::Services::AuthenticatePatron( $query ); $reply = C4::ILSDI::Services::AuthenticatePatron( $query );
is($reply->{'id'}, $borrowernumber, "userid is not case sensitive - Patron authenticated"); is( $reply->{id}, $borrower->{borrowernumber}, "userid is not case sensitive - Patron authenticated" );
is($reply->{'code'}, undef, "Error code undef"); is( $reply->{code}, undef, "Error code undef");
$query->param('username',$borrower->{'cardnumber'}); $query->param( 'username', $borrower->{cardnumber} );
$reply = C4::ILSDI::Services::AuthenticatePatron( $query ); $reply = C4::ILSDI::Services::AuthenticatePatron( $query );
is($reply->{'id'}, $borrowernumber, "cardnumber and password - Patron authenticated"); is( $reply->{id}, $borrower->{borrowernumber}, "cardnumber and password - Patron authenticated" );
is($reply->{'code'}, undef, "Error code undef"); is( $reply->{code}, undef, "Error code undef" );
$query->param( 'password', 'ilsdi-passworD' ); $query->param( 'password', 'ilsdi-passworD' );
$reply = C4::ILSDI::Services::AuthenticatePatron( $query ); $reply = C4::ILSDI::Services::AuthenticatePatron( $query );
is($reply->{'code'}, 'PatronNotFound', "cardnumber and wrong password - PatronNotFount"); is( $reply->{code}, 'PatronNotFound', "cardnumber and wrong password - PatronNotFount" );
is($reply->{'id'}, undef, "id undef"); is( $reply->{id}, undef, "id undef" );
$query->param( 'username', 'randomcardnumber1234' ); $query->param( 'username', 'randomcardnumber1234' );
$query->param('password','ilsdi-password'); $query->param( 'password', $plain_password );
$reply = C4::ILSDI::Services::AuthenticatePatron($query); $reply = C4::ILSDI::Services::AuthenticatePatron($query);
is($reply->{'code'}, 'PatronNotFound', "non-existing cardnumer/userid - PatronNotFound"); is( $reply->{code}, 'PatronNotFound', "non-existing cardnumer/userid - PatronNotFound" );
is($reply->{'id'}, undef, "id undef"); is( $reply->{id}, undef, "id undef");
} $schema->storage->txn_rollback;
};
# End transaction
$dbh->rollback;
$dbh->{AutoCommit} = 1;
$dbh->{RaiseError} = 0;
my $schema = Koha::Database->schema; subtest 'GetPatronInfo/GetBorrowerAttributes test for extended patron attributes' => sub {
plan tests => 1;
$schema->storage->txn_begin; $schema->storage->txn_begin;
$schema->resultset( 'Issue' )->delete_all; $schema->resultset( 'Issue' )->delete_all;
@ -108,15 +106,10 @@ $schema->resultset( 'Category' )->delete_all;
$schema->resultset( 'Item' )->delete_all; # 'Branch' deps. on this $schema->resultset( 'Item' )->delete_all; # 'Branch' deps. on this
$schema->resultset( 'Branch' )->delete_all; $schema->resultset( 'Branch' )->delete_all;
{ # GetPatronInfo/GetBorrowerAttributes test for extended patron attributes:
# Configure Koha to enable ILS-DI server and extended attributes: # Configure Koha to enable ILS-DI server and extended attributes:
t::lib::Mocks::mock_preference( 'ILS-DI', 1 ); t::lib::Mocks::mock_preference( 'ILS-DI', 1 );
t::lib::Mocks::mock_preference( 'ExtendedPatronAttributes', 1 ); t::lib::Mocks::mock_preference( 'ExtendedPatronAttributes', 1 );
my $builder = t::lib::TestBuilder->new;
# Set up a library/branch for our user to belong to: # Set up a library/branch for our user to belong to:
my $lib = $builder->build( { my $lib = $builder->build( {
source => 'Branch', source => 'Branch',
@ -195,7 +188,9 @@ $schema->resultset( 'Branch' )->delete_all;
# Check results: # Check results:
is_deeply( $reply->{'attributes'}, [ $cmp ], 'Test GetPatronInfo - show_attributes parameter' ); is_deeply( $reply->{'attributes'}, [ $cmp ], 'Test GetPatronInfo - show_attributes parameter' );
}
# Cleanup # Cleanup
$schema->storage->txn_rollback; $schema->storage->txn_rollback;
};
1;