|
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&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 |
} |