[Catalyst-commits] r8020 - in trunk/Catalyst-Authentication-Store-DBIx-Class: lib/Catalyst/Authentication/Store/DBIx/Class t t/lib

dandv at dev.catalyst.perl.org dandv at dev.catalyst.perl.org
Sat Jun 28 00:18:54 BST 2008


Author: dandv
Date: 2008-06-28 00:18:54 +0100 (Sat, 28 Jun 2008)
New Revision: 8020

Modified:
   trunk/Catalyst-Authentication-Store-DBIx-Class/lib/Catalyst/Authentication/Store/DBIx/Class/User.pm
   trunk/Catalyst-Authentication-Store-DBIx-Class/t/03-authtest.t
   trunk/Catalyst-Authentication-Store-DBIx-Class/t/lib/TestApp.pm
Log:
Added specific error message for user_class being incorrectly set
Changed the CRLF mode in TestApp.pm and 03-authtest.t to LF only from CRLF



Modified: trunk/Catalyst-Authentication-Store-DBIx-Class/lib/Catalyst/Authentication/Store/DBIx/Class/User.pm
===================================================================
--- trunk/Catalyst-Authentication-Store-DBIx-Class/lib/Catalyst/Authentication/Store/DBIx/Class/User.pm	2008-06-27 16:20:19 UTC (rev 8019)
+++ trunk/Catalyst-Authentication-Store-DBIx-Class/lib/Catalyst/Authentication/Store/DBIx/Class/User.pm	2008-06-27 23:18:54 UTC (rev 8020)
@@ -31,6 +31,9 @@
             Catalyst::Exception->throw("user table does not contain a single primary key column - please specify 'id_field' in config!");
         }
     }
+    if (not $self->{'resultset'}) {
+        Catalyst::Exception->throw("\$c->model('${ \$self->config->{user_class} }') did not return a resultset. Did you set user_class correctly?");
+    }
     if (!$self->{'resultset'}->result_source->has_column($self->config->{'id_field'})) {
         Catalyst::Exception->throw("id_field set to " .  $self->config->{'id_field'} . " but user table has no column by that name!");
     }

