Bug 23816: Fix selenium tests
[koha.git] / t / db_dependent / selenium / authentication.t
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::Mocks;
31 use t::lib::Selenium;
32 use t::lib::TestBuilder;
33
34 my @data_to_cleanup;
35
36 SKIP: {
37     eval { require Selenium::Remote::Driver; };
38     skip "Selenium::Remote::Driver is needed for selenium tests.", 2 if $@;
39
40     my $builder  = t::lib::TestBuilder->new;
41     my $s        = t::lib::Selenium->new;
42     my $driver   = $s->driver;
43
44     subtest 'Staff interface authentication' => sub {
45         plan tests => 5;
46         my $mainpage = $s->base_url . q|mainpage.pl|;
47         $driver->get($mainpage);
48         like( $driver->get_title, qr(Log in to Koha), 'Hitting the main page should redirect to the login form');
49
50         my $patron = $builder->build_object({ class => 'Koha::Patrons', value => { flags => 0 }});
51         my $password = Koha::AuthUtils::generate_password($patron->category);
52         t::lib::Mocks::mock_preference( 'RequireStrongPassword', 0 );
53         $patron->set_password({ password => $password });
54
55         # Patron does not have permission to access staff interface
56         $s->auth( $patron->userid, $password );
57         like( $driver->get_title, qr(Access denied), 'Patron without permission should be redirected to the login form' );
58
59         $driver->get($mainpage . q|?logout.x=1|);
60         $patron->flags(4)->store; # catalogue permission
61         $s->auth( $patron->userid, $password );
62         like( $driver->get_title, qr(Koha staff interface), 'Patron with flags catalogue should be able to login' );
63
64         $driver->get($mainpage . q|?logout.x=1|);
65         like( $driver->get_title(), qr(Log in to Koha), 'If logout is requested, login form should be displayed' );
66
67         $patron->flags(1)->store; # superlibrarian permission
68         $s->auth( $patron->userid, $password );
69         like( $driver->get_title, qr(Koha staff interface), 'Patron with flags superlibrarian should be able to login' );
70
71         push @data_to_cleanup, $patron, $patron->category, $patron->library;
72     };
73
74     subtest 'OPAC interface authentication' => sub {
75         plan tests => 6;
76
77         my $mainpage = $s->opac_base_url . q|opac-main.pl|;
78
79         $driver->get($mainpage . q|?logout.x=1|); # Disconnect first! We are logged in if staff and opac interfaces are separated by ports
80
81         $driver->get($mainpage);
82         like( $driver->get_title, qr(Koha online catalog), 'Hitting the main page should not redirect to the login form');
83
84         my $patron = $builder->build_object({ class => 'Koha::Patrons', value => { flags => 0 }});
85         my $password = Koha::AuthUtils::generate_password($patron->category);
86         t::lib::Mocks::mock_preference( 'RequireStrongPassword', 0 );
87         $patron->set_password({ password => $password });
88
89         # Using the modal
90         $driver->find_element('//a[@class="nav-link login-link loginModal-trigger"]')->click;
91         $s->fill_form( { muserid => $patron->userid, mpassword => $password } );
92         $driver->find_element('//div[@id="loginModal"]//input[@type="submit"]')->click;
93         like( $driver->get_title, qr(Koha online catalog), 'Patron without permission should be able to login to the OPAC using the modal' );
94         $driver->find_element('//div[@id="userdetails"]');
95         like( $driver->get_title, qr(Your library home), 'Patron without permissions should be able to login to the OPAC using the modal');
96
97         $driver->find_element('//a[@id="user-menu"]')->click;
98         $driver->find_element('//a[@id="logout"]')->click;
99         $driver->get($mainpage); # This should not be needed but we the next find_element fails randomly
100
101         { # Temporary debug
102             $driver->error_handler(
103                 sub {
104                     my ( $driver, $selenium_error ) = @_;
105                     print STDERR "\nSTRACE:";
106                     my $i = 1;
107                     while ( (my @call_details = (caller($i++))) ){
108                         print STDERR "\t" . $call_details[1]. ":" . $call_details[2] . " in " . $call_details[3]."\n";
109                     }
110                     print STDERR "\n";
111                     print STDERR sprintf("Is logged in patron: %s (%s)?\n", $patron->firstname, $patron->surname );
112                     $s->capture( $driver );
113                     croak $selenium_error;
114                 }
115             );
116
117             $driver->find_element('//div[@id="login"]'); # logged out
118             $s->add_error_handler; # Reset to the default error handler
119         }
120
121         # Using the form on the right
122         $s->fill_form( { userid => $patron->userid, password => $password } );
123         $s->submit_form;
124         $driver->find_element('//div[@id="userdetails"]');
125         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');
126
127         $driver->find_element('//a[@id="user-menu"]')->click;
128         $driver->find_element('//a[@id="logout"]')->click;
129         $driver->find_element('//div[@id="login"]'); # logged out
130
131
132         $patron->flags(4)->store; # catalogue permission
133         $s->fill_form( { userid => $patron->userid, password => $password } );
134         $s->submit_form;
135         $driver->find_element('//div[@id="userdetails"]');
136         like( $driver->get_title, qr(Your library home), 'Patron with catalogue permission should be able to login to the OPAC');
137
138         $driver->find_element('//a[@id="user-menu"]')->click;
139         $driver->find_element('//a[@id="logout"]')->click;
140         $driver->find_element('//div[@id="login"]'); # logged out
141
142         $patron->flags(1)->store; # superlibrarian permission
143         $s->fill_form( { userid => $patron->userid, password => $password } );
144         $s->submit_form;
145         $driver->find_element('//div[@id="userdetails"]');
146         like( $driver->get_title, qr(Your library home), 'Patron with superlibrarian permission should be able to login to the OPAC');
147
148         $driver->find_element('//a[@id="user-menu"]')->click;
149         $driver->find_element('//a[@id="logout"]')->click;
150         $driver->find_element('//div[@id="login"]'); # logged out
151
152         push @data_to_cleanup, $patron, $patron->category, $patron->library;
153     };
154
155     $driver->quit();
156 };
157
158 END {
159     $_->delete for @data_to_cleanup;
160 };