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