Modified: trunk/Catalyst-Authentication-Store-DBIx-Class/t/03-authtest.t
===================================================================
--- trunk/Catalyst-Authentication-Store-DBIx-Class/t/03-authtest.t	2008-06-27 16:20:19 UTC (rev 8019)
+++ trunk/Catalyst-Authentication-Store-DBIx-Class/t/03-authtest.t	2008-06-27 23:18:54 UTC (rev 8020)
@@ -1,98 +1,106 @@
-#!perl
-
-use strict;
-use warnings;
-use DBI;
-use File::Path;
-use FindBin;
-use Test::More;
-use lib "$FindBin::Bin/lib";
-
-BEGIN {
-    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";
-
-    plan tests => 14;
-
-    $ENV{TESTAPP_DB_FILE} = "$FindBin::Bin/auth.db" unless exists($ENV{TESTAPP_DB_FILE});
-
-    $ENV{TESTAPP_CONFIG} = {
-        name => 'TestApp',
-        authentication => {
-            default_realm => "users",
-            realms => {
-                users => {
-                    credential => {
-                        'class' => "Password",
-                        'password_field' => 'password',
-                        'password_type' => 'clear'
-                    },
-                    store => {
-                        'class' => 'DBIx::Class',
-                        'user_class' => 'TestApp::User',
-                    },
-                },
-            },
-        },
-    };
-
-    $ENV{TESTAPP_PLUGINS} = [
-        qw/Authentication/
-    ];
-}
-
-use SetupDB;
-
-use Catalyst::Test 'TestApp';
-
-# log a user in
-{
-    ok( my $res = request('http://localhost/user_login?username=joeuser&password=hackme'), 'request ok' );
-    is( $res->content, 'joeuser logged in', 'user logged in ok' );
-}
-
-# invalid user
-{
-    ok( my $res = request('http://localhost/user_login?username=foo&password=bar'), 'request ok' );
-    is( $res->content, 'not logged in', 'user not logged in ok' );
-}
-
-# disabled user - no disable check
-{
-    ok( my $res = request('http://localhost/user_login?username=spammer&password=broken'), 'request ok' );
-    is( $res->content, 'spammer logged in', 'status check - disabled user logged in ok' );
-}
-
-# disabled user - should fail login
-{
-    ok( my $res = request('http://localhost/notdisabled_login?username=spammer&password=broken'), 'request ok' );
-    is( $res->content, 'not logged in', 'status check - disabled user not logged in ok' );
-}
-
-# log the user out
-{
-    ok( my $res = request('http://localhost/user_logout'), 'request ok' );
-    is( $res->content, 'logged out', 'user logged out ok' );
-}
-
-# searchargs test
-{
-    ok( my $res = request('http://localhost/searchargs_login?email=nada%40mucho.net&password=much'), 'request ok' );
-    is( $res->content, 'nuffin logged in', 'searchargs based login ok' );
-}
-
-# resultset test
-# searchargs test
-{
-    ok( my $res = request('http://localhost/resultset_login?email=j%40cpants.org&password=letmein'), 'request ok' );
-    is( $res->content, 'jayk logged in', 'resultset based login ok' );
-}
-
-
-# clean up
-unlink $ENV{TESTAPP_DB_FILE};
+#!perl
+
+use strict;
+use warnings;
+use DBI;
+use File::Path;
+use FindBin;
+use Test::More;
+use lib "$FindBin::Bin/lib";
+
+BEGIN {
+    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";
+
+    plan tests => 15;
+
+    $ENV{TESTAPP_DB_FILE} = "$FindBin::Bin/auth.db" unless exists($ENV{TESTAPP_DB_FILE});
+
+    $ENV{TESTAPP_CONFIG} = {
+        name => 'TestApp',
+        authentication => {
+            default_realm => "users",
+            realms => {
+                users => {
+                    credential => {
+                        'class' => "Password",
+                        'password_field' => 'password',
+                        'password_type' => 'clear'
+                    },
+                    store => {
+                        'class' => 'DBIx::Class',
+                        'user_class' => 'TestApp::User',
+                    },
+                },
+            },
+        },
+    };
+
+    $ENV{TESTAPP_PLUGINS} = [
+        qw/Authentication/
+    ];
+}
+
+use SetupDB;
+
+use Catalyst::Test 'TestApp';
+
+# log a user in
+{
+    ok( my $res = request('http://localhost/user_login?username=joeuser&password=hackme'), 'request ok' );
+    is( $res->content, 'joeuser logged in', 'user logged in ok' );
+}
+
+# invalid user
+{
+    ok( my $res = request('http://localhost/user_login?username=foo&password=bar'), 'request ok' );
+    is( $res->content, 'not logged in', 'user not logged in ok' );
+}
+
+# disabled user - no disable check
+{
+    ok( my $res = request('http://localhost/user_login?username=spammer&password=broken'), 'request ok' );
+    is( $res->content, 'spammer logged in', 'status check - disabled user logged in ok' );
+}
+
+# disabled user - should fail login
+{
+    ok( my $res = request('http://localhost/notdisabled_login?username=spammer&password=broken'), 'request ok' );
+    is( $res->content, 'not logged in', 'status check - disabled user not logged in ok' );
+}
+
+# log the user out
+{
+    ok( my $res = request('http://localhost/user_logout'), 'request ok' );
+    is( $res->content, 'logged out', 'user logged out ok' );
+}
+
+# searchargs test
+{
+    ok( my $res = request('http://localhost/searchargs_login?email=nada%40mucho.net&password=much'), 'request ok' );
+    is( $res->content, 'nuffin logged in', 'searchargs based login ok' );
+}
+
+# resultset test
+# searchargs test
+{
+    ok( my $res = request('http://localhost/resultset_login?email=j%40cpants.org&password=letmein'), 'request ok' );
+    is( $res->content, 'jayk logged in', 'resultset based login ok' );
+}
+
+{
+    $ENV{TESTAPP_CONFIG}->{authentication}->{realms}->{users}->{store}->{user_class} = 'Nonexistent::Class';
+    my $res = request('http://localhost/user_login?username=joeuser&password=hackme');
+    like( $res->content, qr/\$\Qc->model('Nonexistent::Class') did not return a resultset. Did you set user_class correctly?/, 'test for wrong user_class' );
+}
+	    
+	    
+
+
+# clean up
+unlink $ENV{TESTAPP_DB_FILE};

