Fix test so that it can restore properly
[catagits/Catalyst-Plugin-Authentication.git] / t / live_app_session.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7
8 BEGIN {
9         eval { require Catalyst::Plugin::Session; require Catalyst::Plugin::Session::State::Cookie };
10         plan skip_all => "This test needs Catalyst::Plugin::Session and Catalyst::Plugin::Session::State::Cookie installed" if $@;
11         plan tests => 12;
12 }
13
14 {
15         package User::SessionRestoring;
16         use base qw/Catalyst::Plugin::Authentication::User::Hash/;
17
18         sub for_session { $_[0]->id }
19         sub store { $_[0]->{store} }
20         
21         package AuthTestApp;
22         use Catalyst qw/
23                 Session
24                 Session::Store::Dummy
25                 Session::State::Cookie
26
27                 Authentication
28                 Authentication::Store::Minimal
29                 Authentication::Credential::Password
30         /;
31
32         use Test::More;
33         use Test::Exception;
34
35         use Digest::MD5 qw/md5/;
36
37         our $users;
38
39         sub moose : Local {
40                 my ( $self, $c ) = @_;
41
42                 ok(!$c->sessionid, "no session id yet");
43                 ok(!$c->user, "no user yet");
44                 ok($c->login( "foo", "s3cr3t" ), "can login with clear");
45                 is( $c->user, $users->{foo}, "user object is in proper place");
46         }
47
48         sub elk : Local {
49                 my ( $self, $c ) = @_;
50
51                 ok( $c->sessionid, "session ID was restored" );
52                 ok( $c->user, "a user was also restored");
53                 is_deeply( $c->user, $users->{foo}, "restored user is the right one (deep test - store might change identity)" );
54
55                 $c->delete_session("bah");
56         }
57
58         sub fluffy_bunny : Local {
59                 my ( $self, $c ) = @_;
60
61                 ok( !$c->sessionid, "no session ID was restored");
62                 ok( !$c->user, "no user was restored");
63         }
64
65         __PACKAGE__->config->{authentication}{users} = $users = {
66                 foo => User::SessionRestoring->new(
67                         id => 'foo',
68                         password => "s3cr3t",
69                 ),
70         };
71
72         __PACKAGE__->setup;
73
74         $users->{foo}{store} = __PACKAGE__->default_auth_store;
75 }
76
77 use Test::WWW::Mechanize::Catalyst qw/AuthTestApp/; # for the cookie support
78
79 my $m = Test::WWW::Mechanize::Catalyst->new;
80
81 $m->get_ok("http://localhost/moose", "get ok");
82 $m->get_ok("http://localhost/elk", "get ok");
83 $m->get_ok("http://localhost/fluffy_bunny", "get ok");
84