Changes to allow for dropping of the 'realms' config hash and instead
[catagits/Catalyst-Plugin-Authentication.git] / t / lib / AuthTestApp.pm
CommitLineData
a3bf437a 1package AuthTestApp;
2use Catalyst qw/
3 Authentication
4 Authentication::Store::Minimal
5 Authentication::Credential::Password
6/;
7
8use Test::More;
9use Test::Exception;
10
11use Digest::MD5 qw/md5/;
12use Digest::SHA1 qw/sha1_base64/;
13
14our $users;
15
bf4d93a4 16sub number_of_elements { return scalar @_ }
17
a3bf437a 18sub moose : Local {
19 my ( $self, $c ) = @_;
20
bf4d93a4 21 is(number_of_elements($c->user), 1, "Array undef");
22 is($c->user, undef, "no user, returns undef");
a3bf437a 23 ok(!$c->user, "no user");
24 ok($c->login( "foo", "s3cr3t" ), "can login with clear");
25 is( $c->user, $users->{foo}, "user object is in proper place");
26
27 ok( !$c->user->roles, "no roles for foo" );
28 my @new = qw/foo bar gorch/;
29 $c->user->roles( @new );
30 is_deeply( [ $c->user->roles ], \@new, "roles set as array");
31
32 $c->logout;
33 ok(!$c->user, "no more user, after logout");
34
35 ok($c->login( "bar", "s3cr3t" ), "can login with crypted");
36 is( $c->user, $users->{bar}, "user object is in proper place");
37 $c->logout;
38
39 ok($c->login("gorch", "s3cr3t"), "can login with hashed");
40 is( $c->user, $users->{gorch}, "user object is in proper place");
41 $c->logout;
42
43 ok($c->login("shabaz", "s3cr3t"), "can login with base64 hashed");
44 is( $c->user, $users->{shabaz}, "user object is in proper place");
45 $c->logout;
46
47 ok($c->login("sadeek", "s3cr3t"), "can login with padded base64 hashed");
48 is( $c->user, $users->{sadeek}, "user object is in proper place");
49 $c->logout;
50
51 ok(!$c->login( "bar", "bad pass" ), "can't login with bad password");
52 ok(!$c->user, "no user");
53
54 throws_ok { $c->login( "baz", "foo" ) } qr/support.*mechanism/, "can't login without any supported mech";
55
56 $c->res->body( "ok" );
57}
58
7c4d44af 59__PACKAGE__->config->{'Plugin::Authentication'}{users} = $users = {
a3bf437a 60 foo => {
61 password => "s3cr3t",
62 },
63 bar => {
64 crypted_password => crypt("s3cr3t", "x8"),
65 },
66 gorch => {
67 hashed_password => md5("s3cr3t"),
68 hash_algorithm => "MD5",
69 },
70 shabaz => {
71 hashed_password => sha1_base64("s3cr3t"),
72 hash_algorithm => "SHA-1"
73 },
74 sadeek => {
75 hashed_password => sha1_base64("s3cr3t").'=',
76 hash_algorithm => "SHA-1"
77 },
78 baz => {},
79};
80
81__PACKAGE__->setup;