Bug 17836: (ILSDI) Regression test
[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 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     my $borrower = $builder->build({
46         source => 'Borrower',
47         value  => {
48             password => Koha::AuthUtils::hash_password( $plain_password )
49         }
50     });
51
52     my $query = new CGI;
53     $query->param( 'username', $borrower->{userid});
54     $query->param( 'password', $plain_password);
55
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");
59
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");
64
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");
70
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");
75
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" );
80
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" );
85
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");
91
92     $schema->storage->txn_rollback;
93 };
94
95
96 subtest 'GetPatronInfo/GetBorrowerAttributes test for extended patron attributes' => sub {
97
98     plan tests => 2;
99
100     $schema->storage->txn_begin;
101
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;
109
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 );
113
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',
119         }
120     } );
121
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,
128         }
129     } );
130
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                     => '',
139         }
140     } );
141
142     # Create a new user:
143     my $brwr = $builder->build( {
144         source => 'Borrower',
145         value  => {
146             categorycode => $cat->{'categorycode'},
147             branchcode   => $lib->{'branchcode'},
148         }
149     } );
150
151     # Authorised value:
152     my $auth = $builder->build( {
153         source => 'AuthorisedValue',
154         value  => {
155             category => $cat->{'categorycode'}
156         }
157     } );
158
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',
166         }
167     } );
168
169     my $members = Test::MockModule->new('C4::Members');
170     $members->mock( 'GetMemberAccountBalance', sub { return ( 10, 10, 0 ); } );
171
172     # Prepare and send web request for IL-SDI server:
173     my $query = new CGI;
174     $query->param( 'service', 'GetPatronInfo' );
175     $query->param( 'patron_id', $brwr->{'borrowernumber'} );
176     $query->param( 'show_attributes', '1' );
177
178     my $reply = C4::ILSDI::Services::GetPatronInfo( $query );
179
180     # Build a structure for comparison:
181     my $cmp = {
182         category_code     => $attr_type->{'category_code'},
183         class             => $attr_type->{'class'},
184         code              => $attr->{'code'},
185         description       => $attr_type->{'description'},
186         display_checkout  => $attr_type->{'display_checkout'},
187         value             => $attr->{'attribute'},
188         value_description => undef,
189     };
190
191     is( $reply->{'charges'}, '10.00',
192         'The \'charges\' attribute should be correctly filled (bug 17836)' );
193
194     # Check results:
195     is_deeply( $reply->{'attributes'}, [ $cmp ], 'Test GetPatronInfo - show_attributes parameter' );
196
197     # Cleanup
198     $schema->storage->txn_rollback;
199 };
200
201 1;