updated copyright and prep for release
[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 Class::MOP;
7 use Class::MOP::Class;
8 use Moose::Object;
9
10 # 1,2
11 my $m; BEGIN { use_ok($m = "Catalyst::Authentication::Credential::Password") }
12 can_ok($m, "authenticate");
13
14 my $app_meta = Class::MOP::Class->create_anon_class( superclasses => ['Moose::Object'] );
15 my $realm_meta = Class::MOP::Class->create_anon_class( superclasses => ['Moose::Object'] );
16 my $user_meta = Class::MOP::Class->create_anon_class( superclasses => ['Moose::Object'] );
17 our ($user_get_password_field_name, $user_password );
18 $user_meta->add_method('get' => sub { $user_get_password_field_name = $_[1]; return $user_password });
19
20 # 3-6 # Test clear passwords if you mess up the password_field
21 {
22     local $user_password = undef;        # The user returns an undef password,
23     local $user_get_password_field_name; # as there is no field named 'mistyped'
24     my $config = { password_type => 'clear', password_field => 'mistyped' };
25     my $i; lives_ok { $i = $m->new($config, $app_meta->name->new, $realm_meta->name->new) } 'Construct instance';
26     ok($i, 'Have instance');
27     my $r = $i->check_password($user_meta->name->new, { username => 'someuser', password => 'password' });
28     is($user_get_password_field_name, 'mistyped',
29         '(Incorrect) field name from config correctly passed to user');
30     ok(! $r, 'Authentication unsuccessful' );
31 }
32
33 # 7-11 # Test clear passwords working, and not working
34 {
35     local $user_password = 'mypassword';
36     local $user_get_password_field_name;
37     my $config = { password_type => 'clear', password_field => 'the_password_field' };
38     my $i; lives_ok { $i = $m->new($config, $app_meta->name->new, $realm_meta->name->new) } 'Construct instance';
39     ok($i, 'Have instance');
40     my $r = $i->check_password($user_meta->name->new, { username => 'someuser', the_password_field => 'mypassword' });
41     is($user_get_password_field_name, 'the_password_field',
42         'Correct field name from config correctly passed to user');
43     ok( $r, 'Authentication successful with correct password' );
44     $r = $i->check_password($user_meta->name->new, { username => 'someuser', the_password_field => 'adifferentpassword' });
45     ok( ! $r, 'Authentication ussuccessful with incorrect password' );
46 }