CGI-Application-Plugin-Authentication
view release on metacpan or search on metacpan
t/02_config.t view on Meta::CPAN
#!/usr/bin/perl -T
use Test::More tests => 69;
use Test::Warn;
use Scalar::Util;
use CGI;
use strict;
use warnings;
use lib qw(t);
###############################################################################
# FAKE our own versions of these methods; newer Perls fail when we use the
# versions from Test::Exception, throwing "Bizarre copy of HASH in sassign...".
sub lives_ok(&;$) {
my ($coderef, $name) = @_;
local $Test::Builder::Level = $Test::Builder::Level + 1;
my $rc = eval { $coderef->() };
ok !$@, $name;
}
sub throws_ok(&$;$) {
my ($coderef, $expecting, $name) = @_;
local $Test::Builder::Level = $Test::Builder::Level + 1;
my $rc = eval { $coderef->() };
like $@, $expecting, $name;
}
# END FAKE
###############################################################################
{
package TestAppConfig;
use base qw(CGI::Application);
use CGI::Application::Plugin::Authentication;
}
my %config = (
DRIVER => [ 'Generic', { user1 => '123', user2 => '123'} ],
STORE => 'Store::Dummy',
LOGIN_RUNMODE => 'login',
LOGOUT_RUNMODE => 'logout',
POST_LOGIN_RUNMODE => 'start',
CREDENTIALS => ['authen_username', 'authen_password'],
LOGIN_SESSION_TIMEOUT => '1h',
);
my $cgiapp=TestAppConfig->new;
lives_ok { $cgiapp->authen->config(%config) } 'All config parameters accepted';
is_deeply( $cgiapp->authen->credentials,[qw/authen_username authen_password/],'credentials set');
isa_ok($cgiapp->authen->drivers,'CGI::Application::Plugin::Authentication::Driver::Generic');
isa_ok($cgiapp->authen->store,'Store::Dummy');
%config = (
DRIVER => [ 'HTPassword', file => 't/htpasswd' ],
STORE => 'Store::Dummy',
LOGIN_URL => '/login.cgi',
LOGOUT_URL => '/',
POST_LOGIN_URL => '/protected/',
CREDENTIALS => ['authen_username', 'authen_password'],
LOGIN_SESSION_TIMEOUT => '1h',
);
lives_ok { TestAppConfig->new->authen->config(%config) } 'All config parameters accepted';
# test DRIVER
throws_ok { TestAppConfig->new->authen->config(DRIVER => { }) } qr/parameter DRIVER is not a string or arrayref/, 'config dies when DRIVER is passed a hashref';
lives_ok { TestAppConfig->new->authen->config(DRIVER => 'MODULE' ) } 'config accepts single DRIVER without options';
lives_ok { TestAppConfig->new->authen->config(DRIVER => [ 'MODULE', option => 'parameter' ] ) } 'config accepts single DRIVER with options';
lives_ok { TestAppConfig->new->authen->config(DRIVER => [ [ 'MODULE', option => 'parameter' ], [ 'MODULE', option => 'parameter' ] ] ) } 'config accepts multiple DRIVERs';
# test STORE
throws_ok { TestAppConfig->new->authen->config(STORE => { }) } qr/parameter STORE is not a string or arrayref/, 'config dies when STORE is passed a hashref';
lives_ok { TestAppConfig->new->authen->config(STORE => 'MODULE' ) } 'config accepts STORE without options';
lives_ok { TestAppConfig->new->authen->config(STORE => [ 'MODULE', option => 'parameter' ] ) } 'config accepts STORE with options';
# test LOGIN_RUNMODE
throws_ok { TestAppConfig->new->authen->config(LOGIN_RUNMODE => { }) } qr/parameter LOGIN_RUNMODE is not a string/, 'config dies when LOGIN_RUNMODE is passed a hashref';
lives_ok { TestAppConfig->new->authen->config(LOGIN_RUNMODE => 'runmode' ) } 'config accepts LOGIN_RUNMODE as a string';
# test LOGIN_URL
throws_ok { TestAppConfig->new->authen->config(LOGIN_URL => { }) } qr/parameter LOGIN_URL is not a string/, 'config dies when LOGIN_URL is passed a hashref';
lives_ok { TestAppConfig->new->authen->config(LOGIN_URL => '/' ) } 'config accepts LOGIN_URL as a string';
warning_like { TestAppConfig->new->authen->config(LOGIN_URL => '/', LOGIN_RUNMODE => 'runmode' ) } qr/authen config warning: parameter LOGIN_URL ignored since we already have LOGIN_RUNMODE/, "LOGIN_URL ignored when LOGIN_RUNMODE is configured";
# test LOGOUT_RUNMODE
throws_ok { TestAppConfig->new->authen->config(LOGOUT_RUNMODE => { }) } qr/parameter LOGOUT_RUNMODE is not a string/, 'config dies when LOGOUT_RUNMODE is passed a hashref';
lives_ok { TestAppConfig->new->authen->config(LOGOUT_RUNMODE => 'runmode' ) } 'config accepts LOGOUT_RUNMODE as a string';
# test LOGOUT_URL
throws_ok { TestAppConfig->new->authen->config(LOGOUT_URL => { }) } qr/parameter LOGOUT_URL is not a string/, 'config dies when LOGOUT_URL is passed a hashref';
lives_ok { TestAppConfig->new->authen->config(LOGOUT_URL => '/' ) } 'config accepts LOGOUT_URL as a string';
warning_like { TestAppConfig->new->authen->config(LOGOUT_URL => '/', LOGOUT_RUNMODE => 'runmode' ) } qr/authen config warning: parameter LOGOUT_URL ignored since we already have LOGOUT_RUNMODE/, "LOGOUT_URL ignored when LOGOUT_RUNMODE is configured...
# test POST_LOGIN_RUNMODE
throws_ok { TestAppConfig->new->authen->config(POST_LOGIN_RUNMODE => { }) } qr/parameter POST_LOGIN_RUNMODE is not a string/, 'config dies when POST_LOGIN_RUNMODE is passed a hashref';
lives_ok { TestAppConfig->new->authen->config(POST_LOGIN_RUNMODE => 'runmode' ) } 'config accepts POST_LOGIN_RUNMODE as a string';
# test POST_LOGIN_URL
throws_ok { TestAppConfig->new->authen->config(POST_LOGIN_URL => { }) } qr/parameter POST_LOGIN_URL is not a string/, 'config dies when POST_LOGIN_URL is passed a hashref';
lives_ok { TestAppConfig->new->authen->config(POST_LOGIN_URL => '/' ) } 'config accepts POST_LOGIN_URL as a string';
warning_like { TestAppConfig->new->authen->config(POST_LOGIN_URL => '/', POST_LOGIN_RUNMODE => 'runmode' ) } qr/authen config warning: parameter POST_LOGIN_URL ignored since we already have POST_LOGIN_RUNMODE/, "POST_LOGIN_UR_URL ignored when POST_...
# test POST_LOGIN_CALLBACK
throws_ok { TestAppConfig->new->authen->config(POST_LOGIN_CALLBACK => { }) } qr/parameter POST_LOGIN_CALLBACK is not a coderef/, 'config dies when POST_LOGIN_CALLBACK is passed a hashref';
throws_ok { TestAppConfig->new->authen->config(POST_LOGIN_CALLBACK => ' ') } qr/parameter POST_LOGIN_CALLBACK is not a coderef/, 'config dies when POST_LOGIN_CALLBACK is passed a string';
lives_ok { TestAppConfig->new->authen->config(POST_LOGIN_CALLBACK => sub { } ) } 'config accepts POST_LOGIN_CALLBACK as a coderef';
# test RENDER_LOGIN
throws_ok { TestAppConfig->new->authen->config(RENDER_LOGIN => { }) } qr/parameter RENDER_LOGIN is not a coderef/, 'config dies when RENDER_LOGIN is passed a hashref';
( run in 1.018 second using v1.01-cache-2.11-cpan-39bf76dae61 )