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

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

Changes since

  • 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 Config::Tiny;
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 a Config::Tiny" );
13
14my $config = Config::Tiny->new;
15$config->{_} = {
16                 site_name => "Test Site",
17               };
18
19eval { OpenGuides::CGI->make_prefs_cookie( config => $config ); };
20is( $@, "", "...but not if it is" );
21
22my $cookie = OpenGuides::CGI->make_prefs_cookie(
23    config                     => $config,
24    username                   => "Kake",
25    include_geocache_link      => 1,
26    preview_above_edit_box     => 1,
27    latlong_traditional        => 1,
28    omit_help_links            => 1,
29    show_minor_edits_in_rc     => 1,
30    default_edit_type          => "tidying",
31    cookie_expires             => "never",
32    track_recent_changes_views => 1,
33);
34isa_ok( $cookie, "CGI::Cookie", "->make_prefs_cookie returns a cookie" );
35
36my $expiry_string = $cookie->expires;
37# Hack off the timezone bit since strptime can't parse it portably.
38# Timezones taken from RFC 822.
39$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)$//;
40print "# (String hacked to $expiry_string)\n";
41my $expiry = Time::Piece->strptime( $expiry_string, "%a, %d-%b-%Y %T");
42print "# Expires: " . $cookie->expires . ", ie $expiry\n";
43my $now = localtime;
44print "# cookie should still be valid in a year, ie " . ($now + ONE_YEAR) . "\n";
45ok( $expiry - ( $now + ONE_YEAR ) > 0, "cookie expiry date correct" );
46
47$ENV{HTTP_COOKIE} = $cookie;
48
49eval { OpenGuides::CGI->get_prefs_from_cookie; };
50ok( $@, "->get_prefs_from_cookie dies if no config object supplied" );
51
52eval { OpenGuides::CGI->get_prefs_from_cookie( config => "foo" ); };
53ok( $@, "...or if config isn't a Config::Tiny" );
54
55eval { OpenGuides::CGI->get_prefs_from_cookie( config => $config ); };
56is( $@, "", "...but not if it is" );
57
58my %prefs = OpenGuides::CGI->get_prefs_from_cookie( config => $config );
59is( $prefs{username}, "Kake",
60    "get_prefs_from_cookie can find username" );
61is( $prefs{include_geocache_link}, 1, "...and geocache prefs" );
62is( $prefs{preview_above_edit_box}, 1, "...and preview prefs" );
63is( $prefs{latlong_traditional}, 1, "...and latlong prefs" );
64is( $prefs{omit_help_links}, 1, "...and help link prefs" );
65is( $prefs{show_minor_edits_in_rc}, 1, "...and minor edits prefs" );
66is( $prefs{default_edit_type}, "tidying", "...and default edit prefs" );
67is( $prefs{cookie_expires}, "never", "...and requested cookie expiry" );
68ok( $prefs{track_recent_changes_views}, "...and recent changes tracking" );
69
70# Check that cookie parsing fails nicely if no cookie set.
71delete $ENV{HTTP_COOKIE};
72%prefs = eval { OpenGuides::CGI->get_prefs_from_cookie( config => $config ); };
73is( $@, "", "->get_prefs_from_cookie doesn't die if no cookie set" );
74is( keys %prefs, 9, "...and returns nine default values" );
Note: See TracBrowser for help on using the repository browser.