Bug 21046: Return the correct borrowernumber when there is empty cardnumber(s)
[koha.git] / t / db_dependent / ILSDI_Services.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
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.
9 #
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.
14 #
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>.
17
18 use Modern::Perl;
19
20 use CGI qw ( -utf8 );
21
22 use Test::More tests => 4;
23 use Test::MockModule;
24 use t::lib::Mocks;
25 use t::lib::TestBuilder;
26
27 use Koha::AuthUtils;
28
29 BEGIN {
30     use_ok('C4::ILSDI::Services');
31 }
32
33 my $schema  = Koha::Database->schema;
34 my $dbh     = C4::Context->dbh;
35 my $builder = t::lib::TestBuilder->new;
36
37 subtest 'AuthenticatePatron test' => sub {
38
39     plan tests => 14;
40
41     $schema->storage->txn_begin;
42
43     my $plain_password = 'tomasito';
44
45     $builder->build({
46         source => 'Borrower',
47         value => {
48             cardnumber => undef,
49         }
50     });
51
52     my $borrower = $builder->build({
53         source => 'Borrower',
54         value  => {
55             cardnumber => undef,
56             password => Koha::AuthUtils::hash_password( $plain_password )
57         }
58     });
59
60     my $query = new CGI;
61     $query->param( 'username', $borrower->{userid});
62     $query->param( 'password', $plain_password);
63
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");
67
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");
72
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");
78
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");
83
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" );
88
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" );
93
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");
99
100     $schema->storage->txn_rollback;
101 };
102
103
104 subtest 'GetPatronInfo/GetBorrowerAttributes test for extended patron attributes' => sub {
105
106     plan tests => 2;
107
108     $schema->storage->txn_begin;
109
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;
117
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 );
121
122     # Set up a library/branch for our user to belong to:
123     my $lib = $builder->build( {
124         source => 'Branch',
125         value => {
126             branchcode => 'T_ILSDI',
127         }
128     } );
129
130     # Create a new category for user to belong to:
131     my $cat = $builder->build( {
132         source => 'Category',
133         value  => {
134             category_type                 => 'A',
135             BlockExpiredPatronOpacActions => -1,
136         }
137     } );
138
139     # Create a new attribute type:
140     my $attr_type = $builder->build( {
141         source => 'BorrowerAttributeType',
142         value  => {
143             code                      => 'DOORCODE',
144             opac_display              => 0,
145             authorised_value_category => '',
146             class                     => '',
147         }
148     } );
149
150     # Create a new user:
151     my $brwr = $builder->build( {
152         source => 'Borrower',
153         value  => {
154             categorycode => $cat->{'categorycode'},
155             branchcode   => $lib->{'branchcode'},
156         }
157     } );
158
159     # Authorised value:
160     my $auth = $builder->build( {
161         source => 'AuthorisedValue',
162         value  => {
163             category => $cat->{'categorycode'}
164         }
165     } );
166
167     # Set the new attribute for our user:
168     my $attr = $builder->build( {
169         source => 'BorrowerAttribute',
170         value  => {
171             borrowernumber => $brwr->{'borrowernumber'},
172             code           => $attr_type->{'code'},
173             attribute      => '1337',
174         }
175     } );
176
177     $builder->build(
178         {
179             source => 'Accountline',
180             value  => {
181                 borrowernumber    => $brwr->{borrowernumber},
182                 accountno         => 1,
183                 accounttype       => 'xxx',
184                 amountoutstanding => 10
185             }
186         }
187     );
188
189     # Prepare and send web request for IL-SDI server:
190     my $query = new CGI;
191     $query->param( 'service', 'GetPatronInfo' );
192     $query->param( 'patron_id', $brwr->{'borrowernumber'} );
193     $query->param( 'show_attributes', '1' );
194
195     my $reply = C4::ILSDI::Services::GetPatronInfo( $query );
196
197     # Build a structure for comparison:
198     my $cmp = {
199         category_code     => $attr_type->{'category_code'},
200         class             => $attr_type->{'class'},
201         code              => $attr->{'code'},
202         description       => $attr_type->{'description'},
203         display_checkout  => $attr_type->{'display_checkout'},
204         value             => $attr->{'attribute'},
205         value_description => undef,
206     };
207
208     is( $reply->{'charges'}, '10.00',
209         'The \'charges\' attribute should be correctly filled (bug 17836)' );
210
211     # Check results:
212     is_deeply( $reply->{'attributes'}, [ $cmp ], 'Test GetPatronInfo - show_attributes parameter' );
213
214     # Cleanup
215     $schema->storage->txn_rollback;
216 };
217
218
219 subtest 'LookupPatron test' => sub {
220
221     plan tests => 9;
222
223     $schema->storage->txn_begin;
224
225     $schema->resultset( 'Issue' )->delete_all;
226     $schema->resultset( 'Borrower' )->delete_all;
227     $schema->resultset( 'BorrowerAttribute' )->delete_all;
228     $schema->resultset( 'BorrowerAttributeType' )->delete_all;
229     $schema->resultset( 'Category' )->delete_all;
230     $schema->resultset( 'Item' )->delete_all; # 'Branch' deps. on this
231     $schema->resultset( 'Branch' )->delete_all;
232
233     my $borrower = $builder->build({
234         source => 'Borrower',
235     });
236
237     my $query = CGI->new();
238     my $bad_result = C4::ILSDI::Services::LookupPatron($query);
239     is( $bad_result->{message}, 'PatronNotFound', 'No parameters' );
240
241     $query->delete_all();
242     $query->param( 'id', $borrower->{firstname} );
243     my $optional_result = C4::ILSDI::Services::LookupPatron($query);
244     is(
245         $optional_result->{id},
246         $borrower->{borrowernumber},
247         'Valid Firstname only'
248     );
249
250     $query->delete_all();
251     $query->param( 'id', 'ThereIsNoWayThatThisCouldPossiblyBeValid' );
252     my $bad_optional_result = C4::ILSDI::Services::LookupPatron($query);
253     is( $bad_optional_result->{message}, 'PatronNotFound', 'Invalid ID' );
254
255     foreach my $id_type (
256         'cardnumber',
257         'userid',
258         'email',
259         'borrowernumber',
260         'surname',
261         'firstname'
262     ) {
263         $query->delete_all();
264         $query->param( 'id_type', $id_type );
265         $query->param( 'id', $borrower->{$id_type} );
266         my $result = C4::ILSDI::Services::LookupPatron($query);
267         is( $result->{'id'}, $borrower->{borrowernumber}, "Checking $id_type" );
268     }
269
270     # Cleanup
271     $schema->storage->txn_rollback;
272 };