Skip to content

Commit

Permalink
tests, pods
Browse files Browse the repository at this point in the history
  • Loading branch information
nobuo-danjou committed Dec 18, 2009
1 parent a47efe8 commit 22a6dac
Show file tree
Hide file tree
Showing 9 changed files with 227 additions and 73 deletions.
5 changes: 4 additions & 1 deletion Changes
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
Revision history for Perl extension Net::Google::GData

0.07 Mon Dec 07 14:40:00 2009
0.07 Mon Dec 18 13:25:50 2009

- now uses Any::Moose
- OAuth support with Net::Google::DataAPI::Auth::OAuth
- Net::Google::DataAPI::Role::Service stops using MooseX::Role::Parameterized
- multipart request support
- less strict role check for feedurl
- support for existing query form for request
Expand Down
7 changes: 4 additions & 3 deletions Makefile.PL
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ requires 'Carp';
requires 'XML::Atom';
requires 'Net::Google::AuthSub';
requires 'Crypt::SSLeay';
requires 'Digest::SHA1';
requires 'Net::OAuth';
requires 'LWP::UserAgent';
requires 'URI';
requires 'Moose' => 0.34;
requires 'Lingua::EN::Inflect::Number';
requires 'MooseX::Role::Parameterized';
requires 'namespace::clean';
requires 'namespace::autoclean';
requires_any_moose;

tests_recursive;
author_tests 'xt';
Expand Down
50 changes: 0 additions & 50 deletions lib/Net/Google/DataAPI/Auth/ClientLogin.pm

This file was deleted.

7 changes: 0 additions & 7 deletions lib/Net/Google/DataAPI/Auth/Noop.pm

This file was deleted.

