source: trunk/t/13_cookies.t @ 730

Last change on this file since 730 was 587, checked in by kake, 17 years ago

Encapsulate config data in OpenGuides::Config.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.0 KB
Line 
1use strict;
2use OpenGuides::Config;
3use OpenGuides::CGI;
4use Time::Piece;
5use Time::Seconds;
6use Test::More tests => 19;
7
8eval { OpenGuides::CGI->make_prefs_cookie; };
9ok( $@, "->make_prefs_cookie dies if no config object supplied" );
10
11eval { OpenGuides::CGI->make_prefs_cookie( config => "foo" ); };
12ok( $@, "...or if config isn't an OpenGuides::Config" );
13
14my $config = OpenGuides::Config->new( vars => { site_name => "Test Site" } );
15
16eval { OpenGuides::CGI->make_prefs_cookie( config => $config ); };
17is( $@, "", "...but not if it is" );
18
19my $cookie = OpenGuides::CGI->make_prefs_cookie(
20    config                     => $config,
21    username                   => "Kake",
22    include_geocache_link      => 1,
23    preview_above_edit_box     => 1,
24    latlong_traditional        => 1,
25    omit_help_links            => 1,
26    show_minor_edits_in_rc     => 1,
27    default_edit_type          => "tidying",
28    cookie_expires             => "never",
29    track_recent_changes_views => 1,
30);
31isa_ok( $cookie, "CGI::Cookie", "->make_prefs_cookie returns a cookie" );
32
33my $expiry_string = $cookie->expires;
34# Hack off the timezone bit since strptime can't parse it portably.
35# Timezones taken from RFC 822.
36$expiry_string =~ s/ (UT|GMT|EST|EDT|CST|CDT|MST|MDT|PST|PDT|1[A-IK-Z]|\+\d\d\d\d|-\d\d\d\d)$//;
37print "# (String hacked to $expiry_string)\n";
38my $expiry = Time::Piece->strptime( $expiry_string, "%a, %d-%b-%Y %T");
39print "# Expires: " . $cookie->expires . ", ie $expiry\n";
40my $now = localtime;
41print "# cookie should still be valid in a year, ie " . ($now + ONE_YEAR) . "\n";
42ok( $expiry - ( $now + ONE_YEAR ) > 0, "cookie expiry date correct" );
43
44$ENV{HTTP_COOKIE} = $cookie;
45
46eval { OpenGuides::CGI->get_prefs_from_cookie; };
47ok( $@, "->get_prefs_from_cookie dies if no config object supplied" );
48
49eval { OpenGuides::CGI->get_prefs_from_cookie( config => "foo" ); };
50ok( $@, "...or if config isn't an OpenGuides::Config" );
51
52eval { OpenGuides::CGI->get_prefs_from_cookie( config => $config ); };
53is( $@, "", "...but not if it is" );
54
55my %prefs = OpenGuides::CGI->get_prefs_from_cookie( config => $config );
56is( $prefs{username}, "Kake",
57    "get_prefs_from_cookie can find username" );
58is( $prefs{include_geocache_link}, 1, "...and geocache prefs" );
59is( $prefs{preview_above_edit_box}, 1, "...and preview prefs" );
60is( $prefs{latlong_traditional}, 1, "...and latlong prefs" );
61is( $prefs{omit_help_links}, 1, "...and help link prefs" );
62is( $prefs{show_minor_edits_in_rc}, 1, "...and minor edits prefs" );
63is( $prefs{default_edit_type}, "tidying", "...and default edit prefs" );
64is( $prefs{cookie_expires}, "never", "...and requested cookie expiry" );
65ok( $prefs{track_recent_changes_views}, "...and recent changes tracking" );
66
67# Check that cookie parsing fails nicely if no cookie set.
68delete $ENV{HTTP_COOKIE};
69%prefs = eval { OpenGuides::CGI->get_prefs_from_cookie( config => $config ); };
70is( $@, "", "->get_prefs_from_cookie doesn't die if no cookie set" );
71is( keys %prefs, 9, "...and returns nine default values" );
Note: See TracBrowser for help on using the repository browser.