App-Dochazka-REST

 view release on metacpan or  search on metacpan

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

        $status = req( $test, 201, 'root', 'POST', "priv/history/nick/demo", 
            '{ "effective":"1977-04-27 15:30", "priv":"inactive" }' );
        is( $status->level, 'OK' );
        is( $status->code, 'DOCHAZKA_CUD_OK' );
        # we use regex comparison in the next test because the exact string
        # returned depends on the timezone setting
        like( $status->payload->{'effective'}, qr/1977-04-27 15:30:00/ );
        is( $status->payload->{'priv'}, 'inactive' );
        is( $status->payload->{'remark'}, undef );
        is( $status->payload->{'eid'}, 2 );
        ok( $status->payload->{'phid'} );
        $tphid = $status->payload->{'phid'};
        
        note( 'check that demo is really inactive now' );
        $status = req( $test, 200, 'demo', 'GET', "priv/self" );
        is( $status->level, 'OK' );
        is( $status->payload->{'priv'}, "inactive" );
    } else {
        note( 'schedule-specific preparations' );

        note( 'give demo a schedule effective 1977-04-27 15:30' );
        $status = req( $test, 201, 'root', 'POST', 'schedule/history/nick/demo', 
            '{ "effective":"1977-04-27 15:30", "sid":' . $ts_sid . ' }' );
        is( $status->level, 'OK' );
        is( $status->code, 'DOCHAZKA_CUD_OK' );
        $tphid = $status->payload->{'shid'};
    }
    
    note( 'GET' );

    note( "GET $base/$tphid as root" );
    $status = req( $test, 200, 'root', 'GET', "$base/$tphid" );
    is( $status->level, 'OK' );
    is( $status->code, 'DISPATCH_HISTORY_RECORD_FOUND' );
    is( $status->payload->{'remark'}, undef );
    is( $status->payload->{ ( ( $base =~ m/^priv/ ) ? 'priv' : 'sid' ) },
        ( ( $base =~ m/^priv/ ) ? 'inactive' : $ts_sid ) );
    is( $status->payload->{'eid'}, 2 );
    is( $status->payload->{$prop}, $tphid );
    like( $status->payload->{'effective'}, qr/1977-04-27 15:30:00/ );
    
    note( 'PUT' );

    note( "PUT operations on $base/$tphid will fail with 405" );
    foreach my $user ( qw( demo root ) ) {
        req( $test, 405, $user, 'PUT', "$base/$tphid" );
    }
    
    note( 'POST' );

    note( "Update the history record inserted above" );
    $status = req( $test, 200, 'root', 'POST', "$base/$tphid", <<"EOS" );
{ "remark" : "I am foo!" }
EOS
    is( $status->level, 'OK' );
    is( $status->code, 'DOCHAZKA_CUD_OK' );
    ok( defined $status->payload );
    ok( exists $status->payload->{'remark'} );
    is( $status->payload->{'remark'}, 'I am foo!' );

    note( "Get the updated history record and check it just to be sure" );
    $status = req( $test, 200, 'root', 'GET', "$base/$tphid" );
    ok( $status->ok );
    is( $status->payload->{'remark'}, 'I am foo!' );

    note( 'DELETE' );

    note( 'delete the privhistory record we created earlier' );
    $status = req( $test, 200, 'root', 'DELETE', "$base/$tphid" );
    is( $status->level, "OK" );
    is( $status->code, 'DOCHAZKA_CUD_OK' );
    
    note( 'not there anymore' );
    req( $test, 404, 'root', 'GET', "$base/$tphid" );
    
    note( 'and demo is a passerby again' );
    $status = req( $test, 200, 'demo', 'GET', "priv/self" );
    is( $status->level, 'OK' );
    is( $status->payload->{'priv'}, "passerby" );
} 

note( 'tear down' );
$status = delete_all_attendance_data();
BAIL_OUT(0) unless $status->ok;

done_testing;



( run in 0.349 second using v1.01-cache-2.11-cpan-63c85eba8c4 )