File: 05_authz_runmodes.t

package info (click to toggle)
libcgi-application-plugin-authorization-perl 0.07%2B~cs0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 356 kB
  • sloc: perl: 1,237; makefile: 17
file content (250 lines) | stat: -rw-r--r-- 7,998 bytes parent folder | download | duplicates (3)
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#!/usr/bin/perl
use Test::More tests => 15;
use Test::Exception;
use Scalar::Util;

use strict;
use warnings;
use lib './t';

$ENV{CGI_APP_RETURN_ONLY} = 1;

###############################################################################
# Define a test application package.
#
# Using this test package still requires that:
# - we call 'start_mode()' explicitly to set the initial run-mode
# - we call 'authz->authz_runmodes()' to set the authorization rules
###############################################################################
{
    package TestApp;
    use base qw(CGI::Application);
    use CGI::Application::Plugin::Authorization;

    sub setup {
        my $self = shift;
        $self->run_modes( [qw(
            test_regexp
            test_coderef
            test_string
            test_all
            )] );
        $self->authz->config(
            DRIVER => [ 'Generic',
                sub {
                    my ($username, $group) = @_;
                    return ($group eq 'ok');
                },
            ] );
    }
    sub test_regexp     { return 'test_regexp' };
    sub test_coderef    { return 'test_coderef' };
    sub test_string     { return 'test_string' };
    sub test_all        { return 'test_all' };
}

###############################################################################
# Make sure that "authz_runmodes()" sets up the runmodes consistently,
# regardless of whether we pass in a list of entries or a list of list-refs.
#
# If this works, we only need to concern ourselves with testing the interface
# in one fashion from here on out.
authz_runmodes_sets_up_consistently: {
    my $app = TestApp->new();
    $app->authz->authz_runmodes(
        ['listref'          => 'for'],
        ['compatibility'    => 'with'],
        ['0.06'             => 'interface'],
        );
    $app->authz->authz_runmodes(
        'newer'     => 'interface',
        'allows'    => 'for',
        'listrefs'  => 'to',
        'be'        => 'absent',
        );
    my @authz  = $app->authz->authz_runmodes();
    my @expect = (
        ['listref'          => 'for'],
        ['compatibility'    => 'with'],
        ['0.06'             => 'interface'],
        ['newer'            => 'interface'],
        ['allows'           => 'for'],
        ['listrefs'         => 'to'],
        ['be'               => 'absent'],
        );
    is_deeply \@authz, \@expect, 'authz_runmodes() is consistent';
}

###############################################################################
# Authz runmode definition: string
authz_runmode_string: {
    test_allow: {
        my $app = TestApp->new();
        $app->start_mode( 'test_string' );
        $app->authz->authz_runmodes(
            'test_string'   => 'ok',
            ':all'          => 'fail',
            );
        my $res = $app->run();
        like $res, qr/test_string/, 'runmode definition: string (allow)';
    }
    test_forbid: {
        my $app = TestApp->new();
        $app->start_mode( 'test_string' );
        $app->authz->authz_runmodes(
            'test_string'   => 'fail',
            ':all'          => 'ok',
            );
        my $res = $app->run();
        like $res, qr/Forbidden/, 'runmode definition: string (forbid)';
    }
}

###############################################################################
# Authz runmode definition: regexp
authz_runmode_regexp: {
    test_allow: {
        my $app = TestApp->new();
        $app->start_mode( 'test_regexp' );
        $app->authz->authz_runmodes(
            qr/regexp/  => 'ok',
            ':all'      => 'fail',
            );
        my $res = $app->run();
        like $res, qr/test_regexp/, 'runmode definition: regexp (allow)';
    }
    test_allow: {
        my $app = TestApp->new();
        $app->start_mode( 'test_regexp' );
        $app->authz->authz_runmodes(
            qr/regexp/  => 'fail',
            ':all'      => 'ok',
            );
        my $res = $app->run();
        like $res, qr/Forbidden/, 'runmode definition: regexp (forbid)';
    }
}

###############################################################################
# Authz runmode definition: coderef
authz_runmode_coderef: {
    test_allow: {
        my $app = TestApp->new();
        $app->start_mode( 'test_coderef' );
        $app->authz->authz_runmodes(
            sub { $_[0] =~ /coderef/ }, 'ok',
            ':all'      => 'fail',
            );
        my $res = $app->run();
        like $res, qr/test_coderef/, 'runmode definition: coderef (allow)';
    }
    test_forbid: {
        my $app = TestApp->new();
        $app->start_mode( 'test_coderef' );
        $app->authz->authz_runmodes(
            sub { $_[0] =~ /coderef/ }, 'fail',
            ':all'      => 'ok',
            );
        my $res = $app->run();
        like $res, qr/Forbidden/, 'runmode definition: coderef (forbid)';
    }
}

###############################################################################
# Authz runmode definition: :all
authz_runmode_all: {
    test_allow: {
        my $app = TestApp->new();
        $app->start_mode( 'test_all' );
        $app->authz->authz_runmodes(
            ':all'  => 'ok',
            );
        my $res = $app->run();
        like $res, qr/test_all/, 'runmode definition: all (allow)';
    }
    test_forbid: {
        my $app = TestApp->new();
        $app->start_mode( 'test_all' );
        $app->authz->authz_runmodes(
            ':all' => 'fail',
            );
        my $res = $app->run();
        like $res, qr/Forbidden/, 'runmode definition: all (forbid)';
    }
}

###############################################################################
# Authz rules: group
authz_rules_group: {
    test_allow: {
        my $app = TestApp->new();
        $app->start_mode( 'test_string' );
        $app->authz->authz_runmodes(
            'test_string'   => 'ok',
            ':all'          => 'fail',
            );
        my $res = $app->run();
        like $res, qr/test_string/, 'authz rule: group (allow)';
    }
    test_forbid: {
        my $app = TestApp->new();
        $app->start_mode( 'test_string' );
        $app->authz->authz_runmodes(
            'test_string'   => 'fail',
            ':all'          => 'ok',
            );
        my $res = $app->run();
        like $res, qr/Forbidden/, 'authz rule: group (forbid)';
    }
}

###############################################################################
# Authz rules: list-ref of groups
authz_rules_group_list: {
    test_allow: {
        my $app = TestApp->new();
        $app->start_mode( 'test_string' );
        $app->authz->authz_runmodes(
            'test_string'   => [qw(any of these is ok)],
            ':all'          => 'fail',
            );
        my $res = $app->run();
        like $res, qr/test_string/, 'authz rule: list-ref of groups (allow)';
    }
    test_forbid: {
        my $app = TestApp->new();
        $app->start_mode( 'test_string' );
        $app->authz->authz_runmodes(
            'test_string'   => [qw(otherwise we just fail)],
            ':all'          => 'ok',
            );
        my $res = $app->run();
        like $res, qr/Forbidden/, 'authz rule: list-ref of groups (forbid)';
    }

}

###############################################################################
# Authz rules: coderef
authz_rules_coderef: {
    test_allow: {
        my $app = TestApp->new();
        $app->start_mode( 'test_string' );
        $app->authz->authz_runmodes(
            'test_string'   => sub { 1 },
            ':all'          => 'fail',
            );
        my $res = $app->run();
        like $res, qr/test_string/, 'authz rule: coderef (allow)';
    }
    test_forbid: {
        my $app = TestApp->new();
        $app->start_mode( 'test_string' );
        $app->authz->authz_runmodes(
            'test_string'   => sub { 0 },
            ':all'          => 'ok',
            );
        my $res = $app->run();
        like $res, qr/Forbidden/, 'authz rule: coderef (forbid)';
    }
}