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

(-)a/t/db_dependent/selenium/authenticate.t (-166 lines)
Lines 1-166 Link Here
1
#!/usr/bin/perl
2
3
# This file is part of Koha.
4
#
5
# Copyright (C) 2017  Catalyst IT
6
#
7
# Koha is free software; you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
#This selenium test is to test authentication, by performing the following: create a category and patron (same as basic_workflow.t). Then the superlibrarian logs out and the created patron must log into the staff intranet and OPAC
21
22
#Note: If you are testing this on kohadevbox with selenium installed in kohadevbox then you need to set the staffClientBaseURL to localhost:8080 and the OPACBaseURL to localhost:80
23
24
use Modern::Perl;
25
26
use Time::HiRes qw(gettimeofday);
27
use C4::Context;
28
use C4::Biblio qw( AddBiblio ); # We shouldn't use it
29
30
use Test::More tests => 9;
31
use MARC::Record;
32
use MARC::Field;
33
34
my $dbh = C4::Context->dbh;
35
my $login = $ENV{KOHA_USER} || 'koha';
36
my $password = $ENV{KOHA_PASS} || 'koha';
37
my $staff_client_base_url =
38
    $ENV{KOHA_INTRANET_URL} || C4::Context->preference("staffClientBaseUrl") || q{};
