Stop accidentally blowing up if we can't restore the user.. Add tests and docs for...
[catagits/Catalyst-Plugin-Authentication.git] / t / lib / AuthSessionTestApp.pm
CommitLineData
a3bf437a 1package User::SessionRestoring;
5c5af345 2use base qw/Catalyst::Authentication::User::Hash/;
a3bf437a 3
4sub for_session { $_[0]->id }
5sub store { $_[0]->{store} }
6
7package AuthSessionTestApp;
8use Catalyst qw/
9 Session
10 Session::Store::Dummy
11 Session::State::Cookie
12
13 Authentication
14 Authentication::Store::Minimal
15 Authentication::Credential::Password
16/;
17
18use Test::More;
19use Test::Exception;
20
21use Digest::MD5 qw/md5/;
22
23our $users;
24
25sub moose : Local {
26 my ( $self, $c ) = @_;
27
28 ok(!$c->sessionid, "no session id yet");
29 ok(!$c->user_exists, "no user exists");
30 ok(!$c->user, "no user yet");
31 ok($c->login( "foo", "s3cr3t" ), "can login with clear");
32 is( $c->user, $users->{foo}, "user object is in proper place");
33}
34
35sub elk : Local {
36 my ( $self, $c ) = @_;
37
38 ok( $c->sessionid, "session ID was restored" );
39 ok( $c->user_exists, "user exists" );
40 ok( $c->user, "a user was also restored");
41 is_deeply( $c->user, $users->{foo}, "restored user is the right one (deep test - store might change identity)" );
9c469e37 42
43 # Rename the user!
44 $users->{bar} = delete $users->{foo};
45}
46
47sub yak : Local {
48 my ( $self, $c ) = @_;
49 ok( $c->sessionid, "session ID was restored after user renamed" );
50 ok( $c->user_exists, "user appears to exist" );
51 ok( !$c->user, "try to restore - user was not restored");
52 ok( !$c->user_exists, "user no longer appears to exist" );
53}
54
55sub goat : Local {
56 my ( $self, $c ) = @_;
57 ok($c->login( "bar", "s3cr3t" ), "can login with clear (new username)");
58 is( $c->user, $users->{bar}, "user object is in proper place");
47c6643f 59 $c->logout;
a3bf437a 60}
61
62sub fluffy_bunny : Local {
9c469e37 63 my ( $self, $c ) = @_;
a3bf437a 64
9c469e37 65 ok( $c->session_is_valid, "session ID is restored after logout");
66 ok( !$c->user, "no user was restored after logout");
47c6643f 67
68 $c->delete_session("bah");
69}
70
71sub possum : Local {
72 my ( $self, $c ) = @_;
73
74 ok( !$c->session_is_valid, "no session ID was restored");
75 $c->session->{definitely_not_a_user} = "moose";
76
77}
78
79sub butterfly : Local {
80 my ( $self, $c ) = @_;
81
82 ok( $c->session_is_valid, "valid session" );
83 ok( !$c->user_exists, "but no user exists" );
84 ok( !$c->user, "no user object either" );
a3bf437a 85}
86
bf4d93a4 87__PACKAGE__->config->{'authentication'}{users} = $users = {
a3bf437a 88 foo => User::SessionRestoring->new(
89 id => 'foo',
90 password => "s3cr3t",
91 ),
92};
93
94__PACKAGE__->setup;
95
96$users->{foo}{store} = __PACKAGE__->default_auth_store;