Bug 14257 - Add show_attributes to GetPatronInfo
[koha-equinox.git] / t / db_dependent / ILSDI_Services.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4
5 use C4::Members qw/AddMember GetMember GetBorrowercategory/;
6 use Koha::Libraries;
7 use CGI qw ( -utf8 );
8
9 use Test::More tests => 16;
10 use t::lib::Mocks;
11 use t::lib::TestBuilder;
12
13 BEGIN {
14     use_ok('C4::ILSDI::Services');
15 }
16
17 my $dbh = C4::Context->dbh;
18
19 # Start transaction
20 $dbh->{AutoCommit} = 0;
21 $dbh->{RaiseError} = 1;
22
23 # Create patron
24 my %data = (
25     firstname =>  'my firstname',
26     surname => 'my surname',
27     categorycode => 'UT',
28     branchcode => 'UT',
29     cardnumber => 'ilsdi-cardnumber',
30     userid => 'ilsdi-userid',
31     password => 'ilsdi-password',
32 );
33
34 # Crate patron category
35 unless ( GetBorrowercategory('UT') ) {
36     $dbh->do("INSERT INTO categories
37     (categorycode,description,enrolmentperiod,upperagelimit,enrolmentfee,overduenoticerequired,reservefee,category_type,default_privacy)
38         VALUES
39     ('UT','Unit tester',99,99,0.000000,1,0.000000,'C','default');");
40 }
41
42 # Create library
43 unless ( Koha::Libraries->find('UT') ) {
44     $dbh->do("INSERT INTO branches (branchcode,branchname) VALUES ('UT','Unit test library');");
45 }
46
47
48 my $borrowernumber = AddMember(%data);
49 my $borrower = GetMember( borrowernumber => $borrowernumber );
50
51 { # AuthenticatePatron test
52
53     my $query = new CGI;
54     $query->param('username',$borrower->{'userid'});
55     $query->param('password','ilsdi-password');
56
57     my $reply = C4::ILSDI::Services::AuthenticatePatron($query);
58     is($reply->{'id'}, $borrowernumber, "userid and password - Patron authenticated");
59     is($reply->{'code'}, undef, "Error code undef");
60
61     $query->param('password','ilsdi-passworD');
62     $reply = C4::ILSDI::Services::AuthenticatePatron($query);
63     is($reply->{'code'}, 'PatronNotFound', "userid and wrong password - PatronNotFound");
64     is($reply->{'id'}, undef, "id undef");
65
66     $query->param('password','ilsdi-password');
67     $query->param('username','wrong-ilsdi-useriD');
68     $reply = C4::ILSDI::Services::AuthenticatePatron($query);
69     is($reply->{'code'}, 'PatronNotFound', "non-existing userid - PatronNotFound");
70     is($reply->{'id'}, undef, "id undef");
71
72     $query->param('username',uc($borrower->{'userid'}));
73     $reply = C4::ILSDI::Services::AuthenticatePatron($query);
74     is($reply->{'id'}, $borrowernumber, "userid is not case sensitive - Patron authenticated");
75     is($reply->{'code'}, undef, "Error code undef");
76
77     $query->param('username',$borrower->{'cardnumber'});
78     $reply = C4::ILSDI::Services::AuthenticatePatron($query);
79     is($reply->{'id'}, $borrowernumber, "cardnumber and password - Patron authenticated");
80     is($reply->{'code'}, undef, "Error code undef");
81
82     $query->param('password','ilsdi-passworD');
83     $reply = C4::ILSDI::Services::AuthenticatePatron($query);
84     is($reply->{'code'}, 'PatronNotFound', "cardnumber and wrong password - PatronNotFount");
85     is($reply->{'id'}, undef, "id undef");
86
87     $query->param('username','randomcardnumber1234');
88     $query->param('password','ilsdi-password');
89     $reply = C4::ILSDI::Services::AuthenticatePatron($query);
90     is($reply->{'code'}, 'PatronNotFound', "non-existing cardnumer/userid - PatronNotFound");
91     is($reply->{'id'}, undef, "id undef");
92
93 }
94
95 # End transaction
96 $dbh->rollback;
97 $dbh->{AutoCommit} = 1;
98 $dbh->{RaiseError} = 0;
99
100 my $schema = Koha::Database->schema;
101 $schema->storage->txn_begin;
102
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
111 { # GetPatronInfo/GetBorrowerAttributes test for extended patron attributes:
112
113     # Configure Koha to enable ILS-DI server and extended attributes:
114     t::lib::Mocks::mock_preference( 'ILS-DI', 1 );
115     t::lib::Mocks::mock_preference( 'ExtendedPatronAttributes', 1 );
116
117     my $builder = t::lib::TestBuilder->new;
118
119     # Set up a library/branch for our user to belong to:
120     my $lib = $builder->build( {
121         source => 'Branch',
122         value => {
123             branchcode => 'T_ILSDI',
124         }
125     } );
126
127     # Create a new category for user to belong to:
128     my $cat = $builder->build( {
129         source => 'Category',
130         value  => {
131             category_type                 => 'A',
132             BlockExpiredPatronOpacActions => -1,
133         }
134     } );
135
136     # Create a new attribute type:
137     my $attr_type = $builder->build( {
138         source => 'BorrowerAttributeType',
139         value  => {
140             code                      => 'DOORCODE',
141             opac_display              => 0,
142             authorised_value_category => '',
143             class                     => '',
144         }
145     } );
146
147     # Create a new user:
148     my $brwr = $builder->build( {
149         source => 'Borrower',
150         value  => {
151             categorycode => $cat->{'categorycode'},
152             branchcode   => $lib,
153         }
154     } );
155
156     # Authorised value:
157     my $auth = $builder->build( {
158         source => 'AuthorisedValue',
159         value  => {
160             category => $cat->{'categorycode'}
161         }
162     } );
163
164     # Set the new attribute for our user:
165     my $attr = $builder->build( {
166         source => 'BorrowerAttribute',
167         value  => {
168             borrowernumber => $brwr->{'borrowernumber'},
169             code           => $attr_type->{'code'},
170             attribute      => '1337',
171             password       => undef,
172         }
173     } );
174
175     # Prepare and send web request for IL-SDI server:
176     my $query = new CGI;
177     $query->param( 'service', 'GetPatronInfo' );
178     $query->param( 'patron_id', $brwr->{'borrowernumber'} );
179     $query->param( 'show_attributes', '1' );
180
181     my $reply = C4::ILSDI::Services::GetPatronInfo( $query );
182
183     # Build a structure for comparision:
184     my $cmp = {
185         category_code     => $attr_type->{'category_code'},
186         class             => $attr_type->{'class'},
187         code              => $attr->{'code'},
188         description       => $attr_type->{'description'},
189         display_checkout  => $attr_type->{'display_checkout'},
190         password          => undef,
191         value             => $attr->{'attribute'},
192         value_description => undef,
193     };
194
195     # Check results:
196     is_deeply( $reply->{'attributes'}, [ $cmp ] );
197 }
198
199 # Cleanup
200 $schema->storage->txn_rollback;