Bug 17741: Use Koha::Patron->holds when deleting a patron
[koha.git] / t / db_dependent / ILSDI_Services.t
blob15806a6f8abf378791cab02456f6d117a364893e
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 => 3;
23 use t::lib::Mocks;
24 use t::lib::TestBuilder;
26 use Koha::AuthUtils;
28 BEGIN {
29 use_ok('C4::ILSDI::Services');
32 my $schema = Koha::Database->schema;
33 my $dbh = C4::Context->dbh;
34 my $builder = t::lib::TestBuilder->new;
36 subtest 'AuthenticatePatron test' => sub {
38 plan tests => 14;
40 $schema->storage->txn_begin;
42 my $plain_password = 'tomasito';
44 my $borrower = $builder->build({
45 source => 'Borrower',
46 value => {
47 password => Koha::AuthUtils::hash_password( $plain_password )
49 });
51 my $query = new CGI;
52 $query->param( 'username', $borrower->{userid});
53 $query->param( 'password', $plain_password);
55 my $reply = C4::ILSDI::Services::AuthenticatePatron( $query );
56 is( $reply->{id}, $borrower->{borrowernumber}, "userid and password - Patron authenticated" );
57 is( $reply->{code}, undef, "Error code undef");
59 $query->param('password','ilsdi-passworD');
60 $reply = C4::ILSDI::Services::AuthenticatePatron( $query );
61 is( $reply->{code}, 'PatronNotFound', "userid and wrong password - PatronNotFound" );
62 is( $reply->{id}, undef, "id undef");
64 $query->param( 'password', $plain_password );
65 $query->param( 'username', 'wrong-ilsdi-useriD' );
66 $reply = C4::ILSDI::Services::AuthenticatePatron( $query );
67 is( $reply->{code}, 'PatronNotFound', "non-existing userid - PatronNotFound" );
68 is( $reply->{id}, undef, "id undef");
70 $query->param( 'username', uc( $borrower->{userid} ));
71 $reply = C4::ILSDI::Services::AuthenticatePatron( $query );
72 is( $reply->{id}, $borrower->{borrowernumber}, "userid is not case sensitive - Patron authenticated" );
73 is( $reply->{code}, undef, "Error code undef");
75 $query->param( 'username', $borrower->{cardnumber} );
76 $reply = C4::ILSDI::Services::AuthenticatePatron( $query );
77 is( $reply->{id}, $borrower->{borrowernumber}, "cardnumber and password - Patron authenticated" );
78 is( $reply->{code}, undef, "Error code undef" );
80 $query->param( 'password', 'ilsdi-passworD' );
81 $reply = C4::ILSDI::Services::AuthenticatePatron( $query );
82 is( $reply->{code}, 'PatronNotFound', "cardnumber and wrong password - PatronNotFount" );
83 is( $reply->{id}, undef, "id undef" );
85 $query->param( 'username', 'randomcardnumber1234' );
86 $query->param( 'password', $plain_password );
87 $reply = C4::ILSDI::Services::AuthenticatePatron($query);
88 is( $reply->{code}, 'PatronNotFound', "non-existing cardnumer/userid - PatronNotFound" );
89 is( $reply->{id}, undef, "id undef");
91 $schema->storage->txn_rollback;
95 subtest 'GetPatronInfo/GetBorrowerAttributes test for extended patron attributes' => sub {
97 plan tests => 1;
99 $schema->storage->txn_begin;
101 $schema->resultset( 'Issue' )->delete_all;
102 $schema->resultset( 'Borrower' )->delete_all;
103 $schema->resultset( 'BorrowerAttribute' )->delete_all;
104 $schema->resultset( 'BorrowerAttributeType' )->delete_all;
105 $schema->resultset( 'Category' )->delete_all;
106 $schema->resultset( 'Item' )->delete_all; # 'Branch' deps. on this
107 $schema->resultset( 'Branch' )->delete_all;
109 # Configure Koha to enable ILS-DI server and extended attributes:
110 t::lib::Mocks::mock_preference( 'ILS-DI', 1 );
111 t::lib::Mocks::mock_preference( 'ExtendedPatronAttributes', 1 );
113 # Set up a library/branch for our user to belong to:
114 my $lib = $builder->build( {
115 source => 'Branch',
116 value => {
117 branchcode => 'T_ILSDI',
119 } );
121 # Create a new category for user to belong to:
122 my $cat = $builder->build( {
123 source => 'Category',
124 value => {
125 category_type => 'A',
126 BlockExpiredPatronOpacActions => -1,
128 } );
130 # Create a new attribute type:
131 my $attr_type = $builder->build( {
132 source => 'BorrowerAttributeType',
133 value => {
134 code => 'DOORCODE',
135 opac_display => 0,
136 authorised_value_category => '',
137 class => '',
139 } );
141 # Create a new user:
142 my $brwr = $builder->build( {
143 source => 'Borrower',
144 value => {
145 categorycode => $cat->{'categorycode'},
146 branchcode => $lib->{'branchcode'},
148 } );
150 # Authorised value:
151 my $auth = $builder->build( {
152 source => 'AuthorisedValue',
153 value => {
154 category => $cat->{'categorycode'}
156 } );
158 # Set the new attribute for our user:
159 my $attr = $builder->build( {
160 source => 'BorrowerAttribute',
161 value => {
162 borrowernumber => $brwr->{'borrowernumber'},
163 code => $attr_type->{'code'},
164 attribute => '1337',
166 } );
168 # Prepare and send web request for IL-SDI server:
169 my $query = new CGI;
170 $query->param( 'service', 'GetPatronInfo' );
171 $query->param( 'patron_id', $brwr->{'borrowernumber'} );
172 $query->param( 'show_attributes', '1' );
174 my $reply = C4::ILSDI::Services::GetPatronInfo( $query );
176 # Build a structure for comparison:
177 my $cmp = {
178 category_code => $attr_type->{'category_code'},
179 class => $attr_type->{'class'},
180 code => $attr->{'code'},
181 description => $attr_type->{'description'},
182 display_checkout => $attr_type->{'display_checkout'},
183 value => $attr->{'attribute'},
184 value_description => undef,
187 # Check results:
188 is_deeply( $reply->{'attributes'}, [ $cmp ], 'Test GetPatronInfo - show_attributes parameter' );
190 # Cleanup
191 $schema->storage->txn_rollback;