expand t/cat_test.t to test session with authentication
Gerda Shank [Fri, 25 May 2012 17:53:09 +0000 (13:53 -0400)]
t/cat_test.t
t/lib/SessionTestApp.pm
t/lib/SessionTestApp/Controller/Root.pm

index 83705e6..7175b1d 100644 (file)
@@ -9,9 +9,13 @@ use HTTP::Request::Common;
 use FindBin qw($Bin);
 use lib "$Bin/lib";
 
+# this test was copied from CatalystX::SimpleLogin
+
 BEGIN {
     plan skip_all => "Need Catalyst::Plugin::Session::State::Cookie"
         unless do { local $@; eval { require Catalyst::Plugin::Session::State::Cookie; } };
+    plan skip_all => "Need Catalyst::Plugin::Authentication"
+        unless do { local $@; eval { require Catalyst::Plugin::Authentication; } };
 }
 
 use Catalyst::Test 'SessionTestApp';
@@ -22,10 +26,17 @@ is($res->code, 200, 'succeeded');
 my $cookie = $res->header('Set-Cookie');
 ok($cookie, 'Have a cookie');
 
-# this checks that cookie persists across a redirect
-($res, $c) = ctx_request(GET 'http://localhost/do_redirect', Cookie => $cookie);
+# check that the cookie has not been reset by the get
+($res, $c) = ctx_request(GET 'http://localhost/page', Cookie => $cookie);
+like($c->res->body, qr/logged in/, 'Am logged in');
+my $new_cookie = $res->header('Set-Cookie');
+is( $cookie, $new_cookie, 'cookie is the same' );
+
+# this checks that cookie exists after a logout and redirect
+# Catalyst::Plugin::Authentication removes the user session (remove_persisted_user)
+($res, $c) = ctx_request(GET 'http://localhost/logout_redirect', Cookie => $cookie);
 is($res->code, 302, 'redirected');
-is($res->header('Location'), 'http://localhost/page', 'Redirected after do_redirect');
-ok($res->header('Set-Cookie'), 'Cookie is still there after redirect');
+is($res->header('Location'), 'http://localhost/from_logout_redirect', 'Redirected after logout_redirect');
+ok($res->header('Set-Cookie'), 'Cookie is there after redirect');
 
 done_testing;
index 9f211e7..f569aaa 100644 (file)
@@ -1,19 +1,39 @@
 #!/usr/bin/env perl
 
 package SessionTestApp;
-use Catalyst qw/Session Session::Store::Dummy Session::State::Cookie/;
+use Catalyst qw/Session Session::Store::Dummy Session::State::Cookie Authentication/;
 
 use strict;
 use warnings;
 
 __PACKAGE__->config('Plugin::Session' => {
-    # needed for live_verify_user_agent.t; should be harmless for other tests 
-    verify_user_agent => 1,  
-    
-    # need for live_verify_address.t; should be harmless for other tests
-    verify_address => 1,
+        # needed for live_verify_user_agent.t; should be harmless for other tests
+        verify_user_agent => 1,
+        verify_address => 1,
 
-});
+    },
+
+    'Plugin::Authentication' => {
+        default => {
+            credential => {
+                class => 'Password',
+                password_field => 'password',
+                password_type => 'clear'
+            },
+            store => {
+                class => 'Minimal',
+                users => {
+                    bob => {
+                        password => "s00p3r",
+                    },
+                    william => {
+                        password => "s3cr3t",
+                    },
+                },
+            },
+        },
+    },
+);
 
 __PACKAGE__->setup;
 
index 203815b..a6d86d1 100644 (file)
@@ -28,10 +28,17 @@ sub logout : Global {
     $c->delete_session("logout");
 }
 
-sub do_redirect : Global {
+sub logout_redirect : Global {
     my ( $self, $c ) = @_;
-    # session is not deleted
-    $c->res->redirect( $c->uri_for('page') );
+
+    $c->logout;
+    $c->res->output("redirect from here");
+    $c->res->redirect( $c->uri_for('from_logout_redirect') );
+}
+
+sub from_logout_redirect : Global {
+    my ( $self, $c ) = @_;
+    $c->res->output( "got here from logout_redirect" );
 }
 
 sub set_session_variable : Global {