38 changes: 31 additions & 7 deletions lib/Net/Google/DataAPI/Auth/OAuth.pm
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ sub get_request_token {

sub get_authorize_token_url {
my ($self) = @_;
$self->has_request_token or $self->get_request_token;
my $url = $self->authorize_token_url;
$url->query_form(
oauth_token => $self->request_token,
Expand All @@ -82,11 +83,14 @@ sub get_access_token {
'access token',
{
request_url => $self->get_access_token_url,
token => $self->clear_request_token,
token_secret => $self->clear_request_token_secret,
%$args,
token => $self->request_token,
token_secret => $self->request_token_secret,
%{$args || {}},
}
);
# now clear them.
$self->clear_request_token;
$self->clear_request_token_secret;
my ($token, $secret) = $self->_res_to_token($res);
$self->access_token($token);
$self->access_token_secret($secret);
Expand All @@ -97,7 +101,7 @@ sub _oauth_request {
my $req = $self->_make_oauth_request($type, $args);
my $res = $self->ua->get($req->to_url);
unless ($res && $res->is_success) {
Carp::cluck sprintf "request failed: %s", $res->status_line;
confess sprintf "request failed: %s", $res ? $res->status_line : 'no response returned';
}
return $res;
}
Expand Down Expand Up @@ -146,22 +150,42 @@ __END__
=head1 NAME
Net::Google::DataAPI::Auth::OAuth -
Net::Google::DataAPI::Auth::OAuth - OAuth support for Google Data APIs
=head1 SYNOPSIS
use Net::Google::DataAPI::Role::OAuth;
use Net::Google::DataAPI::Auth::OAuth;
my $auth = Net::Google::DataAPI::Auth::OAuth->new(
consumer_key => 'consumer.example.com',
consumer_secret => 'mys3cr3t',
scope => ['http://spreadsheets.google.com/feeds/'],
);
my $url = $auth->get_authorize_token_url;
# show the user $url and get $verifier
$auth->get_access_token({verifier => $verifier}) or die;
my $token = $auth->access_token;
my $secret = $auth->access_token_secret;
=head1 DESCRIPTION
Net::Google::Role::DataAPI::OAuth is
Net::Google::DataAPI::Auth::OAuth interacts with google OAuth service
and adds Authorization header to given request.
=head1 AUTHOR
Nobuo Danjou E<lt>nobuo.danjou@gmail.comE<gt>
=head1 SEE ALSO
L<Net::Google::AuthSub>
L<Net::OAuth>
L<Net::Twitter::Role::OAuth>
=head1 LICENSE
This library is free software; you can redistribute it and/or modify
Expand Down
7 changes: 2 additions & 5 deletions lib/Net/Google/DataAPI/Role/Service.pm
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use URI;
use XML::Atom;
use XML::Atom::Entry;
use XML::Atom::Feed;
use Net::Google::DataAPI::Auth::Null;

has gdata_version => (
isa => 'Str',
Expand Down Expand Up @@ -74,11 +75,7 @@ sub _build_ua {
return $ua;
}

sub _build_auth {
my $noop = 'Net::Google::DataAPI::Auth::Noop';
Any::Moose::load_class($noop);
$noop->new;
}
sub _build_auth { Net::Google::DataAPI::Auth::Null->new }

sub _build_service {return $_[0]}

Expand Down
10 changes: 10 additions & 0 deletions t/04_auth/01_authsub.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use strict;
use warnings;

use Test::More;

BEGIN {
use_ok 'Net::Google::DataAPI::Auth::AuthSub';
}

done_testing;
175 changes: 175 additions & 0 deletions t/04_auth/02_oauth.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
use strict;
use warnings;
use Test::More;
use LWP::UserAgent;
use Test::MockModule;
use Test::Exception;
use HTTP::Response;
use URI;

BEGIN {
use_ok 'Net::Google::DataAPI::Auth::OAuth';
}

{
my $ua = Test::MockModule->new('LWP::UserAgent');
$ua->mock('get' => sub {
my $self = shift;
my $uri = URI->new(shift);

is $uri->host, 'www.google.com';
is $uri->scheme, 'https';
is $uri->path, '/accounts/OAuthGetRequestToken';
is {$uri->query_form}->{oauth_consumer_key}, 'myconsumer.example.com';
is {$uri->query_form}->{scope}, 'http://spreadsheets.google.com/feeds/';
is {$uri->query_form}->{oauth_callback}, 'oob';

my $q = URI->new;
my $res = HTTP::Response->new(200);
$q->query_form(
oauth_token => 'myrequesttoken',
oauth_token_secret => 'myrequesttokensecret',
);
$res->content($q->query);
return $res;
}
);

ok my $oauth = Net::Google::DataAPI::Auth::OAuth->new(
scope => ['http://spreadsheets.google.com/feeds/'],
consumer_key => 'myconsumer.example.com',
consumer_secret => 'mys3cr3t',
);
ok $oauth->get_request_token;
is $oauth->request_token, 'myrequesttoken';
is $oauth->request_token_secret, 'myrequesttokensecret';

ok my $url = $oauth->get_authorize_token_url;
is $url, 'https://www.google.com/accounts/OAuthAuthorizeToken?oauth_token=myrequesttoken&hd=default&hl=en';

$ua->mock('get' => sub {
my ($self, $url) = @_;
my $res = HTTP::Response->new(200);
my $q = URI->new;
$q->query_form(
oauth_token => 'myaccesstoken',
oauth_token_secret => 'myaccesstokensecret',
);
$res->content($q->query);
return $res;
}
);

ok $oauth->get_access_token({verifier => 'myverifier'});
is $oauth->access_token, 'myaccesstoken';
is $oauth->access_token_secret, 'myaccesstokensecret';

my $req = HTTP::Request->new(
GET => 'http://spreadsheets.google.com/feeds/spreadsheets/private/full'
);
ok $oauth->sign_request($req);
ok my $header = $req->header('Authorization');
my ($name, $value) = split(/\s/, $header);
is $name, 'OAuth';
my %args = split(/[=,]/, $value);
is $args{oauth_token}, '"myaccesstoken"';
is $args{oauth_signature_method}, '"HMAC-SHA1"';
is $args{oauth_consumer_key}, '"myconsumer.example.com"'
}
{
ok my $oauth = Net::Google::DataAPI::Auth::OAuth->new(
scope => ['http://spreadsheets.google.com/feeds/'],
consumer_key => 'myconsumer.example.com',
consumer_secret => 'mys3cr3t',
);

my $req = HTTP::Request->new(
GET => 'http://spreadsheets.google.com/feeds/spreadsheets/private/full'
);
throws_ok {
$oauth->sign_request($req)
} qr{Missing required parameter 'token'};
}
{
my $ua = Test::MockModule->new('LWP::UserAgent');

ok my $oauth = Net::Google::DataAPI::Auth::OAuth->new(
scope => ['http://spreadsheets.google.com/feeds/'],
consumer_key => 'myconsumer.example.com',
consumer_secret => 'mys3cr3t',
callback => URI->new('http://myconsumer.example.com/callback'),
authorize_token_hl => 'ja',
mobile => 1,
);

{
$ua->mock('get' => sub { });
throws_ok {
$oauth->get_request_token;
} qr{request failed: no response returned};
}
{
$ua->mock('get' => sub { HTTP::Response->new(400) });
throws_ok {
$oauth->get_request_token;
} qr{request failed: 400 Bad Request};
}
{
$ua->mock('get' => sub {
my ($self, $url) = @_;
my $res = HTTP::Response->new(200);
my $q = URI->new;
$q->query_form(
oauth_token => 'myrequesttoken',
oauth_token_secret => 'myrequesttokensecret',
);
$res->content($q->query);
return $res;
}
);
ok my $url = $oauth->get_authorize_token_url;
my $uri = URI->new($url);
is $uri->host, 'www.google.com';
is $uri->scheme, 'https';
is $uri->path, '/accounts/OAuthAuthorizeToken';
is_deeply {$uri->query_form}, {
oauth_token => 'myrequesttoken',
hd => 'default',
hl => 'ja',
btmpl => 'mobile',
};
}
{
$ua->mock('get' => sub {
my $self = shift;
my $uri = URI->new(shift);
my $res = HTTP::Response->new(200);
my $q = URI->new;
$q->query_form(
oauth_token => 'mytoken',
oauth_token_secret => 'mysecret',
);
$res->content($q->query);
return $res;
}
);
throws_ok {
$oauth->get_access_token
} qr{Missing required parameter 'verifier'};
throws_ok {
$oauth->sign_request( HTTP::Request->new(get => 'http://example.com/') )
} qr{Missing required parameter 'token'};
ok $oauth->get_access_token({verifier => 'myverifier'});
my $req = HTTP::Request->new(get => 'http://example.com/');
ok $oauth->sign_request($req);
ok my $header = $req->header('Authorization');
my ($name, $value) = split(/\s/, $header);
is $name, 'OAuth';
my %args = split(/[=,]/, $value);
is $args{oauth_token}, '"mytoken"';
is $args{oauth_signature_method}, '"HMAC-SHA1"';
is $args{oauth_consumer_key}, '"myconsumer.example.com"';
}
}

done_testing;
1 change: 1 addition & 0 deletions xt/01_podspell.t
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ feedurl
url
TODO
param
OAuth

0 comments on commit 22a6dac

Please sign in to comment.