use C4::Debug;
use C4::Context;
+use Koha::Database;
use Carp;
use CGI;
sub checkpw_shib {
$debug and warn "checkpw_shib";
- my ( $dbh, $match ) = @_;
- my ( $retnumber, $userid );
+ my ( $match ) = @_;
my $config = _get_shib_config();
$debug and warn "User Shibboleth-authenticated as: $match";
- # Does the given shibboleth attribute value ($match) match a valid koha user ?
- my $sth = $dbh->prepare(
- "select cardnumber, userid from borrowers where $config->{matchpoint}=?"
- );
- $sth->execute($match);
- if ( $sth->rows ) {
- my @retvals = $sth->fetchrow;
- $retnumber = $retvals[0];
- $userid = $retvals[1];
- return ( 1, $retnumber, $userid );
+ # Does the given shibboleth attribute value ($match) match a valid koha user ?
+ my $borrower =
+ Koha::Database->new()->schema()->resultset('Borrower')
+ ->find( { $config->{matchpoint} => $match } );
+ if ( defined($borrower) ) {
+ return ( 1, $borrower->get_column('cardnumber'), $borrower->get_column('userid') );
}
# If we reach this point, the user is not a valid koha user
Given a database handle and a shib_login attribute, this routine checks for a matching local user and if found returns true, their cardnumber and their userid. If a match is not found, then this returns false.
- my ( $retval, $retcard, $retuserid ) = C4::Auth_with_shibboleth::checkpw_shib( $dbh, $shib_login );
+ my ( $retval, $retcard, $retuserid ) = C4::Auth_with_shibboleth::checkpw_shib( $shib_login );
=head1 SEE ALSO
use Test::More tests => 6;
use Test::MockModule;
use Test::Warn;
-use DBD::Mock;
+use Test::DBIx::Class {schema_class => 'Koha::Schema', connect_info => ['dbi:SQLite:dbname=:memory:','','']};
use CGI;
use C4::Context;
my %mapping = ( 'userid' => { 'is' => 'uid' }, );
$ENV{'uid'} = "test1234";
-#my %shibboleth = (
-# 'matchpoint' => $matchpoint,
-# 'mapping' => \%mapping
-#);
-
# Setup Mocks
## Mock Context
my $context = new Test::MockModule('C4::Context');
-### Mock ->dbh
-$context->mock(
- '_new_dbh',
- sub {
- my $dbh = DBI->connect( 'DBI:Mock:', '', '' )
- || die "Cannot create handle: $DBI::errstr\n";
- return $dbh;
- }
-);
-
### Mock ->config
$context->mock( 'config', \&mockedConfig );
return $return;
}
-# Convenience methods
-## Reset Context
+## Mock Database
+my $database = new Test::MockModule('Koha::Database');
+
+### Mock ->schema
+$database->mock( 'schema', \&mockedSchema );
+
+sub mockedSchema {
+ return Schema();
+}
+
+## Convenience method to reset config
sub reset_config {
$matchpoint = 'userid';
%mapping = ( 'userid' => { 'is' => 'uid' }, );
}
# Tests
-my $dbh = C4::Context->dbh();
+##############################################################
# Can module load
use_ok('C4::Auth_with_shibboleth');
## checkpw_shib
subtest "checkpw_shib tests" => sub {
- plan tests => 12;
-
- my $shib_login = 'test1234';
- my @borrower_results =
- ( [ 'cardnumber', 'userid' ], [ 'testcardnumber', 'test1234' ], );
- $dbh->{mock_add_resultset} = \@borrower_results;
+ plan tests => 13;
+ my $shib_login;
my ( $retval, $retcard, $retuserid );
+ # Setup Mock Database Data
+ fixtures_ok [
+ 'Borrower' => [
+ [qw/cardnumber userid surname address city/],
+ [qw/testcardnumber test1234 renvoize myaddress johnston/],
+ ],
+ ],
+ 'Installed some custom fixtures via the Populate fixture class';
+
# debug off
$C4::Auth_with_shibboleth::debug = '0';
# good user
+ $shib_login = "test1234";
warnings_are {
- ( $retval, $retcard, $retuserid ) = checkpw_shib( $dbh, $shib_login );
+ ( $retval, $retcard, $retuserid ) = checkpw_shib( $shib_login );
}
[], "good user with no debug";
is( $retval, "1", "user authenticated" );
is( $retuserid, "test1234", "expected userid returned" );
# bad user
+ $shib_login = 'martin';
warnings_are {
- ( $retval, $retcard, $retuserid ) = checkpw_shib( $dbh, $shib_login );
+ ( $retval, $retcard, $retuserid ) = checkpw_shib( $shib_login );
}
[], "bad user with no debug";
is( $retval, "0", "user not authenticated" );
- # reset db mock
- $dbh->{mock_add_resultset} = \@borrower_results;
-
# debug on
$C4::Auth_with_shibboleth::debug = '1';
# good user
+ $shib_login = "test1234";
warnings_exist {
- ( $retval, $retcard, $retuserid ) = checkpw_shib( $dbh, $shib_login );
+ ( $retval, $retcard, $retuserid ) = checkpw_shib( $shib_login );
}
[ qr/checkpw_shib/, qr/User Shibboleth-authenticated as:/ ],
"good user with debug enabled";
is( $retuserid, "test1234", "expected userid returned" );
# bad user
+ $shib_login = "martin";
warnings_exist {
- ( $retval, $retcard, $retuserid ) = checkpw_shib( $dbh, $shib_login );
+ ( $retval, $retcard, $retuserid ) = checkpw_shib( $shib_login );
}
[
qr/checkpw_shib/,
"https://testopac.com", "https opac uri returned" );
## _get_shib_config
+# Internal helper function, covered in tests above