bc703518ce5de54904e0cda3d22c38943b59ca8b
[catagits/Catalyst-Plugin-Authentication.git] / t / lib / AuthRealmTestApp.pm
1 package AuthRealmTestApp;
2 use warnings;
3 use strict;
4
5 use Catalyst qw/
6     Authentication
7     Authentication::Store::Minimal
8 /;
9
10 use Test::More;
11 use Test::Exception;
12
13 our $members = {
14     bob => {
15         password => "s00p3r"
16     },
17     william => {
18         password => "s3cr3t"
19     }
20 };
21
22 our $admins = {
23     joe => {
24         password => "31337"
25     }
26 };
27
28 sub moose : Local {
29         my ( $self, $c ) = @_;
30
31         ok(!$c->user, "no user");
32
33     while ( my ($user, $info) = each %$members ) {
34         
35         ok( 
36             $c->authenticate( 
37                 { username => $user, password => $info->{password} }, 
38                 'members' 
39             ), 
40             "user $user authentication" 
41         );
42
43         # check existing realms
44         ok( $c->user_in_realm('members'), "user in members realm");
45         ok(!$c->user_in_realm('admins'),  "user not in admins realm");
46
47         # check an invalid realm
48         ok(!$c->user_in_realm('foobar'), "user not in foobar realm");
49
50         # check if we've got the right user
51         is( $c->user, $info, "user object is in proper place");
52
53         $c->logout;
54
55             # sanity check
56         ok(!$c->user, "no more user after logout");
57
58     }
59
60     while ( my ($user, $info) = each %$admins ) {
61         
62         ok( 
63             $c->authenticate( 
64                 { username => $user, password => $info->{password} }, 
65                 'admins' 
66             ), 
67             "user $user authentication" 
68         );
69
70         # check existing realms
71         ok(!$c->user_in_realm('members'), "user not in members realm");
72         ok( $c->user_in_realm('admins'),  "user in admins realm");
73
74         # check an invalid realm
75         ok(!$c->user_in_realm('foobar'), "user not in foobar realm");
76
77         # check if we've got the right user
78         is( $c->user, $info, "user object is in proper place");
79
80         $c->logout;
81
82             # sanity check
83         ok(!$c->user, "no more user after logout");
84
85     }
86
87         $c->res->body( "ok" );
88 }
89
90 __PACKAGE__->config->{'Plugin::Authentication'} = {  
91     default_realm => 'members',
92     realms => {
93         members => {
94             credential => {
95                 class => 'Password',
96                 password_field => 'password',
97                 password_type => 'clear'
98             },
99             store => {
100                 class => 'Minimal',
101                 users => $members             
102             }
103         },
104         admins => {
105             credential => {
106                 class => 'Password',
107                 password_field => 'password',
108                 password_type => 'clear'
109             },
110             store => {
111                 class => 'Minimal',
112                 users => $admins               
113             }
114         }
115     }
116 };
117
118 __PACKAGE__->setup;