3 # This Koha test module is a stub!
4 # Add more tests here!!!
8 use Test::More tests => 6;
17 my $matchpoint = 'userid';
18 my %mapping = ( 'userid' => { 'is' => 'uid' }, );
19 $ENV{'uid'} = "test1234";
22 # 'matchpoint' => $matchpoint,
23 # 'mapping' => \%mapping
28 my $context = new Test::MockModule('C4::Context');
34 my $dbh = DBI->connect( 'DBI:Mock:', '', '' )
35 || die "Cannot create handle: $DBI::errstr\n";
41 $context->mock( 'config', \&mockedConfig );
47 'matchpoint' => $matchpoint,
48 'mapping' => \%mapping
55 $context->mock( 'preference', \&mockedPref );
61 if ( $param eq 'OPACBaseURL' ) {
62 $return = "testopac.com";
71 $matchpoint = 'userid';
72 %mapping = ( 'userid' => { 'is' => 'uid' }, );
73 $ENV{'uid'} = "test1234";
79 my $dbh = C4::Context->dbh();
82 use_ok('C4::Auth_with_shibboleth');
83 $C4::Auth_with_shibboleth::debug = '0';
87 subtest "shib_ok tests" => sub {
91 # correct config, no debug
92 is( shib_ok(), '1', "good config" );
94 # bad config, no debug
96 warnings_are { $result = shib_ok() }
97 [ { carped => 'shibboleth matchpoint not defined' }, ],
98 "undefined matchpoint = fatal config, warning given";
99 is( $result, '0', "bad config" );
101 $matchpoint = 'email';
102 warnings_are { $result = shib_ok() }
103 [ { carped => 'shibboleth matchpoint not mapped' }, ],
104 "unmapped matchpoint = fatal config, warning given";
105 is( $result, '0', "bad config" );
107 # add test for undefined shibboleth block
113 #my $query = CGI->new();
114 #is(logout_shib($query),"https://".$opac."/Shibboleth.sso/Logout?return="."https://".$opac,"logout_shib");
117 my $query_string = 'language=en-GB';
118 $ENV{QUERY_STRING} = $query_string;
119 $ENV{SCRIPT_NAME} = '/cgi-bin/koha/opac-user.pl';
120 my $query = CGI->new($query_string);
122 login_shib_url($query),
123 'https://testopac.com'
124 . '/Shibboleth.sso/Login?target='
125 . 'https://testopac.com/cgi-bin/koha/opac-user.pl' . '%3F'
131 subtest "get_login_shib tests" => sub {
137 $C4::Auth_with_shibboleth::debug = '0';
138 warnings_are { $login = get_login_shib() }[],
139 "good config with debug off, no warnings recieved";
140 is( $login, "test1234",
141 "good config with debug off, attribute value returned" );
144 $C4::Auth_with_shibboleth::debug = '1';
145 warnings_are { $login = get_login_shib() }[
146 "koha borrower field to match: userid",
147 "shibboleth attribute to match: uid",
148 "uid value: test1234"
150 "good config with debug enabled, correct warnings recieved";
151 is( $login, "test1234",
152 "good config with debug enabled, attribute value returned" );
154 # bad config - with shib_ok implimented, we should never reach this sub with a bad config
158 subtest "checkpw_shib tests" => sub {
161 my $shib_login = 'test1234';
162 my @borrower_results =
163 ( [ 'cardnumber', 'userid' ], [ 'testcardnumber', 'test1234' ], );
164 $dbh->{mock_add_resultset} = \@borrower_results;
166 my ( $retval, $retcard, $retuserid );
169 $C4::Auth_with_shibboleth::debug = '0';
173 ( $retval, $retcard, $retuserid ) = checkpw_shib( $dbh, $shib_login );
175 [], "good user with no debug";
176 is( $retval, "1", "user authenticated" );
177 is( $retcard, "testcardnumber", "expected cardnumber returned" );
178 is( $retuserid, "test1234", "expected userid returned" );
182 ( $retval, $retcard, $retuserid ) = checkpw_shib( $dbh, $shib_login );
184 [], "bad user with no debug";
185 is( $retval, "0", "user not authenticated" );
188 $dbh->{mock_add_resultset} = \@borrower_results;
191 $C4::Auth_with_shibboleth::debug = '1';
195 ( $retval, $retcard, $retuserid ) = checkpw_shib( $dbh, $shib_login );
197 [ qr/checkpw_shib/, qr/User Shibboleth-authenticated as:/ ],
198 "good user with debug enabled";
199 is( $retval, "1", "user authenticated" );
200 is( $retcard, "testcardnumber", "expected cardnumber returned" );
201 is( $retuserid, "test1234", "expected userid returned" );
205 ( $retval, $retcard, $retuserid ) = checkpw_shib( $dbh, $shib_login );
209 qr/User Shibboleth-authenticated as:/,
210 qr/not a valid Koha user/
212 "bad user with debug enabled";
213 is( $retval, "0", "user not authenticated" );
218 is( C4::Auth_with_shibboleth::_get_uri(),
219 "https://testopac.com", "https opac uri returned" );