]> Dogcows Code - chaz/p5-CGI-Ex/blob - t/8_auth_00_base.t
CGI::Ex 2.02
[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 => 33;
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);
19
20 sub login_print { $printed = 1 }
21 sub set_cookie { $set_cookie = 1 }
22 sub delete_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
30 {
31 package Aut2;
32 use base qw(Auth);
33 use vars qw($crypt);
34 BEGIN { $crypt = crypt('123qwe', 'SS') };
35 sub use_crypt { 1 }
36 sub get_pass_by_user { $crypt }
37 }
38
39 my $token = Auth->new->generate_token({user => 'test', real_pass => '123qwe', use_base64 => 1});
40
41 my $form_bad = { cea_user => 'test', cea_pass => '123qw' };
42 my $form_good = { cea_user => 'test', cea_pass => '123qwe' };
43 my $form_good2 = { cea_user => $token };
44 my $form_good3 = { cea_user => 'test/123qwe' };
45 my $cookie_bad = { cea_user => 'test/123qw' };
46 my $cookie_good = { cea_user => 'test/123qwe' };
47 my $cookie_good2 = { cea_user => $token };
48
49 sub form_good { Auth->get_valid_auth({form => {%$form_good}, cookies => {} }) }
50 sub form_good2 { Auth->get_valid_auth({form => {%$form_good2}, cookies => {} }) }
51 sub form_good3 { Aut2->get_valid_auth({form => {%$form_good3}, cookies => {} }) }
52 sub form_bad { Auth->get_valid_auth({form => {%$form_bad}, cookies => {} }) }
53 sub cookie_good { Auth->get_valid_auth({form => {}, cookies => {%$cookie_good} }) }
54 sub cookie_good2 { Auth->get_valid_auth({form => {}, cookies => {%$cookie_good2}}) }
55 sub cookie_bad { Auth->get_valid_auth({form => {}, cookies => {%$cookie_bad} }) }
56
57 $Auth::printed = $Auth::set_cookie = $Auth::deleted_cookie = 0;
58 ok(form_good(), "Got good auth");
59 ok(! $Auth::printed, "Printed was not set");
60 ok($Auth::set_cookie, "Set_cookie called");
61 ok(! $Auth::deleted_cookie, "deleted_cookie was not called");
62
63 $Auth::printed = $Auth::set_cookie = $Auth::deleted_cookie = 0;
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 $Auth::printed = $Auth::set_cookie = $Auth::deleted_cookie = 0;
70 ok(form_good3(), "Got good auth");
71 ok(! $Auth::printed, "Printed was not set");
72 ok($Auth::set_cookie, "Set_cookie called");
73 ok(! $Auth::deleted_cookie, "deleted_cookie was not called");
74
75 $Auth::printed = $Auth::set_cookie = $Auth::deleted_cookie = 0;
76 ok(! form_bad(), "Got bad auth");
77 ok($Auth::printed, "Printed was set");
78 ok(! $Auth::set_cookie, "set_cookie called");
79 ok(! $Auth::deleted_cookie, "deleted_cookie was not called");
80
81 $Auth::printed = $Auth::set_cookie = $Auth::deleted_cookie = 0;
82 ok(cookie_good(), "Got good auth");
83 ok(! $Auth::printed, "Printed was not set");
84 ok($Auth::set_cookie, "Set_cookie called");
85 ok(! $Auth::deleted_cookie, "deleted_cookie was not called");
86
87 $Auth::printed = $Auth::set_cookie = $Auth::deleted_cookie = 0;
88 ok(cookie_good2(), "Got good auth");
89 ok(! $Auth::printed, "Printed was not set");
90 ok($Auth::set_cookie, "Set_cookie called");
91 ok(! $Auth::deleted_cookie, "deleted_cookie was not called");
92
93 $Auth::printed = $Auth::set_cookie = $Auth::deleted_cookie = 0;
94 ok(! cookie_bad(), "Got bad auth");
95 ok($Auth::printed, "Printed was set");
96 ok(! $Auth::set_cookie, "Set_cookie was not called");
97 ok($Auth::deleted_cookie, "deleted_cookie was not called");
98
99
100 SKIP: {
101 skip("Crypt::Blowfish not found", 4) if ! eval { require Crypt::Blowfish };
102
103 {
104 package Aut3;
105 use base qw(Auth);
106 sub use_blowfish { "This_is_my_key" }
107 sub use_base64 { 0 }
108 sub use_plaintext { 1 }
109 }
110
111 my $token2 = Aut3->new->generate_token({user => 'test', real_pass => '123qwe'});
112 my $form_good4 = { cea_user => $token2 };
113
114 sub form_good4 { Aut3->get_valid_auth({form => {%$form_good4}, cookies => {} }) }
115
116 $Auth::printed = $Auth::set_cookie = $Auth::deleted_cookie = 0;
117 ok(form_good4(), "Got good auth");
118 ok(! $Auth::printed, "Printed was not set");
119 ok($Auth::set_cookie, "Set_cookie called");
120 ok(! $Auth::deleted_cookie, "deleted_cookie was not called");
121 };
This page took 0.039704 seconds and 4 git commands to generate.