use Modern::Perl;
+$| = 1;
use Module::Load::Conditional qw/check_install/;
use Test::More;
use Test::MockModule;
BEGIN {
if ( check_install( module => 'Test::DBIx::Class' ) ) {
- plan tests => 6;
- } else {
- plan skip_all => "Need Test::DBIx::Class"
+ plan tests => 17;
+ }
+ else {
+ plan skip_all => "Need Test::DBIx::Class";
}
}
-use Test::DBIx::Class { schema_class => 'Koha::Schema', connect_info => ['dbi:SQLite:dbname=:memory:','',''] };
+use Test::DBIx::Class {
+ schema_class => 'Koha::Schema',
+ connect_info => [ 'dbi:SQLite:dbname=:memory:', '', '' ]
+};
# Mock Variables
my $matchpoint = 'userid';
-my %mapping = ( 'userid' => { 'is' => 'uid' }, );
-$ENV{'uid'} = "test1234";
+my $autocreate = 0;
+my $sync = 0;
+my %mapping = (
+ 'userid' => { 'is' => 'uid' },
+ 'surname' => { 'is' => 'sn' },
+ 'dateexpiry' => { 'is' => 'exp' },
+ 'categorycode' => { 'is' => 'cat' },
+ 'address' => { 'is' => 'add' },
+ 'city' => { 'is' => 'city' },
+);
+$ENV{'uid'} = "test1234";
+$ENV{'sn'} = undef;
+$ENV{'exp'} = undef;
+$ENV{'cat'} = undef;
+$ENV{'add'} = undef;
+$ENV{'city'} = undef;
# Setup Mocks
## Mock Context
### Mock ->config
$context->mock( 'config', \&mockedConfig );
-sub mockedConfig {
- my $param = shift;
-
- my %shibboleth = (
- 'matchpoint' => $matchpoint,
- 'mapping' => \%mapping
- );
-
- return \%shibboleth;
-}
-
### Mock ->preference
+my $OPACBaseURL = "testopac.com";
+my $staffClientBaseURL = "teststaff.com";
$context->mock( 'preference', \&mockedPref );
-sub mockedPref {
- my $param = $_[1];
- my $return;
+### Mock ->tz
+$context->mock( 'timezone', sub { return 'local'; } );
- if ( $param eq 'OPACBaseURL' ) {
- $return = "testopac.com";
- }
-
- return $return;
-}
+### Mock ->interface
+my $interface = 'opac';
+$context->mock( 'interface', \&mockedInterface );
## 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' }, );
- $ENV{'uid'} = "test1234";
-
- return 1;
-}
-
# Tests
##############################################################
# Can module load
-use_ok('C4::Auth_with_shibboleth');
+use C4::Auth_with_shibboleth;
+require_ok('C4::Auth_with_shibboleth');
$C4::Auth_with_shibboleth::debug = '0';
# Subroutine tests
## debug off
$C4::Auth_with_shibboleth::debug = '0';
warnings_are { $login = get_login_shib() }[],
- "good config with debug off, no warnings recieved";
+ "good config with debug off, no warnings received";
is( $login, "test1234",
"good config with debug off, attribute value returned" );
"shibboleth attribute to match: uid",
"uid value: test1234"
],
- "good config with debug enabled, correct warnings recieved";
+ "good config with debug enabled, correct warnings received";
is( $login, "test1234",
"good config with debug enabled, attribute value returned" );
-# bad config - with shib_ok implimented, we should never reach this sub with a bad config
+# bad config - with shib_ok implemented, we should never reach this sub with a bad config
};
## checkpw_shib
subtest "checkpw_shib tests" => sub {
- plan tests => 13;
+ plan tests => 21;
my $shib_login;
my ( $retval, $retcard, $retuserid );
[qw/cardnumber userid surname address city/],
[qw/testcardnumber test1234 renvoize myaddress johnston/],
],
+ 'Category' => [ [qw/categorycode default_privacy/], [qw/S never/], ]
],
'Installed some custom fixtures via the Populate fixture class';
# good user
$shib_login = "test1234";
warnings_are {
- ( $retval, $retcard, $retuserid ) = checkpw_shib( $shib_login );
+ ( $retval, $retcard, $retuserid ) = checkpw_shib($shib_login);
}
[], "good user with no debug";
is( $retval, "1", "user authenticated" );
# bad user
$shib_login = 'martin';
warnings_are {
- ( $retval, $retcard, $retuserid ) = checkpw_shib( $shib_login );
+ ( $retval, $retcard, $retuserid ) = checkpw_shib($shib_login);
}
[], "bad user with no debug";
is( $retval, "0", "user not authenticated" );
+ # autocreate user
+ $autocreate = 1;
+ $shib_login = 'test4321';
+ $ENV{'uid'} = 'test4321';
+ $ENV{'sn'} = "pika";
+ $ENV{'exp'} = "2017";
+ $ENV{'cat'} = "S";
+ $ENV{'add'} = 'Address';
+ $ENV{'city'} = 'City';
+ warnings_are {
+ ( $retval, $retcard, $retuserid ) = checkpw_shib($shib_login);
+ }
+ [], "new user added with no debug";
+ is( $retval, "1", "user authenticated" );
+ is( $retuserid, "test4321", "expected userid returned" );
+ ok my $new_user = ResultSet('Borrower')
+ ->search( { 'userid' => 'test4321' }, { rows => 1 } ), "new user found";
+ is_fields [qw/surname dateexpiry address city/], $new_user->next,
+ [qw/pika 2017 Address City/],
+ 'Found $new_users surname';
+ $autocreate = 0;
+
+ # sync user
+ $sync = 1;
+ $ENV{'city'} = 'AnotherCity';
+ warnings_are {
+ ( $retval, $retcard, $retuserid ) = checkpw_shib($shib_login);
+ }
+ [], "good user with sync";
+
+ ok my $sync_user = ResultSet('Borrower')
+ ->search( { 'userid' => 'test4321' }, { rows => 1 } ), "sync user found";
+
+ is_fields [qw/surname dateexpiry address city/], $sync_user->next,
+ [qw/pika 2017 Address AnotherCity/],
+ 'Found $sync_user synced city';
+ $sync = 0;
+
# debug on
$C4::Auth_with_shibboleth::debug = '1';
# good user
$shib_login = "test1234";
warnings_exist {
- ( $retval, $retcard, $retuserid ) = checkpw_shib( $shib_login );
+ ( $retval, $retcard, $retuserid ) = checkpw_shib($shib_login);
}
- [ qr/checkpw_shib/, qr/User Shibboleth-authenticated as:/ ],
+ [
+ qr/checkpw_shib/,
+ qr/koha borrower field to match: userid/,
+ qr/shibboleth attribute to match: uid/,
+ qr/User Shibboleth-authenticated as:/
+ ],
"good user with debug enabled";
is( $retval, "1", "user authenticated" );
is( $retcard, "testcardnumber", "expected cardnumber returned" );
# bad user
$shib_login = "martin";
warnings_exist {
- ( $retval, $retcard, $retuserid ) = checkpw_shib( $shib_login );
+ ( $retval, $retcard, $retuserid ) = checkpw_shib($shib_login);
}
[
qr/checkpw_shib/,
+ qr/koha borrower field to match: userid/,
+ qr/shibboleth attribute to match: uid/,
qr/User Shibboleth-authenticated as:/,
qr/not a valid Koha user/
],
};
-## _get_uri
+## _get_uri - opac
+$OPACBaseURL = "testopac.com";
+is( C4::Auth_with_shibboleth::_get_uri(),
+ "https://testopac.com", "https opac uri returned" );
+
+$OPACBaseURL = "http://testopac.com";
+my $result;
+warnings_are { $result = C4::Auth_with_shibboleth::_get_uri() }[
+ "shibboleth interface: $interface",
+"Shibboleth requires OPACBaseURL/staffClientBaseURL to use the https protocol!"
+],
+ "improper protocol - received expected warning";
+is( $result, "https://testopac.com", "https opac uri returned" );
+
+$OPACBaseURL = "https://testopac.com";
is( C4::Auth_with_shibboleth::_get_uri(),
"https://testopac.com", "https opac uri returned" );
+$OPACBaseURL = undef;
+warnings_are { $result = C4::Auth_with_shibboleth::_get_uri() }
+[ "shibboleth interface: $interface", "OPACBaseURL not set!" ],
+ "undefined OPACBaseURL - received expected warning";
+is( $result, "https://", "https $interface uri returned" );
+
+## _get_uri - intranet
+$interface = 'intranet';
+$staffClientBaseURL = "teststaff.com";
+is( C4::Auth_with_shibboleth::_get_uri(),
+ "https://teststaff.com", "https $interface uri returned" );
+
+$staffClientBaseURL = "http://teststaff.com";
+warnings_are { $result = C4::Auth_with_shibboleth::_get_uri() }[
+ "shibboleth interface: $interface",
+"Shibboleth requires OPACBaseURL/staffClientBaseURL to use the https protocol!"
+],
+ "improper protocol - received expected warning";
+is( $result, "https://teststaff.com", "https $interface uri returned" );
+
+$staffClientBaseURL = "https://teststaff.com";
+is( C4::Auth_with_shibboleth::_get_uri(),
+ "https://teststaff.com", "https $interface uri returned" );
+
+$staffClientBaseURL = undef;
+warnings_are { $result = C4::Auth_with_shibboleth::_get_uri() }
+[ "shibboleth interface: $interface", "staffClientBaseURL not set!" ],
+ "undefined staffClientBaseURL - received expected warning";
+is( $result, "https://", "https $interface uri returned" );
+
## _get_shib_config
# Internal helper function, covered in tests above
+
+sub mockedConfig {
+ my $param = shift;
+
+ my %shibboleth = (
+ 'autocreate' => $autocreate,
+ 'sync' => $sync,
+ 'matchpoint' => $matchpoint,
+ 'mapping' => \%mapping
+ );
+
+ return \%shibboleth;
+}
+
+sub mockedPref {
+ my $param = $_[1];
+ my $return;
+
+ if ( $param eq 'OPACBaseURL' ) {
+ $return = $OPACBaseURL;
+ }
+
+ if ( $param eq 'staffClientBaseURL' ) {
+ $return = $staffClientBaseURL;
+ }
+
+ return $return;
+}
+
+sub mockedInterface {
+ return $interface;
+}
+
+sub mockedSchema {
+ return Schema();
+}
+
+## Convenience method to reset config
+sub reset_config {
+ $matchpoint = 'userid';
+ $autocreate = 0;
+ $sync = 0;
+ %mapping = (
+ 'userid' => { 'is' => 'uid' },
+ 'surname' => { 'is' => 'sn' },
+ 'dateexpiry' => { 'is' => 'exp' },
+ 'categorycode' => { 'is' => 'cat' },
+ 'address' => { 'is' => 'add' },
+ 'city' => { 'is' => 'city' },
+ );
+ $ENV{'uid'} = "test1234";
+ $ENV{'sn'} = undef;
+ $ENV{'exp'} = undef;
+ $ENV{'cat'} = undef;
+ $ENV{'add'} = undef;
+ $ENV{'city'} = undef;
+
+ return 1;
+}
+