Bug 21199: Hide patron's attributes from ILSDI if required
[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                      => 'HIDEME',
144             opac_display              => 0,
145             authorised_value_category => '',
146             class                     => '',
147         }
148     } );
149     my $attr_type_visible = $builder->build( {
150         source => 'BorrowerAttributeType',
151         value  => {
152             code                      => 'SHOWME',
153             opac_display              => 1,
154             authorised_value_category => '',
155             class                     => '',
156         }
157     } );
158
159     # Create a new user:
160     my $brwr = $builder->build( {
161         source => 'Borrower',
162         value  => {
163             categorycode => $cat->{'categorycode'},
164             branchcode   => $lib->{'branchcode'},
165         }
166     } );
167
168     # Authorised value:
169     my $auth = $builder->build( {
170         source => 'AuthorisedValue',
171         value  => {
172             category => $cat->{'categorycode'}
173         }
174     } );
175
176     # Set the new attribute for our user:
177     my $attr_hidden = $builder->build( {
178         source => 'BorrowerAttribute',
179         value  => {
180             borrowernumber => $brwr->{'borrowernumber'},
181             code           => $attr_type->{'code'},
182             attribute      => '1337 hidden',
183         }
184     } );
185     my $attr_shown = $builder->build( {
186         source => 'BorrowerAttribute',
187         value  => {
188             borrowernumber => $brwr->{'borrowernumber'},
189             code           => $attr_type_visible->{'code'},
190             attribute      => '1337 shown',
191         }
192     } );
193
194     $builder->build(
195         {
196             source => 'Accountline',
197             value  => {
198                 borrowernumber    => $brwr->{borrowernumber},
199                 accountno         => 1,
200                 accounttype       => 'xxx',
201                 amountoutstanding => 10
202             }
203         }
204     );
205
206     # Prepare and send web request for IL-SDI server:
207     my $query = new CGI;
208     $query->param( 'service', 'GetPatronInfo' );
209     $query->param( 'patron_id', $brwr->{'borrowernumber'} );
210     $query->param( 'show_attributes', '1' );
211
212     my $reply = C4::ILSDI::Services::GetPatronInfo( $query );
213
214     # Build a structure for comparison:
215     my $cmp = {
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,
223     };
224
225     is( $reply->{'charges'}, '10.00',
226         'The \'charges\' attribute should be correctly filled (bug 17836)' );
227
228     # Check results:
229     is_deeply( $reply->{'attributes'}, [ $cmp ], 'Test GetPatronInfo - show_attributes parameter' );
230
231     # Cleanup
232     $schema->storage->txn_rollback;
233 };
234
235
236 subtest 'LookupPatron test' => sub {
237
238     plan tests => 9;
239
240     $schema->storage->txn_begin;
241
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;
249
250     my $borrower = $builder->build({
251         source => 'Borrower',
252     });
253
254     my $query = CGI->new();
255     my $bad_result = C4::ILSDI::Services::LookupPatron($query);
256     is( $bad_result->{message}, 'PatronNotFound', 'No parameters' );
257
258     $query->delete_all();
259     $query->param( 'id', $borrower->{firstname} );
260     my $optional_result = C4::ILSDI::Services::LookupPatron($query);
261     is(
262         $optional_result->{id},
263         $borrower->{borrowernumber},
264         'Valid Firstname only'
265     );
266
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' );
271
272     foreach my $id_type (
273         'cardnumber',
274         'userid',
275         'email',
276         'borrowernumber',
277         'surname',
278         'firstname'
279     ) {
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" );
285     }
286
287     # Cleanup
288     $schema->storage->txn_rollback;
289 };