]> Dogcows Code - chaz/p5-CGI-Ex/blob - t/8_auth_00_base.t
add PSGI handler
[chaz/p5-CGI-Ex] / t / 8_auth_00_base.t
1 # -*- Mode: Perl; -*-
2
3 =head1 NAME
4
5 8_auth_00_base.t - Testing of the CGI::Ex::Auth module.
6
7 =cut
8
9 use strict;
10 use Test::More tests => 68;
11
12 use_ok('CGI::Ex::Auth');
13
14 {
15 package Auth;
16 use base qw(CGI::Ex::Auth);
17 use strict;
18 use vars qw($printed $set_cookie $deleted_cookie $failed_login_user $cookie);
19
20 sub login_print { $failed_login_user = shift->login_hash_common->{'cea_user'}; $printed = 1 }
21 sub set_cookie { shift; $cookie = shift; $set_cookie = 1 }
22 sub delete_cookie { $cookie = {}; $deleted_cookie = 1 }
23 sub get_pass_by_user { '123qwe' }
24 sub script_name { $0 }
25 sub no_cookie_verify { 1 }
26 sub secure_hash_keys { ['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'bbbbbbbbbbbbbbbbbbbbbbbbbbb', 'ccc'] }
27 sub failed_sleep { 0 }
28
29 sub reset {
30 $Auth::printed = $Auth::set_cookie = $Auth::deleted_cookie = 0;
31 $Auth::failed_login_user = '';
32 $Auth::cookie = {};
33 }
34 }
35
36 {
37 package Aut2;
38 use base qw(Auth);
39 use vars qw($crypt);
40 BEGIN { $crypt = crypt('123qwe', 'SS') };
41 sub use_crypt { 1 }
42 sub get_pass_by_user { {password => $crypt, foobar => 'baz'} }
43 }
44
45 my $token = Auth->new->generate_token({user => 'test', real_pass => '123qwe', use_base64 => 1});
46
47 my $form_bad = { cea_user => 'test', cea_pass => '123qw' };
48 my $form_good = { cea_user => 'test', cea_pass => '123qwe' };
49 my $form_good2 = { cea_user => $token };
50 my $form_good3 = { cea_user => 'test/123qwe' };
51 my $cookie_bad = { cea_user => 'test/123qw' };
52 my $cookie_good = { cea_user => 'test/123qwe' };
53 my $cookie_good2 = { cea_user => $token };
54
55 sub form_good { Auth->get_valid_auth({form => {%$form_good}, cookies => {} }) }
56 Auth::reset();
57 ok(form_good(), "Got good auth");
58 ok(! $Auth::printed, "Printed was not set");
59 ok($Auth::set_cookie, "Set_cookie called");
60 ok(! $Auth::deleted_cookie, "deleted_cookie was not called");
61
62 sub form_good2 { Auth->get_valid_auth({form => {%$form_good2}, cookies => {} }) }
63 Auth::reset();
64 ok(form_good2(), "Got good auth");
65 ok(! $Auth::printed, "Printed was not set");
66 ok($Auth::set_cookie, "Set_cookie called");
67 ok(! $Auth::deleted_cookie, "deleted_cookie was not called");
68
69 sub form_good3 { Aut2->get_valid_auth({form => {%$form_good3}, cookies => {} }) }
70 Auth::reset();
71 ok(form_good3(), "Got good auth");
72 ok(! $Auth::printed, "Printed was not set");
73 ok($Auth::set_cookie, "Set_cookie called");
74 ok(! $Auth::deleted_cookie, "deleted_cookie was not called");
75
76 sub form_bad { Auth->get_valid_auth({form => {%$form_bad}, cookies => {} }) }
77 Auth::reset();
78 ok(! form_bad(), "Got bad auth");
79 ok($Auth::printed, "Printed was set");
80 ok(! $Auth::set_cookie, "set_cookie called");
81 ok(! $Auth::deleted_cookie, "deleted_cookie was not called");
82 is($Auth::failed_login_user, 'test', 'correct user on failed passed information');
83
84 sub cookie_good { Auth->get_valid_auth({form => {}, cookies => {%$cookie_good} }) }
85 Auth::reset();
86 ok(cookie_good(), "Got good auth");
87 ok(! $Auth::printed, "Printed was not set");
88 ok($Auth::set_cookie, "Set_cookie called");
89 ok(! $Auth::deleted_cookie, "deleted_cookie was not called");
90
91 sub cookie_good2 { Auth->get_valid_auth({form => {}, cookies => {%$cookie_good2}}) }
92 Auth::reset();
93 ok(cookie_good2(), "Got good auth");
94 ok(! $Auth::printed, "Printed was not set");
95 ok($Auth::set_cookie, "Set_cookie called");
96 ok(! $Auth::deleted_cookie, "deleted_cookie was not called");
97
98 sub cookie_bad { Auth->get_valid_auth({form => {}, cookies => {%$cookie_bad} }) }
99 Auth::reset();
100 ok(! cookie_bad(), "Got bad auth");
101 ok($Auth::printed, "Printed was set");
102 ok(! $Auth::set_cookie, "Set_cookie was not called");
103 ok($Auth::deleted_cookie, "deleted_cookie was called");
104 is($Auth::failed_login_user, 'test', 'correct user on failed passed information');
105
106 sub combined_good { Auth->get_valid_auth({form => {cea_user => "test"}, cookies => {%$cookie_good}}) }
107 Auth::reset();
108 ok(combined_good(), "Got good auth") || do {
109 my $e = $@;
110 use CGI::Ex::Dump qw(debug);
111 debug $e;
112 die;
113 };
114 ok(! $Auth::printed, "Printed was not set");
115 ok($Auth::set_cookie, "Set_cookie was called");
116 ok(! $Auth::deleted_cookie, "deleted_cookie was not called");
117
118 sub combined_bad { Auth->get_valid_auth({form => {cea_user => "test2"}, cookies => {%$cookie_good}}) }
119 Auth::reset();
120 ok(! combined_bad(), "Got bad auth");
121 ok($Auth::printed, "Printed was set");
122 ok(! $Auth::set_cookie, "Set_cookie was not called");
123 ok($Auth::deleted_cookie, "deleted_cookie was called");
124 is($Auth::failed_login_user, 'test2', 'correct user on failed passed information');
125
126 sub combined_bad2 { Auth->get_valid_auth({form => {cea_user => "test"}, cookies => {%$cookie_bad}}) }
127 Auth::reset();
128 ok(! combined_bad2(), "Got bad auth");
129 ok($Auth::printed, "Printed was set");
130 ok(! $Auth::set_cookie, "Set_cookie was not called");
131 ok($Auth::deleted_cookie, "deleted_cookie was called");
132 is($Auth::failed_login_user, 'test', 'correct user on failed passed information');
133
134 sub combined_bad3 { Auth->get_valid_auth({form => {cea_user => "test2/123"}, cookies => {%$cookie_good}}) }
135 Auth::reset();
136 ok(! combined_bad3(), "Got bad auth");
137 ok($Auth::printed, "Printed was set");
138 ok(! $Auth::set_cookie, "Set_cookie was not called");
139 ok($Auth::deleted_cookie, "deleted_cookie was called");
140 is($Auth::failed_login_user, 'test2', 'correct user on failed passed information');
141
142 ###----------------------------------------------------------------###
143
144 Auth::reset();
145 Auth->get_valid_auth({form => {%$form_good}, cookies => {}});
146 ok($Auth::set_cookie, "Set_cookie called");
147 ok($Auth::cookie->{'expires'}, "Cookie had expires");
148
149 Auth::reset();
150 Auth->get_valid_auth({form => {%$form_good}, cookies => {}, use_session_cookie => 0});
151 ok($Auth::set_cookie, "Set_cookie called");
152 ok($Auth::cookie->{'expires'}, "Cookie had expires");
153
154 Auth::reset();
155 Auth->get_valid_auth({form => {%$form_good}, cookies => {}, use_session_cookie => 1});
156 ok($Auth::set_cookie, "Set_cookie called");
157 ok(! $Auth::cookie->{'expires'}, "Session cookie");
158
159 Auth::reset();
160 Auth->get_valid_auth({form => {%$form_good}, cookies => {}, use_plaintext => 1});
161 ok($Auth::set_cookie, "Set_cookie called");
162 ok(! $Auth::cookie->{'expires'}, "Session cookie");
163
164 Auth::reset();
165 Auth->get_valid_auth({form => {%$form_good}, cookies => {}, use_plaintext => 1, use_session_cookie => 0});
166 ok($Auth::set_cookie, "Set_cookie called");
167 ok($Auth::cookie->{'expires'}, "Cookie had expires");
168
169 Auth::reset();
170 Auth->get_valid_auth({form => {%$form_good}, cookies => {}, use_plaintext => 1, use_session_cookie => 1});
171 ok($Auth::set_cookie, "Set_cookie called");
172 ok(! $Auth::cookie->{'expires'}, "Session cookie");
173
174
175 ###----------------------------------------------------------------###
176
177 my $auth = Aut2->get_valid_auth({form => {%$form_good3}});
178 my $data = $auth->last_auth_data;
179 ok($auth && $data, "Aut2 worked again");
180 ok($data->{'foobar'} eq 'baz', 'And it contained the correct value');
181
182 SKIP: {
183 skip("Crypt::Blowfish not found", 4) if ! eval { require Crypt::Blowfish };
184
185 {
186 package Aut3;
187 use base qw(Auth);
188 sub use_blowfish { "This_is_my_key" }
189 sub use_base64 { 0 }
190 sub use_plaintext { 1 }
191 }
192
193 my $token2 = Aut3->new->generate_token({user => 'test', real_pass => '123qwe'});
194 my $form_good4 = { cea_user => $token2 };
195
196 sub form_good4 { Aut3->get_valid_auth({form => {%$form_good4}, cookies => {} }) }
197
198 Auth::reset();
199 ok(form_good4(), "Got good auth");
200 ok(! $Auth::printed, "Printed was not set");
201 ok($Auth::set_cookie, "Set_cookie called");
202 ok(! $Auth::deleted_cookie, "deleted_cookie was not called");
203 };
This page took 0.040376 seconds and 4 git commands to generate.