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