File Coverage

t/30-authenticate_Basic.t
Criterion Covered Total %
statement 138 141 97.8
branch n/a
condition n/a
subroutine 20 20 100.0
pod n/a
total 158 161 98.1


line stmt bran cond sub pod time code
1 1     1   4 use strict;
  1         1  
  1         30  
2 1     1   4 use warnings;
  1         0  
  1         23  
3              
4 1     1   563 use Test::More;
  1         14366  
  1         11  
5 1     1   843 use Plack::Test;
  1         1239  
  1         51  
6              
7             BEGIN {
8            
9 1     1   403 use Dancer2;
  1         300771  
  1         6  
10            
11 1     1   6 set session => undef; # explicit
12 1         24572 set plugins => {
13             'HTTP::Auth::Extensible' => {
14             realms => {
15             some_realm => {
16             scheme => "Basic",
17             provider => "Config",
18             users => [
19             { user => "dave",
20             pass => "beer",
21             name => "David Precious",
22             roles => [
23             'BeerDrinker',
24             'VodkaDrinker',
25             ],
26             },
27             ]
28             }
29             }
30             }
31             };
32 1         98 set logger => "file";
33             # set log => "core";
34 1         10726 set show_errors => 1;
35 1         96 set serializer => "YAML";
36            
37 1     1   62991 use Dancer2::Plugin::HTTP::Auth::Extensible;
  1         3  
  1         4  
38 1     1   54 no warnings 'uninitialized';
  1         2  
  1         224  
39              
40 1         16398 get '/' => sub { "Access does not need any authorization" };
  1         40845  
41            
42             get '/auth' => http_requires_authentication sub {
43 1         4 "Access granted for default realm"
44 1         4155 };
45              
46             get '/beer' => http_requires_role 'BeerDrinker' => sub {
47 1         7 "Enjoy your Beer!"
48 1         652 };
49              
50             get '/vodka' => http_requires_role 'VodkaDrinker' => sub {
51 1         5 "Enjoy your Vodka!"
52 1         803 };
53              
54             get '/martini' => http_requires_role 'MartiniDrinker' => sub {
55 0         0 "Enjoy your Martini!"
56 1         647 };
57              
58             get '/juice' => http_requires_role 'JuiceDrinker' => sub {
59 0         0 "Enjoy your Juice!"
60 1         687 };
61              
62             get '/drinker' => http_requires_role qr{Drinker$} => sub {
63 1         6 "Enjoy your bevarage!"
64 1         754 };
65              
66             get '/thirsty' => http_requires_any_role [ 'BeerDrinker', 'JuiceDrinker' ] => sub {
67 1         6 "Enjoy your drinks, one at a time!"
68 1         796 };
69              
70             get '/alcohol' => http_requires_all_roles [ 'BeerDrinker', 'VodkaDrinker' ] => sub {
71 1         5 "Enjoy your alcohol, but don't drink too much!"
72 1         721 };
73              
74             get '/cocktail' => http_requires_all_roles [ 'VodkaDrinker', 'MartiniDrinker' ] => sub {
75 0           "Stirred, not shaken!"
76 1         807 };
77              
78             } # BEGIN
79              
80 1         10 my $app = Dancer2->runner->psgi_app;
81              
82             {
83 1         4013 is (
  1         7  
84             ref $app,
85             'CODE',
86             'Got app'
87             );
88             };
89              
90             test_psgi $app, sub {
91 1     1   6412 my $cb = shift;
92 1         16 my $req = HTTP::Request->new( GET => '/');
93 1         3668 my $res = $cb->( $req );
94 1         7863 is (
95             $res->code,
96             200,
97             'Status 200: root resource accessible without login'
98             );
99 1         597 is (
100             $res->content,
101             qq|Access does not need any authorization|,
102             'Delivering: root resource accessible without login'
103             );
104 1         742 };
105              
106             test_psgi $app, sub {
107 1     1   95 my $cb = shift;
108 1         7 my $req = HTTP::Request->new( GET => '/auth');
109 1         94 my $res = $cb->( $req );
110 1         2611 is (
111             $res->code,
112             401,
113             'Status 401: without HTTP-field Autorization'
114             );
115 1         527 is (
116             $res->headers->header('WWW-Authenticate'),
117             qq|Basic realm="some_realm"|,
118             'HTTP-field: WWW-Authentication without HTTP-field Autorization'
119             );
120 1         396 isnt ( # negative testing, we should not get this content
121             $res->content,
122             qq|Access granted for default realm|,
123             'Delivering: without HTTP-field Autorization'
124             );
125 1         419 };
126              
127              
128             test_psgi $app, sub {
129 1     1   98 my $cb = shift;
130 1         8 my $req = HTTP::Request->new( GET => '/auth');
131 1         97 $req->authorization_basic ( 'foo', 'bar');
132 1         86 my $res = $cb->( $req );
133 1         2768 is (
134             $res->code,
135             401,
136             'Status 401: without proper credentials'
137             );
138 1         582 is (
139             $res->headers->header('WWW-Authenticate'),
140             qq|Basic realm="some_realm"|,
141             'HTTP-field: WWW-Authentication without proper credentials'
142             );
143 1         313 isnt ( # negative testing, we should not get this content
144             $res->content,
145             qq|Access granted for default realm|,
146             'Delivering: without proper credentials'
147             );
148 1         408 };
149              
150             test_psgi $app, sub {
151 1     1   129 my $cb = shift;
152 1         9 my $req = HTTP::Request->new( GET => '/auth');
153 1         94 $req->authorization_basic ( 'dave', 'beer');
154 1         127 my $res = $cb->( $req );
155 1         2532 is (
156             $res->code,
157             200,
158             'Status 200: with the right credentials'
159             );
160 1         509 isnt ( # negative testing, we should not be required to authenticate
161             $res->headers->header('WWW-Authenticate'),
162             qq|Basic realm="some_realm"|,
163             'HTTP-field: WWW-Authentication with the right credentials'
164             );
165 1         434 is (
166             $res->content,
167             qq|Access granted for default realm|,
168             'Delivering: with the right credentials'
169             );
170 1         432 };
171              
172             #
173             # Roles
174             #
175              
176             test_psgi $app, sub {
177 1     1   95 my $cb = shift;
178 1         7 my $req = HTTP::Request->new( GET => '/beer');
179 1         83 $req->authorization_basic ( 'dave', 'beer');
180 1         55 my $res = $cb->( $req );
181 1         10702 is (
182             $res->code,
183             200,
184             'Status 200: BeerDrinker'
185             );
186 1         599 is (
187             $res->content,
188             qq|Enjoy your Beer!|,
189             'Delivering: BeerDrinker'
190             );
191 1         394 };
192              
193             test_psgi $app, sub {
194 1     1   95 my $cb = shift;
195 1         7 my $req = HTTP::Request->new( GET => '/vodka');
196 1         89 $req->authorization_basic ( 'dave', 'beer');
197 1         91 my $res = $cb->( $req );
198 1         3386 is (
199             $res->code,
200             200,
201             'Status 200: VodkaDrinker'
202             );
203 1         519 is (
204             $res->content,
205             qq|Enjoy your Vodka!|,
206             'Delivering: VodkaDrinker'
207             );
208 1         366 };
209              
210             test_psgi $app, sub {
211 1     1   98 my $cb = shift;
212 1         7 my $req = HTTP::Request->new( GET => '/martini');
213 1         87 $req->authorization_basic ( 'dave', 'beer');
214 1         58 my $res = $cb->( $req );
215 1         3095 is (
216             $res->code,
217             403,
218             'Status 403: not a MartiniDrinker'
219             );
220 1         587 is (
221             $res->content,
222             qq|Permission denied for resource: '/martini'|,
223             'Delivering: not a MartiniDrinker'
224             );
225 1         311 isnt (
226             $res->code,
227             200,
228             'Status 200: not a MartiniDrinker'
229             );
230 1         309 unlike (
231             $res->content,
232             qr|Enjoy your Martini!|,
233             'Delivering: not a MartiniDrinker'
234             );
235 1         332 };
236              
237             test_psgi $app, sub {
238 1     1   97 my $cb = shift;
239 1         9 my $req = HTTP::Request->new( GET => '/juice');
240 1         86 $req->authorization_basic ( 'dave', 'beer');
241 1         57 my $res = $cb->( $req );
242 1         3155 is (
243             $res->code,
244             403,
245             'Status 403: not a JuiceDrinker'
246             );
247 1         554 is (
248             $res->content,
249             qq|Permission denied for resource: '/juice'|,
250             'Delivering: not a JuiceDrinker'
251             );
252 1         309 isnt (
253             $res->code,
254             200,
255             'Status 200: not a JuiceDrinker'
256             );
257 1         313 unlike (
258             $res->content,
259             qr|Enjoy your Juice!|,
260             'Delivering: not a JuiceDrinker'
261             );
262 1         336 };
263              
264             test_psgi $app, sub {
265 1     1   96 my $cb = shift;
266 1         8 my $req = HTTP::Request->new( GET => '/drinker');
267 1         87 $req->authorization_basic ( 'dave', 'beer');
268 1         58 my $res = $cb->( $req );
269 1         3381 is (
270             $res->code,
271             200,
272             'Status 200: Regular Drinker'
273             );
274 1         549 is (
275             $res->content,
276             qq|Enjoy your bevarage!|,
277             'Delivering: Regular Drinker'
278             );
279 1         338 };
280              
281             test_psgi $app, sub {
282 1     1   103 my $cb = shift;
283 1         8 my $req = HTTP::Request->new( GET => '/thirsty');
284 1         92 $req->authorization_basic ( 'dave', 'beer');
285 1         61 my $res = $cb->( $req );
286 1         3004 is (
287             $res->code,
288             200,
289             'Status 200: Thirsty Drinker'
290             );
291 1         502 is (
292             $res->content,
293             qq|Enjoy your drinks, one at a time!|,
294             'Delivering: Thirsty Drinker'
295             );
296 1         365 };
297              
298             test_psgi $app, sub {
299 1     1   93 my $cb = shift;
300 1         7 my $req = HTTP::Request->new( GET => '/alcohol');
301 1         82 $req->authorization_basic ( 'dave', 'beer');
302 1         54 my $res = $cb->( $req );
303 1         3020 is (
304             $res->code,
305             200,
306             'Status 200: Alcoholic Drinker'
307             );
308 1         550 is (
309             $res->content,
310             qq|Enjoy your alcohol, but don't drink too much!|,
311             'Delivering: Alcoholic Drinker'
312             );
313 1         332 };
314              
315             test_psgi $app, sub {
316 1     1   95 my $cb = shift;
317 1         8 my $req = HTTP::Request->new( GET => '/cocktail');
318 1         78 $req->authorization_basic ( 'dave', 'beer');
319 1         58 my $res = $cb->( $req );
320 1         3020 is (
321             $res->code,
322             403,
323             'Status 403: not Bond, James Bond'
324             );
325 1         631 is (
326             $res->content,
327             qq|Permission denied for resource: '/cocktail'|,
328             'Delivering: not Bond, James Bond'
329             );
330 1         307 isnt (
331             $res->code,
332             200,
333             'Status 200: not Bond, James Bond'
334             );
335 1         346 unlike (
336             $res->content,
337             qr|Stirred, not shaken!|,
338             'Delivering: not Bond, James Bond'
339             );
340 1         377 };
341              
342 1         369 done_testing();