Bug 12412: Add ability for plugins to convert arbitrary files to MARC from record...
[koha.git] / t / Auth_with_shibboleth.t
blob82c95b99bc00ae32759183d023f243a7f223af6a
1 #!/usr/bin/perl
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>.
18 use Modern::Perl;
20 use Module::Load::Conditional qw/check_install/;
21 use Test::More;
22 use Test::MockModule;
23 use Test::Warn;
25 use CGI;
26 use C4::Context;
28 BEGIN {
29 if ( check_install( module => 'Test::DBIx::Class' ) ) {
30 plan tests => 6;
31 } else {
32 plan skip_all => "Need Test::DBIx::Class"
36 use Test::DBIx::Class { schema_class => 'Koha::Schema', connect_info => ['dbi:SQLite:dbname=:memory:','',''] };
38 # Mock Variables
39 my $matchpoint = 'userid';
40 my %mapping = ( 'userid' => { 'is' => 'uid' }, );
41 $ENV{'uid'} = "test1234";
43 # Setup Mocks
44 ## Mock Context
45 my $context = new Test::MockModule('C4::Context');
47 ### Mock ->config
48 $context->mock( 'config', \&mockedConfig );
50 sub mockedConfig {
51 my $param = shift;
53 my %shibboleth = (
54 'matchpoint' => $matchpoint,
55 'mapping' => \%mapping
58 return \%shibboleth;
61 ### Mock ->preference
62 $context->mock( 'preference', \&mockedPref );
64 sub mockedPref {
65 my $param = $_[1];
66 my $return;
68 if ( $param eq 'OPACBaseURL' ) {
69 $return = "testopac.com";
72 return $return;
75 ## Mock Database
76 my $database = new Test::MockModule('Koha::Database');
78 ### Mock ->schema
79 $database->mock( 'schema', \&mockedSchema );
81 sub mockedSchema {
82 return Schema();
85 ## Convenience method to reset config
86 sub reset_config {
87 $matchpoint = 'userid';
88 %mapping = ( 'userid' => { 'is' => 'uid' }, );
89 $ENV{'uid'} = "test1234";
91 return 1;
94 # Tests
95 ##############################################################
97 # Can module load
98 use_ok('C4::Auth_with_shibboleth');
99 $C4::Auth_with_shibboleth::debug = '0';
101 # Subroutine tests
102 ## shib_ok
103 subtest "shib_ok tests" => sub {
104 plan tests => 5;
105 my $result;
107 # correct config, no debug
108 is( shib_ok(), '1', "good config" );
110 # bad config, no debug
111 $matchpoint = undef;
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
125 reset_config();
128 ## logout_shib
129 #my $query = CGI->new();
130 #is(logout_shib($query),"https://".$opac."/Shibboleth.sso/Logout?return="."https://".$opac,"logout_shib");
132 ## login_shib_url
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'
142 . $query_string,
143 "login shib url"
146 ## get_login_shib
147 subtest "get_login_shib tests" => sub {
148 plan tests => 4;
149 my $login;
151 # good config
152 ## debug off
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" );
159 ## debug on
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
173 ## checkpw_shib
174 subtest "checkpw_shib tests" => sub {
175 plan tests => 13;
177 my $shib_login;
178 my ( $retval, $retcard, $retuserid );
180 # Setup Mock Database Data
181 fixtures_ok [
182 'Borrower' => [
183 [qw/cardnumber userid surname address city/],
184 [qw/testcardnumber test1234 renvoize myaddress johnston/],
187 'Installed some custom fixtures via the Populate fixture class';
189 # debug off
190 $C4::Auth_with_shibboleth::debug = '0';
192 # good user
193 $shib_login = "test1234";
194 warnings_are {
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" );
202 # bad user
203 $shib_login = 'martin';
204 warnings_are {
205 ( $retval, $retcard, $retuserid ) = checkpw_shib( $shib_login );
207 [], "bad user with no debug";
208 is( $retval, "0", "user not authenticated" );
210 # debug on
211 $C4::Auth_with_shibboleth::debug = '1';
213 # good user
214 $shib_login = "test1234";
215 warnings_exist {
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" );
224 # bad user
225 $shib_login = "martin";
226 warnings_exist {
227 ( $retval, $retcard, $retuserid ) = checkpw_shib( $shib_login );
230 qr/checkpw_shib/,
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" );
239 ## _get_uri
240 is( C4::Auth_with_shibboleth::_get_uri(),
241 "https://testopac.com", "https opac uri returned" );
243 ## _get_shib_config
244 # Internal helper function, covered in tests above