Make error more useful
[catagits/Catalyst-Plugin-Authentication.git] / t / 05_password.t
1 use strict;
2 use warnings;
3
4 use Test::More tests => 11;
5 use Test::Exception;
6 use Test::MockObject;
7
8 # 1,2
9 my $m; BEGIN { use_ok($m = "Catalyst::Authentication::Credential::Password") }
10 can_ok($m, "authenticate");
11
12 my $app = Test::MockObject->new;
13 my $realm = Test::MockObject->new;
14 my $user = Test::MockObject->new;
15 our ($user_get_password_field_name, $user_password );
16 $user->mock('get' => sub { $user_get_password_field_name = $_[1]; return $user_password });
17
18 # 3-6 # Test clear passwords if you mess up the password_field
19 {
20     local $user_password = undef;        # The user returns an undef password, 
21     local $user_get_password_field_name; # as there is no field named 'mistyped'
22     my $config = { password_type => 'clear', password_field => 'mistyped' };
23     my $i; lives_ok { $i = $m->new($config, $app, $realm) } 'Construct instance';
24     ok($i, 'Have instance');
25     my $r = $i->check_password($user, { username => 'someuser', password => 'password' });
26     is($user_get_password_field_name, 'mistyped', 
27         '(Incorrect) field name from config correctly passed to user');
28     ok(! $r, 'Authentication unsuccessful' );
29 }
30
31 # 7-11 # Test clear passwords working, and not working
32 {
33     local $user_password = 'mypassword';         
34     local $user_get_password_field_name;
35     my $config = { password_type => 'clear', password_field => 'the_password_field' };
36     my $i; lives_ok { $i = $m->new($config, $app, $realm) } 'Construct instance';
37     ok($i, 'Have instance');
38     my $r = $i->check_password($user, { username => 'someuser', the_password_field => 'mypassword' });
39     is($user_get_password_field_name, 'the_password_field', 
40         'Correct field name from config correctly passed to user');
41     ok( $r, 'Authentication successful with correct password' );
42     $r = $i->check_password($user, { username => 'someuser', the_password_field => 'adifferentpassword' });
43     ok( ! $r, 'Authentication ussuccessful with incorrect password' );
44 }