3 # Add more tests here!!!
7 use Test::More tests => 6;
16 my $matchpoint = 'userid';
17 my %mapping = ( 'userid' => { 'is' => 'uid' }, );
18 $ENV{'uid'} = "test1234";
21 # 'matchpoint' => $matchpoint,
22 # 'mapping' => \%mapping
27 my $context = new Test::MockModule('C4::Context');
33 my $dbh = DBI->connect( 'DBI:Mock:', '', '' )
34 || die "Cannot create handle: $DBI::errstr\n";
40 $context->mock( 'config', \&mockedConfig );
46 'matchpoint' => $matchpoint,
47 'mapping' => \%mapping
54 $context->mock( 'preference', \&mockedPref );
60 if ( $param eq 'OPACBaseURL' ) {
61 $return = "testopac.com";
70 $matchpoint = 'userid';
71 %mapping = ( 'userid' => { 'is' => 'uid' }, );
72 $ENV{'uid'} = "test1234";
78 my $dbh = C4::Context->dbh();
81 use_ok('C4::Auth_with_shibboleth');
82 $C4::Auth_with_shibboleth::debug = '0';
86 subtest "shib_ok tests" => sub {
90 # correct config, no debug
91 is( shib_ok(), '1', "good config" );
93 # bad config, no debug
95 warnings_are { $result = shib_ok() }
96 [ { carped => 'shibboleth matchpoint not defined' }, ],
97 "undefined matchpoint = fatal config, warning given";
98 is( $result, '0', "bad config" );
100 $matchpoint = 'email';
101 warnings_are { $result = shib_ok() }
102 [ { carped => 'shibboleth matchpoint not mapped' }, ],
103 "unmapped matchpoint = fatal config, warning given";
104 is( $result, '0', "bad config" );
106 # add test for undefined shibboleth block
112 #my $query = CGI->new();
113 #is(logout_shib($query),"https://".$opac."/Shibboleth.sso/Logout?return="."https://".$opac,"logout_shib");
116 my $query_string = 'language=en-GB';
117 $ENV{QUERY_STRING} = $query_string;
118 $ENV{SCRIPT_NAME} = '/cgi-bin/koha/opac-user.pl';
119 my $query = CGI->new($query_string);
121 login_shib_url($query),
122 'https://testopac.com'
123 . '/Shibboleth.sso/Login?target='
124 . 'https://testopac.com/cgi-bin/koha/opac-user.pl' . '%3F'
130 subtest "get_login_shib tests" => sub {
136 $C4::Auth_with_shibboleth::debug = '0';
137 warnings_are { $login = get_login_shib() }[],
138 "good config with debug off, no warnings recieved";
139 is( $login, "test1234",
140 "good config with debug off, attribute value returned" );
143 $C4::Auth_with_shibboleth::debug = '1';
144 warnings_are { $login = get_login_shib() }[
145 "koha borrower field to match: userid",
146 "shibboleth attribute to match: uid",
147 "uid value: test1234"
149 "good config with debug enabled, correct warnings recieved";
150 is( $login, "test1234",
151 "good config with debug enabled, attribute value returned" );
153 # bad config - with shib_ok implimented, we should never reach this sub with a bad config
157 subtest "checkpw_shib tests" => sub {
160 my $shib_login = 'test1234';
161 my @borrower_results =
162 ( [ 'cardnumber', 'userid' ], [ 'testcardnumber', 'test1234' ], );
163 $dbh->{mock_add_resultset} = \@borrower_results;
165 my ( $retval, $retcard, $retuserid );
168 $C4::Auth_with_shibboleth::debug = '0';
172 ( $retval, $retcard, $retuserid ) = checkpw_shib( $dbh, $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" );
181 ( $retval, $retcard, $retuserid ) = checkpw_shib( $dbh, $shib_login );
183 [], "bad user with no debug";
184 is( $retval, "0", "user not authenticated" );
187 $dbh->{mock_add_resultset} = \@borrower_results;
190 $C4::Auth_with_shibboleth::debug = '1';
194 ( $retval, $retcard, $retuserid ) = checkpw_shib( $dbh, $shib_login );
196 [ qr/checkpw_shib/, qr/User Shibboleth-authenticated as:/ ],
197 "good user with debug enabled";
198 is( $retval, "1", "user authenticated" );
199 is( $retcard, "testcardnumber", "expected cardnumber returned" );
200 is( $retuserid, "test1234", "expected userid returned" );
204 ( $retval, $retcard, $retuserid ) = checkpw_shib( $dbh, $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" );