Big checkin of the Session plugin
[catagits/Catalyst-Plugin-Session.git] / t / 02_session_data.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 19;
7 use Test::MockObject;
8 use Test::Deep;
9
10 my $m; BEGIN { use_ok($m = "Catalyst::Plugin::Session") }
11
12 my %config;
13 my $log = Test::MockObject->new;
14 my $req = Test::MockObject->new;
15 my @mock_isa = ();
16 my %session;
17
18 $log->set_true(qw/fatal warn/);
19
20 $req->set_always(address => "127.0.0.1");
21
22 {
23         package MockCxt;
24         use base $m;
25         sub new { bless {}, $_[0] }
26         sub config { \%config };
27         sub log { $log }
28         sub request { $req }
29         sub debug { 0 }
30         sub isa { 1 } # subvert the plugin tests, we're faking them
31         sub get_session_data { \%session }
32         sub delete_session_data { }
33 }
34
35 {
36         my $c = MockCxt->new;
37         $c->setup;
38
39         $c->prepare;
40         ok(!$c->{session}, "without a session ID prepare doesn't load a session");
41 }
42
43 {
44         %config = (session => { expires => 100 });
45
46         %session = (
47                 __expires => time() + 1000,
48                 __created => time(),
49                 __updated => time(),
50                 __address => "127.0.0.1",
51         );
52
53         my $c = MockCxt->new;
54         $c->setup;
55
56         $c->sessionid("the_session");
57         $c->prepare;
58
59         ok($c->{session}, 'session "restored" with session id');
60 }
61
62 {
63         %session = (
64                 __expires => time() - 100, # a while ago
65                 __created => time() - 1000,
66                 __udpated => time() - 1000,
67                 __address => "127.0.0.1",
68         );
69
70         my $c = MockCxt->new;
71         $c->setup;
72
73         $c->sessionid("the_session");
74         $c->prepare;
75
76         ok(!$c->{session}, "expired sessions are deleted");
77         like($c->session_delete_reason, qr/expire/i, "with appropriate reason");
78         ok(!$c->sessionid, "sessionid is also cleared");
79 }
80
81 {
82         %session = (
83                 __expires => time() + 1000,
84                 __created => time(),
85                 __updated => time(),
86                 __address => "unlocalhost",
87         );
88
89         my $c = MockCxt->new;
90         $c->setup;
91
92         $c->sessionid("the_session");
93         $c->prepare;
94
95         ok(!$c->{session}, "hijacked sessions are deleted");
96         like($c->session_delete_reason, qr/mismatch/, "with appropriate reason");
97         ok(!$c->sessionid, "sessionid is also cleared");
98 }
99
100 {
101         %session = (
102                 __expires => time() + 1000,
103                 __created => time(),
104                 __updated => time(),
105                 __address => "unlocalhost",
106         );
107
108         $config{session}{verify_address} = 0;
109
110         my $c = MockCxt->new;
111         $c->setup;
112
113         $c->sessionid("the_session");
114         $c->prepare;
115
116         ok($c->{session}, "address mismatch is OK if verify_address is disabled");
117 }
118
119 {
120         %session = ();
121         %config = ();
122
123         my $now = time;
124
125         my $c = MockCxt->new;
126         $c->setup;
127         $c->prepare;
128
129         ok($c->session, "creating a session works");
130         ok($c->sessionid, "session id generated");
131
132         cmp_ok($c->session->{__created}, ">=", $now, "__created time is logical");
133         cmp_ok($c->session->{__updated}, ">=", $now, "__updated time is logical");
134         cmp_ok($c->session->{__expires}, ">=", ($now + $config{session}{expire}), "__expires time is logical");
135         is($c->session->{__address}, $c->request->address, "address is also correct");
136
137         cmp_deeply(
138                 [ keys %{ $c->{session} } ],
139                 bag(qw/__expires __created __updated __address/),
140                 "initial keys in session are all there",
141         );
142 }
143
144
145
146 {
147         %session = (
148                 __expires => time() + 1000,
149                 __created => time(),
150                 __updated => time(),
151                 __address => "127.0.0.1",
152         );
153
154         $config{session}{expire} = 2000;
155
156         my $c = MockCxt->new;
157         $c->setup;
158
159         my $now = time();
160         
161         $c->sessionid("the_session");
162         $c->prepare;
163         $c->finalize;
164
165         ok($c->{session}, "session is still alive after 1/2 expired and finalized");
166
167         cmp_ok($c->session->{__expires}, ">=", $now + 2000, "session expires time extended");
168 }
169