File: 64-Lemonldap-NG-Handler-PSGI-DevOps-with-param.t

package info (click to toggle)
lemonldap-ng 2.21.2%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 28,024 kB
  • sloc: perl: 77,414; javascript: 25,284; xml: 6,473; makefile: 1,303; sh: 453; sql: 159; python: 53; php: 26
file content (160 lines) | stat: -rw-r--r-- 4,405 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
use Test::More;
use JSON;
use MIME::Base64;
use LWP::UserAgent;

BEGIN {
    require 't/test-psgi-lib.pm';
}

### vhostOptions are overridden by fastcgi_param
init(
    'Lemonldap::NG::Handler::Server',
    {
        #logLevel     => 'debug',
        vhostOptions => {
            'test3.example.com' => {
                vhostHttps          => 0,
                vhostPort           => 80,
                vhostDevOpsRulesUrl =>
                  'http://donotuse.example.com/myfile.json',
            },
        },
    }
);

my $res;

# Unauthorized queries
ok(
    $res = $client->_get(
        '/',                 undef,
        'test3.example.com', undef,
        VHOSTTYPE => 'DevOps',
        RULES_URL => 'http://devops.example.com/file.json'
    ),
    'Unauthorized query'
);
ok( $res->[0] == 302, 'Code is 302' ) or explain( $res->[0], 302 );
${ $res->[1] }[1] =~ m#http://auth\.example\.com/\?url=(.+?)%#;
ok( decode_base64 $1 eq 'http://test3.example.com/', 'Redirect URL found' )
  or explain( decode_base64 $1, 'http://test3.example.com/' );
count(3);

Time::Fake->offset("+700s");

ok(
    $res = $client->_get(
        '/',                 undef,
        'test3.example.com', undef,
        HTTPS_REDIRECT => 'on',
        PORT_REDIRECT  => 8443,
        VHOSTTYPE      => 'DevOps',
        RULES_URL      => 'http://devops.example.com/file.json'
    ),
    'Unauthorized query 2'
);
ok( $res->[0] == 302, 'Code is 302' ) or explain( $res->[0], 302 );
${ $res->[1] }[1] =~ m#http://auth\.example\.com/\?url=(.+?)%#;
ok( decode_base64 $1 eq 'https://test3.example.com:8443/',
    'Redirect URL found' )
  or explain( decode_base64 $1, 'https://test3.example.com:8443/' );
count(3);

# Authorized queries
ok(
    $res = $client->_get(
        '/',                 undef,
        'test3.example.com', "lemonldap=$sessionId",
        VHOSTTYPE => 'DevOps',
        RULES_URL => 'http://devops.example.com/file.json'
    ),
    'Authorized query'
);
ok( $res->[0] == 200, 'Code is 200' ) or explain( $res->[0], 200 );
my %headers = @{ $res->[1] };
ok( $headers{User} eq 'dwho', "'User' => 'dwho'" )
  or explain( \%headers, 'dwho' );
ok( $headers{Name} eq '', "'Name' => ''" ) or explain( \%headers, 'No Name' );
ok( $headers{Mail} eq '', "'Mail' => ''" ) or explain( \%headers, 'No Mail' );
ok( keys %headers == 7,   "Seven headers sent" )
  or explain( \%headers, 'Seven headers' );
count(6);

ok(
    $res = $client->_get(
        '/testyes',          undef,
        'test3.example.com', "lemonldap=$sessionId",
        VHOSTTYPE => 'DevOps',
        RULES_URL => 'http://devops.example.com/file.json'
    ),
    'Authorized query'
);
ok( $res->[0] == 200, 'Code is 200' ) or explain( $res->[0], 200 );
count(2);

Time::Fake->offset("+100s");

# Denied queries
ok(
    $res = $client->_get(
        '/deny',             undef,
        'test3.example.com', "lemonldap=$sessionId",
        VHOSTTYPE => 'DevOps',
        RULES_URL => 'http://devops.example.com/file.json'
    ),
    'Denied query'
);
ok( $res->[0] == 403, 'Code is 403' ) or explain( $res->[0], 403 );
count(2);

Time::Fake->offset("+600s");

ok(
    $res = $client->_get(
        '/testno',           undef,
        'test3.example.com', "lemonldap=$sessionId",
        VHOSTTYPE => 'DevOps',
        RULES_URL => 'http://devops.example.com/file.json'
    ),
    'Denied query'
);
ok( $res->[0] == 403, 'Code is 403' ) or explain( $res->[0], 403 );
count(2);

done_testing( count() );

clean();

# Redefine LWP methods for tests
no warnings 'redefine';

sub LWP::UserAgent::request {
    my ( $self, $req ) = @_;
    ok( $req->header('host') eq 'devops.example.com', 'Host header found' )
      or explain( $req->headers(), 'devops.example.com' );
    ok( $req->as_string() =~ m#http://devops.example.com/file.json#,
        'Rules file URL found' )
      or
      explain( $req->as_string(), 'GET http://devops.example.com/file.json' );
    count(2);
    my $httpResp;
    my $s = '{
  "rules": {
    "^/deny": "deny",
    "^/testno": "$uid ne qq{dwho}",
    "^/testyes": "$uid eq qq{dwho}",
    "default": "accept"
  },
  "headers": {
    "User": "$uid",
    "Mail": "$mail",
    "Name": "$cn"
  }
}';
    $httpResp = HTTP::Response->new( 200, 'OK' );
    $httpResp->header( 'Content-Type',   'application/json' );
    $httpResp->header( 'Content-Length', length($s) );
    $httpResp->content($s);
    return $httpResp;
}