App-Dochazka-REST

 view release on metacpan or  search on metacpan

t/dispatch/history.t  view on Meta::CPAN

    req( $test, 403, 'demo', 'GET', "$base/4534" );

    note( '"succeed" (404) as root' );
    req( $test, 404, 'root', 'GET', "$base/4534" );

    note( 'GET history of various invalid EIDs' );
    foreach my $inv_eid ( 'asas', '!*!*', 'A long list of useless words followed by lots of spaces                                           \\,', '3.1415926', '; drop database dochazka-test;' ) {
        foreach my $user ( qw( demo root ) ) {
            req( $test, 400, 'demo', 'GET', "$base/$inv_eid" );
        }
    }

    foreach my $inv_eid ( '0', '-1' ) {
        # - as demo
        req( $test, 403, 'demo', 'GET', "$base/$inv_eid" );
        # - as root
        req( $test, 404, 'root', 'GET', "$base/$inv_eid" );
    }

    foreach my $inv_eid ( '3443/plus/several/bogus/levels/of/subresources' ) {
        # - as demo (entire resource is invalid, so ACL check is not reached)
        req( $test, 400, 'demo', 'GET', "$base/$inv_eid" );
        # - as root
        req( $test, 400, 'root', 'GET', "$base/$inv_eid" );
    }

    note( 'PUT' );
    req( $test, 405, 'demo', 'PUT', "$base/2" );
    req( $test, 405, 'active', 'PUT', "$base/2" );
    req( $test, 405, 'root', 'PUT', "$base/2" );
    
    note( 'POST' );

    note( 'dates before 1892-01-01 will not make it through the trigger' );
    foreach my $ts ( 
        '1869-04-28 19:15',
        '1891-01-01 00:00',
        '1891-12-31 23:55',
        '1000-01-01 00:05',
        '1500-12-20',
    ) {
        $j = ( $base =~ m/^priv/ )
            ? "{ \"effective\":\"$ts\", \"priv\":\"inactive\" }"
            : "{ \"effective\":\"$ts\", \"sid\":$ts_sid }";
        dbi_err( $test, 500, 'root', 'POST', "$base/2", 
            $j,
            qr/No dates earlier than 1892-01-01 please/
        );
    }

    note( 'we will be inserting a bunch of records so push them onto an array' );
    note( 'for easy deletion later' );
    my @history_recs_to_delete;
    # - be nice
    $j = ( $base =~ m/^priv/ )
        ? '{ "effective":"1969-04-28 19:15", "priv":"inactive" }'
        : '{ "effective":"1969-04-28 19:15", "sid":' . $ts_sid . ' }';

    req( $test, 403, 'demo', 'POST', "$base/2", $j );
    $status = req( $test, 201, 'root', 'POST', "$base/2", $j );
    if ( $status->not_ok ) {
        diag( $status->code . ' ' . $status->text );
    }
    is( $status->level, 'OK' );
    my $pho = $status->payload;
    my $prop = ( $base =~ m/^priv/ ) ? 'phid' : 'shid';
    ok( exists $pho->{$prop}, "$prop exists in payload after POST $base/2" );
    ok( defined $pho->{$prop}, "$prop defined in payload after POST $base/2" );
    push @history_recs_to_delete, { eid => $pho->{eid}, $prop => $pho->{$prop} };

    note( 'be pathological' );
    $j = '{ "effective":"1979-05-24", "horse" : "E-Or" }';
    req( $test, 403, 'demo', 'POST', "$base/2", $j );
    req( $test, 400, 'root', 'POST', "$base/2", $j );

    note( 'addition of privlevel makes the above request less pathological' );
    $j = ( $base =~ m/^priv/ )
        ? '{ "effective":"1979-05-24", "horse" : "E-Or", "priv" : "admin" }'
        : '{ "effective":"1979-05-24", "horse" : "E-Or", "sid" : ' . $ts_sid . ' }';
    req( $test, 403, 'demo', 'POST', "$base/2", $j );
    $status = req( $test, 201, 'root', 'POST', "$base/2", $j );
    is( $status->level, 'OK' );
    $pho = $status->payload;
    push @history_recs_to_delete, { eid => $pho->{eid}, $prop => $pho->{$prop} };

    if ( $base =~ m/^priv/ ) {
        # check if demo really is an admin
        $status = req( $test, 200, 'demo', 'GET', "employee/self/full" );
        is( $status->level, 'OK' );
        is( $status->code, 'DISPATCH_EMPLOYEE_PROFILE_FULL' );
        ok( exists $status->{'payload'} );
        ok( exists $status->payload->{'priv'} );
        is( $status->payload->{'priv'}, 'admin' );
    }

    note( 'DELETE' );
    req( $test, 405, 'demo', 'DELETE', "$base/2" );
    req( $test, 405, 'active', 'DELETE', "$base/2" );
    req( $test, 405, 'root', 'DELETE', "$base/2" );
    
    note( 'teardown: we have some records queued for deletion' );
    delete_history_recs( $base, \@history_recs_to_delete );
    @history_recs_to_delete = ();
}

note( '===========================================' );
note( '"{priv,schedule}/history/eid/:eid/:tsrange" resource' );
note( '===========================================' );

