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