App-Dochazka-REST

 view release on metacpan or  search on metacpan

t/model/employee.t  view on Meta::CPAN

# *************************************************************************
# Copyright (c) 2014-2017, SUSE LLC
#
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# 3. Neither the name of SUSE LLC nor the names of its contributors may be
# used to endorse or promote products derived from this software without
# specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
# *************************************************************************

#!perl
use 5.012;
use strict;
use warnings;

#use App::CELL::Test::LogToFile;
use App::CELL qw( $meta $site );
use Data::Dumper;
use App::Dochazka::REST::ConnBank qw( $dbix_conn );
use App::Dochazka::REST::Model::Employee qw(
    eid_exists
    get_all_sync_employees
    list_employees_by_priv
    nick_exists
);
use App::Dochazka::REST::Model::Shared qw( noof );
use App::Dochazka::REST::Test;
use Test::Fatal;
use Test::More;
use Test::Warnings;

note( 'initialize, connect to database, and set up a testing plan' );
initialize_regression_test();

note( 'get EID of root employee from DOCHAZKA_EID_OF_ROOT site parameter' );
# (root employee is created at dbinit time)
my $eid_of_root = $site->DOCHAZKA_EID_OF_ROOT;
ok( $eid_of_root, "EID of root is not zero or undef" );

note( 'define some helper subroutines' );

sub test_root_accessors {
    my $emp = shift;
    is( $emp->remark, 'dbinit' );
    is( $emp->supervisor, undef );
    is( $emp->sec_id, undef );
    is( $emp->nick, 'root' );
    is( $emp->eid, $eid_of_root );
    is( $emp->priv( $dbix_conn ), 'admin' );
    is( $emp->schedule( $dbix_conn ), undef );
    is( $emp->email, 'root@site.org' );
    is( $emp->fullname, 'Root Immutable' );
}

note( 'get initial number of employees' );
my $noof_employees = noof( $dbix_conn, 'employees' );
is( $noof_employees, 2, 'initial number of employees is 2' );

note( 'get initial employee nicks' );
my $status = list_employees_by_priv( $dbix_conn, 'all' );
test_employee_list( $status, [ 'demo', 'root' ] );

note( 'attempt to spawn an employee with an illegal attribute' );
like( exception { App::Dochazka::REST::Model::Employee->spawn( 'hooligan' => 'sneaking in' ); }, 
      qr/not listed in the validation options: hooligan/ );

note( 'spawn an empty employee object' );
my $emp = App::Dochazka::REST::Model::Employee->spawn;
is( ref( $emp ), 'App::Dochazka::REST::Model::Employee' );
isa_ok( $emp, 'App::Dochazka::REST::Model::Employee' );

note( 'try to reset the object in an illegal manner' );
like( exception { $emp->reset( 'hooligan' => 'sneaking in' ); }, 
      qr/not listed in the validation options: hooligan/ );

note( 'attempt to load a non-existent nick' );
$status = App::Dochazka::REST::Model::Employee->load_by_nick( $dbix_conn, 'mrfu' ); 
is( $status->level, 'NOTICE' );
is( $status->code, 'DISPATCH_NO_RECORDS_FOUND', "Mr. Fu's nick doesn't exist" );
is( $status->{'count'}, 0, "Mr. Fu's nick doesn't exist" );
ok( ! exists $status->{'payload'} );
ok( ! defined( $status->payload ) );

note( 'attempt to load root by nick' );
$status = App::Dochazka::REST::Model::Employee->load_by_nick( $dbix_conn, 'root' ); 
is( $status->level, 'OK' );
is( $status->code, 'DISPATCH_RECORDS_FOUND', "Root employee loaded into object" );
isa_ok( $status->payload, 'App::Dochazka::REST::Model::Employee' );



( run in 1.749 second using v1.01-cache-2.11-cpan-54e63673c56 )