97f7102f8e4eb340da2f606e1e2439c43ead0b83
[catagits/Catalyst-Plugin-Session.git] / t / live_app.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::State::Cookie; Catalyst::Plugin::Session::State::Cookie->VERSION(0.03) }
10       or plan skip_all =>
11       "Catalyst::Plugin::Session::State::Cookie 0.03 or higher is required for this test";
12
13     eval {
14         require Test::WWW::Mechanize::Catalyst;
15         Test::WWW::Mechanize::Catalyst->VERSION(0.51);
16     }
17     or plan skip_all =>
18         'Test::WWW::Mechanize::Catalyst >= 0.51 is required for this test';
19 }
20
21 use lib "t/lib";
22 use Test::WWW::Mechanize::Catalyst "SessionTestApp";
23
24 my $ua1 = Test::WWW::Mechanize::Catalyst->new;
25 my $ua2 = Test::WWW::Mechanize::Catalyst->new;
26
27 $_->get_ok( "http://localhost/page", "initial get" ) for $ua1, $ua2;
28
29 $ua1->content_contains( "please login", "ua1 not logged in" );
30 $ua2->content_contains( "please login", "ua2 not logged in" );
31
32 $ua1->get_ok( "http://localhost/login", "log ua1 in" );
33 $ua1->content_contains( "logged in", "ua1 logged in" );
34
35 $_->get_ok( "http://localhost/page", "get main page" ) for $ua1, $ua2;
36
37 $ua1->content_contains( "you are logged in", "ua1 logged in" );
38 $ua2->content_contains( "please login",      "ua2 not logged in" );
39
40 $ua2->get_ok( "http://localhost/login", "get main page" );
41 $ua2->content_contains( "logged in", "log ua2 in" );
42
43 $_->get_ok( "http://localhost/page", "get main page" ) for $ua1, $ua2;
44
45 $ua1->content_contains( "you are logged in", "ua1 logged in" );
46 $ua2->content_contains( "you are logged in", "ua2 logged in" );
47
48 my ( $u1_expires ) = ($ua1->content =~ /(\d+)$/);
49 my ( $u2_expires ) = ($ua2->content =~ /(\d+)$/);
50
51 sleep 1;
52
53 $_->get_ok( "http://localhost/page", "get main page" ) for $ua1, $ua2;
54
55 $ua1->content_contains( "you are logged in", "ua1 logged in" );
56 $ua2->content_contains( "you are logged in", "ua2 logged in" );
57
58 my ( $u1_expires_updated ) = ($ua1->content =~ /(\d+)$/);
59 my ( $u2_expires_updated ) = ($ua2->content =~ /(\d+)$/);
60
61 cmp_ok( $u1_expires, "<", $u1_expires_updated, "expiry time updated");
62 cmp_ok( $u2_expires, "<", $u2_expires_updated, "expiry time updated");
63
64 $ua2->get_ok( "http://localhost/logout", "log ua2 out" );
65 $ua2->content_like( qr/logged out/, "ua2 logged out" );
66 $ua2->content_like( qr/after 2 request/,
67     "ua2 made 2 requests for page in the session" );
68
69 $_->get_ok( "http://localhost/page", "get main page" ) for $ua1, $ua2;
70
71 $ua1->content_contains( "you are logged in", "ua1 logged in" );
72 $ua2->content_contains( "please login",      "ua2 not logged in" );
73
74 $ua1->get_ok( "http://localhost/logout", "log ua1 out" );
75 $ua1->content_like( qr/logged out/, "ua1 logged out" );
76 $ua1->content_like( qr/after 4 requests/,
77     "ua1 made 4 request for page in the session" );
78
79 $_->get_ok( "http://localhost/page", "get main page" ) for $ua1, $ua2;
80
81 $ua1->content_contains( "please login", "ua1 not logged in" );
82 $ua2->content_contains( "please login", "ua2 not logged in" );
83
84 my $ua3 = Test::WWW::Mechanize::Catalyst->new;
85 $ua3->get_ok( "http://localhost/login", "log ua3 in" );
86 $ua3->get_ok( "http://localhost/dump_these_loads_session");
87 $ua3->content_contains('NOT');
88
89 my $ua4 = Test::WWW::Mechanize::Catalyst->new;
90 $ua4->get_ok( "http://localhost/page", "initial get" );
91 $ua4->content_contains( "please login", "ua4 not logged in" );
92
93 $ua4->get_ok( "http://localhost/login", "log ua4 in" );
94 $ua4->content_contains( "logged in", "ua4 logged in" );
95
96 $ua4->get_ok( "http://localhost/extend_session_expires", "ua4 extend expire session" );
97
98 my ( $ua4_expires ) = ($ua4->content =~ /(\d+)$/);
99
100 ok( ($ua4_expires - time() - 86400) >= 0, 'extend_session_expires with really long value' );
101
102 sleep 1;
103
104 $ua4->get( "http://localhost/page", "get page" );
105 my ( $ua4_expires_updated ) = ($ua4->content =~ /(\d+)$/);
106 diag( "ua4_expires => $ua4_expires");
107 diag( "ua4_expires_updated => $ua4_expires_updated");
108 ok( $ua4_expires < $ua4_expires_updated, 'update extended session' );
109
110 diag("Testing against Catalyst $Catalyst::VERSION");
111 diag("Testing Catalyst::Plugin::Session $Catalyst::Plugin::Session::VERSION");
112
113 done_testing;