CGI-Application-Framework
view release on metacpan or search on metacpan
t/05-relogin.t view on Meta::CPAN
return {
required => [ qw ( password banana ) ],
constraints => {
banana => qr/organic/i,
password => qr/...../, # at least five chars
},
msgs => {
any_errors => 'some_errors', # just want to set a true value here
prefix => 'err_',
},
};
}
}
#######################################################################
# Subclass with a custom _login_authenticate that ignores the username
# and password and always returns the user 'bubba'
{
package MyTestApp_custom_login_authenticate;
use base 'MyTestApp';
# This strange _login_authenticate sub always returns the user 'bubba'
sub _login_authenticate {
my $self = shift;
my ($user) = TestCDBI::Users->search(
username => 'bubba',
);
if ($user) {
$self->stash->{'User_OK'} = 1;
$self->stash->{'Password_OK'} = 1;
}
return (1, $user);
}
}
# Set up query and app
my ($query, $app);
$query = new CGI;
$query->param('come_from_rm', 'login');
$query->param('current_rm', 'login');
$query->param('rm', 'main_display');
#######################################################################
# Fake that we've come from the login page with good parameters
$app = MyTestApp->new(QUERY => $query);
$query->param('username', 'test');
$query->param('password', 'seekrit');
$app->run;
ok($app->stash->{'User_OK'}, '[login, good parms] valid user');
ok($app->stash->{'Password_OK'}, '[login, good parms] valid password');
my $session_id;
if ($app->stash->{'Cookie'} =~ m{^\s*session_id=(\S*);\s+path=/\s*$}i ) {
$session_id = $1;
}
like($session_id, qr/^[a-fA-F0-9]+$/, '[login, good parms] Session looks valid');
is($app->session->{username}, 'test', '[login, good parms] user is test');
is($app->session->{wubba}, 'yes', '[login, good parms] session value wubba');
$app->session->{'consistency'} = 'the goblin of little minds';
is($app->session->{'consistency'}, 'the goblin of little minds', '[login, good parms] stored session value consistency');
ok($app->stash->{'Seen_Run_Mode'}{'main_display'}, '[login, good parms] fall through to main_display');
is($app->stash->{'Final_Run_Mode'}, 'main_display', '[login, good parms] final page was main_display');
my $link = $app->make_link(
qs_args => {
rm => 'page_the_second',
}
);
#######################################################################
# link from main_display to page_the_second
$Consistency_Match = qr/goblin/;
my $uri = URI->new($link);
my %link_param = $uri->query_form;
$query = new CGI;
$query->param($_, $link_param{$_}) for keys %link_param;
$app = MyTestApp->new(QUERY => $query);
$app->run;
ok($app->stash->{'Seen_Run_Mode'}{'page_the_second'}, '[page_the_second, pass _relogin_test] fall through to page_the_second');
is($app->stash->{'Final_Run_Mode'}, 'page_the_second', '[page_the_secondpass _relogin_test] final page was page_the_second');
#######################################################################
# link from main_display to page_the_second, but fail consistency check
$Consistency_Match = qr/hobgoblin/;
$query = new CGI;
$query->param($_, $link_param{$_}) for keys %link_param;
$app = MyTestApp->new(QUERY => $query);
$app->stash->{'Suppress_Output'} = 1;
$app->run;
ok($app->stash->{'Seen_Run_Mode'}{'relogin'}, '[page_the_second, fail _relogin_test] redirected to relogin');
is($app->stash->{'Final_Run_Mode'}, 'relogin', '[page_the_second, fail _relogin_test] final page was relogin');
undef $app->stash->{'Suppress_Output'};
$Consistency_Match = qr/goblin/;
( run in 2.532 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )