[Jifty-commit] r5166 - in jifty/trunk: . lib/Jifty/Plugin t/TestApp-Plugin-OAuth/t
Jifty commits
jifty-commit at lists.jifty.org
Tue Feb 26 03:35:44 EST 2008
Author: sartak
Date: Tue Feb 26 03:35:43 2008
New Revision: 5166
Added:
jifty/trunk/t/TestApp-Plugin-OAuth/lib/TestApp/Plugin/OAuth/Model/Favorite.pm
Modified:
jifty/trunk/ (props changed)
jifty/trunk/lib/Jifty/Plugin/OAuth.pm
jifty/trunk/t/TestApp-Plugin-OAuth/lib/TestApp/Plugin/OAuth/Model/User.pm
jifty/trunk/t/TestApp-Plugin-OAuth/t/06-read-only.t
Log:
r52026 at onn: sartak | 2008-02-26 02:54:23 -0500
More tests for, and cleanups of, OAuth
Modified: jifty/trunk/lib/Jifty/Plugin/OAuth.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Plugin/OAuth.pm (original)
+++ jifty/trunk/lib/Jifty/Plugin/OAuth.pm Tue Feb 26 03:35:43 2008
@@ -4,7 +4,7 @@
use base qw/Jifty::Plugin/;
-our $VERSION = 0.01;
+our $VERSION = '0.01';
sub init {
Jifty::CurrentUser->mk_accessors(qw(is_oauthed oauth_token));
@@ -25,7 +25,7 @@
return 'ignore' if $token->__value('can_write');
# we have been forbidden from writing!
- Jifty->log->error("Unable to $right " . ref($record) . " " . $record->id . " because the OAuth access token does not allow it.");
+ Jifty->log->error("Unable to $right " . ref($record) . " " . ($record->id||'new') . " because the OAuth access token does not allow it.");
return 'deny';
});
@@ -50,14 +50,13 @@
return 1 if $token->__value('can_write');
# we have been forbidden from writing!
- Jifty->log->debug("Unable to $type " . ref($record) . " " . $record->id . " because the OAuth access token does not allow it.");
+ Jifty->log->debug("Unable to $type " . ref($record) . " " . ($record->id||'new') . " because the OAuth access token does not allow it.");
my $ret = Class::ReturnValue->new;
$ret->as_array(0, "Your OAuth access token denies you write access.");
$ret->as_error(
errno => 1,
message => 'Your OAuth access token denies you write access.',
);
- my $return = $ret->return_value;
return $ret->return_value;
},
);
Added: jifty/trunk/t/TestApp-Plugin-OAuth/lib/TestApp/Plugin/OAuth/Model/Favorite.pm
==============================================================================
--- (empty file)
+++ jifty/trunk/t/TestApp-Plugin-OAuth/lib/TestApp/Plugin/OAuth/Model/Favorite.pm Tue Feb 26 03:35:43 2008
@@ -0,0 +1,47 @@
+use strict;
+use warnings;
+
+package TestApp::Plugin::OAuth::Model::Favorite;
+use Jifty::DBI::Schema;
+
+use TestApp::Plugin::OAuth::Record schema {
+ column 'owner' =>
+ refers_to TestApp::Plugin::OAuth::Model::User;
+ column 'thing' =>
+ type is 'text';
+};
+
+# you only create favorites for yourself
+sub before_create {
+ my $self = shift;
+ my $args = shift;
+
+ $args->{owner} = Jifty->web->current_user->user_object;
+
+ return 1;
+}
+
+sub current_user_can {
+ my $self = shift;
+ my $right = shift;
+
+ # all can read
+ return 1 if $right eq 'read';
+
+ # logged in users can create
+ return Jifty->web->current_user->user_object if $right eq 'create';
+
+ # only the owner may update his favorites
+ return 0 unless Jifty->web->current_user->id == $self->owner->id;
+
+ # none can delete
+ return 0 if $right eq 'delete';
+
+ # oauthed can update, non-oauthed can't
+ return !Jifty->web->current_user->is_oauthed if $right eq 'update';
+
+ die "Favorite->current_user_can($right) check fell through";
+}
+
+1;
+
Modified: jifty/trunk/t/TestApp-Plugin-OAuth/lib/TestApp/Plugin/OAuth/Model/User.pm
==============================================================================
--- jifty/trunk/t/TestApp-Plugin-OAuth/lib/TestApp/Plugin/OAuth/Model/User.pm (original)
+++ jifty/trunk/t/TestApp-Plugin-OAuth/lib/TestApp/Plugin/OAuth/Model/User.pm Tue Feb 26 03:35:43 2008
@@ -8,6 +8,8 @@
column 'tasty' =>
type is 'boolean',
default is 'f';
+ column 'favorites' =>
+ refers_to TestApp::Plugin::OAuth::Model::FavoriteCollection by 'owner';
};
Modified: jifty/trunk/t/TestApp-Plugin-OAuth/t/06-read-only.t
==============================================================================
--- jifty/trunk/t/TestApp-Plugin-OAuth/t/06-read-only.t (original)
+++ jifty/trunk/t/TestApp-Plugin-OAuth/t/06-read-only.t Tue Feb 26 03:35:43 2008
@@ -5,7 +5,7 @@
use Test::More;
BEGIN {
if (eval { require Net::OAuth::Request; require Crypt::OpenSSL::RSA; 1 }) {
- plan tests => 22;
+ plan tests => 28;
}
else {
plan skip_all => "Net::OAuth or Crypt::OpenSSL::RSA isn't installed";
@@ -85,9 +85,7 @@
# REST DELETE {{{
response_is(
url => "/=/model/User/id/$uid.yml!DELETE",
- id => $uid,
code => 200,
- method => 'POST',
testname => "200 - protected resource request",
consumer_secret => 'bar',
oauth_consumer_key => 'foo',
@@ -102,4 +100,37 @@
$user_copy->load($uid);
is($user_copy->name, "You Zer", "REST DELETE doesn't work while the consumer has no write access");
# }}}
+# REST POST {{{
+response_is(
+ url => "/=/model/Favorite.yml",
+ thing => 'tests',
+ code => 200,
+ testname => "200 - protected resource request",
+ consumer_secret => 'bar',
+ oauth_consumer_key => 'foo',
+ oauth_signature_method => 'PLAINTEXT',
+ oauth_token => $token_obj->token,
+ token_secret => $token_obj->secret,
+);
+
+$cmech->content_like(qr/failure: 1/, "failed to create");
+
+my $favorites = TestApp::Plugin::OAuth::Model::FavoriteCollection->new(
+ current_user => Jifty::CurrentUser->superuser,
+);
+$favorites->unlimit;
+is($favorites->count, 0, "no favorites found");
+# }}}
+# user REST POST {{{
+$umech->post("$URL/=/model/Favorite.yml",
+ { thing => 'more tests' },
+);
+$umech->content_contains("success: 1", "created a favorite");
+$favorites = TestApp::Plugin::OAuth::Model::FavoriteCollection->new(
+ current_user => Jifty::CurrentUser->superuser,
+);
+$favorites->unlimit;
+is($favorites->count, 1, "favorite created");
+is($favorites->first->thing, 'more tests', "correct argument");
+# }}}
More information about the Jifty-commit
mailing list