Modified: trunk/Catalyst-Authentication-Store-DBIx-Class/t/lib/TestApp.pm
===================================================================
--- trunk/Catalyst-Authentication-Store-DBIx-Class/t/lib/TestApp.pm	2008-06-27 16:20:19 UTC (rev 8019)
+++ trunk/Catalyst-Authentication-Store-DBIx-Class/t/lib/TestApp.pm	2008-06-27 23:18:54 UTC (rev 8020)
@@ -1,163 +1,169 @@
-package TestApp;
-
-use strict;
-use Catalyst;
-use Data::Dumper;
-
-TestApp->config( $ENV{TESTAPP_CONFIG} );
-
-TestApp->setup( @{$ENV{TESTAPP_PLUGINS}} );
-
-sub user_login : Global {
-    my ( $self, $c ) = @_;
-
-    ## this allows anyone to login regardless of status.
-    $c->authenticate({ username => $c->request->params->{'username'},
-                       password => $c->request->params->{'password'}
-                     });
-
-    if ( $c->user_exists ) {
-        if ( $c->req->params->{detach} ) {
-            $c->detach( $c->req->params->{detach} );
-        }
-        $c->res->body( $c->user->get('username') . ' logged in' );
-    }
-    else {
-        $c->res->body( 'not logged in' );
-    }
-}
-
-sub notdisabled_login : Global {
-    my ( $self, $c ) = @_;
-
-    $c->authenticate({ username => $c->request->params->{'username'},
-                       password => $c->request->params->{'password'},
-                       status => [ 'active', 'registered' ]
-                     });
-
-    if ( $c->user_exists ) {
-        if ( $c->req->params->{detach} ) {
-            $c->detach( $c->req->params->{detach} );
-        }
-        $c->res->body( $c->user->get('username') .  ' logged in' );
-    }
-    else {
-        $c->res->body( 'not logged in' );
-    }
-}
-
-sub searchargs_login : Global {
-    my ( $self, $c ) = @_;
-
-    my $username = $c->request->params->{'username'} || '';
-    my $email = $c->request->params->{'email'} || '';
-    
-    $c->authenticate({ 
-                        password => $c->request->params->{'password'},
-                        dbix_class => {
-                            searchargs => [ { "-or" => [ username => $username,
-                                                        email => $email ]}, 
-                                            { prefetch => qw/ map_user_role /}
-                                          ]
-                        }
-                     });
-
-    if ( $c->user_exists ) {
-        if ( $c->req->params->{detach} ) {
-            $c->detach( $c->req->params->{detach} );
-        }
-        $c->res->body( $c->user->get('username') .  ' logged in' );
-    }
-    else {
-        $c->res->body( 'not logged in' );
-    }
-}
-
-sub resultset_login : Global {
-    my ( $self, $c ) = @_;
-
-    my $username = $c->request->params->{'username'} || '';
-    my $email = $c->request->params->{'email'} || '';
-    
-    
-    my $rs = $c->model('TestApp::User')->search({ "-or" => [ username => $username,
-                                                              email => $email ]});
-    
-    $c->authenticate({ 
-                        password => $c->request->params->{'password'},
-                        dbix_class => { resultset => $rs }
-                     });
-                     
-    if ( $c->user_exists ) {
-        if ( $c->req->params->{detach} ) {
-            $c->detach( $c->req->params->{detach} );
-        }
-        $c->res->body( $c->user->get('username') .  ' logged in' );
-    }
-    else {
-        $c->res->body( 'not logged in' );
-    }
-}
-
-## need to add a resultset login test and a search args login test
-
-sub user_logout : Global {
-    my ( $self, $c ) = @_;
-
-    $c->logout;
-
-    if ( ! $c->user ) {
-        $c->res->body( 'logged out' );
-    }
-    else {
-        $c->res->body( 'not logged ok' );
-    }
-}
-
-sub get_session_user : Global {
-    my ( $self, $c ) = @_;
- 
-    if ( $c->user_exists ) {
-        $c->res->body($c->user->get('username')); # . " " . Dumper($c->user->get_columns()) );
-    }
-}
-
-sub is_admin : Global {
-    my ( $self, $c ) = @_;
-
-    eval {
-        if ( $c->assert_user_roles( qw/admin/ ) ) {
-            $c->res->body( 'ok' );
-        }
-    };
-    if ($@) {
-        $c->res->body( 'failed' );
-    }
-}
-
-sub is_admin_user : Global {
-    my ( $self, $c ) = @_;
-
-    eval {
-        if ( $c->assert_user_roles( qw/admin user/ ) ) {
-            $c->res->body( 'ok' );
-        }
-    };
-    if ($@) {
-        $c->res->body( 'failed' );
-    }
-}
-
-sub set_usersession : Global {
-    my ( $self, $c, $value ) = @_;
-    $c->user_session->{foo} = $value;
-    $c->res->body( 'ok' );
-}
-
-sub get_usersession : Global {
-    my ( $self, $c ) = @_;
-    $c->res->body( $c->user_session->{foo} || '' );
-}
-
-
-1;
+package TestApp;
+
+use strict;
+use Catalyst;
+use Data::Dumper;
+
+TestApp->config( $ENV{TESTAPP_CONFIG} );
+
+TestApp->setup( @{$ENV{TESTAPP_PLUGINS}} );
+
+sub user_login : Global {
+    my ( $self, $c ) = @_;
+
+    ## this allows anyone to login regardless of status.
+    eval {
+        $c->authenticate({ username => $c->request->params->{'username'},
+                           password => $c->request->params->{'password'}
+                         });
+        1;
+    } or do {
+        return $c->res->body($@);
+    };
+
+    if ( $c->user_exists ) {
+        if ( $c->req->params->{detach} ) {
+            $c->detach( $c->req->params->{detach} );
+        }
+        $c->res->body( $c->user->get('username') . ' logged in' );
+    }
+    else {
+        $c->res->body( 'not logged in' );
+    }
+}
+
+
+sub notdisabled_login : Global {
+    my ( $self, $c ) = @_;
+
+    $c->authenticate({ username => $c->request->params->{'username'},
+                       password => $c->request->params->{'password'},
+                       status => [ 'active', 'registered' ]
+                     });
+
+    if ( $c->user_exists ) {
+        if ( $c->req->params->{detach} ) {
+            $c->detach( $c->req->params->{detach} );
+        }
+        $c->res->body( $c->user->get('username') .  ' logged in' );
+    }
+    else {
+        $c->res->body( 'not logged in' );
+    }
+}
+
+sub searchargs_login : Global {
+    my ( $self, $c ) = @_;
+
+    my $username = $c->request->params->{'username'} || '';
+    my $email = $c->request->params->{'email'} || '';
+    
+    $c->authenticate({ 
+                        password => $c->request->params->{'password'},
+                        dbix_class => {
+                            searchargs => [ { "-or" => [ username => $username,
+                                                        email => $email ]}, 
+                                            { prefetch => qw/ map_user_role /}
+                                          ]
+                        }
+                     });
+
+    if ( $c->user_exists ) {
+        if ( $c->req->params->{detach} ) {
+            $c->detach( $c->req->params->{detach} );
+        }
+        $c->res->body( $c->user->get('username') .  ' logged in' );
+    }
+    else {
+        $c->res->body( 'not logged in' );
+    }
+}
+
+sub resultset_login : Global {
+    my ( $self, $c ) = @_;
+
+    my $username = $c->request->params->{'username'} || '';
+    my $email = $c->request->params->{'email'} || '';
+    
+    
+    my $rs = $c->model('TestApp::User')->search({ "-or" => [ username => $username,
+                                                              email => $email ]});
+    
+    $c->authenticate({ 
+                        password => $c->request->params->{'password'},
+                        dbix_class => { resultset => $rs }
+                     });
+                     
+    if ( $c->user_exists ) {
+        if ( $c->req->params->{detach} ) {
+            $c->detach( $c->req->params->{detach} );
+        }
+        $c->res->body( $c->user->get('username') .  ' logged in' );
+    }
+    else {
+        $c->res->body( 'not logged in' );
+    }
+}
+
+## need to add a resultset login test and a search args login test
+
+sub user_logout : Global {
+    my ( $self, $c ) = @_;
+
+    $c->logout;
+
+    if ( ! $c->user ) {
+        $c->res->body( 'logged out' );
+    }
+    else {
+        $c->res->body( 'not logged ok' );
+    }
+}
+
+sub get_session_user : Global {
+    my ( $self, $c ) = @_;
+ 
+    if ( $c->user_exists ) {
+        $c->res->body($c->user->get('username')); # . " " . Dumper($c->user->get_columns()) );
+    }
+}
+
+sub is_admin : Global {
+    my ( $self, $c ) = @_;
+
+    eval {
+        if ( $c->assert_user_roles( qw/admin/ ) ) {
+            $c->res->body( 'ok' );
+        }
+    };
+    if ($@) {
+        $c->res->body( 'failed' );
+    }
+}
+
+sub is_admin_user : Global {
+    my ( $self, $c ) = @_;
+
+    eval {
+        if ( $c->assert_user_roles( qw/admin user/ ) ) {
+            $c->res->body( 'ok' );
+        }
+    };
+    if ($@) {
+        $c->res->body( 'failed' );
+    }
+}
+
+sub set_usersession : Global {
+    my ( $self, $c, $value ) = @_;
+    $c->user_session->{foo} = $value;
+    $c->res->body( 'ok' );
+}
+
+sub get_usersession : Global {
+    my ( $self, $c ) = @_;
+    $c->res->body( $c->user_session->{foo} || '' );
+}
+
+
+1;




More information about the Catalyst-commits mailing list