App-Dochazka-REST

 view release on metacpan or  search on metacpan

t/ldap.t  view on Meta::CPAN

    note( 'DATA MODEL TESTS' );
    note( '####################################################' );

    note( 'create object for LDAP user' );
    my $ex_obj = App::Dochazka::REST::Model::Employee->spawn(
        'nick' => $ex,
        'sync' => 1,
    );
    my $noex_obj = App::Dochazka::REST::Model::Employee->spawn(
        'nick' => $noex,
        'sync' => 1,
    );
    my $root_obj = App::Dochazka::REST::Model::Employee->spawn(
        'nick' => 'root',
        'sync' => 1,
    );

    note( "System users cannot be synced from LDAP" );
    $emp = $root_obj->clone();
    $status = $emp->ldap_sync();
    ok( $status->not_ok, "Employee sync operation failed" );
    is( $status->code, 'DOCHAZKA_LDAP_SYSTEM_USER_NOSYNC', "and for the right reason" );

    note( "Test that existing LDAP user can be synced" );
    note( "------------------------------------------" );

    note( "1. assert that $ex employee object has non-nick properties unpopulated" );
    $emp = $ex_obj->clone();
    my @props = grep( !/^nick/, keys( %{ $site->DOCHAZKA_LDAP_MAPPING } ) );
    foreach my $prop ( @props ) {
        is( $emp->{$prop}, undef, "$prop property is undef" );
    }

    note( "2. populate $ex employee object from LDAP: succeed" );
    $status = $emp->ldap_sync();
    diag( Dumper $status ) unless $status->ok;
    ok( $status->ok, "Employee sync operation succeeded" );
    is( $status->code, 'DOCHAZKA_LDAP_SYNC_SUCCESS' );

    note( "3. assert that mapped properties now have values - these could only have come from LDAP" );
    foreach my $prop ( @props ) {
        ok( $emp->{$prop}, "$prop property has value " . $emp->{$prop} );
    }

    note( "Test that non-existing LDAP user can *not* be synced" );
    note( "----------------------------------------------------" );

    note( "1. populate $noex employee object from LDAP: fail" );
    $emp = $noex_obj->clone();
    $status = $emp->ldap_sync();
    ok( $status->not_ok );


    note( '####################################################' );
    note( 'DISPATCH TESTS' );
    note( '####################################################' );

    note( "GET employee/nick/$noex/ldap returns 404" );
    req( $test, 404, 'root', 'GET', "employee/nick/$noex/ldap" );

    note( "PUT employee/nick/$noex/ldap returns 404" );
    req( $test, 404, 'root', 'PUT', "employee/nick/$noex/ldap" );

    note( "GET employee/nick/$ex/ldap returns 200" );
    $status = req( $test, 200, 'root', 'GET', "employee/nick/$ex/ldap" );
    is( $status->level, 'OK' );
    ok( $status->payload, "There is a payload" );
    map {
        my $value = $status->payload->{$_};
        ok( $value, "$_ property has value $value" );
    } @props;

    note( 'Ensure that LDAP user does not exist in Dochazka database' );
    $status = App::Dochazka::REST::Model::Employee->load_by_nick( $dbix_conn, $ex ); 
    $emp->delete( $faux_context ) if $status->ok;

    note( 'Assert that LDAP user does not exist in Dochazka database' );
    $status = App::Dochazka::REST::Model::Employee->load_by_nick( $dbix_conn, $ex ); 
    is( $status->level, 'NOTICE' );
    is( $status->code, 'DISPATCH_NO_RECORDS_FOUND', "nick doesn't exist" );
    is( $status->{'count'}, 0, "nick doesn't exist" );
    ok( ! exists $status->{'payload'} );
    ok( ! defined( $status->payload ) );

    note( "PUT employee/nick/$ex/ldap" );
    $status = req( $test, 200, 'root', 'PUT', "employee/nick/$ex/ldap" );
    is( $status->level, 'OK' );
    is( $status->code, 'DOCHAZKA_CUD_OK' );

    note( "Assert that $ex employee exists in Dochazka database" );
    $status = App::Dochazka::REST::Model::Employee->load_by_nick( $dbix_conn, $ex ); 
    is( $status->code, 'DISPATCH_RECORDS_FOUND', "Nick $ex exists" );
    $emp = $status->payload;
    is( $emp->nick, $ex, "Nick is the right string" );

    note( "Assert that mapped properties have values" );
    foreach my $prop ( @props ) {
        ok( $emp->{$prop}, "$prop property has value " . $emp->{$prop} );
    }

    note( "Assert that employee $ex is a passerby" );
    is( $emp->nick, $ex );
    is( $emp->priv( $dbix_conn ), 'passerby' );
    my $eid = $emp->eid;
    ok( $eid > 0 );

    note( "Make $ex an active employee" );
    $status = req( $test, 201, 'root', 'POST', "priv/history/eid/$eid", 
        "{ \"effective\":\"1892-01-01\", \"priv\":\"active\" }" );
    ok( $status->ok, "New privhistory record created for $ex" );
    is( $status->code, 'DOCHAZKA_CUD_OK', "Status code is as expected" );

    note( "Employee $ex is an active" );
    is( $emp->priv( $dbix_conn ), 'active' );

    note( "Depopulate fullname field" );
    my $saved_fullname = $emp->fullname;
    $emp->fullname( undef );
    is( $emp->fullname, undef );
    $status = $emp->update( $faux_context );
    ok( $status->ok );

    note( "Assert that fullname field is empty" );
    $status = req( $test, 200, 'root', 'GET', "employee/nick/$ex" );
    is( $status->level, 'OK' );
    is( $status->payload->{fullname}, undef );

    note( "Set password of employee $ex to \"$ex\"" );
    $status = req( $test, 200, 'root', 'PUT', "employee/nick/$ex", 
        "{\"password\":\"$ex\"}" );
    is( $status->level, 'OK' );

    note( "PUT employee/nick/$ex/ldap" );
    $status = req( $test, 200, $ex, 'PUT', "employee/nick/$ex/ldap" );
    is( $status->level, 'OK' );

    note( "Assert that fullname field is populated as expected" );
    $status = req( $test, 200, 'root', 'GET', "employee/nick/$ex" );
    is( $status->level, 'OK' );
    is( $status->payload->{fullname}, $saved_fullname );

    note( "Cleanup" );
    $status = delete_all_attendance_data();
    BAIL_OUT(0) unless $status->ok;
}

done_testing;



( run in 1.028 second using v1.01-cache-2.11-cpan-39bf76dae61 )