23975fba032ec814fa10a8be719b7eeb754c5095
[catagits/Catalyst-Plugin-Session.git] / lib / Catalyst / Plugin / Session / Test / Store.pm
1 #!/usr/bin/perl
2
3 package Catalyst::Plugin::Session::Test::Store;
4
5 use strict;
6 use warnings;
7
8 use Test::More tests => 19;
9 use File::Temp;
10 use File::Spec;
11
12 use Catalyst ();
13
14 sub import {
15         shift;
16         my %args = @_;
17
18         my $backend = $args{backend};
19         my $cfg     = $args{config};
20
21         my $p = "Session::Store::$backend";
22         use_ok(my $m = "Catalyst::Plugin::$p");
23
24         isa_ok(bless({}, $m), "Catalyst::Plugin::Session::Store");
25
26         our $restored_session_id;
27
28         {
29                 package SessionStoreTest;
30                 use Catalyst qw/-Engine=Test Session Session::State/;
31                 push our(@ISA), $m;
32
33                 our $VERSION = "0.01";
34
35                 use Test::More;
36
37                 sub prepare_cookies {
38                         my $c = shift;
39                         $c->sessionid($restored_session_id) if defined $restored_session_id;
40                         $c->NEXT::prepare_cookies(@_);
41                 }
42
43                 sub create_session : Global {
44                         my ( $self, $c ) = @_;
45                         ok(!$c->sessionid, "no session id yet");
46                         ok($c->session, "session created");
47                         ok($c->sessionid, "with a session id");
48
49                         $restored_session_id = $c->sessionid;
50
51                         $c->session->{magic} = "møøse";
52                 }
53
54                 sub recover_session : Global {
55                         my ( $self, $c ) = @_;
56                         ok($c->sessionid, "session id exists");
57                         is($c->sessionid, $restored_session_id, "and is the one we saved in the last action");
58                         ok($c->session, "a session exists");
59                         is($c->session->{magic}, "møøse", "and it contains what we put in on the last attempt");
60                         $c->delete_session("user logout");
61                         $restored_session_id = undef;
62                 }
63
64                 sub after_session : Global {
65                         my ( $self, $c ) = @_;
66                         ok(!$c->sessionid, "no session id");
67                         ok(!$c->session->{magic}, "session data not restored");
68                         ok(!$c->session_delete_reason, "no reason for deletion");
69                 }
70
71                 @{ __PACKAGE__->config->{session} }{ keys %$cfg } = values %$cfg;
72                 
73                 __PACKAGE__->setup;
74         }
75
76         {
77                 package SessionStoreTest2;
78                 use Catalyst qw/-Engine=Test Session Session::State/;
79                 push our(@ISA), $m;
80
81                 our $VERSION = "123";
82
83                 use Test::More;
84
85                 sub prepare_cookies {
86                         my $c = shift;
87                         $c->sessionid($restored_session_id) if defined $restored_session_id;
88                         $c->NEXT::prepare_cookies(@_);
89                 }
90
91                 sub create_session : Global {
92                         my ( $self, $c ) = @_;
93
94                         $c->session->{magic} = "møøse";
95
96                         $restored_session_id = $c->sessionid;
97                 }
98
99                 sub recover_session : Global {
100                         my ( $self, $c ) = @_;
101
102                         ok(!$c->sessionid, "no session id");
103
104                         is($c->session_delete_reason, "session expired", "reason is that the session expired");
105
106                         ok(!$c->session->{magic}, "no saved data");
107                 }
108
109                 __PACKAGE__->config->{session}{expires} = 0;
110
111                 @{ __PACKAGE__->config->{session} }{ keys %$cfg } = values %$cfg;
112
113                 __PACKAGE__->setup;
114         }
115
116         use Test::More;
117
118         can_ok($m, "get_session_data");
119         can_ok($m, "store_session_data");
120         can_ok($m, "delete_session_data");
121         can_ok($m, "delete_expired_sessions");
122
123         {
124                 package t1; 
125                 use Catalyst::Test "SessionStoreTest";
126
127                 get("/create_session");
128                 get("/recover_session");
129                 get("/after_session");
130         }
131
132         {
133                 package t2;
134                 use Catalyst::Test "SessionStoreTest2";
135
136                 get("/create_session");
137                 sleep 1; # let the session expire
138                 get("/recover_session");
139         }
140 }
141
142 __PACKAGE__;
143
144 __END__
145
146 =pod
147
148 =head1 NAME
149
150 Catalyst::Plugin::Session::Test::Store - Reusable sanity for session storage
151 engines.
152
153 =head1 SYNOPSIS
154
155         #!/usr/bin/perl
156
157         use Catalyst::Plugin::Session::Test::Store (
158                 backend => "FastMmap",
159                 config => {
160                         storage => "/tmp/foo",
161                 },
162         );
163
164 =head1 DESCRIPTION
165
166 =cut
167
168