Teach Credential::Password about base64 encoded passwords. Also include a
[catagits/Catalyst-Plugin-Authentication.git] / t / live_app.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More 'no_plan';
7
8 {
9         package AuthTestApp;
10         use Catalyst qw/
11                 Authentication
12                 Authentication::Store::Minimal
13                 Authentication::Credential::Password
14         /;
15
16         use Test::More;
17         use Test::Exception;
18
19         use Digest::MD5 qw/md5/;
20     use Digest::SHA1 qw/sha1_base64/;
21
22         our $users;
23
24         sub moose : Local {
25                 my ( $self, $c ) = @_;
26
27                 ok(!$c->user, "no user");
28                 ok($c->login( "foo", "s3cr3t" ), "can login with clear");
29                 is( $c->user, $users->{foo}, "user object is in proper place");
30
31                 ok( !$c->user->roles, "no roles for foo" );
32                 my @new = qw/foo bar gorch/;
33                 $c->user->roles( @new );
34                 is_deeply( [ $c->user->roles ], \@new, "roles set as array");
35
36                 $c->logout;
37                 ok(!$c->user, "no more user, after logout");
38
39
40                 ok($c->login( "bar", "s3cr3t" ), "can login with crypted");
41                 is( $c->user, $users->{bar}, "user object is in proper place");
42                 $c->logout;
43
44                 ok($c->login("gorch", "s3cr3t"), "can login with hashed");
45                 is( $c->user, $users->{gorch}, "user object is in proper place");
46                 $c->logout;
47
48                 ok($c->login("shabaz", "s3cr3t"), "can login with base64 hashed");
49                 is( $c->user, $users->{shabaz}, "user object is in proper place");
50                 $c->logout;
51
52                 ok($c->login("sadeek", "s3cr3t"), "can login with padded base64 hashed");
53                 is( $c->user, $users->{sadeek}, "user object is in proper place");
54                 $c->logout;
55
56                 ok(!$c->login( "bar", "bad pass" ), "can't login with bad password");
57                 ok(!$c->user, "no user");
58
59                 throws_ok { $c->login( "baz", "foo" ) } qr/support.*mechanism/, "can't login without any supported mech";
60
61                 $c->res->body( "ok" );
62         }
63
64         __PACKAGE__->config->{authentication}{users} = $users = {
65                 foo => {
66                         password => "s3cr3t",
67                 },
68                 bar => {
69                         crypted_password => crypt("s3cr3t", "x8"),
70                 },
71                 gorch => {
72                         hashed_password => md5("s3cr3t"),
73                         hash_algorithm => "MD5",
74                 },
75         shabaz => {
76             hashed_password => sha1_base64("s3cr3t"),
77             hash_algorithm => "SHA-1"
78         },
79         sadeek => {
80             hashed_password => sha1_base64("s3cr3t").'=',
81             hash_algorithm => "SHA-1"
82         },
83                 baz => {},
84         };
85
86         __PACKAGE__->setup;
87 }
88
89 use Catalyst::Test qw/AuthTestApp/;
90
91 ok( get("/moose"), "get ok");