File: permissions.t

package info (click to toggle)
libmojomojo-perl 1.01%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 4,272 kB
  • ctags: 879
  • sloc: perl: 14,055; sh: 145; xml: 120; ruby: 6; makefile: 2
file content (148 lines) | stat: -rw-r--r-- 4,215 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More tests => 32;

BEGIN {
    $ENV{CATALYST_CONFIG} = 't/var/mojomojo.yml';
};

use_ok( 'Catalyst::Test', 'MojoMojo' );
use_ok( 'Test::WWW::Mechanize::Catalyst', 'MojoMojo' );
my $m = Test::WWW::Mechanize::Catalyst->new;

my(undef, $c) = ctx_request('/');

# do not use the cache
$c->cache->set( cache_permission_data => 0 );

my $anon_login = $c->pref('anonymous_user');
my $anonymous = $c->model('DBIC::Person') ->search( {login => $anon_login} )->first;

# Test check_permissions on page ------------------------------------
# Anonymous on '/'
check_perms('/', $anonymous, [qw/create view edit/], [ 'attachment', 'delete' ]);

# Anonymous on subpage /foo/bar
check_perms('/foo/bar', $anonymous, [qw/create view edit/], [ 'attachment', 'delete' ]);


my $admin = $c->model('DBIC::Person') ->search( {login => 'admin'} )->first;

# Admin on '/'
check_perms('/', $admin, [qw/create attachment view edit delete/], []);

# Add person 'test' (role User)
use lib qw(t/lib);
use MojoMojoTestSchema;
my $schema = MojoMojoTestSchema->get_schema;
ok(my $usertest = $schema->resultset('Person')->create(
        {
         active => 1,
         login => 'test',
         email => 'test@test.org',
         name => 'Gaston Lagaffe',
         pass => 'test',
        }
    ), "User test is created");

# Person test is a User
ok($schema->resultset('RoleMember')->create(
        {
         role   => 2,
         person => $usertest->id,
         admin  => 0,
        }
    ), "test is a User");


# Create page /foo and /foo/bar
my $person = $schema->resultset('Person')->find( 1 );
my ($child_path_pages, $child_proto_pages) = $schema->resultset('Page')->path_pages('/foo/bar');
ok($schema->resultset('Page')->create_path_pages(
    path_pages => $child_path_pages,
    proto_pages => $child_proto_pages,
    creator => $person->id,
), "Create page /foo and /foo/bar");



  # # User have no permission on /foo only
   ok( $schema->resultset('PathPermissions')->create(
           {
            path                => '/foo',
            role                => 2,
            apply_to_subpages   => 'no',
            create_allowed      => 'no',
            delete_allowed      => 'no',
            edit_allowed        => 'no',
            view_allowed        => 'no',
            attachment_allowed  => 'no',
           }
       ), "User test have no permission on '/foo'");

   ok( $schema->resultset('PathPermissions')->create(
           {
            path                => '/foo',
            role                => 2,
            apply_to_subpages   => 'yes',
            create_allowed      => 'yes',
            delete_allowed      => 'yes',
            edit_allowed        => 'yes',
            view_allowed        => 'yes',
            attachment_allowed  => 'yes',
           }
       ), "User test have all permissions on subpages '/foo'");

# User test on '/foo'
check_perms('/foo', $usertest, [], [qw/create attachment view edit delete/]);

# User test on '/foo/bar'
check_perms('/foo/bar', $usertest, [qw/create attachment view edit delete/], []);


sub check_perms{
  my $path    = shift;
  my $user    = shift;
  my $allowed = shift;
  my $denied  = shift;

  my $username = $user->login;

  my $perms = $c->check_permissions( $path,  $user );

  foreach my $p (@$allowed){
    is($$perms{$p}, 1, "$username can $p on $path");
  }
  foreach my $p (@$denied){
    is($$perms{$p}, 0, "$username can not $p on $path");
  }
}



sub login{
   my $mech  = shift;
   my $login = shift;
   my $pass  = shift;

   $mech->post('/.login', {
                           login => $login,
                           pass  => $pass 
                          });
   ok $mech->success, "logging in as $login"
 }

END{
  # Delete user test
  $schema->resultset('Person')->search({ login => 'test'})->first->delete;
  $schema->resultset('PathPermissions')->search({ path => '/foo'})->delete;

  $schema->resultset('Page')->search({ name => 'bar'})->delete;
  $schema->resultset('Page')->search({ name => 'foo'})->delete;
  $schema->resultset('PageVersion')->search({ name => 'bar'})->delete;
  $schema->resultset('PageVersion')->search({ name => 'foo'})->delete;
}