Skip to content

Commit

Permalink
Live testing framework
Browse files Browse the repository at this point in the history
  • Loading branch information
nigelm committed Jan 27, 2010
1 parent d3eecaa commit da82a99
Show file tree
Hide file tree
Showing 3 changed files with 184 additions and 0 deletions.
82 changes: 82 additions & 0 deletions t/lib/AuthTestApp.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package AuthTestApp;
use warnings;
use strict;

use Catalyst qw/
Authentication
/;

use Test::More;
use Test::Exception;

# this info needs to match that in TestWebServer
our $members = {
insecure => { password => '123456' },
paranoid => { password => 'very_secure_password!' }
};

sub testnotworking : Local {
my ( $self, $c ) = @_;

ok( !$c->user, "no user" );
while ( my ( $user, $info ) = each %$members ) {
ok(
!$c->authenticate(
{ username => $user, password => $info->{password} }, 'members'
),
"user $user authentication"
);
ok(
!$c->authenticate(
{ username => $user, password => 'wrong password' }, 'members'
),
"user $user authentication - wrong password"
);
}
$c->res->body("ok");
}

sub testworking : Local {
my ( $self, $c ) = @_;

ok( !$c->user, "no user" );
while ( my ( $user, $info ) = each %$members ) {
ok(
$c->authenticate(
{ username => $user, password => $info->{password} }, 'members'
),
"user $user authentication"
);
ok(
!$c->authenticate(
{ username => $user, password => 'wrong password' }, 'members'
),
"user $user authentication - wrong password"
);

$c->logout;

# sanity check
ok( !$c->user, "no more user after logout" );

}
$c->res->body("ok");
}

__PACKAGE__->config->{'Plugin::Authentication'} = {
default_realm => 'members',
realms => {
members => {
credential => {
class => 'RemoteHTTP',
url => 'http://127.0.0.1:8080/stuff.html',
},
store => {
class => 'Minimal',
users => $members
}
},
}
};

__PACKAGE__->setup;
72 changes: 72 additions & 0 deletions t/lib/TestWebServer.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#!/usr/bin/perl
#
# This is a test web server used to check http authentication
# It always requires basic authentication, with a fixed user/pass
# All successful request return a short piece of text with details
# of the request embedded....
#
package TestWebServer;
use base qw(HTTP::Server::Simple::CGI);

use strict;
use warnings;

use Carp;
use MIME::Base64;

# hash of usernames (keys) and passwords (values)
my $user_set = {
insecure => '123456',
paranoid => 'very_secure_password!',
};

# This next set of methods re-implements most of HTTP::Server::Simple::Authen
# because that and its dependancies are not typically installed in
# a normal catalyst installation
sub do_authenticate {
my $self = shift;

if ( ( $ENV{HTTP_AUTHORIZATION} || '' ) =~ /^Basic (.*?)$/ ) {
my ( $user, $pass ) = split /:/, ( MIME::Base64::decode($1) || ':' );
warn "user = $user, pass = $pass\n";
if ( exists( $user_set->{$user} ) && ( $user_set->{$user} eq $pass ) ) {
return $user;
}
}

return;
}

sub authen_realm { "Authorized area" }

sub authenticate {
my $self = shift;

my $user = $self->do_authenticate();
unless ( defined $user ) {
my $realm = $self->authen_realm();
print "HTTP/1.0 401\r\n";
print qq(WWW-Authenticate: Basic realm="$realm"\r\n\r\n);
print "Authentication required.";
return;
}
return $user;
}

sub handle_request {
my ( $self, $cgi ) = @_;

my $user = $self->authenticate or return;

print(
"HTTP/1.0 200 OK\r\n",
$cgi->header,
$cgi->start_html("Response"),
$cgi->h1("Response"),
$cgi->p( sprintf( 'Path is %s', $cgi->path_info() ) ),
$cgi->p( sprintf( 'Authenticated as %s', $user ) ),
$cgi->end_html
);
}

1;
30 changes: 30 additions & 0 deletions t/live.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use strict;
use warnings;

use Test::More;

BEGIN {
plan skip_all => "HTTP::Server::Simple is required for this test"
unless eval { require HTTP::Server::Simple };
plan skip_all =>
"Catalyst::Authentication::Store::Minimal is required for this test"
unless eval { require Catalyst::Authentication::Store::Minimal };
plan "no_plan";
}

use lib 't/lib';
use TestWebServer;
use Catalyst::Test qw/AuthTestApp/;

# this test should be run *without* the authenticating server
ok( get("/testnotworking"), "get ok" );

my $pid = TestWebServer->new(8080)->background;
ok( $pid, 'Start authenticating web server' );
sleep(1);# give it time to start

# this test should be run *with* the authenticating server
ok( get("/testworking"), "get ok" );

# and kill off the test web server
kill 9, $pid;

0 comments on commit da82a99

Please sign in to comment.