View | Details | Raw Unified | Return to bug 16320
Collapse All | Expand All

(-)a/t/db_dependent/ILSDI_Services.t (-89 / +83 lines)
Lines 1-122 Link Here
1
#!/usr/bin/perl
1
#!/usr/bin/perl
2
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
3
use Modern::Perl;
18
use Modern::Perl;
4
19
5
use C4::Members qw/AddMember GetMember GetBorrowercategory/;
6
use Koha::Libraries;
7
use CGI qw ( -utf8 );
20
use CGI qw ( -utf8 );
8
21
9
use Test::More tests => 16;
22
use Test::More tests => 3;
10
use t::lib::Mocks;
23
use t::lib::Mocks;
11
use t::lib::TestBuilder;
24
use t::lib::TestBuilder;
12
25
26
use Koha::AuthUtils;
27
13
BEGIN {
28
BEGIN {
14
    use_ok('C4::ILSDI::Services');
29
    use_ok('C4::ILSDI::Services');
15
}
30
}
16
31
17
my $dbh = C4::Context->dbh;
32
my $schema  = Koha::Database->schema;
18
33
my $dbh     = C4::Context->dbh;
19
# Start transaction
34
my $builder = t::lib::TestBuilder->new;
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
35
48
my $borrowernumber = AddMember(%data);
36
subtest 'AuthenticatePatron test' => sub {
49
my $borrower = GetMember( borrowernumber => $borrowernumber );
50
37
51
{ # AuthenticatePatron test
38
    plan tests => 14;
52
39
53
    my $query = new CGI;
40
    $schema->storage->txn_begin;
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
41
61
    $query->param('password','ilsdi-passworD');
42
    my $plain_password = 'tomasito';
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
43
66
    $query->param('password','ilsdi-password');
44
    my $borrower = $builder->build({
67
    $query->param('username','wrong-ilsdi-useriD');
45
        source => 'Borrower',
68
    $reply = C4::ILSDI::Services::AuthenticatePatron($query);
46
        value  => {
69
    is($reply->{'code'}, 'PatronNotFound', "non-existing userid - PatronNotFound");
47
            password => Koha::AuthUtils::hash_password( $plain_password )
70
    is($reply->{'id'}, undef, "id undef");
48
        }
49
    });
71
50
72
    $query->param('username',uc($borrower->{'userid'}));
51
    my $query = new CGI;
73
    $reply = C4::ILSDI::Services::AuthenticatePatron($query);
52
    $query->param( 'username', $borrower->{userid});
74
    is($reply->{'id'}, $borrowernumber, "userid is not case sensitive - Patron authenticated");
53
    $query->param( 'password', $plain_password);
75
    is($reply->{'code'}, undef, "Error code undef");
76
54
77
    $query->param('username',$borrower->{'cardnumber'});
55
    my $reply = C4::ILSDI::Services::AuthenticatePatron( $query );
78
    $reply = C4::ILSDI::Services::AuthenticatePatron($query);
56
    is( $reply->{id}, $borrower->{borrowernumber}, "userid and password - Patron authenticated" );
79
    is($reply->{'id'}, $borrowernumber, "cardnumber and password - Patron authenticated");
57
    is( $reply->{code}, undef, "Error code undef");
80
    is($reply->{'code'}, undef, "Error code undef");
81
58
82
    $query->param('password','ilsdi-passworD');
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 );
83
    $reply = C4::ILSDI::Services::AuthenticatePatron($query);
87
    $reply = C4::ILSDI::Services::AuthenticatePatron($query);
84
    is($reply->{'code'}, 'PatronNotFound', "cardnumber and wrong password - PatronNotFount");
88
    is( $reply->{code}, 'PatronNotFound', "non-existing cardnumer/userid - PatronNotFound" );
85
    is($reply->{'id'}, undef, "id undef");
89
    is( $reply->{id}, undef, "id undef");
86
90
87
    $query->param('username','randomcardnumber1234');
91
    $schema->storage->txn_rollback;
88
    $query->param('password','ilsdi-password');
92
};
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
93
}
94
95
# End transaction
96
$dbh->rollback;
97
$dbh->{AutoCommit} = 1;
98
$dbh->{RaiseError} = 0;
99
94
100
my $schema = Koha::Database->schema;
95
subtest 'GetPatronInfo/GetBorrowerAttributes test for extended patron attributes' => sub {
101
$schema->storage->txn_begin;
102
96
103
$schema->resultset( 'Issue' )->delete_all;
97
    plan tests => 1;
104
$schema->resultset( 'Borrower' )->delete_all;
105
$schema->resultset( 'BorrowerAttribute' )->delete_all;
106
$schema->resultset( 'BorrowerAttributeType' )->delete_all;
107
$schema->resultset( 'Category' )->delete_all;
108
$schema->resultset( 'Item' )->delete_all; # 'Branch' deps. on this
109
$schema->resultset( 'Branch' )->delete_all;
110
98
99
    $schema->storage->txn_begin;
111
100
112
{ # GetPatronInfo/GetBorrowerAttributes test for extended patron attributes:
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;
113
108
114
    # Configure Koha to enable ILS-DI server and extended attributes:
109
    # Configure Koha to enable ILS-DI server and extended attributes:
115
    t::lib::Mocks::mock_preference( 'ILS-DI', 1 );
110
    t::lib::Mocks::mock_preference( 'ILS-DI', 1 );
116
    t::lib::Mocks::mock_preference( 'ExtendedPatronAttributes', 1 );
111
    t::lib::Mocks::mock_preference( 'ExtendedPatronAttributes', 1 );
117
112
118
    my $builder = t::lib::TestBuilder->new;
119
120
    # Set up a library/branch for our user to belong to:
113
    # Set up a library/branch for our user to belong to:
121
    my $lib = $builder->build( {
114
    my $lib = $builder->build( {
122
        source => 'Branch',
115
        source => 'Branch',
Lines 195-201 $schema->resultset( 'Branch' )->delete_all; Link Here
195
188
196
    # Check results:
189
    # Check results:
197
    is_deeply( $reply->{'attributes'}, [ $cmp ], 'Test GetPatronInfo - show_attributes parameter' );
190
    is_deeply( $reply->{'attributes'}, [ $cmp ], 'Test GetPatronInfo - show_attributes parameter' );
198
}
199
191
200
# Cleanup
192
    # Cleanup
201
$schema->storage->txn_rollback;
193
    $schema->storage->txn_rollback;
194
};
195
196
1;
202
- 

Return to bug 16320