Skip to content

Commit

Permalink
basic implementation seems to work
Browse files Browse the repository at this point in the history
  • Loading branch information
autarch committed Jul 9, 2009
1 parent 1ea46c4 commit 346f998
Show file tree
Hide file tree
Showing 2 changed files with 179 additions and 0 deletions.
114 changes: 114 additions & 0 deletions lib/Plone/UserAgent.pm
Expand Up @@ -5,6 +5,120 @@ use warnings;

our $VERSION = '0.01';

use Config::INI::Reader;
use File::HomeDir;
use HTTP::Cookies;
use Moose;
use Moose::Util::TypeConstraints;
use MooseX::NonMoose;
use URI;

extends 'LWP::UserAgent';

has username =>
( is => 'rw',
isa => 'Str',
predicate => '_has_username',
writer => '_set_username',
);

has password =>
( is => 'rw',
isa => 'Str',
predicate => '_has_password',
writer => '_set_password',
);

my $uri = subtype as class_type('URI');
coerce $uri
=> from 'Str'
=> via { URI->new( $_ ) };

has base_uri =>
( is => 'ro',
isa => $uri,
required => 1,
coerce => 1,
);

has config_file =>
( is => 'ro',
isa => 'Str',
lazy => 1,
default => sub { File::HomeDir->my_home() . '/.plone-useragentrc' },
);

has _config_data =>
( is => 'ro',
isa => 'HashRef',
lazy => 1,
builder => '_build_config_data',
);


sub BUILD
{
my $self = shift;

unless ( $self->_has_username && $self->_has_password )
{
my $config = $self->_config_data();

die 'Must provide a username and password or a valid config file'
unless $config && $config->{'-'}{username} && $config->{'-'}{password};

$self->_set_username( $config->{'-'}{username} );
$self->_set_password( $config->{'-'}{password} );
}

$self->cookie_jar( HTTP::Cookies->new() )
unless $self->cookie_jar();
}

sub _build_config_data
{
my $self = shift;

my $file = $self->config_file();

return {} unless -f $file;

return Config::INI::Reader->read_file($file) || {};
}

sub login
{
my $self = shift;

my $uri = $self->_make_uri( '/logged_out' );

my $response =
$self->post( $uri,
{ __ac_name => $self->username(),
__ac_password => $self->password(),
submit => 'Log in',
},
);

die "Could not log in to $uri"
unless $response->is_success();
}

sub _make_uri
{
my $self = shift;
my $path = shift;

my $uri = $self->base_uri()->clone();

$uri->path( $uri->path() . $path );

return $uri;
}

no Moose;

__PACKAGE__->meta()->make_immutable();

1;

Expand Down
65 changes: 65 additions & 0 deletions t/basic.t
@@ -0,0 +1,65 @@
use strict;
use warnings;

use Test::More 'no_plan';
use Test::Exception;

use Plone::UserAgent;


{
throws_ok( sub { Plone::UserAgent->new( base_uri => 'http://example.com' ) },
qr/\QMust provide a username and password or a valid config file/,
'cannot create a new ua without a username & password' );

no warnings 'redefine';
local *Plone::UserAgent::_build_config_data = sub
{
return { '-' => {} };
};

throws_ok( sub { Plone::UserAgent->new( base_uri => 'http://example.com' ) },
qr/\QMust provide a username and password or a valid config file/,
'cannot create a new ua without a username & password' );
}

{
my $ua = Plone::UserAgent->new( base_uri => 'http://example.com',
username => 'foo',
password => 'bar',
);

is( $ua->_make_uri('/whatever'),
'http://example.com/whatever',
'_make_uri uses base uri' );
}

{
my $ua = Plone::UserAgent->new( base_uri => 'http://example.com',
username => 'foo',
password => 'bar',
);

my @post;
my $rc = 200;

no warnings 'redefine';
local *LWP::UserAgent::post = sub { shift; @post = @_; return HTTP::Response->new($rc); };

$ua->login();

is_deeply( \@post,
[ 'http://example.com/logged_out',
{ __ac_name => 'foo',
__ac_password => 'bar',
submit => 'Log in',
},
],
'login method makes expected post' );

$rc = 500;
throws_ok( sub { $ua->login() },
qr{\QCould not log in to http://example.com/logged_out},
'throws an error when login fails' );
}

0 comments on commit 346f998

Please sign in to comment.