3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22 use Test::More tests => 4;
25 use t::lib::TestBuilder;
30 use_ok('C4::ILSDI::Services');
33 my $schema = Koha::Database->schema;
34 my $dbh = C4::Context->dbh;
35 my $builder = t::lib::TestBuilder->new;
37 subtest 'AuthenticatePatron test' => sub {
41 $schema->storage->txn_begin;
43 my $plain_password = 'tomasito';
52 my $borrower = $builder->build({
56 password => Koha::AuthUtils::hash_password( $plain_password )
61 $query->param( 'username', $borrower->{userid});
62 $query->param( 'password', $plain_password);
64 my $reply = C4::ILSDI::Services::AuthenticatePatron( $query );
65 is( $reply->{id}, $borrower->{borrowernumber}, "userid and password - Patron authenticated" );
66 is( $reply->{code}, undef, "Error code undef");
68 $query->param('password','ilsdi-passworD');
69 $reply = C4::ILSDI::Services::AuthenticatePatron( $query );
70 is( $reply->{code}, 'PatronNotFound', "userid and wrong password - PatronNotFound" );
71 is( $reply->{id}, undef, "id undef");
73 $query->param( 'password', $plain_password );
74 $query->param( 'username', 'wrong-ilsdi-useriD' );
75 $reply = C4::ILSDI::Services::AuthenticatePatron( $query );
76 is( $reply->{code}, 'PatronNotFound', "non-existing userid - PatronNotFound" );
77 is( $reply->{id}, undef, "id undef");
79 $query->param( 'username', uc( $borrower->{userid} ));
80 $reply = C4::ILSDI::Services::AuthenticatePatron( $query );
81 is( $reply->{id}, $borrower->{borrowernumber}, "userid is not case sensitive - Patron authenticated" );
82 is( $reply->{code}, undef, "Error code undef");
84 $query->param( 'username', $borrower->{cardnumber} );
85 $reply = C4::ILSDI::Services::AuthenticatePatron( $query );
86 is( $reply->{id}, $borrower->{borrowernumber}, "cardnumber and password - Patron authenticated" );
87 is( $reply->{code}, undef, "Error code undef" );
89 $query->param( 'password', 'ilsdi-passworD' );
90 $reply = C4::ILSDI::Services::AuthenticatePatron( $query );
91 is( $reply->{code}, 'PatronNotFound', "cardnumber and wrong password - PatronNotFount" );
92 is( $reply->{id}, undef, "id undef" );
94 $query->param( 'username', 'randomcardnumber1234' );
95 $query->param( 'password', $plain_password );
96 $reply = C4::ILSDI::Services::AuthenticatePatron($query);
97 is( $reply->{code}, 'PatronNotFound', "non-existing cardnumer/userid - PatronNotFound" );
98 is( $reply->{id}, undef, "id undef");
100 $schema->storage->txn_rollback;
104 subtest 'GetPatronInfo/GetBorrowerAttributes test for extended patron attributes' => sub {
108 $schema->storage->txn_begin;
110 $schema->resultset( 'Issue' )->delete_all;
111 $schema->resultset( 'Borrower' )->delete_all;
112 $schema->resultset( 'BorrowerAttribute' )->delete_all;
113 $schema->resultset( 'BorrowerAttributeType' )->delete_all;
114 $schema->resultset( 'Category' )->delete_all;
115 $schema->resultset( 'Item' )->delete_all; # 'Branch' deps. on this
116 $schema->resultset( 'Branch' )->delete_all;
118 # Configure Koha to enable ILS-DI server and extended attributes:
119 t::lib::Mocks::mock_preference( 'ILS-DI', 1 );
120 t::lib::Mocks::mock_preference( 'ExtendedPatronAttributes', 1 );
122 # Set up a library/branch for our user to belong to:
123 my $lib = $builder->build( {
126 branchcode => 'T_ILSDI',
130 # Create a new category for user to belong to:
131 my $cat = $builder->build( {
132 source => 'Category',
134 category_type => 'A',
135 BlockExpiredPatronOpacActions => -1,
139 # Create a new attribute type:
140 my $attr_type = $builder->build( {
141 source => 'BorrowerAttributeType',
145 authorised_value_category => '',
149 my $attr_type_visible = $builder->build( {
150 source => 'BorrowerAttributeType',
154 authorised_value_category => '',
160 my $brwr = $builder->build( {
161 source => 'Borrower',
163 categorycode => $cat->{'categorycode'},
164 branchcode => $lib->{'branchcode'},
169 my $auth = $builder->build( {
170 source => 'AuthorisedValue',
172 category => $cat->{'categorycode'}
176 # Set the new attribute for our user:
177 my $attr_hidden = $builder->build( {
178 source => 'BorrowerAttribute',
180 borrowernumber => $brwr->{'borrowernumber'},
181 code => $attr_type->{'code'},
182 attribute => '1337 hidden',
185 my $attr_shown = $builder->build( {
186 source => 'BorrowerAttribute',
188 borrowernumber => $brwr->{'borrowernumber'},
189 code => $attr_type_visible->{'code'},
190 attribute => '1337 shown',
196 source => 'Accountline',
198 borrowernumber => $brwr->{borrowernumber},
200 accounttype => 'xxx',
201 amountoutstanding => 10
206 # Prepare and send web request for IL-SDI server:
208 $query->param( 'service', 'GetPatronInfo' );
209 $query->param( 'patron_id', $brwr->{'borrowernumber'} );
210 $query->param( 'show_attributes', '1' );
212 my $reply = C4::ILSDI::Services::GetPatronInfo( $query );
214 # Build a structure for comparison:
216 category_code => $attr_type_visible->{'category_code'},
217 class => $attr_type_visible->{'class'},
218 code => $attr_shown->{'code'},
219 description => $attr_type_visible->{'description'},
220 display_checkout => $attr_type_visible->{'display_checkout'},
221 value => $attr_shown->{'attribute'},
222 value_description => undef,
225 is( $reply->{'charges'}, '10.00',
226 'The \'charges\' attribute should be correctly filled (bug 17836)' );
229 is_deeply( $reply->{'attributes'}, [ $cmp ], 'Test GetPatronInfo - show_attributes parameter' );
232 $schema->storage->txn_rollback;
236 subtest 'LookupPatron test' => sub {
240 $schema->storage->txn_begin;
242 $schema->resultset( 'Issue' )->delete_all;
243 $schema->resultset( 'Borrower' )->delete_all;
244 $schema->resultset( 'BorrowerAttribute' )->delete_all;
245 $schema->resultset( 'BorrowerAttributeType' )->delete_all;
246 $schema->resultset( 'Category' )->delete_all;
247 $schema->resultset( 'Item' )->delete_all; # 'Branch' deps. on this
248 $schema->resultset( 'Branch' )->delete_all;
250 my $borrower = $builder->build({
251 source => 'Borrower',
254 my $query = CGI->new();
255 my $bad_result = C4::ILSDI::Services::LookupPatron($query);
256 is( $bad_result->{message}, 'PatronNotFound', 'No parameters' );
258 $query->delete_all();
259 $query->param( 'id', $borrower->{firstname} );
260 my $optional_result = C4::ILSDI::Services::LookupPatron($query);
262 $optional_result->{id},
263 $borrower->{borrowernumber},
264 'Valid Firstname only'
267 $query->delete_all();
268 $query->param( 'id', 'ThereIsNoWayThatThisCouldPossiblyBeValid' );
269 my $bad_optional_result = C4::ILSDI::Services::LookupPatron($query);
270 is( $bad_optional_result->{message}, 'PatronNotFound', 'Invalid ID' );
272 foreach my $id_type (
280 $query->delete_all();
281 $query->param( 'id_type', $id_type );
282 $query->param( 'id', $borrower->{$id_type} );
283 my $result = C4::ILSDI::Services::LookupPatron($query);
284 is( $result->{'id'}, $borrower->{borrowernumber}, "Checking $id_type" );
288 $schema->storage->txn_rollback;