Bug 16155: (QA followup) fix small bug in t/db_dependent/ILSDI_Services.t
[koha-equinox.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 => 3;
23 use t::lib::Mocks;
24 use t::lib::TestBuilder;
25
26 use Koha::AuthUtils;
27
28 BEGIN {
29     use_ok('C4::ILSDI::Services');
30 }
31
32 my $schema  = Koha::Database->schema;
33 my $dbh     = C4::Context->dbh;
34 my $builder = t::lib::TestBuilder->new;
35
36 subtest 'AuthenticatePatron test' => sub {
37
38     plan tests => 14;
39
40     $schema->storage->txn_begin;
41
42     my $plain_password = 'tomasito';
43
44     my $borrower = $builder->build({
45         source => 'Borrower',
46         value  => {
47             password => Koha::AuthUtils::hash_password( $plain_password )
48         }
49     });
50
51     my $query = new CGI;
52     $query->param( 'username', $borrower->{userid});
53     $query->param( 'password', $plain_password);
54
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");
58
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");
63
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");
69
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");
74
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" );
79
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" );
84
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");
90
91     $schema->storage->txn_rollback;
92 };
93
94
95 subtest 'GetPatronInfo/GetBorrowerAttributes test for extended patron attributes' => sub {
96
97     plan tests => 1;
98
99     $schema->storage->txn_begin;
100
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;
108
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 );
112
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',
118         }
119     } );
120
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,
127         }
128     } );
129
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                     => '',
138         }
139     } );
140
141     # Create a new user:
142     my $brwr = $builder->build( {
143         source => 'Borrower',
144         value  => {
145             categorycode => $cat->{'categorycode'},
146             branchcode   => $lib->{'branchcode'},
147         }
148     } );
149
150     # Authorised value:
151     my $auth = $builder->build( {
152         source => 'AuthorisedValue',
153         value  => {
154             category => $cat->{'categorycode'}
155         }
156     } );
157
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',
165         }
166     } );
167
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' );
173
174     my $reply = C4::ILSDI::Services::GetPatronInfo( $query );
175
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,
185     };
186
187     # Check results:
188     is_deeply( $reply->{'attributes'}, [ $cmp ], 'Test GetPatronInfo - show_attributes parameter' );
189
190     # Cleanup
191     $schema->storage->txn_rollback;
192 };
193
194 1;