X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=t%2Fdb_dependent%2FILSDI_Services.t;h=a4335519478a6df2a8fb6469c1fe0cc0c8b01cf7;hb=03a547da32195aa98cd9cd28b8d64f7bc399fa02;hp=4d584a24ab9bc6f604af205b5cd01f177454e7fe;hpb=c4f7164cb92c63043c4877aef79714c5461496c9;p=koha.git diff --git a/t/db_dependent/ILSDI_Services.t b/t/db_dependent/ILSDI_Services.t index 4d584a24ab..a433551947 100644 --- a/t/db_dependent/ILSDI_Services.t +++ b/t/db_dependent/ILSDI_Services.t @@ -1,92 +1,255 @@ #!/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 . + use Modern::Perl; -use C4::Members qw/AddMember GetMember GetBorrowercategory/; -use C4::Branch; -use CGI; +use CGI qw ( -utf8 ); + +use Test::More tests => 4; +use Test::MockModule; +use t::lib::Mocks; +use t::lib::TestBuilder; -use Test::More tests => 15; +use Koha::AuthUtils; BEGIN { use_ok('C4::ILSDI::Services'); } -my $dbh = C4::Context->dbh; - -# Start transaction -$dbh->{AutoCommit} = 0; -$dbh->{RaiseError} = 1; - -# Create patron -my %data = ( - firstname => 'my firstname', - surname => 'my surname', - categorycode => 'PT', - branchcode => 'UT', - cardnumber => 'ilsdi-cardnumber', - userid => 'ilsdi-userid', - password => 'ilsdi-password', -); - -# Crate patron category -unless ( GetBorrowercategory('PT') ) { - $dbh->do("INSERT INTO categories - (categorycode,description,enrolmentperiod,upperagelimit,enrolmentfee,overduenoticerequired,reservefee,category_type,default_privacy) - VALUES - ('PT','Unit tester',99,99,0.000000,1,0.000000,'C','default');"); -} - -# Create branch -unless ( GetBranchDetail('UT') ) { - $dbh->do("INSERT INTO branches (branchcode,branchname) VALUES ('UT','Unit test library');"); -} - - -my $borrowernumber = AddMember(%data); -my $borrower = GetMember( borrowernumber => $borrowernumber ); +my $schema = Koha::Database->schema; +my $dbh = C4::Context->dbh; +my $builder = t::lib::TestBuilder->new; -{ # AuthenticatePatron test +subtest 'AuthenticatePatron test' => sub { - my $query = new CGI; - $query->param('username',$borrower->{'userid'}); - $query->param('password','ilsdi-password'); + plan tests => 14; - my $reply = C4::ILSDI::Services::AuthenticatePatron($query); - is($reply->{'id'}, $borrowernumber, "userid and password - Patron authenticated"); - is($reply->{'code'}, undef, "Error code undef"); + $schema->storage->txn_begin; - $query->param('password','ilsdi-passworD'); - $reply = C4::ILSDI::Services::AuthenticatePatron($query); - is($reply->{'code'}, 'PatronNotFound', "userid and wrong password - PatronNotFound"); - is($reply->{'id'}, undef, "id undef"); + my $plain_password = 'tomasito'; - $query->param('password','ilsdi-password'); - $query->param('username','wrong-ilsdi-useriD'); - $reply = C4::ILSDI::Services::AuthenticatePatron($query); - is($reply->{'code'}, 'PatronNotFound', "non-existing userid - PatronNotFound"); - is($reply->{'id'}, undef, "id undef"); + my $borrower = $builder->build({ + source => 'Borrower', + value => { + password => Koha::AuthUtils::hash_password( $plain_password ) + } + }); - $query->param('username',uc($borrower->{'userid'})); - $reply = C4::ILSDI::Services::AuthenticatePatron($query); - is($reply->{'id'}, $borrowernumber, "userid is not case sensitive - Patron authenticated"); - is($reply->{'code'}, undef, "Error code undef"); + my $query = new CGI; + $query->param( 'username', $borrower->{userid}); + $query->param( 'password', $plain_password); - $query->param('username',$borrower->{'cardnumber'}); - $reply = C4::ILSDI::Services::AuthenticatePatron($query); - is($reply->{'id'}, $borrowernumber, "cardnumber and password - Patron authenticated"); - is($reply->{'code'}, undef, "Error code undef"); + my $reply = C4::ILSDI::Services::AuthenticatePatron( $query ); + is( $reply->{id}, $borrower->{borrowernumber}, "userid and password - Patron authenticated" ); + is( $reply->{code}, undef, "Error code undef"); $query->param('password','ilsdi-passworD'); + $reply = C4::ILSDI::Services::AuthenticatePatron( $query ); + is( $reply->{code}, 'PatronNotFound', "userid and wrong password - PatronNotFound" ); + is( $reply->{id}, undef, "id undef"); + + $query->param( 'password', $plain_password ); + $query->param( 'username', 'wrong-ilsdi-useriD' ); + $reply = C4::ILSDI::Services::AuthenticatePatron( $query ); + is( $reply->{code}, 'PatronNotFound', "non-existing userid - PatronNotFound" ); + is( $reply->{id}, undef, "id undef"); + + $query->param( 'username', uc( $borrower->{userid} )); + $reply = C4::ILSDI::Services::AuthenticatePatron( $query ); + is( $reply->{id}, $borrower->{borrowernumber}, "userid is not case sensitive - Patron authenticated" ); + is( $reply->{code}, undef, "Error code undef"); + + $query->param( 'username', $borrower->{cardnumber} ); + $reply = C4::ILSDI::Services::AuthenticatePatron( $query ); + is( $reply->{id}, $borrower->{borrowernumber}, "cardnumber and password - Patron authenticated" ); + is( $reply->{code}, undef, "Error code undef" ); + + $query->param( 'password', 'ilsdi-passworD' ); + $reply = C4::ILSDI::Services::AuthenticatePatron( $query ); + is( $reply->{code}, 'PatronNotFound', "cardnumber and wrong password - PatronNotFount" ); + is( $reply->{id}, undef, "id undef" ); + + $query->param( 'username', 'randomcardnumber1234' ); + $query->param( 'password', $plain_password ); $reply = C4::ILSDI::Services::AuthenticatePatron($query); - is($reply->{'code'}, 'PatronNotFound', "cardnumber and wrong password - PatronNotFount"); - is($reply->{'id'}, undef, "id undef"); - - $query->param('username','randomcardnumber1234'); - $query->param('password','ilsdi-password'); - $reply = C4::ILSDI::Services::AuthenticatePatron($query); - is($reply->{'code'}, 'PatronNotFound', "non-existing cardnumer/userid - PatronNotFound"); - is($reply->{'id'}, undef, "id undef"); - -} - + is( $reply->{code}, 'PatronNotFound', "non-existing cardnumer/userid - PatronNotFound" ); + is( $reply->{id}, undef, "id undef"); + + $schema->storage->txn_rollback; +}; + + +subtest 'GetPatronInfo/GetBorrowerAttributes test for extended patron attributes' => sub { + + plan tests => 2; + + $schema->storage->txn_begin; + + $schema->resultset( 'Issue' )->delete_all; + $schema->resultset( 'Borrower' )->delete_all; + $schema->resultset( 'BorrowerAttribute' )->delete_all; + $schema->resultset( 'BorrowerAttributeType' )->delete_all; + $schema->resultset( 'Category' )->delete_all; + $schema->resultset( 'Item' )->delete_all; # 'Branch' deps. on this + $schema->resultset( 'Branch' )->delete_all; + + # Configure Koha to enable ILS-DI server and extended attributes: + t::lib::Mocks::mock_preference( 'ILS-DI', 1 ); + t::lib::Mocks::mock_preference( 'ExtendedPatronAttributes', 1 ); + + # Set up a library/branch for our user to belong to: + my $lib = $builder->build( { + source => 'Branch', + value => { + branchcode => 'T_ILSDI', + } + } ); + + # Create a new category for user to belong to: + my $cat = $builder->build( { + source => 'Category', + value => { + category_type => 'A', + BlockExpiredPatronOpacActions => -1, + } + } ); + + # Create a new attribute type: + my $attr_type = $builder->build( { + source => 'BorrowerAttributeType', + value => { + code => 'DOORCODE', + opac_display => 0, + authorised_value_category => '', + class => '', + } + } ); + + # Create a new user: + my $brwr = $builder->build( { + source => 'Borrower', + value => { + categorycode => $cat->{'categorycode'}, + branchcode => $lib->{'branchcode'}, + } + } ); + + # Authorised value: + my $auth = $builder->build( { + source => 'AuthorisedValue', + value => { + category => $cat->{'categorycode'} + } + } ); + + # Set the new attribute for our user: + my $attr = $builder->build( { + source => 'BorrowerAttribute', + value => { + borrowernumber => $brwr->{'borrowernumber'}, + code => $attr_type->{'code'}, + attribute => '1337', + } + } ); + + my $members = Test::MockModule->new('C4::Members'); + $members->mock( 'GetMemberAccountBalance', sub { return ( 10, 10, 0 ); } ); + + # Prepare and send web request for IL-SDI server: + my $query = new CGI; + $query->param( 'service', 'GetPatronInfo' ); + $query->param( 'patron_id', $brwr->{'borrowernumber'} ); + $query->param( 'show_attributes', '1' ); + + my $reply = C4::ILSDI::Services::GetPatronInfo( $query ); + + # Build a structure for comparison: + my $cmp = { + category_code => $attr_type->{'category_code'}, + class => $attr_type->{'class'}, + code => $attr->{'code'}, + description => $attr_type->{'description'}, + display_checkout => $attr_type->{'display_checkout'}, + value => $attr->{'attribute'}, + value_description => undef, + }; + + is( $reply->{'charges'}, '10.00', + 'The \'charges\' attribute should be correctly filled (bug 17836)' ); + + # Check results: + is_deeply( $reply->{'attributes'}, [ $cmp ], 'Test GetPatronInfo - show_attributes parameter' ); + + # Cleanup + $schema->storage->txn_rollback; +}; + + +subtest 'LookupPatron test' => sub { + + plan tests => 9; + + $schema->storage->txn_begin; + + $schema->resultset( 'Issue' )->delete_all; + $schema->resultset( 'Borrower' )->delete_all; + $schema->resultset( 'BorrowerAttribute' )->delete_all; + $schema->resultset( 'BorrowerAttributeType' )->delete_all; + $schema->resultset( 'Category' )->delete_all; + $schema->resultset( 'Item' )->delete_all; # 'Branch' deps. on this + $schema->resultset( 'Branch' )->delete_all; + + my $borrower = $builder->build({ + source => 'Borrower', + }); + + my $query = CGI->new(); + my $bad_result = C4::ILSDI::Services::LookupPatron($query); + is( $bad_result->{message}, 'PatronNotFound', 'No parameters' ); + + $query->delete_all(); + $query->param( 'id', $borrower->{firstname} ); + my $optional_result = C4::ILSDI::Services::LookupPatron($query); + is( + $optional_result->{id}, + $borrower->{borrowernumber}, + 'Valid Firstname only' + ); + + $query->delete_all(); + $query->param( 'id', 'ThereIsNoWayThatThisCouldPossiblyBeValid' ); + my $bad_optional_result = C4::ILSDI::Services::LookupPatron($query); + is( $bad_optional_result->{message}, 'PatronNotFound', 'Invalid ID' ); + + foreach my $id_type ( + 'cardnumber', + 'userid', + 'email', + 'borrowernumber', + 'surname', + 'firstname' + ) { + $query->delete_all(); + $query->param( 'id_type', $id_type ); + $query->param( 'id', $borrower->{$id_type} ); + my $result = C4::ILSDI::Services::LookupPatron($query); + is( $result->{'id'}, $borrower->{borrowernumber}, "Checking $id_type" ); + } + + # Cleanup + $schema->storage->txn_rollback; +};