new class for mechanize tests, fix Auth/mech
This commit is contained in:
parent
869d12fb82
commit
90dfb83d9d
2 changed files with 87 additions and 27 deletions
|
|
@ -26,13 +26,14 @@ use WebGUI::Test; # Must use this before any other WebGUI modules
|
|||
use WebGUI::Asset;
|
||||
use WebGUI::VersionTag;
|
||||
use WebGUI::Session;
|
||||
plan skip_all => 'set WEBGUI_LIVE to enable this test' unless $ENV{WEBGUI_LIVE};
|
||||
use WebGUI::Test::Mechanize;
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
# Init
|
||||
my $session = WebGUI::Test->session;
|
||||
my $node = WebGUI::Asset->getImportNode( $session );
|
||||
my @versionTags = ( WebGUI::VersionTag->getWorking( $session ) );
|
||||
my $versionTag = WebGUI::VersionTag->getWorking( $session );
|
||||
WebGUI::Test->addToCleanup($versionTag);
|
||||
|
||||
# Override some settings to make things easier to test
|
||||
# userFunctionStyleId
|
||||
|
|
@ -49,14 +50,13 @@ $user->username( $USERNAME );
|
|||
$user->addToGroups( ['3'] );
|
||||
my $auth = WebGUI::Operation::Auth::getInstance( $session, $user->authMethod, $user->userId );
|
||||
$auth->saveParams( $user->userId, $user->authMethod, {
|
||||
'identifier' => Digest::MD5::md5_base64( $IDENTIFIER ),
|
||||
'identifier' => $auth->hashPassword($IDENTIFIER)
|
||||
});
|
||||
|
||||
my ($mech, $redirect, $response, $url);
|
||||
my ($redirect, $response, $url);
|
||||
|
||||
# Get the site's base URL
|
||||
my $baseUrl = 'http://' . $session->config->get('sitename')->[0];
|
||||
$baseUrl .= $session->config->get('gateway');
|
||||
my $baseUrl = 'http://localhost/';
|
||||
|
||||
# Make an asset we can login on
|
||||
my $asset
|
||||
|
|
@ -68,27 +68,17 @@ my $asset
|
|||
groupIdEdit => 3, # Admins
|
||||
styleTemplateId => 'PBtmpl0000000000000132',
|
||||
});
|
||||
$versionTags[-1]->commit;
|
||||
$versionTag->commit;
|
||||
my $assetUrl = $baseUrl . $asset->get('url');
|
||||
WebGUI::Test->addToCleanup(@versionTags);
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
# Tests
|
||||
|
||||
if ( !eval { require Test::WWW::Mechanize; 1; } ) {
|
||||
plan skip_all => 'Cannot load Test::WWW::Mechanize. Will not test.';
|
||||
}
|
||||
$mech = Test::WWW::Mechanize->new;
|
||||
$mech->get( $baseUrl );
|
||||
if ( !$mech->success ) {
|
||||
plan skip_all => "Cannot load URL '$baseUrl'. Will not test.";
|
||||
}
|
||||
|
||||
my $mech = WebGUI::Test::Mechanize->new( config => WebGUI::Test->config );
|
||||
plan tests => 40; # Increment this number for each test you create
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
# no form: Test logging in on a normal page sends the user back to the same page
|
||||
$mech = Test::WWW::Mechanize->new;
|
||||
$mech->get( $assetUrl );
|
||||
$mech->base_is( $assetUrl, "We got the page we were expecting" );
|
||||
$url = $assetUrl . '?op=auth;method=login;username=' . $USERNAME . ';identifier=' . $IDENTIFIER;
|
||||
|
|
@ -100,7 +90,7 @@ $mech->content_contains( "ARTICLE", "We are shown the article" );
|
|||
#----------------------------------------------------------------------------
|
||||
# no form: Test logging in on a normal page sends user back to same page AFTER at least one
|
||||
# failed attempt
|
||||
$mech = Test::WWW::Mechanize->new;
|
||||
$mech = WebGUI::Test::Mechanize->new( config => WebGUI::Test->config );
|
||||
$mech->get( $assetUrl );
|
||||
$mech->base_is( $assetUrl, "We got the page we were expecting" );
|
||||
$url = $assetUrl . '?op=auth;method=login;username=' . $USERNAME . ';identifier=nowai';
|
||||
|
|
@ -119,7 +109,7 @@ $mech->content_contains( "ARTICLE", "We are shown the article" );
|
|||
|
||||
#----------------------------------------------------------------------------
|
||||
# displayLogin: Test logging in on a normal page sends the user back to the same page
|
||||
$mech = Test::WWW::Mechanize->new;
|
||||
$mech = WebGUI::Test::Mechanize->new( config => WebGUI::Test->config );
|
||||
$mech->get( $assetUrl );
|
||||
$mech->base_is( $assetUrl, "We got the page we were expecting" );
|
||||
$mech->get_ok( $assetUrl . "?op=auth;method=displayLogin" );
|
||||
|
|
@ -139,7 +129,7 @@ $mech->base_is( $assetUrl, "We were redirected to the same page after login" );
|
|||
#----------------------------------------------------------------------------
|
||||
# displayLogin: Test logging in on a normal page sends user back to same page AFTER at least one
|
||||
# failed attempt
|
||||
$mech = Test::WWW::Mechanize->new;
|
||||
$mech = WebGUI::Test::Mechanize->new( config => WebGUI::Test->config );
|
||||
$mech->get( $assetUrl );
|
||||
$mech->base_is( $assetUrl, "We got the page we were expecting" );
|
||||
$mech->get_ok( $assetUrl . "?op=auth;method=displayLogin" );
|
||||
|
|
@ -162,7 +152,7 @@ $mech->base_is( $assetUrl, "We were redirected to the same page after login and
|
|||
|
||||
#----------------------------------------------------------------------------
|
||||
# displayLogin: Test logging in on an operation other than ?op=auth
|
||||
$mech = Test::WWW::Mechanize->new;
|
||||
$mech = WebGUI::Test::Mechanize->new( config => WebGUI::Test->config );
|
||||
$mech->get( $assetUrl . '?op=listUsers' );
|
||||
$mech->base_is( $assetUrl . '?op=listUsers', "We got the page we were expecting" );
|
||||
$mech->get_ok( $assetUrl . "?op=auth;method=displayLogin" );
|
||||
|
|
@ -180,7 +170,7 @@ $mech->base_is( $assetUrl, "We weren't redirected");
|
|||
#----------------------------------------------------------------------------
|
||||
# displayLogin: Test logging in on an operation other than ?op=auth after at least one
|
||||
# failed attempt
|
||||
$mech = Test::WWW::Mechanize->new;
|
||||
$mech = WebGUI::Test::Mechanize->new( config => WebGUI::Test->config );
|
||||
$mech->get( $assetUrl . '?op=listUsers' );
|
||||
$mech->base_is( $assetUrl . '?op=listUsers', "We got the page we were expecting" );
|
||||
$mech->get_ok( $assetUrl . "?op=auth;method=displayLogin" );
|
||||
|
|
@ -204,7 +194,7 @@ $mech->base_is( $assetUrl, "We weren't redirected" );
|
|||
|
||||
#----------------------------------------------------------------------------
|
||||
# displayLogin: Test logging in after directly going to ?op=auth;method=init
|
||||
$mech = Test::WWW::Mechanize->new;
|
||||
$mech = WebGUI::Test::Mechanize->new( config => WebGUI::Test->config );
|
||||
$mech->get_ok( $assetUrl . '?op=auth;method=init' );
|
||||
$mech->base_is( $assetUrl . '?op=auth;method=init', "We got the page we were expecting" );
|
||||
$mech->get_ok( $assetUrl . "?op=auth;method=displayLogin" );
|
||||
|
|
@ -223,7 +213,7 @@ $mech->base_is( $assetUrl, "We were redirected to the right page" );
|
|||
#----------------------------------------------------------------------------
|
||||
# displayLogin: Test logging in after directly going to ?op=auth;method=init and failing
|
||||
# at least once.
|
||||
$mech = Test::WWW::Mechanize->new;
|
||||
$mech = WebGUI::Test::Mechanize->new( config => WebGUI::Test->config );
|
||||
$mech->get_ok( $assetUrl . '?op=auth;method=init' );
|
||||
$mech->base_is( $assetUrl . '?op=auth;method=init', "We got the page we were expecting" );
|
||||
$mech->get_ok( $assetUrl . "?op=auth;method=displayLogin" );
|
||||
|
|
@ -246,7 +236,7 @@ $mech->base_is( $assetUrl, "We were redirected to the right place" );
|
|||
|
||||
#----------------------------------------------------------------------------
|
||||
# returnUrl: Test logging in on a normal page sends the user back to the same page
|
||||
$mech = Test::WWW::Mechanize->new;
|
||||
$mech = WebGUI::Test::Mechanize->new( config => WebGUI::Test->config );
|
||||
$mech->get( $assetUrl );
|
||||
$mech->base_is( $assetUrl, "We got the page we were expecting" );
|
||||
$url = $assetUrl
|
||||
|
|
@ -259,7 +249,7 @@ $mech->base_is( $baseUrl . 'root/import', "We were redirected properly" );
|
|||
#----------------------------------------------------------------------------
|
||||
# returnUrl: Test logging in on a normal page sends user back to same page AFTER at least one
|
||||
# failed attempt
|
||||
$mech = Test::WWW::Mechanize->new;
|
||||
$mech = WebGUI::Test::Mechanize->new( config => WebGUI::Test->config );
|
||||
$mech->get( $assetUrl );
|
||||
$mech->base_is( $assetUrl, "We got the page we were expecting" );
|
||||
$url = $assetUrl
|
||||
|
|
|
|||
70
t/lib/WebGUI/Test/Mechanize.pm
Normal file
70
t/lib/WebGUI/Test/Mechanize.pm
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
package WebGUI::Test::Mechanize;
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
use parent 'Test::WWW::Mechanize::PSGI';
|
||||
|
||||
use WebGUI;
|
||||
use WebGUI::Config;
|
||||
use WebGUI::Session;
|
||||
use WebGUI::Middleware::Session;
|
||||
use Plack::Middleware::NullLogger;
|
||||
use Try::Tiny;
|
||||
BEGIN {
|
||||
@Plack::Middleware::NullLogger::ISA = qw(Plack::Middleware);
|
||||
}
|
||||
|
||||
sub new {
|
||||
my $class = shift;
|
||||
my %options = @_;
|
||||
my $config_file = delete $options{config};
|
||||
my $wg = WebGUI->new( config => $config_file );
|
||||
my $app = $wg->to_app;
|
||||
$app = WebGUI::Middleware::Session->wrap($app, config => $wg->config);
|
||||
$app = Plack::Middleware::NullLogger->wrap($app);
|
||||
$options{app} = $app;
|
||||
my $self = $class->SUPER::new(%options);
|
||||
$self->{_webgui_config} = $wg->config;
|
||||
return $self;
|
||||
}
|
||||
|
||||
sub session {
|
||||
my $self = shift;
|
||||
return $self->{_webgui_session}
|
||||
if $self->{_webgui_session};
|
||||
my $session = WebGUI::Session->open($self->{_webgui_config}, undef, $self->sessionId);
|
||||
$self->{_webgui_session} = $session;
|
||||
return $session;
|
||||
}
|
||||
|
||||
sub sessionId {
|
||||
my $self = shift;
|
||||
return $self->{_webgui_sessionId}
|
||||
if $self->{_webgui_sessionId};
|
||||
my $sessionId;
|
||||
my $cookieName = $self->{_webgui_config}->get('cookieName');
|
||||
$self->cookie_jar->scan(sub {
|
||||
my ($key, $value) = @_[1,2];
|
||||
if ($key eq $cookieName) {
|
||||
$sessionId = $value;
|
||||
}
|
||||
});
|
||||
if (! $sessionId) {
|
||||
die "Unable to find session cookie!";
|
||||
}
|
||||
$self->{_webgui_sessionId} = $sessionId;
|
||||
return $sessionId;
|
||||
}
|
||||
|
||||
sub DESTROY {
|
||||
my $self = shift;
|
||||
try {
|
||||
my $session = $self->session;
|
||||
$session->var->end;
|
||||
$session->close;
|
||||
};
|
||||
}
|
||||
|
||||
1;
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue