Bug 21937: Add test to show autoBarcode annual increment bug
[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::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 $patron = $builder->build_object({ class => 'Koha::Patrons', value => { flags => 0 }});
51         $patron->update_password( $patron->userid, $password );
52
53         # Patron does not have permission to access staff interface
54         $s->auth( $patron->userid, $password );
55         like( $driver->get_title, qr(Access denied), 'Patron without permission should be redirected to the login form' );
56
57         $driver->get($mainpage . q|?logout.x=1|);
58         $patron->flags(4)->store; # catalogue permission
59         $s->auth( $patron->userid, $password );
60         like( $driver->get_title, qr(Koha staff client), 'Patron with flags catalogue should be able to login' );
61
62         $driver->get($mainpage . q|?logout.x=1|);
63         like( $driver->get_title(), qr(Log in to Koha), 'If logout is requested, login form should be displayed' );
64
65         $patron->flags(1)->store; # superlibrarian permission
66         $s->auth( $patron->userid, $password );
67         like( $driver->get_title, qr(Koha staff client), 'Patron with flags superlibrarian should be able to login' );
68     };
69
70     subtest 'OPAC interface authentication' => sub {
71         plan tests => 6;
72
73         my $mainpage = $s->opac_base_url . q|opac-main.pl|;
74
75         $driver->get($mainpage . q|?logout.x=1|); # Disconnect first! We are logged in if staff and opac interfaces are separated by ports
76
77         $driver->get($mainpage);
78         like( $driver->get_title, qr(Koha online catalog), 'Hitting the main page should not redirect to the login form');
79
80         my $password = Koha::AuthUtils::generate_password();
81         my $patron = $builder->build_object({ class => 'Koha::Patrons', value => { flags => 0 }});
82         $patron->update_password( $patron->userid, $password );
83
84         # Using the modal
85         $driver->find_element('//a[@class="login-link loginModal-trigger"]')->click;
86         $s->fill_form( { muserid => $patron->userid, mpassword => $password } );
87         $driver->find_element('//div[@id="loginModal"]//input[@type="submit"]')->click;
88         like( $driver->get_title, qr(Koha online catalog), 'Patron without permission should be able to login to the OPAC using the modal' );
89         $driver->find_element('//div[@id="userdetails"]');
90         like( $driver->get_title, qr(Your library home), 'Patron without permissions should be able to login to the OPAC using the modal');
91
92         $driver->find_element('//a[@id="logout"]')->click;
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 };