Fix RT#82944
[catagits/Catalyst-Authentication-Store-DBIx-Class.git] / t / 05-auth-roles-relationship.t
1 #!perl
2
3 use strict;
4 use warnings;
5 use DBI;
6 use File::Path;
7 use FindBin;
8 use Test::More;
9 use lib "$FindBin::Bin/lib";
10
11 BEGIN {
12     eval { require DBD::SQLite }
13         or plan skip_all =>
14         "DBD::SQLite is required for this test";
15
16     eval { require DBIx::Class }
17         or plan skip_all =>
18         "DBIx::Class is required for this test";
19
20     eval { require Catalyst::Plugin::Authorization::Roles }
21         or plan skip_all =>
22         "Catalyst::Plugin::Authorization::Roles is required for this test";
23
24     plan tests => 8;
25
26     use TestApp;
27     TestApp->config( {
28         name => 'TestApp',
29         authentication => {
30             default_realm => "users",
31             realms => {
32                 users => {
33                     credential => {
34                         'class' => "Password",
35                         'password_field' => 'password',
36                         'password_type' => 'clear'
37                     },
38                     store => {
39                         'class' => 'DBIx::Class',
40                         'user_model' => 'TestApp::User',
41                         'role_relation' => 'roles',
42                         'role_field' => 'role'
43                     },
44                 },
45             },
46         },
47     } );
48
49     TestApp->setup(
50         qw/Authentication
51            Authorization::Roles
52            /
53     );
54 }
55
56 use Catalyst::Test 'TestApp';
57
58 # test user's admin access
59 {
60     ok( my $res = request('http://localhost/user_login?username=jayk&password=letmein&detach=is_admin'), 'request ok' );
61     is( $res->content, 'ok', 'user is an admin' );
62 }
63
64 # test unauthorized user's admin access
65 {
66     ok( my $res = request('http://localhost/user_login?username=nuffin&password=much&detach=is_admin'), 'request ok' );
67     is( $res->content, 'failed', 'user is not an admin' );
68 }
69
70 # test multiple auth roles
71 {
72     ok( my $res = request('http://localhost/user_login?username=jayk&password=letmein&detach=is_admin_user'), 'request ok' );
73     is( $res->content, 'ok', 'user is an admin and a user' );
74 }
75
76 # test multiple unauth roles
77 {
78     ok( my $res = request('http://localhost/user_login?username=nuffin&password=much&detach=is_admin_user'), 'request ok' );
79     is( $res->content, 'failed', 'user is not an admin and a user' );
80 }