3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use Module
::Load
::Conditional qw
/check_install/;
29 if ( check_install
( module
=> 'Test::DBIx::Class' ) ) {
32 plan skip_all
=> "Need Test::DBIx::Class"
36 use Test
::DBIx
::Class
{ schema_class
=> 'Koha::Schema', connect_info
=> ['dbi:SQLite:dbname=:memory:','',''] };
39 my $matchpoint = 'userid';
40 my %mapping = ( 'userid' => { 'is' => 'uid' }, );
41 $ENV{'uid'} = "test1234";
45 my $context = new Test
::MockModule
('C4::Context');
48 $context->mock( 'config', \
&mockedConfig
);
54 'matchpoint' => $matchpoint,
55 'mapping' => \
%mapping
62 $context->mock( 'preference', \
&mockedPref
);
68 if ( $param eq 'OPACBaseURL' ) {
69 $return = "testopac.com";
76 my $database = new Test
::MockModule
('Koha::Database');
79 $database->mock( 'schema', \
&mockedSchema
);
85 ## Convenience method to reset config
87 $matchpoint = 'userid';
88 %mapping = ( 'userid' => { 'is' => 'uid' }, );
89 $ENV{'uid'} = "test1234";
95 ##############################################################
98 use_ok
('C4::Auth_with_shibboleth');
99 $C4::Auth_with_shibboleth
::debug
= '0';
103 subtest
"shib_ok tests" => sub {
107 # correct config, no debug
108 is
( shib_ok
(), '1', "good config" );
110 # bad config, no debug
112 warnings_are
{ $result = shib_ok
() }
113 [ { carped
=> 'shibboleth matchpoint not defined' }, ],
114 "undefined matchpoint = fatal config, warning given";
115 is
( $result, '0', "bad config" );
117 $matchpoint = 'email';
118 warnings_are
{ $result = shib_ok
() }
119 [ { carped
=> 'shibboleth matchpoint not mapped' }, ],
120 "unmapped matchpoint = fatal config, warning given";
121 is
( $result, '0', "bad config" );
123 # add test for undefined shibboleth block
129 #my $query = CGI->new();
130 #is(logout_shib($query),"https://".$opac."/Shibboleth.sso/Logout?return="."https://".$opac,"logout_shib");
133 my $query_string = 'language=en-GB';
134 $ENV{QUERY_STRING
} = $query_string;
135 $ENV{SCRIPT_NAME
} = '/cgi-bin/koha/opac-user.pl';
136 my $query = CGI
->new($query_string);
138 login_shib_url
($query),
139 'https://testopac.com'
140 . '/Shibboleth.sso/Login?target='
141 . 'https://testopac.com/cgi-bin/koha/opac-user.pl' . '%3F'
147 subtest
"get_login_shib tests" => sub {
153 $C4::Auth_with_shibboleth
::debug
= '0';
154 warnings_are
{ $login = get_login_shib
() }[],
155 "good config with debug off, no warnings recieved";
156 is
( $login, "test1234",
157 "good config with debug off, attribute value returned" );
160 $C4::Auth_with_shibboleth
::debug
= '1';
161 warnings_are
{ $login = get_login_shib
() }[
162 "koha borrower field to match: userid",
163 "shibboleth attribute to match: uid",
164 "uid value: test1234"
166 "good config with debug enabled, correct warnings recieved";
167 is
( $login, "test1234",
168 "good config with debug enabled, attribute value returned" );
170 # bad config - with shib_ok implimented, we should never reach this sub with a bad config
174 subtest
"checkpw_shib tests" => sub {
178 my ( $retval, $retcard, $retuserid );
180 # Setup Mock Database Data
183 [qw
/cardnumber userid surname address city/],
184 [qw
/testcardnumber test1234 renvoize myaddress johnston/],
187 'Installed some custom fixtures via the Populate fixture class';
190 $C4::Auth_with_shibboleth
::debug
= '0';
193 $shib_login = "test1234";
195 ( $retval, $retcard, $retuserid ) = checkpw_shib
( $shib_login );
197 [], "good user with no debug";
198 is
( $retval, "1", "user authenticated" );
199 is
( $retcard, "testcardnumber", "expected cardnumber returned" );
200 is
( $retuserid, "test1234", "expected userid returned" );
203 $shib_login = 'martin';
205 ( $retval, $retcard, $retuserid ) = checkpw_shib
( $shib_login );
207 [], "bad user with no debug";
208 is
( $retval, "0", "user not authenticated" );
211 $C4::Auth_with_shibboleth
::debug
= '1';
214 $shib_login = "test1234";
216 ( $retval, $retcard, $retuserid ) = checkpw_shib
( $shib_login );
218 [ qr/checkpw_shib/, qr/User Shibboleth-authenticated as:/ ],
219 "good user with debug enabled";
220 is
( $retval, "1", "user authenticated" );
221 is
( $retcard, "testcardnumber", "expected cardnumber returned" );
222 is
( $retuserid, "test1234", "expected userid returned" );
225 $shib_login = "martin";
227 ( $retval, $retcard, $retuserid ) = checkpw_shib
( $shib_login );
231 qr/User Shibboleth-authenticated as:/,
232 qr/not a valid Koha user/
234 "bad user with debug enabled";
235 is
( $retval, "0", "user not authenticated" );
240 is
( C4
::Auth_with_shibboleth
::_get_uri
(),
241 "https://testopac.com", "https opac uri returned" );
244 # Internal helper function, covered in tests above