5 use C4::Members qw/AddMember GetMember GetBorrowercategory/;
9 use Test::More tests => 16;
11 use t::lib::TestBuilder;
14 use_ok('C4::ILSDI::Services');
17 my $dbh = C4::Context->dbh;
20 $dbh->{AutoCommit} = 0;
21 $dbh->{RaiseError} = 1;
25 firstname => 'my firstname',
26 surname => 'my surname',
29 cardnumber => 'ilsdi-cardnumber',
30 userid => 'ilsdi-userid',
31 password => 'ilsdi-password',
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)
39 ('UT','Unit tester',99,99,0.000000,1,0.000000,'C','default');");
43 unless ( Koha::Libraries->find('UT') ) {
44 $dbh->do("INSERT INTO branches (branchcode,branchname) VALUES ('UT','Unit test library');");
48 my $borrowernumber = AddMember(%data);
49 my $borrower = GetMember( borrowernumber => $borrowernumber );
51 { # AuthenticatePatron test
54 $query->param('username',$borrower->{'userid'});
55 $query->param('password','ilsdi-password');
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");
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");
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");
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");
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");
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");
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");
97 $dbh->{AutoCommit} = 1;
98 $dbh->{RaiseError} = 0;
100 my $schema = Koha::Database->schema;
101 $schema->storage->txn_begin;
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;
111 { # GetPatronInfo/GetBorrowerAttributes test for extended patron attributes:
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 );
117 my $builder = t::lib::TestBuilder->new;
119 # Set up a library/branch for our user to belong to:
120 my $lib = $builder->build( {
123 branchcode => 'T_ILSDI',
127 # Create a new category for user to belong to:
128 my $cat = $builder->build( {
129 source => 'Category',
131 category_type => 'A',
132 BlockExpiredPatronOpacActions => -1,
136 # Create a new attribute type:
137 my $attr_type = $builder->build( {
138 source => 'BorrowerAttributeType',
142 authorised_value_category => '',
148 my $brwr = $builder->build( {
149 source => 'Borrower',
151 categorycode => $cat->{'categorycode'},
157 my $auth = $builder->build( {
158 source => 'AuthorisedValue',
160 category => $cat->{'categorycode'}
164 # Set the new attribute for our user:
165 my $attr = $builder->build( {
166 source => 'BorrowerAttribute',
168 borrowernumber => $brwr->{'borrowernumber'},
169 code => $attr_type->{'code'},
175 # Prepare and send web request for IL-SDI server:
177 $query->param( 'service', 'GetPatronInfo' );
178 $query->param( 'patron_id', $brwr->{'borrowernumber'} );
179 $query->param( 'show_attributes', '1' );
181 my $reply = C4::ILSDI::Services::GetPatronInfo( $query );
183 # Build a structure for comparision:
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'},
191 value => $attr->{'attribute'},
192 value_description => undef,
196 is_deeply( $reply->{'attributes'}, [ $cmp ] );
200 $schema->storage->txn_rollback;