Stop accidentally blowing up if we can't restore the user.. Add tests and docs for...
Tomas Doran [Thu, 5 Feb 2009 21:43:52 +0000 (21:43 +0000)]
Changes
lib/Catalyst/Authentication/Realm.pm
lib/Catalyst/Plugin/Authentication.pm
t/lib/AuthSessionTestApp.pm
t/live_app_session.t

diff --git a/Changes b/Changes
index fff580c..64727e4 100644 (file)
--- a/Changes
+++ b/Changes
@@ -1,6 +1,8 @@
 Revision history for Perl extension Catalyst::Plugin::Authentication
 
+0.100092_01 - UNRELEASED
       - Add hook for failing user restore.
+        - Add test for this.
       - Fix bug in Credential::Password with password_type: clear.
         - Add test for this.
       - Add mock object tests for Credential::Password with password_type: 
index eb57f60..d7d33e6 100644 (file)
@@ -358,6 +358,11 @@ Restores the user from the given frozen_user parameter, or if not provided,
 using the response from $self->user_is_restorable();  Uses $self->from_session()
 to decode the frozen user.
 
+=head2 failed_user_restore($c)
+
+If there is a session to restore, but the restore fails for any reason then this method 
+is called. This method supplied just removes the persisted user, but can be overridden
+if required to have more complex logic (e.g. finding a the user by their 'old' username).
 
 =head2 from_session($c, $frozenuser )
 
index 11d25f0..21a601c 100644 (file)
@@ -2,9 +2,7 @@ package Catalyst::Plugin::Authentication;
 
 use base qw/Class::Accessor::Fast Class::Data::Inheritable/;
 
-BEGIN {
-    __PACKAGE__->mk_accessors(qw/_user/);
-}
+__PACKAGE__->mk_accessors(qw/_user/);
 
 use strict;
 use warnings;
@@ -13,7 +11,7 @@ use Tie::RefHash;
 use Class::Inspector;
 use Catalyst::Authentication::Realm;
 
-our $VERSION = "0.100092";
+our $VERSION = "0.100092_01";
 
 sub set_authenticated {
     my ( $c, $user, $realmname ) = @_;
@@ -189,7 +187,7 @@ sub auth_restore_user {
     $c->_user( my $user = $realm->restore_user( $c, $frozen_user ) );
     
     # this sets the realm the user originated in.
-    $user->auth_realm($realm->name);
+    $user->auth_realm($realm->name) if $user;
         
     return $user;
 
index 49ec14b..b6e4a50 100644 (file)
@@ -39,15 +39,31 @@ sub elk : Local {
        ok( $c->user_exists, "user exists" );
        ok( $c->user, "a user was also restored");
        is_deeply( $c->user, $users->{foo}, "restored user is the right one (deep test - store might change identity)" );
-    
+       
+       # Rename the user!
+       $users->{bar} = delete $users->{foo};
+}
+
+sub yak : Local {
+    my ( $self, $c ) = @_;
+    ok( $c->sessionid, "session ID was restored after user renamed" );
+    ok( $c->user_exists, "user appears to exist" );
+    ok( !$c->user, "try to restore - user was not restored");
+    ok( !$c->user_exists, "user no longer appears to exist" );
+}
+
+sub goat : Local {
+    my ( $self, $c ) = @_;
+    ok($c->login( "bar", "s3cr3t" ), "can login with clear (new username)");
+    is( $c->user, $users->{bar}, "user object is in proper place");
     $c->logout;
 }
 
 sub fluffy_bunny : Local {
-       my ( $self, $c ) = @_;
+    my ( $self, $c ) = @_;
 
-       ok( $c->session_is_valid, "no session ID was restored");
-       ok( !$c->user, "no user was restored");
+    ok( $c->session_is_valid, "session ID is restored after logout");
+    ok( !$c->user, "no user was restored after logout");
        
     $c->delete_session("bah");
 }
index 8bb1999..a1c3ae1 100644 (file)
@@ -6,7 +6,7 @@ use Test::More;
 BEGIN {
        eval { require Test::WWW::Mechanize::Catalyst; require Catalyst::Plugin::Session; require Catalyst::Plugin::Session::State::Cookie };
        plan skip_all => "This test needs Test::WWW::Mechanize::Catalyst, Catalyst::Plugin::Session and Catalyst::Plugin::Session::State::Cookie installed" if $@;
-       plan tests => 20;
+       plan tests => 28;
 }
 
 use lib 't/lib';
@@ -16,6 +16,8 @@ my $m = Test::WWW::Mechanize::Catalyst->new;
 
 $m->get_ok("http://localhost/moose", "get ok");
 $m->get_ok("http://localhost/elk", "get ok");
+$m->get_ok("http://localhost/yak", "get ok");
+$m->get_ok("http://localhost/goat", "get ok");
 $m->get_ok("http://localhost/fluffy_bunny", "get ok");
 $m->get_ok("http://localhost/possum", "get ok");
 $m->get_ok("http://localhost/butterfly", "get ok");