Bug 12001: Move GetMemberAccountBalance to Koha::Account->non_issues_charges
[srvgit] / 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     my $borrower = $builder->build({
46         source => 'Borrower',
47         value  => {
48             password => Koha::AuthUtils::hash_password( $plain_password )
49         }
50     });
51
52     my $query = new CGI;
53     $query->param( 'username', $borrower->{userid});
54     $query->param( 'password', $plain_password);
55
56     my $reply = C4::ILSDI::Services::AuthenticatePatron( $query );
57     is( $reply->{id}, $borrower->{borrowernumber}, "userid and password - Patron authenticated" );
58     is( $reply->{code}, undef, "Error code undef");
59
60     $query->param('password','ilsdi-passworD');
61     $reply = C4::ILSDI::Services::AuthenticatePatron( $query );
62     is( $reply->{code}, 'PatronNotFound', "userid and wrong password - PatronNotFound" );
63     is( $reply->{id}, undef, "id undef");
64
65     $query->param( 'password', $plain_password );
66     $query->param( 'username', 'wrong-ilsdi-useriD' );
67     $reply = C4::ILSDI::Services::AuthenticatePatron( $query );
68     is( $reply->{code}, 'PatronNotFound', "non-existing userid - PatronNotFound" );
69     is( $reply->{id}, undef, "id undef");
70
71     $query->param( 'username', uc( $borrower->{userid} ));
72     $reply = C4::ILSDI::Services::AuthenticatePatron( $query );
73     is( $reply->{id}, $borrower->{borrowernumber}, "userid is not case sensitive - Patron authenticated" );
74     is( $reply->{code}, undef, "Error code undef");
75
76     $query->param( 'username', $borrower->{cardnumber} );
77     $reply = C4::ILSDI::Services::AuthenticatePatron( $query );
78     is( $reply->{id}, $borrower->{borrowernumber}, "cardnumber and password - Patron authenticated" );
79     is( $reply->{code}, undef, "Error code undef" );
80
81     $query->param( 'password', 'ilsdi-passworD' );
82     $reply = C4::ILSDI::Services::AuthenticatePatron( $query );
83     is( $reply->{code}, 'PatronNotFound', "cardnumber and wrong password - PatronNotFount" );
84     is( $reply->{id}, undef, "id undef" );
85
86     $query->param( 'username', 'randomcardnumber1234' );
87     $query->param( 'password', $plain_password );
88     $reply = C4::ILSDI::Services::AuthenticatePatron($query);
89     is( $reply->{code}, 'PatronNotFound', "non-existing cardnumer/userid - PatronNotFound" );
90     is( $reply->{id}, undef, "id undef");
91
92     $schema->storage->txn_rollback;
93 };
94
95
96 subtest 'GetPatronInfo/GetBorrowerAttributes test for extended patron attributes' => sub {
97
98     plan tests => 2;
99
100     $schema->storage->txn_begin;
101
102     $schema->resultset( 'Issue' )->delete_all;
103     $schema->resultset( 'Borrower' )->delete_all;
104     $schema->resultset( 'BorrowerAttribute' )->delete_all;
105     $schema->resultset( 'BorrowerAttributeType' )->delete_all;
106     $schema->resultset( 'Category' )->delete_all;
107     $schema->resultset( 'Item' )->delete_all; # 'Branch' deps. on this
108     $schema->resultset( 'Branch' )->delete_all;
109
110     # Configure Koha to enable ILS-DI server and extended attributes:
111     t::lib::Mocks::mock_preference( 'ILS-DI', 1 );
112     t::lib::Mocks::mock_preference( 'ExtendedPatronAttributes', 1 );
113
114     # Set up a library/branch for our user to belong to:
115     my $lib = $builder->build( {
116         source => 'Branch',
117         value => {
118             branchcode => 'T_ILSDI',
119         }
120     } );
121
122     # Create a new category for user to belong to:
123     my $cat = $builder->build( {
124         source => 'Category',
125         value  => {
126             category_type                 => 'A',
127             BlockExpiredPatronOpacActions => -1,
128         }
129     } );
130
131     # Create a new attribute type:
132     my $attr_type = $builder->build( {
133         source => 'BorrowerAttributeType',
134         value  => {
135             code                      => 'DOORCODE',
136             opac_display              => 0,
137             authorised_value_category => '',
138             class                     => '',
139         }
140     } );
141
142     # Create a new user:
143     my $brwr = $builder->build( {
144         source => 'Borrower',
145         value  => {
146             categorycode => $cat->{'categorycode'},
147             branchcode   => $lib->{'branchcode'},
148         }
149     } );
150
151     # Authorised value:
152     my $auth = $builder->build( {
153         source => 'AuthorisedValue',
154         value  => {
155             category => $cat->{'categorycode'}
156         }
157     } );
158
159     # Set the new attribute for our user:
160     my $attr = $builder->build( {
161         source => 'BorrowerAttribute',
162         value  => {
163             borrowernumber => $brwr->{'borrowernumber'},
164             code           => $attr_type->{'code'},
165             attribute      => '1337',
166         }
167     } );
168
169     $builder->build(
170         {
171             source => 'Accountline',
172             value  => {
173                 borrowernumber    => $brwr->{borrowernumber},
174                 accountno         => 1,
175                 accounttype       => 'xxx',
176                 amountoutstanding => 10
177             }
178         }
179     );
180
181     # Prepare and send web request for IL-SDI server:
182     my $query = new CGI;
183     $query->param( 'service', 'GetPatronInfo' );
184     $query->param( 'patron_id', $brwr->{'borrowernumber'} );
185     $query->param( 'show_attributes', '1' );
186
187     my $reply = C4::ILSDI::Services::GetPatronInfo( $query );
188
189     # Build a structure for comparison:
190     my $cmp = {
191         category_code     => $attr_type->{'category_code'},
192         class             => $attr_type->{'class'},
193         code              => $attr->{'code'},
194         description       => $attr_type->{'description'},
195         display_checkout  => $attr_type->{'display_checkout'},
196         value             => $attr->{'attribute'},
197         value_description => undef,
198     };
199
200     is( $reply->{'charges'}, '10.00',
201         'The \'charges\' attribute should be correctly filled (bug 17836)' );
202
203     # Check results:
204     is_deeply( $reply->{'attributes'}, [ $cmp ], 'Test GetPatronInfo - show_attributes parameter' );
205
206     # Cleanup
207     $schema->storage->txn_rollback;
208 };
209
210
211 subtest 'LookupPatron test' => sub {
212
213     plan tests => 9;
214
215     $schema->storage->txn_begin;
216
217     $schema->resultset( 'Issue' )->delete_all;
218     $schema->resultset( 'Borrower' )->delete_all;
219     $schema->resultset( 'BorrowerAttribute' )->delete_all;
220     $schema->resultset( 'BorrowerAttributeType' )->delete_all;
221     $schema->resultset( 'Category' )->delete_all;
222     $schema->resultset( 'Item' )->delete_all; # 'Branch' deps. on this
223     $schema->resultset( 'Branch' )->delete_all;
224
225     my $borrower = $builder->build({
226         source => 'Borrower',
227     });
228
229     my $query = CGI->new();
230     my $bad_result = C4::ILSDI::Services::LookupPatron($query);
231     is( $bad_result->{message}, 'PatronNotFound', 'No parameters' );
232
233     $query->delete_all();
234     $query->param( 'id', $borrower->{firstname} );
235     my $optional_result = C4::ILSDI::Services::LookupPatron($query);
236     is(
237         $optional_result->{id},
238         $borrower->{borrowernumber},
239         'Valid Firstname only'
240     );
241
242     $query->delete_all();
243     $query->param( 'id', 'ThereIsNoWayThatThisCouldPossiblyBeValid' );
244     my $bad_optional_result = C4::ILSDI::Services::LookupPatron($query);
245     is( $bad_optional_result->{message}, 'PatronNotFound', 'Invalid ID' );
246
247     foreach my $id_type (
248         'cardnumber',
249         'userid',
250         'email',
251         'borrowernumber',
252         'surname',
253         'firstname'
254     ) {
255         $query->delete_all();
256         $query->param( 'id_type', $id_type );
257         $query->param( 'id', $borrower->{$id_type} );
258         my $result = C4::ILSDI::Services::LookupPatron($query);
259         is( $result->{'id'}, $borrower->{borrowernumber}, "Checking $id_type" );
260     }
261
262     # Cleanup
263     $schema->storage->txn_rollback;
264 };