39
my $base_url= $staff_client_base_url . "/cgi-bin/koha/";
40
my $opac_url = $ENV{KOHA_OPAC_URL} || C4::Context->preference("OPACBaseURL") || q{};
41
42
43
our $sample_data = {
44
    category => {
45
        categorycode    => 'test_cat',
46
        description     => 'test cat description',
47
        enrolmentperiod => '12',
48
        category_type   => 'A'
49
    },
50
    patron => {
51
        surname    => 'test_patron_surname',
52
        cardnumber => '4242424242',
53
        userid     => 'test_username',
54
        password   => 'Password123',
55
        password2  => 'Password123'
56
    },
57
};
58
59
my $patronusername="test_username";
60
my $patronpassword="password";
61
62
our ( $borrowernumber, $start, $prev_time, $cleanup_needed );
63
64
SKIP: {
65
    eval { require Selenium::Remote::Driver; };
66
    skip "Selenium::Remote::Driver is needed for selenium tests.", 20 if $@;
67
68
    $cleanup_needed = 1;
69
70
    open my $fh, '>>', '/tmp/output.txt';
71
72
    my $driver = Selenium::Remote::Driver->new;
73
    $start = gettimeofday;
74
    $prev_time = $start;
75
    $driver->get($base_url."mainpage.pl");
76
    like( $driver->get_title(), qr(Log in to Koha), );
77
    auth( $driver, $login, $password );
78
    time_diff("main");
79
80
    $driver->get($base_url.'admin/categories.pl');
81
    like( $driver->get_title(), qr(Patron categories), );
82
    $driver->find_element('//a[@id="newcategory"]')->click;
83
    like( $driver->get_title(), qr(New category), );
84
    fill_form( $driver, $sample_data->{category} );
85
    $driver->find_element('//fieldset[@class="action"]/input[@type="submit"]')->click;
86
87
    time_diff("add patron category");
88
    $driver->get($base_url.'/members/memberentry.pl?op=add&amp;categorycode='.$sample_data->{category}{categorycode});
89
    like( $driver->get_title(), qr(Add .*$sample_data->{category}{description}), );
90
    fill_form( $driver, $sample_data->{patron} );
91
    $driver->find_element('//button[@id="saverecord"]')->click;
92
    like( $driver->get_title(), qr(Patron details for $sample_data->{patron}{surname}), );
93
    time_diff("add patron");
94
95
    $driver->get($base_url.'/mainpage.pl?logout.x=1');
96
    like( $driver->get_title(), qr(Log in to Koha), );
97
    time_diff("Logout");
98
99
    $driver->get($base_url."mainpage.pl");
100
    like( $driver->get_title(), qr(Log in to Koha), );
101
    patron_auth( $driver, $sample_data->{patron} );
102
    time_diff("New patron logs into intranet");
103
104
    $driver->get($base_url.'/mainpage.pl?logout.x=1');
105
    like( $driver->get_title(), qr(Log in to Koha), );
106
    time_diff("Logout of new patron from staff intranet");
107
108
    $driver->get($opac_url);
109
    like( $driver->get_title(), qr(Koha online catalog), );
110
    patron_opac_auth( $driver, $sample_data->{patron} );
111
    time_diff("New patron logs into OPAC");
112
113
    close $fh;
114
    $driver->quit();
115
};
116
117
END {
118
    cleanup() if $cleanup_needed;
119
};
120
121
sub auth {
122
    my ( $driver, $login, $password) = @_;
123
    fill_form( $driver, { userid => $login, password => $password } );
124
    my $login_button = $driver->find_element('//input[@id="submit"]');
125
    $login_button->submit();
126
}
127
128
sub patron_auth {
129
    my ( $driver,$patronusername, $patronpassword) = @_;
130
    fill_form( $driver, { userid => $patronusername, password => $patronpassword } );
131
    my $login_button = $driver->find_element('//input[@id="submit"]');
132
    $login_button->submit();
133
}
134
135
sub patron_opac_auth {
136
    my ( $driver,$patronusername, $patronpassword) = @_;
137
    fill_form( $driver, { userid => $patronusername, password => $patronpassword } );
138
    my $login_button = $driver->find_element('//input[@value="Log in"]');
139
    $login_button->submit();
140
}
141
142
sub fill_form {
143
    my ( $driver, $values ) = @_;
144
    while ( my ( $id, $value ) = each %$values ) {
145
        my $element = $driver->find_element('//*[@id="'.$id.'"]');
146
        my $tag = $element->get_tag_name();
147
        if ( $tag eq 'input' ) {
148
            $driver->find_element('//input[@id="'.$id.'"]')->send_keys($value);
149
        } elsif ( $tag eq 'select' ) {
150
            $driver->find_element('//select[@id="'.$id.'"]/option[@value="'.$value.'"]')->click;
151
        }
152
    }
153
}
154
155
sub cleanup {
156
    my $dbh = C4::Context->dbh;
157
    $dbh->do(q|DELETE FROM categories WHERE categorycode = ?|, {}, $sample_data->{category}{categorycode});
158
    $dbh->do(q|DELETE FROM borrowers WHERE userid = ?|, {}, $sample_data->{patron}{userid});
159
}
160
161
sub time_diff {
162
    my $lib = shift;
163
    my $now = gettimeofday;
164
    warn "CP $lib = " . sprintf("%.2f", $now - $prev_time ) . "\n";
165
    $prev_time = $now;
166
}
(-)a/t/db_dependent/selenium/authentication.t (+131 lines)
Line 0 Link Here
1
#!/usr/bin/perl
2
3
# This file is part of Koha.
4
#
5
# Copyright (C) 2017  Catalyst IT
6
# Copyright 2018 Koha Development team
7
#
8
# Koha is free software; you can redistribute it and/or modify it
9
# under the terms of the GNU General Public License as published by
10
# the Free Software Foundation; either version 3 of the License, or
11
# (at your option) any later version.
12
#
13
# Koha is distributed in the hope that it will be useful, but
14
# WITHOUT ANY WARRANTY; without even the implied warranty of
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
# GNU General Public License for more details.
17
#
18
# You should have received a copy of the GNU General Public License
19
# along with Koha; if not, see <http://www.gnu.org/licenses>.
20
21
#This selenium test is to test authentication, by performing the following: create a category and patron (same as basic_workflow.t). Then the superlibrarian logs out and the created patron must log into the staff intranet and OPAC
22
23
#Note: If you are testing this on kohadevbox with selenium installed in kohadevbox then you need to set the staffClientBaseURL to localhost:8080 and the OPACBaseURL to localhost:80
24
25
use Modern::Perl;
26
use Test::More tests => 2;
27
28
use C4::Context;
29
use Koha::AuthUtils;
30
use t::lib::Selenium;
31
use t::lib::TestBuilder;
32
33
my @data_to_cleanup;
34
35
SKIP: {
36
    eval { require Selenium::Remote::Driver; };
37
    skip "Selenium::Remote::Driver is needed for selenium tests.", 2 if $@;
38
39
    my $builder  = t::lib::TestBuilder->new;
40
    my $s        = t::lib::Selenium->new;
41
    my $driver   = $s->driver;
42
43
    subtest 'Staff interface authentication' => sub {
44
        plan tests => 5;
45
        my $mainpage = $s->base_url . q|mainpage.pl|;
46
        $driver->get($mainpage);
47
        like( $driver->get_title, qr(Log in to Koha), 'Hitting the main page should redirect to the login form');
48
49
        my $password = Koha::AuthUtils::generate_password();
50
        my $digest = Koha::AuthUtils::hash_password( $password );
51
        my $patron = $builder->build_object({ class => 'Koha::Patrons', value => { flags => 0 }});
52
        $patron->update_password( $patron->userid, $digest );
53
54
        # Patron does not have permission to access staff interface
55
        $s->auth( $patron->userid, $password );
56
        like( $driver->get_title, qr(Access denied), 'Patron without permission should be redirected to the login form' );
57
58
        $driver->get($mainpage . q|?logout.x=1|);
59
        $patron->flags(4)->store; # catalogue permission
60
        $s->auth( $patron->userid, $password );
61
        like( $driver->get_title, qr(Koha staff client), 'Patron with flags catalogue should be able to login' );
62
63
        $driver->get($mainpage . q|?logout.x=1|);
64
        like( $driver->get_title(), qr(Log in to Koha), 'If logout is requested, login form should be displayed' );
65
66
        $patron->flags(1)->store; # superlibrarian permission
67
        $s->auth( $patron->userid, $password );
68
        like( $driver->get_title, qr(Koha staff client), 'Patron with flags superlibrarian should be able to login' );
69
    };
70
71
    subtest 'OPAC interface authentication' => sub {
72
        plan tests => 6;
73
74
        my $mainpage = $s->opac_base_url . q|opac-main.pl|;
75
        $driver->get($mainpage);
76
        like( $driver->get_title, qr(Koha online catalog), 'Hitting the main page should not redirect to the login form');
77
78
        my $password = Koha::AuthUtils::generate_password();
79
        my $digest = Koha::AuthUtils::hash_password( $password );
80
        my $patron = $builder->build_object({ class => 'Koha::Patrons', value => { flags => 0 }});
81
        $patron->update_password( $patron->userid, $digest );
82
83
        # Using the modal
84
        $driver->find_element('//a[@class="login-link loginModal-trigger"]')->click;
85
        $s->fill_form( { muserid => $patron->userid, mpassword => $password } );
86
        $driver->find_element('//div[@id="loginModal"]//input[@type="submit"]')->click;
87
        like( $driver->get_title, qr(Koha online catalog), 'Patron without permission should be able to login to the OPAC using the modal' );
88
        $driver->find_element('//div[@id="userdetails"]');
89
        like( $driver->get_title, qr(Your library home), 'Patron without permissions should be able to login to the OPAC using the modal');
90
91
        $driver->find_element('//a[@id="logout"]')->click;
92
        $driver->capture_screenshot('1.png');
93
        $driver->find_element('//div[@id="login"]'); # logged out
94
95
        # Using the form on the right
96
        $s->fill_form( { userid => $patron->userid, password => $password } );
97
        $s->submit_form;
98
        $driver->find_element('//div[@id="userdetails"]');
99
        like( $driver->get_title, qr(Your library home), 'Patron without permissions should be able to login to the OPAC using the form on the right');
100
101
        $driver->find_element('//a[@id="logout"]')->click;
102
        $driver->find_element('//div[@id="login"]'); # logged out
103
104
105
        $patron->flags(4)->store; # catalogue permission
106
        $s->fill_form( { userid => $patron->userid, password => $password } );
107
        $s->submit_form;
108
        $driver->find_element('//div[@id="userdetails"]');
109
        like( $driver->get_title, qr(Your library home), 'Patron with catalogue permission should be able to login to the OPAC');
110
111
        $driver->find_element('//a[@id="logout"]')->click;
112
        $driver->find_element('//div[@id="login"]'); # logged out
113
114
        $patron->flags(1)->store; # superlibrarian permission
115
        $s->fill_form( { userid => $patron->userid, password => $password } );
116
        $s->submit_form;
117
        $driver->find_element('//div[@id="userdetails"]');
118
        like( $driver->get_title, qr(Your library home), 'Patron with superlibrarian permission should be able to login to the OPAC');
119
120
        $driver->find_element('//a[@id="logout"]')->click;
121
        $driver->find_element('//div[@id="login"]'); # logged out
122
123
        push @data_to_cleanup, $patron, $patron->category, $patron->library;
124
    };
125
126
    $driver->quit();
127
};
128
129
END {
130
    $_->delete for @data_to_cleanup;
131
};
(-)a/t/lib/Selenium.pm (-2 / +16 lines)
Lines 22-28 use Carp qw( croak ); Link Here
22
use C4::Context;
22
use C4::Context;
23
23
24
use base qw(Class::Accessor);
24
use base qw(Class::Accessor);
25
__PACKAGE__->mk_accessors(qw(login password base_url selenium_addr selenium_port driver));
25
__PACKAGE__->mk_accessors(qw(login password base_url opac_base_url selenium_addr selenium_port driver));
26
26
27
sub new {
27
sub new {
28
    my ( $class, $params ) = @_;
28
    my ( $class, $params ) = @_;
Lines 31-36 sub new { Link Here
31
    $self->{login}    = $params->{login}    || $config->{login};
31
    $self->{login}    = $params->{login}    || $config->{login};
32
    $self->{password} = $params->{password} || $config->{password};
32
    $self->{password} = $params->{password} || $config->{password};
33
    $self->{base_url} = $params->{base_url} || $config->{base_url};
33
    $self->{base_url} = $params->{base_url} || $config->{base_url};
34
    $self->{opac_base_url} = $params->{opac_base_url} || $config->{opac_base_url};
34
    $self->{selenium_addr} = $params->{selenium_addr} || $config->{selenium_addr};
35
    $self->{selenium_addr} = $params->{selenium_addr} || $config->{selenium_addr};
35
    $self->{selenium_port} = $params->{selenium_port} || $config->{selenium_port};
36
    $self->{selenium_port} = $params->{selenium_port} || $config->{selenium_port};
36
    $self->{driver} = Selenium::Remote::Driver->new(
37
    $self->{driver} = Selenium::Remote::Driver->new(
Lines 54-59 sub config { Link Here
54
        login    => $ENV{KOHA_USER} || 'koha',
55
        login    => $ENV{KOHA_USER} || 'koha',
55
        password => $ENV{KOHA_PASS} || 'koha',
56
        password => $ENV{KOHA_PASS} || 'koha',
56
        base_url => ( $ENV{KOHA_INTRANET_URL} || C4::Context->preference("staffClientBaseURL") ) . "/cgi-bin/koha/",
57
        base_url => ( $ENV{KOHA_INTRANET_URL} || C4::Context->preference("staffClientBaseURL") ) . "/cgi-bin/koha/",
58
        opac_base_url => ( $ENV{KOHA_OPAC_URL} || C4::Context->preference("OPACBaseURL") ) . "/cgi-bin/koha/",
57
        selenium_addr => $ENV{SELENIUM_ADDR} || 'localhost',
59
        selenium_addr => $ENV{SELENIUM_ADDR} || 'localhost',
58
        selenium_port => $ENV{SELENIUM_PORT} || 4444,
60
        selenium_port => $ENV{SELENIUM_PORT} || 4444,
59
    };
61
    };
Lines 72-77 sub auth { Link Here
72
    $login_button->submit();
74
    $login_button->submit();
73
}
75
}
74
76
77
sub opac_auth {
78
    my ( $self, $login, $password ) = @_;
79
80
    $login ||= $self->login;
81
    $password ||= $self->password;
82
    my $mainpage = $self->base_url . 'opac-main.pl';
83
84
    $self->driver->get($mainpage);
85
    $self->fill_form( { userid => $login, password => $password } );
86
    my $login_button = $self->driver->find_element('//input[@id="submit"]');
87
    $login_button->submit();
88
}
89
75
sub fill_form {
90
sub fill_form {
76
    my ( $self, $values ) = @_;
91
    my ( $self, $values ) = @_;
77
    while ( my ( $id, $value ) = each %$values ) {
92
    while ( my ( $id, $value ) = each %$values ) {
78
- 

Return to bug 19181