App-Dochazka-REST
view release on metacpan or search on metacpan
t/dispatch/priv.t view on Meta::CPAN
note( 'define delete_ph_recs() function after initialization because it uses \$test' );
sub delete_ph_recs {
my ( $set ) = @_;
foreach my $rec ( @$set ) {
my $status = req( $test, 200, 'root', 'DELETE', "/priv/history/phid/" . $rec->{phid} );
is( $status->level, 'OK' );
is( $status->code, 'DOCHAZKA_CUD_OK' );
}
}
my $res;
note( '=============================' );
note( '"priv/self/?:ts" resource' );
note( '=============================' );
my $base = "priv/self";
docu_check($test, "$base/?:ts");
note( 'GET' );
note( "GET $base as demo" );
my $status = req( $test, 200, 'demo', 'GET', $base );
is( $status->level, 'OK' );
is( $status->code, "DISPATCH_EMPLOYEE_PRIV" );
ok( defined $status->payload );
ok( exists $status->payload->{'priv'} );
is( $status->payload->{'priv'}, 'passerby' );
note( "GET $base as root" );
$status = req( $test, 200, 'root', 'GET', $base );
is( $status->level, 'OK' );
is( $status->code, "DISPATCH_EMPLOYEE_PRIV" );
ok( defined $status->payload );
ok( exists $status->payload->{'priv'} );
is( $status->payload->{'priv'}, 'admin' );
note( "GET $base/1000-12-31 23:59 as root" );
$status = req( $test, 200, 'root', 'GET', "$base/1000-12-31 23:59" );
is( $status->level, 'OK' );
is( $status->code, "DISPATCH_EMPLOYEE_PRIV_AS_AT" );
is_deeply( $status->payload, {
timestamp => "1000-12-31 23:59",
nick => "root",
priv => "passerby",
eid => "1"
} );
note( "GET $base/1892-01-01 00:01 as root" );
$status = req( $test, 200, 'root', 'GET', "$base/1892-01-01 00:01" );
is( $status->level, 'OK' );
is( $status->code, "DISPATCH_EMPLOYEE_PRIV_AS_AT" );
is_deeply( $status->payload, {
timestamp => "1892-01-01 00:01",
nick => "root",
priv => "admin",
eid => "1"
} );
note( 'PUT, POST, DELETE -> 405' );
foreach my $base ( '/priv/self', '/priv/self/1892-01-01' ) {
req( $test, 405, 'demo', 'PUT', $base );
req( $test, 405, 'demo', 'POST', $base );
req( $test, 405, 'demo', 'DELETE', $base );
#
req( $test, 405, 'root', 'PUT', $base );
req( $test, 405, 'root', 'POST', $base );
req( $test, 405, 'root', 'DELETE', $base );
}
note( '===========================================' );
note( '"priv/eid/:eid/?:ts" resource' );
note( '===========================================' );
$base = "priv/eid";
docu_check($test, "$base/:eid/?:ts");
note( 'GET' );
note( "GET $base/1 as demo" );
req( $test, 403, 'demo', 'GET', "$base/1" );
note( "GET $base/1 as root" );
$status = req( $test, 200, 'root', 'GET', "$base/1" );
is( $status->level, 'OK' );
is( $status->code, "DISPATCH_EMPLOYEE_PRIV" );
ok( defined $status->payload );
is_deeply( $status->payload, {
"priv" => "admin",
"eid" => "1",
"nick" => "root"
});
note( '- as root, with timestamp (before 1892 A.D. root was a passerby)' );
$status = req( $test, 200, 'root', 'GET', "$base/1/1891-12-31 23:59" );
is( $status->level, 'OK' );
is( $status->code, "DISPATCH_EMPLOYEE_PRIV_AS_AT" );
is_deeply( $status->payload, {
timestamp => "1891-12-31 23:59",
nick => "root",
priv => "passerby",
eid => "1"
} );
note( '- as root, with timestamp (root became an admin on 1892-01-01 at 00:00)' );
$status = req( $test, 200, 'root', 'GET', "$base/1/1892-01-01 00:01" );
is( $status->level, 'OK' );
is( $status->code, "DISPATCH_EMPLOYEE_PRIV_AS_AT" );
is_deeply( $status->payload, {
timestamp => "1892-01-01 00:01",
nick => "root",
priv => "admin",
eid => "1"
} );
note( 'PUT, POST, DELETE -> 405' );
foreach my $base ( '/priv/eid/1', '/priv/eid/1/1892-01-01' ) {
req( $test, 405, 'demo', 'PUT', $base );
req( $test, 405, 'demo', 'POST', $base );
req( $test, 405, 'demo', 'DELETE', $base );
#
req( $test, 405, 'root', 'PUT', $base );
req( $test, 405, 'root', 'POST', $base );
req( $test, 405, 'root', 'DELETE', $base );
}
note( '===========================================' );
note( '"priv/nick/:nick/?:ts" resource' );
note( '===========================================' );
$base = "priv/nick";
docu_check($test, "$base/:nick/?:ts");
note( 'GET' );
req( $test, 403, 'demo', 'GET', "$base/root" );
$status = req( $test, 200, 'root', 'GET', "$base/root" );
is( $status->level, 'OK' );
is( $status->code, "DISPATCH_EMPLOYEE_PRIV" );
ok( defined $status->payload );
is_deeply( $status->payload, {
"priv" => "admin",
"eid" => "1",
"nick" => "root"
});
note( '- as root, with timestamp (before 1892 A.D. root was a passerby)' );
$status = req( $test, 200, 'root', 'GET', "$base/root/1891-12-31 23:59" );
is( $status->level, 'OK' );
is( $status->code, "DISPATCH_EMPLOYEE_PRIV_AS_AT" );
is_deeply( $status->payload, {
timestamp => "1891-12-31 23:59",
nick => "root",
priv => "passerby",
eid => "1"
} );
note( '- as root, with timestamp (root became an admin on 1892-01-01 at 00:00)' );
$status = req( $test, 200, 'root', 'GET', "$base/root/1892-01-01 00:01" );
is( $status->level, 'OK' );
is( $status->code, "DISPATCH_EMPLOYEE_PRIV_AS_AT" );
is_deeply( $status->payload, {
timestamp => "1892-01-01 00:01",
nick => "root",
priv => "admin",
eid => "1"
} );
note( 'PUT, POST, DELETE -> 405' );
foreach my $user ( qw( demo root ) ) {
foreach my $method ( qw( PUT POST DELETE ) ) {
foreach my $uri ( "$base/root", "$base/root/1892-01-01" ) {
req( $test, 405, $user, $method, $uri );
}
}
}
note( 'tear down' );
$status = delete_all_attendance_data();
BAIL_OUT(0) unless $status->ok;
done_testing;
( run in 0.659 second using v1.01-cache-2.11-cpan-39bf76dae61 )