Fix bug, patch from Anthony Gladdish on mailing list
Tomas Doran [Wed, 17 Nov 2010 18:56:52 +0000 (18:56 +0000)]
Changes
lib/Catalyst/Authentication/Store/DBIx/Class/User.pm
t/11-authsessions-load-app-context.t [new file with mode: 0644]
t/lib/Catalyst/Authentication/Store/Person.pm [new file with mode: 0644]
t/lib/Catalyst/Authentication/Store/Person/User.pm [new file with mode: 0644]

diff --git a/Changes b/Changes
index 0e6bee6..3584114 100644 (file)
--- a/Changes
+++ b/Changes
@@ -1,5 +1,8 @@
 Revision history for Catalyst-Plugin-Authentication-Store-DBIx-Class
 
+0.1401  2010-11-16
+        * Fix call to ->load which was not passing $c
+
 0.1400  2010-09-01
         * Make can() work as well as AUTOLOADing.
 
index b0bd033..32de3cd 100644 (file)
@@ -190,7 +190,7 @@ sub from_session {
         return $self->load({
             map { ($_ => $frozenuser->{$_}) }
             @{ $self->config->{id_field} }
-        });
+        }, $c);
     }
 
     return $self->load( { $self->config->{'id_field'} => $frozenuser }, $c);
diff --git a/t/11-authsessions-load-app-context.t b/t/11-authsessions-load-app-context.t
new file mode 100644 (file)
index 0000000..e458c9f
--- /dev/null
@@ -0,0 +1,79 @@
+#!perl
+
+use strict;
+use warnings;
+use DBI;
+use File::Path;
+use FindBin;
+use Test::More;
+use lib "$FindBin::Bin/lib";
+
+BEGIN {
+    eval { require Test::WWW::Mechanize::Catalyst }
+      or plan skip_all =>
+      "Test::WWW::Mechanize::Catalyst is required for this test";
+
+    eval { require DBD::SQLite }
+        or plan skip_all =>
+        "DBD::SQLite is required for this test";
+
+    eval { require DBIx::Class }
+        or plan skip_all =>
+        "DBIx::Class is required for this test";
+
+    eval { require Catalyst::Plugin::Session;
+           die unless $Catalyst::Plugin::Session::VERSION >= 0.02 }
+        or plan skip_all =>
+        "Catalyst::Plugin::Session >= 0.02 is required for this test";
+
+    eval { require Catalyst::Plugin::Session::State::Cookie; }
+        or plan skip_all =>
+        "Catalyst::Plugin::Session::State::Cookie is required for this test";
+
+
+    plan tests => 4;
+
+    $ENV{TESTAPP_CONFIG} = {
+        name => 'TestApp',     
+        authentication => {
+            default_realm => "users",
+            realms => {
+                users => {
+                    credential => {
+                        'class' => 'Password',
+                        'password_field' => 'password',
+                                       },
+                    store => {
+                        'class' => 'Person',                        
+                        'use_userdata_from_session' => 0,
+                    },
+                },
+            },
+        },
+    };
+
+    $ENV{TESTAPP_PLUGINS} = [
+        qw/Authentication
+            Session
+                   Session::Store::Dummy                  
+            Session::State::Cookie                              
+           /
+    ];
+}
+
+use Test::WWW::Mechanize::Catalyst 'TestApp';
+my $m = Test::WWW::Mechanize::Catalyst->new;
+
+# log a user in
+{
+    $m->get_ok( 'http://localhost/user_login?username=joeuser&password=hackme', undef, 'request ok' );
+    $m->content_is( 'joeuser logged in', 'user logged in ok' );
+}
+
+# verify the user is still logged in
+{
+    $m->get_ok( 'http://localhost/get_session_user', undef, 'request ok' );
+    $m->content_is( 'joeuser', 'user still logged in' );
+}
+
+
diff --git a/t/lib/Catalyst/Authentication/Store/Person.pm b/t/lib/Catalyst/Authentication/Store/Person.pm
new file mode 100644 (file)
index 0000000..2ab0af3
--- /dev/null
@@ -0,0 +1,21 @@
+package Catalyst::Authentication::Store::Person;
+
+use strict;
+use warnings;
+use base qw/Catalyst::Authentication::Store::DBIx::Class/;
+
+our $VERSION= "0.01";
+
+sub new {
+    my ( $class, $config, $app ) = @_;    
+    $config->{user_class}       = 'TestApp::User';
+    $config->{store_user_class} = 'Catalyst::Authentication::Store::Person::User';
+    $config->{role_relation}    = 'role';
+    $config->{role_field}       = 'role';
+
+    return $class->SUPER::new( $config, $app );
+}
+
+__PACKAGE__;
+
+__END__
\ No newline at end of file
diff --git a/t/lib/Catalyst/Authentication/Store/Person/User.pm b/t/lib/Catalyst/Authentication/Store/Person/User.pm
new file mode 100644 (file)
index 0000000..b7b8828
--- /dev/null
@@ -0,0 +1,25 @@
+package Catalyst::Authentication::Store::Person::User;
+
+use strict;
+use warnings;
+use base qw/Catalyst::Authentication::Store::DBIx::Class::User/;
+use base qw/Class::Accessor::Fast/;
+use Data::Dump;
+
+sub load {
+    my ($self, $authinfo, $c) = @_;    
+       if ( exists( $authinfo->{'id'} ) ) {            
+        $self->_user( $c->model('TestApp::User')->find($authinfo->{'id'}) );           
+    } elsif ( exists( $authinfo->{'username'} ) ) {
+               my $u = $c->model('TestApp::User')->find({ username => $authinfo->{'username'} });
+           $self->_user( $u );
+       }
+    if ($self->get_object) {
+        return $self;
+    } else {
+        return undef;
+    }
+}
+
+1;
+__END__
\ No newline at end of file