Bug 9302: (QA follow-up) Consistency follow-up
[koha.git] / t / db_dependent / ILSDI_Services.t
blobd918e43a733d72ec0ec2b7285ced2aba060e3e24
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 my $borrower = $builder->build({
46 source => 'Borrower',
47 value => {
48 password => Koha::AuthUtils::hash_password( $plain_password )
50 });
52 my $query = new CGI;
53 $query->param( 'username', $borrower->{userid});
54 $query->param( 'password', $plain_password);
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");
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");
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");
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");
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" );
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" );
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");
92 $schema->storage->txn_rollback;
96 subtest 'GetPatronInfo/GetBorrowerAttributes test for extended patron attributes' => sub {
98 plan tests => 2;
100 $schema->storage->txn_begin;
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;
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 );
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',
120 } );
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,
129 } );
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 => '',
140 } );
142 # Create a new user:
143 my $brwr = $builder->build( {
144 source => 'Borrower',
145 value => {
146 categorycode => $cat->{'categorycode'},
147 branchcode => $lib->{'branchcode'},
149 } );
151 # Authorised value:
152 my $auth = $builder->build( {
153 source => 'AuthorisedValue',
154 value => {
155 category => $cat->{'categorycode'}
157 } );
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',
167 } );
169 $builder->build(
171 source => 'Accountline',
172 value => {
173 borrowernumber => $brwr->{borrowernumber},
174 accountno => 1,
175 accounttype => 'xxx',
176 amountoutstanding => 10
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' );
187 my $reply = C4::ILSDI::Services::GetPatronInfo( $query );
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,
200 is( $reply->{'charges'}, '10.00',
201 'The \'charges\' attribute should be correctly filled (bug 17836)' );
203 # Check results:
204 is_deeply( $reply->{'attributes'}, [ $cmp ], 'Test GetPatronInfo - show_attributes parameter' );
206 # Cleanup
207 $schema->storage->txn_rollback;
211 subtest 'LookupPatron test' => sub {
213 plan tests => 9;
215 $schema->storage->txn_begin;
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;
225 my $borrower = $builder->build({
226 source => 'Borrower',
229 my $query = CGI->new();
230 my $bad_result = C4::ILSDI::Services::LookupPatron($query);
231 is( $bad_result->{message}, 'PatronNotFound', 'No parameters' );
233 $query->delete_all();
234 $query->param( 'id', $borrower->{firstname} );
235 my $optional_result = C4::ILSDI::Services::LookupPatron($query);
237 $optional_result->{id},
238 $borrower->{borrowernumber},
239 'Valid Firstname only'
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' );
247 foreach my $id_type (
248 'cardnumber',
249 'userid',
250 'email',
251 'borrowernumber',
252 'surname',
253 'firstname'
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" );
262 # Cleanup
263 $schema->storage->txn_rollback;