Added high performance version of the Sessions code as a branch
[catagits/Catalyst-Plugin-Session.git] / t / live_simple_session.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7 use Data::Dumper;
8 local $Data::Dumper::Sortkeys = 1;
9 use Clone;
10
11 BEGIN {
12
13     eval { require Test::WWW::Mechanize::Catalyst }
14         or plan skip_all =>
15         "Test::WWW::Mechanize::Catalyst is required for this test";
16
17     plan tests => 36;
18 }
19
20 use lib "t/lib";
21 use Test::WWW::Mechanize::Catalyst "SessionTestApp";
22
23 my $ua = Test::WWW::Mechanize::Catalyst->new;
24
25 # initial request - should not set cookie
26 $ua->get_ok( "http://localhost/page", "initial get" );
27 $ua->content_contains( "please login", "ua not logged in" );
28 is_deeply get_cookie(), undef, "no cookies yet";
29
30 # request that checks the session - should not set cookie
31 $ua->get_ok( "http://localhost/inspect_session",
32     "check for value in session" );
33 $ua->content_contains( "value of logged_in is 'undef'",
34     "check ua 'logged_in' val" );
35 is_deeply get_cookie(), undef, "no cookies yet";
36
37 # Login - should create a session
38 $ua->get_ok( "http://localhost/login", "log ua in" );
39 $ua->content_contains( "logged in", "ua logged in" );
40
41 # check that the session cookie created
42 my $session_id = get_cookie()->{val};
43 ok $session_id, "found a session cookie ($session_id)";
44
45 # check session loaded from store
46 $ua->get_ok( "http://localhost/page", "get main page" );
47 $ua->content_contains( "you are logged in", "ua logged in" );
48 is get_cookie()->{val}, $session_id, "session id has not changed";
49
50 # check that the expires time is updated
51 {
52     my $min_lifetime
53         = SessionTestApp->config->{session}{min_lifetime};
54     my $max_lifetime
55         = SessionTestApp->config->{session}{max_lifetime};
56
57     # do some requests until the expires changes
58     my $original_expiry = get_cookie()->{expires};
59
60     for ( 1 .. 10 ) {
61         sleep 1;
62         $ua->get("http://localhost/inspect_session");
63         my $new_expiry = get_cookie()->{expires};
64         next if $new_expiry == $original_expiry;
65         $original_expiry = $new_expiry;
66         last;
67     }
68
69     # expiry just updated - check it stays the same
70     $ua->get_ok(
71         "http://localhost/inspect_session",
72         "get page to see expiry not changed"
73     );
74     is get_cookie()->{expires}, $original_expiry,
75         "expiry is still '$original_expiry'";
76     is get_cookie()->{val}, $session_id, "session id has not changed";
77
78     # sleep so that we go past the min lifetime
79     ok sleep $_, "sleep $_ so expires get extended"
80         for $max_lifetime - $min_lifetime + 1;
81
82     # expiry just updated - check it stays the same
83     $ua->get_ok(
84         "http://localhost/inspect_session",
85         "get page to see expiry has changed"
86     );
87     my $new_expiry = get_cookie()->{expires};
88     cmp_ok $new_expiry, '>', $original_expiry,
89         "expiry updated to '$new_expiry'";
90     is get_cookie()->{val}, $session_id, "session id has not changed";
91
92     # sleep beyond the lifetime and see that the session gets expired
93     ok sleep $_, "sleep $_ so session is too old" for $max_lifetime + 2;
94     $ua->get_ok(
95         "http://localhost/inspect_session",
96         "get page to see session expired"
97     );
98     is get_cookie(), undef, "Cookie has been reset";
99
100 }
101
102 # check that a session that is not in the db is deleted
103
104 my @session_ids_to_test = (
105     'a' x 40,                      # valid session id
106     'This is not valid @#$%^&',    # bad value
107 );
108
109 foreach my $new_session_id (@session_ids_to_test) {
110
111     pass "--- Testing session_id '$new_session_id' ---";
112
113     $ua->get_ok( "http://localhost/login", "log ua in" );
114     $ua->content_contains( "logged in", "ua logged in" );
115
116     my $session_id = get_cookie()->{val};
117     ok $session_id, "have session_id '$session_id'";
118
119     # change the value in the cookie to a valid value
120     ok set_cookie_val($new_session_id),
121         "change cookie value to '$new_session_id'";
122
123     # check that the cookie gets deleted
124     $ua->get_ok(
125         "http://localhost/inspect_session",
126         "get page to see if session is deleted"
127     );
128     is get_cookie(), undef, "Cookie has been reset";
129
130 }
131
132 #############################################################################
133
134 sub get_cookie {
135     my $cookie_jar = $ua->cookie_jar;
136
137     my $cookie_data = undef;
138
139     $cookie_jar->scan(
140         sub {
141             my ($version, $key,     $val,       $path,
142                 $domain,  $port,    $path_spec, $secure,
143                 $expires, $discard, $hash
144             ) = @_;
145
146             # warn "cookie key: $key";
147
148             if ( $key eq 'sessiontestapp_session' ) {
149                 $cookie_data = {
150                     val     => $val,
151                     expires => $expires,
152                 };
153             }
154         }
155     );
156
157     return $cookie_data;
158 }
159
160 sub set_cookie_val {
161     my $new_val    = shift;
162     my $cookie_jar = $ua->cookie_jar;
163
164     $cookie_jar->scan(
165         sub {
166             my ( $version, $key, $val, $path, $domain ) = @_;
167
168             # warn "cookie key: $key";
169
170             if ( $key eq 'sessiontestapp_session' ) {
171
172                 $cookie_jar->set_cookie( $version, $key, $new_val, $path,
173                     $domain );
174
175             }
176         }
177     );
178
179     return 1;
180 }