foreach $base ( "priv/history/eid", "schedule/history/eid" ) {
    note( "testing $base" );

    note( 'docu_check()' );
    docu_check($test, "$base/:eid/:tsrange");

    note( 'GET' );

    note( 'fail 403 as demo' );
    req( $test, 403, 'demo', 'GET', $base. '/' . $site->DOCHAZKA_EID_OF_ROOT . 
        '/[1891-12-30, 1892-01-02)' );

t/dispatch/history.t  view on Meta::CPAN

    note( '"succeed" (404) as root' );
    req( $test, 404, 'root', 'GET', $uri );

    note( 'non-existent EID');
    my $ts = '\'2015-01-06 14:55\'';
    req( $test, 403, 'demo', 'GET', "$base/4534/now" );
    req( $test, 404, 'root', 'GET', "$base/4534/now" );

    note( 'invalid EID (caught by Path::Router validations)' );
    foreach my $user ( qw( demo root ) ) {
        req( $test, 400, $user, 'GET', "$base/asas/now" );
    }

    note( 'PUT, POST, DELETE' );
    foreach my $user ( qw( demo root ) ) {
        foreach my $method ( qw( PUT POST DELETE ) ) {
            req( $test, 405, $user, $method, "$base/23/now" );
        }
    }
}


#===========================================
# "{priv,schedule}/history/nick/:nick" resource
#===========================================
foreach $base ( "priv/history/nick", "schedule/history/nick" ) {
    note( "testing $base" );

    note( 'docu_check()' );
    docu_check($test, "$base/:nick");

    note( 'GET' );
    note( 'root employee: fail 403 as demo' );
    req( $test, 403, 'demo', 'GET', "$base/root" );
    note( 'root employee: succeed as root' );
    $status = req( $test, 200, 'root', 'GET', "$base/root" );
    is( $status->level, 'OK' );
    is( $status->code, "DISPATCH_RECORDS_FOUND" );
    ok( defined $status->payload );
    ok( exists $status->payload->{'nick'} );
    is( $status->payload->{'nick'}, 'root' );
    ok( exists $status->payload->{'history'} );
    is( scalar @{ $status->payload->{'history'} }, 1 );
    is( $status->payload->{'history'}->[0]->{'eid'}, 1 );
    ok( exists $status->payload->{'history'}->[0]->{'effective'} );

    note( 'non-existent employee' );
    req( $test, 403, 'demo', 'GET', "$base/rotoroot" );
    req( $test, 404, 'root', 'GET', "$base/rotoroot" );
    
    note( 'PUT' );
    req( $test, 405, 'demo', 'PUT', "$base/asdf" );
    req( $test, 405, 'root', 'PUT', "$base/asdf" );
    
    note( "POST" );
    $j = ( $base =~ m/^priv/ ) 
        ? '{ "effective":"1969-04-27 9:45", "priv":"inactive" }'
        : '{ "effective":"1969-04-27 9:45", "sid":' . $ts_sid . ' }';
    req( $test, 403, 'demo', 'POST', "$base/demo", $j );
    $status = req( $test, 201, 'root', 'POST', "$base/demo", $j );
    if ( $status->not_ok ) {
        diag( $status->code . ' ' . $status->text );
    }
    is( $status->level, 'OK' );
    my $pho = $status->payload;
    my $prop = ( $base =~ m/^priv/ ) ? 'phid' : 'shid';
    push my @history_recs_to_delete, { nick => 'demo', $prop => $pho->{$prop} };
    
    note( 'DELETE' );
    req( $test, 405, 'demo', 'DELETE', "$base/madagascar" );
    req( $test, 405, 'active', 'DELETE', "$base/madagascar" );
    req( $test, 405, 'root', 'DELETE', "$base/madagascar" );
   
    note( 'teardown: we have some records queued for deletion' );
    delete_history_recs( $base, \@history_recs_to_delete );
    @history_recs_to_delete = ();
}


note( '===========================================' );
note( '"{priv,schedule}/history/nick/:nick/:tsrange" resource' );
note( '===========================================' );

foreach $base ( "priv/history/nick", "schedule/history/nick" ) {
    note( "testing $base" );

    note( 'docu_check()' );
    docu_check($test, "$base/:nick/:tsrange");

    note( 'GET' );

    note( 'GET root: fail 403 as demo' ); 
    req( $test, 403, 'demo', 'GET', "$base/root/[1891-12-30, 1892-01-02)" );
    note( 'GET root as root: employee, with tsrange, records found' );
    $status = req( $test, 200, 'root', 'GET', "$base/root/[1891-12-30, 1892-01-02)" );
    is( $status->level, 'OK' );
    is( $status->code, "DISPATCH_RECORDS_FOUND" );
    ok( defined $status->payload );
    ok( exists $status->payload->{'nick'} );
    is( $status->payload->{'nick'}, 'root' );
    ok( exists $status->payload->{'history'} );
    is( scalar @{ $status->payload->{'history'} }, 1 );
    is( $status->payload->{'history'}->[0]->{'eid'}, 1 );
    ok( exists $status->payload->{'history'}->[0]->{'effective'} );

    note( 'non-existent employee' );
    my $tsr = '[1891-12-30, 1892-01-02)';
    req( $test, 403, 'demo', 'GET', "$base/humphreybogart/$tsr" );
    req( $test, 404, 'root', 'GET', "$base/humphreybogart/$tsr" );

    note( 'root employee, with tsrange but no records found' );
    req( $test, 403, 'demo', 'GET', "$base/root/[1999-12-31 23:59, 2000-01-01 00:01)" );
    req( $test, 404, 'root', 'GET', "$base/root/[1999-12-31 23:59, 2000-01-01 00:01)" );
    
    note( 'PUT, POST, DELETE' );
    foreach my $user ( qw( demo root ) ) {
        foreach my $method ( qw( PUT POST DELETE ) ) {
            req( $test, 405, $user, $method, "$base/root/[1999-12-31 23:59, 2000-01-01 00:01)" );
        }
    }
}    



( run in 0.394 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )