Line 0
Link Here
|
0 |
- |
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 = 'koha'; |
36 |
my $password = 'koha'; |
37 |
my $base_url= 'http://'.C4::Context->preference("staffClientBaseURL")."/cgi-bin/koha/"; |
38 |
my $opac_url= C4::Context->preference("OPACBaseURL"); |
39 |
|
40 |
our $sample_data = { |
41 |
category => { |
42 |
categorycode => 'test_cat', |
43 |
description => 'test cat description', |
44 |
enrolmentperiod => '12', |
45 |
category_type => 'A' |
46 |
}, |
47 |
patron => { |
48 |
surname => 'test_patron_surname', |
49 |
cardnumber => '4242424242', |
50 |
userid => 'test_username', |
51 |
password => 'password', |
52 |
password2 => 'password' |
53 |
}, |
54 |
}; |
55 |
|
56 |
my $patronusername="test_username"; |
57 |
my $patronpassword="password"; |
58 |
|
59 |
our ( $borrowernumber, $start, $prev_time, $cleanup_needed ); |
60 |
|
61 |
SKIP: { |
62 |
eval { require Selenium::Remote::Driver; }; |
63 |
skip "Selenium::Remote::Driver is needed for selenium tests.", 20 if $@; |
64 |
|
65 |
$cleanup_needed = 1; |
66 |
|
67 |
open my $fh, '>>', '/tmp/output.txt'; |
68 |
|
69 |
my $driver = Selenium::Remote::Driver->new; |
70 |
$start = gettimeofday; |
71 |
$prev_time = $start; |
72 |
$driver->get($base_url."mainpage.pl"); |
73 |
like( $driver->get_title(), qr(Log in to Koha), ); |
74 |
auth( $driver, $login, $password ); |
75 |
time_diff("main"); |
76 |
|
77 |
$driver->get($base_url.'admin/categories.pl'); |
78 |
like( $driver->get_title(), qr(Patron categories), ); |
79 |
$driver->find_element('//a[@id="newcategory"]')->click; |
80 |
like( $driver->get_title(), qr(New category), ); |
81 |
fill_form( $driver, $sample_data->{category} ); |
82 |
$driver->find_element('//fieldset[@class="action"]/input[@type="submit"]')->click; |
83 |
|
84 |
time_diff("add patron category"); |
85 |
$driver->get($base_url.'/members/memberentry.pl?op=add&categorycode='.$sample_data->{category}{categorycode}); |
86 |
like( $driver->get_title(), qr(Add .*$sample_data->{category}{description}), ); |
87 |
fill_form( $driver, $sample_data->{patron} ); |
88 |
$driver->find_element('//button[@id="saverecord"]')->click; |
89 |
like( $driver->get_title(), qr(Patron details for $sample_data->{patron}{surname}), ); |
90 |
time_diff("add patron"); |
91 |
|
92 |
$driver->get($base_url.'/mainpage.pl?logout.x=1'); |
93 |
like( $driver->get_title(), qr(Log in to Koha), ); |
94 |
time_diff("Logout"); |
95 |
|
96 |
$driver->get($base_url."mainpage.pl"); |
97 |
like( $driver->get_title(), qr(Log in to Koha), ); |
98 |
patron_auth( $driver, $sample_data->{patron} ); |
99 |
time_diff("New patron logs into intranet"); |
100 |
|
101 |
$driver->get($base_url.'/mainpage.pl?logout.x=1'); |
102 |
like( $driver->get_title(), qr(Log in to Koha), ); |
103 |
time_diff("Logout of new patron from staff intranet"); |
104 |
|
105 |
$driver->get($opac_url); |
106 |
like( $driver->get_title(), qr(Koha online catalog), ); |
107 |
patron_opac_auth( $driver, $sample_data->{patron} ); |
108 |
time_diff("New patron logs into OPAC"); |
109 |
|
110 |
close $fh; |
111 |
$driver->quit(); |
112 |
}; |
113 |
|
114 |
END { |
115 |
cleanup() if $cleanup_needed; |
116 |
}; |
117 |
|
118 |
sub auth { |
119 |
my ( $driver, $login, $password) = @_; |
120 |
fill_form( $driver, { userid => 'koha', password => 'koha' } ); |
121 |
my $login_button = $driver->find_element('//input[@id="submit"]'); |
122 |
$login_button->submit(); |
123 |
} |
124 |
|
125 |
sub patron_auth { |
126 |
my ( $driver,$patronusername, $patronpassword) = @_; |
127 |
fill_form( $driver, { userid => $patronusername, password => $patronpassword } ); |
128 |
my $login_button = $driver->find_element('//input[@id="submit"]'); |
129 |
$login_button->submit(); |
130 |
} |
131 |
|
132 |
sub patron_opac_auth { |
133 |
my ( $driver,$patronusername, $patronpassword) = @_; |
134 |
fill_form( $driver, { userid => $patronusername, password => $patronpassword } ); |
135 |
my $login_button = $driver->find_element('//input[@value="Log in"]'); |
136 |
$login_button->submit(); |
137 |
} |
138 |
|
139 |
sub fill_form { |
140 |
my ( $driver, $values ) = @_; |
141 |
while ( my ( $id, $value ) = each %$values ) { |
142 |
my $element = $driver->find_element('//*[@id="'.$id.'"]'); |
143 |
my $tag = $element->get_tag_name(); |
144 |
if ( $tag eq 'input' ) { |
145 |
$driver->find_element('//input[@id="'.$id.'"]')->send_keys($value); |
146 |
} elsif ( $tag eq 'select' ) { |
147 |
$driver->find_element('//select[@id="'.$id.'"]/option[@value="'.$value.'"]')->click; |
148 |
} |
149 |
} |
150 |
} |
151 |
|
152 |
sub cleanup { |
153 |
my $dbh = C4::Context->dbh; |
154 |
$dbh->do(q|DELETE FROM categories WHERE categorycode = ?|, {}, $sample_data->{category}{categorycode}); |
155 |
$dbh->do(q|DELETE FROM borrowers WHERE userid = ?|, {}, $sample_data->{patron}{userid}); |
156 |
} |
157 |
|
158 |
sub time_diff { |
159 |
my $lib = shift; |
160 |
my $now = gettimeofday; |
161 |
warn "CP $lib = " . sprintf("%.2f", $now - $prev_time ) . "\n"; |
162 |
$prev_time = $now; |
163 |
} |