Extensible/Test/App.pm

filter: Statementstmt 94.7% 325 / 343 Branchbran 66.2% 49 / 74 Conditioncond 40.0% 2 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 97.1% 101 / 104 Podpod 0.0% 0 / 2 totaltotal 89.8% 477 / 531 CC 33 SCAR 7.3
CC
33
Cov
89%
__ANON__
6.5
__ANON__
6.0
__ANON__
5.6
CRAP 2.1
« Extensible/Provider/Config.pm ↑ Summary Extensible/Role/Provider.pm »
1 package Dancer2::Plugin::Auth::Extensible::Test::App;
2  
3 =head1 NAME
4  
5 Dancer2::Plugin::Auth::Extensible::Test::App - Dancer2 app for testing providers
6  
7 =cut
8  
9 2 our $VERSION = '0.714';
10  
11 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Test::More;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Test::Deep qw(bag cmp_deeply);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Test::Fatal;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use Dancer2 appname => 'TestApp';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use Dancer2::Plugin::Auth::Extensible;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use YAML ();
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 2 set session => 'simple';
22 2 set logger => 'capture';
23 2 set log => 'debug';
24 2 set show_errors => 1;
25  
26 # nasty shared global makes it easy to pass data between app and test script
27 2 our $data = {};
28  
29 config->{plugins}->{"Auth::Extensible"}->{password_reset_send_email} =
30 2 __PACKAGE__ . "::email_send";
31 config->{plugins}->{"Auth::Extensible"}->{welcome_send} =
32 2 __PACKAGE__ . "::email_send";
33  
34 2 ▶sub email_send {
Subroutine email_send: called CC=1 SCAR=0.0
Pod undocumented
35 2 my ( $plugin, %args ) = @_;
36 2 $data = { %args, called => 1 };
37 }
38  
39 # we need the plugin object and a provider for provider tests
40 2 my $plugin = app->with_plugin('Auth::Extensible');
41 2 my $provider = $plugin->auth_provider('config1');
42  
43 2 my @provider_can = ();
44  
45 2 ▶push @provider_can, 'record_lastlogin' if $plugin->config->{record_lastlogin};
Branch: 1 branch
BranchTrueFalseTotal
if $plugin->config->{"record_lastlogin"} 1 1 2
46  
47 2 ▶config->{plugins}->{"Auth::Extensible"}->{reset_password_handler} = 1
Branch: 1 branch
BranchTrueFalseTotal
if $provider->can("get_user_by_code") 1 1 2
48 if $provider->can('get_user_by_code');
49  
50 #
51 # IMPORTANT NOTE
52 #
53 # We use "isnt exception {...}, undef, ..." a lot which is REALLY BAD
54 # practice. This should only ever be done in provider tests since we cannot
55 # be sure what exception message a provider returns and we do NOT mandate
56 # specific messages so we can only test if something died.
57 #
58 # When writing new provider tests please always test first with a "like /qr/"
59 # instead and then once the tests are all working against one provider switch
60 # them to the bad "isnt undef" style so they are portable.
61 #
62  
63 2 ▶subtest 'Provider authenticate_user tests' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
64 2 my $ret;
65 2 push @provider_can, 'authenticate_user';
66  
67 2 ▶ isnt exception { $ret = $provider->authenticate_user(); },
Subroutine __ANON__: called CC=1 SCAR=0.0
68 undef,
69 2 "authenticate_user with no args dies.";
70  
71 2 ▶ isnt exception { $ret = $provider->authenticate_user(''); },
Subroutine __ANON__: called CC=1 SCAR=0.0
72 undef,
73 2 "authenticate_user with empty username and no password dies.";
74  
75 2 ▶ isnt exception { $ret = $provider->authenticate_user(undef, ''); },
Subroutine __ANON__: called CC=1 SCAR=0.0
76 undef,
77 2 "authenticate_user with undef username and empty password dies.";
78  
79 2 ▶ is exception { $ret = $provider->authenticate_user('', ''); },
Subroutine __ANON__: called CC=1 SCAR=0.0
80 undef,
81 2 "authenticate_user with empty username and empty password lives.";
82 2 ok !$ret, "... and returns a false value.";
83  
84 2 ▶ is exception { $ret = $provider->authenticate_user('unknown', 'beer'); },
Subroutine __ANON__: called CC=1 SCAR=0.0
85 undef,
86 2 "authenticate_user with unknown user lives.";
87 2 ok !$ret, "... and returns a false value.";
88  
89 2 ▶ is exception { $ret = $provider->authenticate_user('dave', 'notcorrect'); },
Subroutine __ANON__: called CC=1 SCAR=0.0
90 undef,
91 2 "authenticate_user with known user and bad password lives.";
92 2 ok !$ret, "... and returns a false value.";
93  
94 2 ▶ is exception { $ret = $provider->authenticate_user('dave', 'beer'); },
Subroutine __ANON__: called CC=1 SCAR=0.0
95 undef,
96 2 "authenticate_user with known user and good password.";
97 2 ok $ret, "... and returns a true value.";
98 2 };
99  
100 SKIP: {
101 2 ▶ skip "Provider has no get_user_details method", 1
Branch: 1 branch
BranchTrueFalseTotal
unless $provider->can("get_user_details") 0 2 2
102 unless $provider->can('get_user_details');
103  
104 2 ▶ subtest 'Provider get_user_details tests' => sub {
Subroutine __ANON__: called CC=4 SCAR=14.1
105 2 my $ret;
106  
107 2 push @provider_can, 'get_user_details';
108  
109 2 ▶ isnt exception { $ret = $provider->get_user_details(); },
Subroutine __ANON__: called CC=1 SCAR=0.0
110 undef,
111 2 "get_user_details with no args dies.";
112  
113 2 ▶ is exception { $ret = $provider->get_user_details(''); },
Subroutine __ANON__: called CC=1 SCAR=0.0
114 undef,
115 2 "get_user_details with empty username lives.";
116 2 ok !$ret, "... and returns a false value.";
117  
118 2 ▶ is exception { $ret = $provider->get_user_details('unknown'); },
Subroutine __ANON__: called CC=1 SCAR=0.0
119 undef,
120 2 "get_user_details with unknown user lives.";
121 2 ok !$ret, "... and returns a false value.";
122  
123 2 ▶ is exception { $ret = $provider->get_user_details('dave'); },
Subroutine __ANON__: called CC=1 SCAR=0.0
124 undef,
125 2 "get_user_details with known user lives.";
126 2 ok $ret, "... and returns a true value";
127 2 ▶ ok blessed($ret) || ref($ret) eq 'HASH',
Branch: 1 branch
BranchTrueFalseTotal
&ok(scalar(builtin::blessed($ret) || ref $ret eq "HASH"), "... which is either an object or a hash reference") or diag(explain($ret)) 2 0 2
Condition33%1/3
builtin::blessed($ret) || ref $ret eq 'HASH'
l!l&&r!l&&!r
020
MC/DC0%0/2
builtin::blessed($ret) || ref $ret eq 'HASH'
builtin::blessed($ret) ref $ret eq 'HASH'
Truth table33%1/3
A || B
A= builtin::blessed($ret)B= ref $ret eq 'HASH'
ABresult
1X1
011
000
128 "... which is either an object or a hash reference"
129 or diag explain $ret;
130 2 ▶ is blessed($ret) ? $ret->name : $ret->{name}, 'David Precious',
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($ret) ? : 0 2 2
131 "... and user's name is David Precious.";
132 2 };
133 }
134  
135 SKIP: {
136 2 ▶ skip "Provider has no get_user_roles method", 1
Branch: 1 branch
BranchTrueFalseTotal
unless $provider->can("get_user_roles") 0 2 2
137 unless $provider->can('get_user_roles');
138  
139 2 ▶ subtest 'Provider get_user_roles tests' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
140 2 my $ret;
141  
142 2 push @provider_can, 'get_user_roles';
143  
144 2 ▶ isnt exception { $ret = $provider->get_user_roles(); },
Subroutine __ANON__: called CC=1 SCAR=0.0
145 undef,
146 2 "get_user_roles with no args dies.";
147  
148 2 ▶ is exception { $ret = $provider->get_user_roles(''); }, undef,
Subroutine __ANON__: called CC=1 SCAR=0.0
149 2 "get_user_roles with empty username lives";
150 2 ok !$ret, "... and returns false value.";
151  
152 2 ▶ is exception { $ret = $provider->get_user_roles('unknown'); }, undef,
Subroutine __ANON__: called CC=1 SCAR=0.0
153 2 "get_user_roles with unknown user lives";
154 2 ok !$ret, "... and returns false value.";
155  
156 2 ▶ is exception { $ret = $provider->get_user_roles('dave'); }, undef,
Subroutine __ANON__: called CC=1 SCAR=0.0
157 2 "get_user_roles with known user \"dave\" lives";
158 2 ok $ret, "... and returns true value";
159 2 is ref($ret), 'ARRAY', "... which is an array reference";
160 2 cmp_deeply $ret, bag( "BeerDrinker", "Motorcyclist" ),
161 "... and dave is a BeerDrinker and Motorcyclist.";
162 2 };
163 }
164  
165 SKIP: {
166 2 ▶ skip "Provider has no create_user method", 1
Branch: 1 branch
BranchTrueFalseTotal
unless $provider->can("create_user") 1 1 2
167 unless $provider->can('create_user');
168  
169 1 ▶ subtest 'Provider create_user tests' => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
170 1 my $ret;
171  
172 1 push @provider_can, 'create_user';
173  
174 1 ▶ isnt exception { $ret = $provider->create_user(); },
Subroutine __ANON__: called CC=1 SCAR=0.0
175 undef,
176 1 "create_user with no args dies.";
177  
178 1 ▶ isnt exception { $ret = $provider->create_user(username => ''); },
Subroutine __ANON__: called CC=1 SCAR=0.0
179 undef,
180 1 "create_user with empty username dies.";
181  
182 1 ▶ isnt exception { $ret = $provider->create_user(username => 'dave'); },
Subroutine __ANON__: called CC=1 SCAR=0.0
183 undef,
184 1 "create_user with existing username dies.";
185  
186 1 ▶ is exception {
Subroutine __ANON__: called CC=1 SCAR=0.0
187 1 $ret = $provider->get_user_details('provider_create_user');
188 },
189 undef,
190 1 "get_user_details \"provider_create_user\" lives";
191 1 ok !defined $ret, "... and does not return a user.";
192  
193 1 ▶ is exception {
Subroutine __ANON__: called CC=1 SCAR=0.0
194 1 $ret = $provider->create_user(
195 username => 'provider_create_user',
196 name => 'Create User'
197 );
198 },
199 undef,
200 1 "create_user \"provider_create_user\" lives";
201  
202 1 ok defined $ret, "... and returns a user";
203 1 ▶ is blessed($ret) ? $ret->name : $ret->{name}, "Create User",
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($ret) ? : 0 1 1
204 "... and user's name is correct.";
205  
206 1 ▶ is exception {
Subroutine __ANON__: called CC=1 SCAR=0.0
207 1 $ret = $provider->get_user_details('provider_create_user');
208 },
209 undef,
210 1 "get_user_details \"provider_create_user\" lives";
211 1 ok defined $ret, "... and now *does* return a user.";
212 1 ▶ is blessed($ret) ? $ret->name : $ret->{name}, "Create User",
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($ret) ? : 0 1 1
213 "... and user's name is correct.";
214 1 };
215 }
216  
217 SKIP: {
218 2 ▶ skip "Provider has no set_user_details method", 1
Branch: 1 branch
BranchTrueFalseTotal
unless $provider->can("set_user_details") 1 1 2
219 unless $provider->can('set_user_details');
220  
221 1 ▶ subtest 'Provider set_user_details tests' => sub {
Subroutine __ANON__: called CC=4 SCAR=14.0
222 1 my $ret;
223  
224 1 push @provider_can, 'set_user_details';
225  
226 1 ▶ isnt exception { $ret = $provider->set_user_details(); },
Subroutine __ANON__: called CC=1 SCAR=0.0
227 undef,
228 1 "set_user_details with no args dies.";
229  
230 1 ▶ isnt exception { $ret = $provider->set_user_details(''); },
Subroutine __ANON__: called CC=1 SCAR=0.0
231 undef,
232 1 "set_user_details with empty username dies.";
233  
234 1 ▶ is exception {
Subroutine __ANON__: called CC=1 SCAR=0.0
235 1 $ret = $provider->create_user(
236 username => 'provider_set_user_details',
237 name => 'Initial Name'
238 );
239 },
240 undef,
241 1 "Create a user for testing lives";
242  
243 1 ▶ is exception {
Subroutine __ANON__: called CC=1 SCAR=0.0
244 1 $ret = $provider->get_user_details('provider_set_user_details')
245 },
246 undef,
247 1 "... and get_user_details on new user lives";
248  
249 1 ▶ is blessed($ret) ? $ret->name : $ret->{name}, 'Initial Name',
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($ret) ? : 0 1 1
250 "... and user has expected name.";
251  
252 1 ▶ is exception {
Subroutine __ANON__: called CC=1 SCAR=0.0
253 1 $ret = $provider->set_user_details( 'provider_set_user_details',
254 name => 'New Name', );
255 },
256 undef,
257 1 "Using set_user_details to change user's name lives";
258  
259 1 ▶ is blessed($ret) ? $ret->name : $ret->{name}, 'New Name',
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($ret) ? : 0 1 1
260 "... and returned user has expected name.";
261  
262 1 ▶ is exception {
Subroutine __ANON__: called CC=1 SCAR=0.0
263 1 $ret = $provider->get_user_details('provider_set_user_details')
264 },
265 undef,
266 1 "... and get_user_details on new user lives";
267  
268 1 ▶ is blessed($ret) ? $ret->name : $ret->{name}, 'New Name',
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($ret) ? : 0 1 1
269 "... and returned user has expected name.";
270 1 };
271 }
272  
273 SKIP: {
274 2 ▶ skip "Provider has no get_user_by_code method", 1
Branch: 1 branch
BranchTrueFalseTotal
unless $provider->can("get_user_by_code") 1 1 2
275 unless $provider->can('get_user_by_code');
276  
277 1 ▶ subtest 'Provider get_user_by_code tests' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
278 1 my $ret;
279  
280 1 push @provider_can, 'get_user_by_code';
281  
282 1 ▶ isnt exception { $ret = $provider->get_user_by_code(); },
Subroutine __ANON__: called CC=1 SCAR=0.0
283 undef,
284 1 "get_user_by_code with no args dies.";
285  
286 1 ▶ isnt exception { $ret = $provider->get_user_by_code(''); },
Subroutine __ANON__: called CC=1 SCAR=0.0
287 undef,
288 1 "get_user_by_code with empty code dies.";
289  
290 1 ▶ is exception { $ret = $provider->get_user_by_code('nosuchcode'); },
Subroutine __ANON__: called CC=1 SCAR=0.0
291 undef,
292 1 "get_user_by_code with non-existant code lives";
293 1 ok !defined $ret, "... and returns undef.";
294  
295 1 ▶ is exception {
Subroutine __ANON__: called CC=1 SCAR=0.0
296 1 $ret = $provider->create_user(
297 username => 'provider_get_user_by_code',
298 pw_reset_code => '01234567890get_user_by_code',
299 );
300 },
301 undef,
302 1 "Create a user for testing lives";
303  
304 1 ▶ is exception {
Subroutine __ANON__: called CC=1 SCAR=0.0
305 1 $ret = $provider->get_user_by_code('01234567890get_user_by_code');
306 },
307 undef,
308 1 "get_user_by_code with non-existant code lives";
309 1 ok defined $ret, "... and returns something true";
310  
311 1 is $ret, 'provider_get_user_by_code',
312 "... and returned username is correct.";
313 1 };
314 }
315  
316 SKIP: {
317 2 ▶ skip "Provider has no set_user_password method", 1
Branch: 1 branch
BranchTrueFalseTotal
unless $provider->can("set_user_password") 1 1 2
318 unless $provider->can('set_user_password');
319  
320 1 ▶ subtest 'Provider set_user_password tests' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
321 1 my $ret;
322  
323 1 push @provider_can, 'set_user_password';
324  
325 1 ▶ isnt exception { $ret = $provider->set_user_password(); },
Subroutine __ANON__: called CC=1 SCAR=0.0
326 undef,
327 1 "set_user_password with no args dies.";
328  
329 1 ▶ isnt exception { $ret = $provider->set_user_password(''); },
Subroutine __ANON__: called CC=1 SCAR=0.0
330 undef,
331 1 "set_user_password with username but undef password dies";
332  
333 1 ▶ isnt exception { $ret = $provider->set_user_password( undef, '' ); },
Subroutine __ANON__: called CC=1 SCAR=0.0
334 undef,
335 1 "set_user_password with password but undef username dies";
336  
337 1 ▶ is exception {
Subroutine __ANON__: called CC=1 SCAR=0.0
338 1 $ret =
339 $provider->create_user( username => 'provider_set_user_password' )
340 },
341 undef,
342 1 "Create a user for testing lives";
343  
344 1 ▶ is exception {
Subroutine __ANON__: called CC=1 SCAR=0.0
345 1 $ret = $provider->set_user_password( 'provider_set_user_password',
346 'aNicePassword' )
347 },
348 1 undef, "set_user_password for our new user lives";
349  
350 1 ▶ is exception {
Subroutine __ANON__: called CC=1 SCAR=0.0
351 1 $ret = $provider->authenticate_user( 'provider_set_user_password',
352 'aNicePassword' )
353 },
354 1 undef, "... and authenticate_user with correct password lives";
355 1 ok $ret, "... and authenticate_user passes (returns true)";
356  
357 1 ▶ is exception {
Subroutine __ANON__: called CC=1 SCAR=0.0
358 1 $ret = $provider->authenticate_user( 'provider_set_user_password',
359 'badpwd' )
360 },
361 1 undef, "... and whilst authenticate_user with bad password lives";
362 1 ok !$ret, "... it returns false.";
363 1 };
364 }
365  
366 SKIP: {
367 2 ▶ skip "Provider has no password_expired method", 1
Branch: 1 branch
BranchTrueFalseTotal
unless $provider->can("password_expired") 1 1 2
368 unless $provider->can('password_expired');
369  
370 1 ▶ subtest 'Provider password_expired tests' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
371 1 my $ret;
372  
373 1 push @provider_can, 'password_expired';
374  
375 1 ▶ isnt exception { $ret = $provider->password_expired(); },
Subroutine __ANON__: called CC=1 SCAR=0.0
376 undef,
377 1 "password_expired with no args dies.";
378  
379 1 ▶ is exception {
Subroutine __ANON__: called CC=1 SCAR=0.0
380 1 $ret =
381 $provider->create_user( username => 'provider_password_expired' )
382 },
383 undef,
384 1 "Create a user for testing lives";
385  
386 1 ▶ is exception {
Subroutine __ANON__: called CC=1 SCAR=0.0
387 1 $ret = $provider->password_expired($ret)
388 },
389 undef,
390 1 "... and password_expired for user lives";
391  
392 1 ok $ret, "... and password is expired since it has never been set.";
393  
394 1 ▶ is exception {
Subroutine __ANON__: called CC=1 SCAR=0.0
395 1 $ret = $provider->set_user_password( 'provider_password_expired',
396 'password' )
397 },
398 undef,
399 1 "Setting password for user lives";
400  
401 1 ▶ is exception {
Subroutine __ANON__: called CC=1 SCAR=0.0
402 1 $ret = $provider->password_expired($ret)
403 },
404 undef,
405 1 "... and password_expired for user lives";
406  
407 1 ok !$ret, "... and password is now *not* expired.";
408  
409 1 ▶ is exception {
Subroutine __ANON__: called CC=1 SCAR=0.0
410 1 $ret = $provider->set_user_details( 'provider_password_expired',
411 pw_changed => DateTime->now->subtract( weeks => 1 ) )
412 },
413 undef,
414 1 "Set pw_changed to one week ago lives and so now password is expired";
415  
416 1 ▶ is exception {
Subroutine __ANON__: called CC=1 SCAR=0.0
417 1 $ret = $provider->password_expired($ret)
418 },
419 undef,
420 1 "... and password_expired for user lives";
421  
422 1 ok $ret, "... and password *is* now expired since expiry is 2 days.";
423  
424 1 };
425 }
426  
427 2 ▶subtest "Plugin coverage testing" => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
428 # DO NOT use this for testing things that can be tested elsewhere since
429 # these tests are purely to catch the code paths that we can't get to
430 # any other way.
431  
432 2 ▶ like exception { $plugin->realm() }, qr/realm name not provided/,
Subroutine __ANON__: called CC=1 SCAR=0.0
433 "Calling realm method with no args dies";
434  
435 2 ▶ like exception { $plugin->realm('') }, qr/realm name not provided/,
Subroutine __ANON__: called CC=1 SCAR=0.0
436 "... and calling it with single empty arg dies.";
437  
438 2 foreach my $username ( undef, +{}, '', 'username' ) {
439 8 foreach my $password ( undef, +{}, '', 'password' ) {
440 32 my $ret = $plugin->authenticate_user( $username, $password );
441 32 is $ret, 0,
442 "Checking authenticate_user with username/password: "
443 . mydumper($username) . "/"
444 . mydumper($password);
445 }
446 }
447 2 };
448  
449 64 ▶sub mydumper {
Subroutine mydumper: called CC=4 SCAR=13.9
Pod undocumented
450 64 my $val = shift;
451 64 ▶ !defined $val && return '(undef)';
Branch: 1 branch
BranchTrueFalseTotal
defined $val or return "(undef)" 48 16 64
452 48 ▶ ref($val) ne '' && return ref($val);
Branch: 1 branch
BranchTrueFalseTotal
ref $val ne "" and return ref $val 16 32 48
453 32 ▶ $val eq '' && return '(empty)';
Branch: 1 branch
BranchTrueFalseTotal
$val eq "" and return "(empty)" 16 16 32
454 16 $val;
455 };
456  
457 # hooks
458  
459 88 ▶hook before_authenticate_user => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
460 88 debug "before_authenticate_user", to_json( shift, { canonical => 1 } );
461 2 };
462 88 ▶hook after_authenticate_user => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
463 88 debug "after_authenticate_user", to_json( shift, { canonical => 1 } );
464 2 };
465 8 ▶hook before_create_user => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
466 8 debug "before_create_user", to_json( shift, { canonical => 1 } );
467 2 };
468 8 ▶hook after_create_user => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
469 8 my ( $username, $user, $errors ) = @_;
470 8 ▶ my $ret = $user ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$user ? : 6 2 8
471 8 ▶ debug "after_create_user,$username,$ret,",scalar @$errors ? 'yes' : 'no';
Branch: 1 branch
BranchTrueFalseTotal
scalar @$errors ? : 2 6 8
472 2 };
473  
474 # and finally the routes for the main plugin tests
475  
476 2 ▶get '/provider_can' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
477 2 send_as YAML => \@provider_can;
478 2 };
479  
480 4 ▶get '/' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
481 4 "Index always accessible";
482 2 };
483  
484 46 ▶post '/authenticate_user' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
485 46 my $params = body_parameters->as_hashref;
486 my @ret = authenticate_user( $params->{username}, $params->{password},
487 46 $params->{realm} );
488 46 send_as YAML => \@ret;
489 2 };
490  
491 9 ▶post '/create_user' => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
492 9 my $params = body_parameters->as_hashref;
493 9 my $user = create_user %$params;
494 8 ▶ return $user ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$user ? : 6 2 8
495 2 };
496  
497 10 ▶post '/get_user_details' => sub {
Subroutine __ANON__: called CC=4 SCAR=17.2
498 10 my $params = body_parameters->as_hashref;
499 10 my $user = get_user_details $params->{username}, $params->{realm};
500 10 ▶ if ( blessed($user) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($user)) 0 10 10
501 0 ▶ if ( $user->isa('DBIx::Class::Row')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($user->isa('DBIx::Class::Row')) { } 0 0 0
502 0 $user = +{ $user->get_columns };
503 }
504 else {
505 # assume some kind of hash-backed object
506 0 $user = \%$user;
507 }
508 }
509 10 ▶ return $user ? send_as YAML => $user : 0;
Branch: 1 branch
BranchTrueFalseTotal
$user ? : 4 6 10
510 2 };
511  
512 15 ▶get '/session_data' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
513 15 my $session = session->data;
514 15 send_as YAML => $session;
515 2 };
516  
517 2 ▶get '/logged_in_user_lastlogin' => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
518 2 my $dt = logged_in_user_lastlogin;
519 2 ▶ if ( ref($dt) eq 'DateTime' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $dt eq "DateTime") 1 1 2
520 1 return $dt->ymd;
521 }
522 1 return 'not set';
523 2 };
524  
525 12 ▶get '/logged_in_user' => sub {
Subroutine __ANON__: called CC=4 SCAR=17.9
526 12 my $user = logged_in_user;
527 12 ▶ if ( blessed($user) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($user)) 0 12 12
528 0 ▶ if ( $user->isa('DBIx::Class::Row')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($user->isa('DBIx::Class::Row')) { } 0 0 0
529 0 $user = +{ $user->get_columns };
530 }
531 else {
532 # assume some kind of hash-backed object
533 0 $user = \%$user;
534 }
535 }
536 12 ▶ send_as YAML => $user ? $user : 'none';
Branch: 1 branch
BranchTrueFalseTotal
$user ? : 6 6 12
537 2 };
538  
539 2 ▶get '/logged_in_user_twice' => sub {
Subroutine __ANON__: called CC=4 SCAR=18.7
540 2 logged_in_user; # retrieve
541 2 my $user = logged_in_user; # should now be stashed in var
542 2 ▶ if ( blessed($user) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($user)) 0 2 2
543 0 ▶ if ( $user->isa('DBIx::Class::Row')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($user->isa('DBIx::Class::Row')) { } 0 0 0
544 0 $user = +{ $user->get_columns };
545 }
546 else {
547 # assume some kind of hash-backed object
548 0 $user = \%$user;
549 }
550 }
551 2 ▶ send_as YAML => $user ? $user : 'none';
Branch: 1 branch
BranchTrueFalseTotal
$user ? : 2 0 2
552 2 };
553  
554 24 ▶get '/loggedin' => require_login sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
555 24 "You are logged in";
556 2 };
557  
558 2 ▶get qr{/regex/(.+)} => require_login sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
559 2 return "Matched";
560 2 };
561  
562 2 get '/require_login_no_sub' => require_login;
563  
564 2 get '/require_login_not_coderef' => require_login { a => 1 };
565  
566 2 ▶get '/roles' => require_login sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
567 2 ▶ my $roles = user_roles() || [];
Condition50%1/2
&user_roles() || []
l!l
20
MC/DC0%0/1
&user_roles() || []
&user_roles()
Truth table50%1/2
A
A= &user_roles()
Aresult
11
00
568 2 return join ',', sort @$roles;
569 2 };
570  
571 2 ▶get '/roles/:user' => require_login sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
572 2 my $user = param 'user';
573 2 return join ',', sort @{ user_roles($user) };
574 2 };
575  
576 2 ▶get '/roles/:user/:realm' => require_login sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
577 2 my $user = param 'user';
578 2 my $realm = param 'realm';
579 2 return join ',', sort @{ user_roles($user, $realm) };
580 2 };
581  
582 2 ▶get '/user_roles' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
583 2 return join ',', sort @{ user_roles() };
584 2 };
585  
586 2 ▶get '/beer' => require_role BeerDrinker => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
587 2 "You can have a beer";
588 2 };
589  
590 0 ▶get '/piss' => require_role BearGrylls => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
591 0 "You can drink piss";
592 2 };
593  
594 2 ▶get '/piss/regex' => require_role qr/beer/i => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
595 2 "You can drink piss now";
596 2 };
597  
598 2 ▶get '/anyrole' => require_any_role ['Foo','BeerDrinker'] => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
599 2 "Matching one of multiple roles works";
600 2 };
601  
602 2 ▶get '/allroles' => require_all_roles ['BeerDrinker', 'Motorcyclist'] => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
603 2 "Matching multiple required roles works";
604 2 };
605  
606 0 ▶get '/not_allroles' => require_all_roles ['BeerDrinker', 'BadRole'] => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
607 0 "Matching multiple required roles should fail";
608 2 };
609  
610 2 ▶get '/does_dave_drink_beer' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
611 2 return user_has_role('dave', 'BeerDrinker');
612 2 };
613  
614 2 ▶get '/does_dave_drink_cider' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
615 2 return user_has_role('dave', 'CiderDrinker');
616 2 };
617  
618 2 ▶get '/does_undef_drink_beer' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
619 2 return user_has_role(undef, 'BeerDrinker');
620 2 };
621  
622 0 ▶get '/user_password' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
623 0 return user_password params('query');
624 2 };
625 12 ▶post '/user_password' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
626 12 return user_password %{ body_parameters->as_hashref };
627 2 };
628  
629 3 ▶get '/update_current_user' => sub {
Subroutine __ANON__: called CC=3 SCAR=16.0
630 3 my $user = update_current_user name => "I love cider";
631 3 ▶ if ( blessed($user) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($user)) 0 3 3
632 0 ▶ if ( $user->isa('DBIx::Class::Row')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($user->isa('DBIx::Class::Row')) { } 0 0 0
633 0 $user = +{ $user->get_columns };
634 }
635 else {
636 # assume some kind of hash-backed object
637 0 $user = \%$user;
638 }
639 }
640 3 YAML::Dump $user;
641 2 };
642  
643 2 ▶get '/update_user_name/:realm' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
644 2 my $realm = param 'realm';
645 2 YAML::Dump update_user 'mark', realm => $realm, name => "Wiltshire Apples $realm";
646 2 };
647  
648 4 ▶post '/update_user' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
649 4 my $params = body_parameters->as_hashref;
650 4 my $username = delete $params->{username};
651 4 send_as YAML => update_user $username, %$params;
652 2 };
653  
654 8 ▶get '/get_user_mark/:realm' => sub {
Subroutine __ANON__: called CC=3 SCAR=14.2
655 8 my $realm = param 'realm';
656 8 content_type 'text/x-yaml';
657 8 my $user = get_user_details 'mark', $realm;
658 8 ▶ if ( blessed($user) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($user)) 0 8 8
659 0 ▶ if ( $user->isa('DBIx::Class::Row')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($user->isa('DBIx::Class::Row')) { } 0 0 0
660 0 $user = +{ $user->get_columns };
661 }
662 else {
663 # assume some kind of hash-backed object
664 0 $user = \%$user;
665 }
666 }
667 8 YAML::Dump $user;
668 2 };
669  
670 8 ▶post '/auth_provider' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
671 8 $plugin->auth_provider( body_parameters->get('realm') );
672 4 return;
673 2 };
674  
675 3 ▶get '/logged_in_user_password_expired' => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
676 3 my $ret = logged_in_user_password_expired;
677 3 ▶ return $ret ? 'yes' : 'no';
Branch: 1 branch
BranchTrueFalseTotal
$ret ? : 1 2 3
678 2 };
679  
680 2 1;
« Extensible/Provider/Config.pm ↑ Summary Extensible/Role/Provider.pm »