3 # Add more tests here!!!
7 use Test::More tests => 6;
10 use Test::DBIx::Class {schema_class => 'Koha::Schema', connect_info => ['dbi:SQLite:dbname=:memory:','','']};
16 my $matchpoint = 'userid';
17 my %mapping = ( 'userid' => { 'is' => 'uid' }, );
18 $ENV{'uid'} = "test1234";
22 my $context = new Test::MockModule('C4::Context');
25 $context->mock( 'config', \&mockedConfig );
31 'matchpoint' => $matchpoint,
32 'mapping' => \%mapping
39 $context->mock( 'preference', \&mockedPref );
45 if ( $param eq 'OPACBaseURL' ) {
46 $return = "testopac.com";
53 my $database = new Test::MockModule('Koha::Database');
56 $database->mock( 'schema', \&mockedSchema );
62 ## Convenience method to reset config
64 $matchpoint = 'userid';
65 %mapping = ( 'userid' => { 'is' => 'uid' }, );
66 $ENV{'uid'} = "test1234";
72 ##############################################################
75 use_ok('C4::Auth_with_shibboleth');
76 $C4::Auth_with_shibboleth::debug = '0';
80 subtest "shib_ok tests" => sub {
84 # correct config, no debug
85 is( shib_ok(), '1', "good config" );
87 # bad config, no debug
89 warnings_are { $result = shib_ok() }
90 [ { carped => 'shibboleth matchpoint not defined' }, ],
91 "undefined matchpoint = fatal config, warning given";
92 is( $result, '0', "bad config" );
94 $matchpoint = 'email';
95 warnings_are { $result = shib_ok() }
96 [ { carped => 'shibboleth matchpoint not mapped' }, ],
97 "unmapped matchpoint = fatal config, warning given";
98 is( $result, '0', "bad config" );
100 # add test for undefined shibboleth block
106 #my $query = CGI->new();
107 #is(logout_shib($query),"https://".$opac."/Shibboleth.sso/Logout?return="."https://".$opac,"logout_shib");
110 my $query_string = 'language=en-GB';
111 $ENV{QUERY_STRING} = $query_string;
112 $ENV{SCRIPT_NAME} = '/cgi-bin/koha/opac-user.pl';
113 my $query = CGI->new($query_string);
115 login_shib_url($query),
116 'https://testopac.com'
117 . '/Shibboleth.sso/Login?target='
118 . 'https://testopac.com/cgi-bin/koha/opac-user.pl' . '%3F'
124 subtest "get_login_shib tests" => sub {
130 $C4::Auth_with_shibboleth::debug = '0';
131 warnings_are { $login = get_login_shib() }[],
132 "good config with debug off, no warnings recieved";
133 is( $login, "test1234",
134 "good config with debug off, attribute value returned" );
137 $C4::Auth_with_shibboleth::debug = '1';
138 warnings_are { $login = get_login_shib() }[
139 "koha borrower field to match: userid",
140 "shibboleth attribute to match: uid",
141 "uid value: test1234"
143 "good config with debug enabled, correct warnings recieved";
144 is( $login, "test1234",
145 "good config with debug enabled, attribute value returned" );
147 # bad config - with shib_ok implimented, we should never reach this sub with a bad config
151 subtest "checkpw_shib tests" => sub {
155 my ( $retval, $retcard, $retuserid );
157 # Setup Mock Database Data
160 [qw/cardnumber userid surname address city/],
161 [qw/testcardnumber test1234 renvoize myaddress johnston/],
164 'Installed some custom fixtures via the Populate fixture class';
167 $C4::Auth_with_shibboleth::debug = '0';
170 $shib_login = "test1234";
172 ( $retval, $retcard, $retuserid ) = checkpw_shib( $shib_login );
174 [], "good user with no debug";
175 is( $retval, "1", "user authenticated" );
176 is( $retcard, "testcardnumber", "expected cardnumber returned" );
177 is( $retuserid, "test1234", "expected userid returned" );
180 $shib_login = 'martin';
182 ( $retval, $retcard, $retuserid ) = checkpw_shib( $shib_login );
184 [], "bad user with no debug";
185 is( $retval, "0", "user not authenticated" );
188 $C4::Auth_with_shibboleth::debug = '1';
191 $shib_login = "test1234";
193 ( $retval, $retcard, $retuserid ) = checkpw_shib( $shib_login );
195 [ qr/checkpw_shib/, qr/User Shibboleth-authenticated as:/ ],
196 "good user with debug enabled";
197 is( $retval, "1", "user authenticated" );
198 is( $retcard, "testcardnumber", "expected cardnumber returned" );
199 is( $retuserid, "test1234", "expected userid returned" );
202 $shib_login = "martin";
204 ( $retval, $retcard, $retuserid ) = checkpw_shib( $shib_login );
208 qr/User Shibboleth-authenticated as:/,
209 qr/not a valid Koha user/
211 "bad user with debug enabled";
212 is( $retval, "0", "user not authenticated" );
217 is( C4::Auth_with_shibboleth::_get_uri(),
218 "https://testopac.com", "https opac uri returned" );
221 # Internal helper function, covered in tests above