Bug 17776: (QA follow-up) Consistent regex for Plack detection
[koha.git] / t / db_dependent / ILSDI_Services.t
blob9b6908ff1292dc7037cffc439938f87e21f1de1e
1 #!/usr/bin/perl
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>.
18 use Modern::Perl;
20 use CGI qw ( -utf8 );
22 use Test::More tests => 4;
23 use Test::MockModule;
24 use t::lib::Mocks;
25 use t::lib::TestBuilder;
27 use Koha::AuthUtils;
29 BEGIN {
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 {
39 plan tests => 14;
41 $schema->storage->txn_begin;
43 my $plain_password = 'tomasito';
45 $builder->build({
46 source => 'Borrower',
47 value => {
48 cardnumber => undef,
50 });
52 my $borrower = $builder->build({
53 source => 'Borrower',
54 value => {
55 cardnumber => undef,
56 password => Koha::AuthUtils::hash_password( $plain_password )
58 });
60 my $query = new CGI;
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 {
106 plan tests => 2;
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( {
124 source => 'Branch',
125 value => {
126 branchcode => 'T_ILSDI',
128 } );
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,
137 } );
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 => '',
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 => '',
157 } );
159 # Create a new user:
160 my $brwr = $builder->build( {
161 source => 'Borrower',
162 value => {
163 categorycode => $cat->{'categorycode'},
164 branchcode => $lib->{'branchcode'},
166 } );
168 # Authorised value:
169 my $auth = $builder->build( {
170 source => 'AuthorisedValue',
171 value => {
172 category => $cat->{'categorycode'}
174 } );
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',
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',
192 } );
194 $builder->build(
196 source => 'Accountline',
197 value => {
198 borrowernumber => $brwr->{borrowernumber},
199 accountno => 1,
200 accounttype => 'xxx',
201 amountoutstanding => 10
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' );
212 my $reply = C4::ILSDI::Services::GetPatronInfo( $query );
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,
225 is( $reply->{'charges'}, '10.00',
226 'The \'charges\' attribute should be correctly filled (bug 17836)' );
228 # Check results:
229 is_deeply( $reply->{'attributes'}, [ $cmp ], 'Test GetPatronInfo - show_attributes parameter' );
231 # Cleanup
232 $schema->storage->txn_rollback;
236 subtest 'LookupPatron test' => sub {
238 plan tests => 9;
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 (
273 'cardnumber',
274 'userid',
275 'email',
276 'borrowernumber',
277 'surname',
278 'firstname'
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" );
287 # Cleanup
288 $schema->storage->txn_rollback;