App-Dochazka-CLI

 view release on metacpan or  search on metacpan

Changes  view on Meta::CPAN

- fix bug "'get employee NON-EXISTENT-NICK' should produce an error message;
  instead, it says 'undef'"

0.006  2014-08-10 18:37 CEST
- Build.PL: add dependency HTTP::Cookies
- bin/dochazka-cli: use App::Dochazka::Model::Employee instead of inheriting from it
- HTTP.pm: construct LWP::UserAgent object with cookie_jar
- config/CLI_Config.pm: make a proper _Config.pm file for the application

0.007  2014-08-10 20:12 CEST
- bin/dochazka-cli: prompt for nick/password if not given in site configuration
- HTTP.pm: add 'init_ua' and 'cookie_jar' functions, use nick/password
  prompted from user if not given in site config
- Parser.pm: add GET COO[KIES]
- Build.PL: update dependencies

0.008  2014-08-10 22:51 CEST
- fix bug "bin/dochazka-cli throws 'Odd number of arguments' error at startup"

0.009  2014-08-10 22:56 CEST
- Parser.pm: add GET SES[SION]

0.010  2014-08-11 16:15 CEST
- goodbye 'Term::ReadLine::Perl5' (depends on 'rlib' which cannot be
  packaged due to licensing issues), hello 'Term::ReadLine::Gnu'

0.011  2014-10-17 23:01 CEST
- Build.PL: add Getopt::Long dependency 
- get user and password from command-line options instead of from site
  configuration

0.012  2014-10-18 10:06 CEST
- process command-line options: 'help', 'user', 'password'
- add dependencies: Getopt::Long, Pod::Usage
- log to $HOME/.dochazka-cli.log by default, user can override in /etc/dochazka-cli
- prompt for password if none specified on command line

0.013  2014-10-19 07:55 CEST
- CLI.pm: document commands and their syntax
- Parser.pm: work on top-level GET commands

0.014  2014-10-19 08:44 CEST
- move command POD to Parser.pm where it belongs
- Parser.pm: add a couple more top-level GET commands

0.015  2014-10-19 13:35 CEST

Changes  view on Meta::CPAN


0.151  2015-04-07 17:08 CEST
- Guide.pm: start writing proper CLI documentation
- CLI.pm: move verbiage to Guide and link to it
- Parser.pm: fix RT#102671 (Trailing '0' was throwing the parser)
  https://rt.cpan.org/Ticket/Display.html?id=102671
- cleanup

0.152  2015-04-07 18:39 CEST
- TokenMap.pm: add PASSWORD token and distinguish it from PASSERBY
- CommandMap.pm: add some synonyms; add password-reset commands 'EMPLOYEE
  PASSWORD' and 'EMPLOYEE_SPEC PASSWORD'
- Commands/Employee.pm: add dummy handlers for the above commands
- Guide.pm: document some commands

0.153  2015-04-07 22:08 CEST
- Commands/Employee.pm: implement password change handlers

0.154  2015-04-09 10:13 CEST
- Build.PL: acknowledge Web::MREST dependency
- Guide.pm: document more commands/workflows

0.155  2015-04-09 10:20 CEST
- change all instances of 'App::Dochazka::REST::Model' to 'App::Dochazka::Model'

0.156  2015-04-09 10:27 CEST
- RestTest/Top.pm: fix dependency

Changes  view on Meta::CPAN

0.169  2015-04-14 15:39 CEST
- t/cmd_active/001-init.t: new unit that creates a schedule history record for
  employee "worker", so he can insert attendance intervals
- Commands/History.pm: SCHEDULE_SPEC can be either scode or sid, but we forgot
  to convert to lowercase => fix
- Util.pm: truncate_to was generating a warning => fix
- t/cmd_admin/history.t: add schedule history test case

0.170  2015-04-15 09:36 CEST
- Guide.pm: add section on creating new employees
- Commands/Employee.pm: allow new password to be specified on the command line
  and be more verbose when asking employee to repeat it

0.171  2015-04-15 10:15 CEST
- make history commands take _DATE only (instead of deprecated _DATESTAMP)

0.172  2015-04-15 10:30 CEST
- get rid of _DATESTAMP completely, replace it with _TIMESTAMP
- replace the old _TIMESTAMP with _TIMESTAMPDEPR

0.173  2015-04-15 20:08 CEST

Changes  view on Meta::CPAN

  authenticate_to_server(), because it might result in "plan skip_all"

0.193  2015-07-01 17:13 CEST
- implement REST test and normal commands for 'employee/team',
  'employee/eid/:eid/team' and 'employee/nick/:nick/team' resources

0.194  2015-07-01 22:39 CEST
- implement "EMPLOYEE TEAM" and "EMPLOYEE_SPEC TEAM" commands

0.195  2015-07-04 19:43 CEST
- bin/dochazka-cli: honor sitedir option; prompt for password

0.196  2015-07-04 22:12 CEST
- bin/dochazka-cli: use init_prompt() instead of init_timepiece()
- bin/dochazka-cli: initialize prompt date every time prompt is displayed
- Util.pm: import init_timepiece() from App::Dochazka
- Util.pm: implement init_prompt()
- Commands/Misc.pm: use init_prompt() instead of init_timepiece()

0.197  2015-07-05 14:19 CEST
- bin/dochazka-cli: $sitedir fall back to /etc/dochazka-cli

Changes  view on Meta::CPAN


0.198  2015-07-21 16:13 CEST
- Define $VERSION only in CLI.pm and Guide.pm
- Rename App::Dochazka::Common and update copyright statement

0.199  2015-07-22 14:51 CEST
- Convert MREST_CLI_URI_BASE to a meta param
- bin/dochazka-cli
    - get home directory using File::HomeDir
    - add --version option and take optional URL as argument
    - prompt for username and password if not supplied

0.200  2015-07-23 08:54 CEST
- Util.pm: modify lookup_employee() to take hash of properties ...
- t/: make sure config is loaded in test suite
- Util.pm and t/: Rename App::Dochazka to App::Dochazka::Common
- Build.PL: rename App::Dochazka to App::Dochazka::Common
- bin/dochazka-cli: do not display password as it is typed and ignore password
  stored in config if username not taken from config
- Do not generate Makefile.PL anymore

0.201  2015-07-23 18:47 CEST
- t/: adapt to current state
- Util.pm: use GET employee/.../minimal in determine_employee()
- TokenMap.pm: add LDAP token
- Commands/RestTest/Employee.pm: implement employee/nick/:nick/ldap resource
- Commands/Employee.pm: implement employee_ldap()
- CommandMap.pm: enable LDAP commands

Changes  view on Meta::CPAN


0.207  2015-07-27 14:08 CEST
- Build.PL: change Web::MREST dependency to Web::MREST::CLI

0.208  2015-07-27 14:43 CEST
- Util.pm: import normalize_filespec() from Web::MREST::CLI::UserAgent

0.209  2015-07-27 21:34 CEST
- Util.pm: make CRIT log message more reasonable
- Commands/Employee.pm: use Term::ReadKey noecho mode when user types his
  password
- Commands/Employee.pm: fix a regression in lookup_employee()

0.210  2015-07-28 12:32 CEST
- Improve error message when user enters attempts to enter a new interval with
  a non-existent activity code
  Fixes: https://github.com/smithfarm/dochazka-cli/issues/10

0.211  2015-07-30 15:23 CEST
- Util.pm: drop init_cli_client() in favor of the one from Web::MREST::CLI
- bin/dochazka-cli: enable --early-debug option

bin/dochazka-cli  view on Meta::CPAN

    Authenticating to server at http://localhost:5000
    Username: 
    Server is alive
    Dochazka(2015-03-11) root ADMIN>

This is the Dochazka command-line interface (CLI). Options:

    --debug     -d      Make parser display debug messages
    --help      -h      Get help
    --noauth    -n      Omit authentication
    --password  -p      Specify password
    --sitedir   -s      Specify sitedir
    --user      -u      Specify username
    --version   -v      Display App::Dochazka::CLI version number

For more information, see L<http://metacpan.org/pod/App::Dochazka::CLI>.


=head1 DESCRIPTION

This script is used to start the Dochazka command line interface, hereinafter

bin/dochazka-cli  view on Meta::CPAN

}


# -------------------------------------------------------------------------
# main
# -------------------------------------------------------------------------

# process command-line options
my $help = 0;
my $user = '';
my $password = '';
my $sitedir;
my $early_debug;
GetOptions( 
    'help|?' => \$help, 
    'user|u=s' => \$user, 
    'password|p=s' => \$password,
    'sitedir|s=s' => \$sitedir,
    'noauth|n' => \$noauth,
    'debug|d' => \$debug_mode,
    'early-debug|e=s' => \$early_debug,
    'version|v' => \$versionparam,
);

pod2usage(1) if $help;

print "Debug mode: parser state dumps will be generated\n" if $debug_mode;

bin/dochazka-cli  view on Meta::CPAN

    }
}

$meta->set( 'MREST_CLI_URI_BASE', $server );

# authenticate unless --noauth given
if ( ! $noauth ) {
    if ( ! $user ) {
        if ( $user = $site->DOCHAZKA_REST_LOGIN_NICK ) {
            print "Username $user set from site configuration\n";
            if ( $password = $site->DOCHAZKA_REST_LOGIN_PASSWORD ) {
                print "Password set from site configuration\n";
            }
        } else {
            print "Username: ";
            chomp( $user = <STDIN> );
        }
    }
    print "Authenticating to server at $server as user $user\n";
    if ( ! $password ) {
        ReadMode ('noecho');
        print "Password: ";
        chomp( $password = <STDIN> );
        ReadMode ('restore');
        print "\n";
    }
    my $status = authenticate_to_server( user => $user, password => $password );
    if ( $status->not_ok ) {

        # Handle two classic scenarios:
        # 1. server not running
        if ( $status->payload =~ m/Connection refused/ ) {
            print "Server refused connection - is it running?\n";
            exit;
        }
        # 2. authentication failed
        if ( $status->text =~ m/Authentication failed/ ) {

config/CLI_Config.pm  view on Meta::CPAN

#
# Main configuration file
# -----------------------------------

# DOCHAZKA_REST_LOGIN_NICK
#     nick to use when we authenticate ourselves to the App::Dochazka::REST server
#     when this is set to '' or undef, App::Dochazka::CLI will prompt for it
set( 'DOCHAZKA_REST_LOGIN_NICK', undef );

# DOCHAZKA_REST_LOGIN_NICK
#     password to use when we authenticate ourselves to the App::Dochazka::REST server
#     when this is set to '' or undef, App::Dochazka::CLI will prompt for a password
#     WARNING: PUTTING YOUR PASSWORD HERE MAY BE CONVENIENT, BUT IS PROBABLY UNSAFE
set( 'DOCHAZKA_REST_LOGIN_PASSWORD', undef );

# MREST_CLI_COOKIE_JAR
#     default location of the cookie jar
set( 'MREST_CLI_COOKIE_JAR', "$ENV{HOME}/.cookies.txt" );

# DOCHAZKA_CLI_LOG_FILE
#     default location of the log file
set( 'DOCHAZKA_CLI_LOG_FILE', "$ENV{HOME}/.dochazka-cli.log" );

ext/CLI_SiteConfig.pm.example  view on Meta::CPAN

#
# To override a default, uncomment the "set" call and change the value. 
#

# DOCHAZKA_REST_LOGIN_NICK
#     nick to use when we authenticate ourselves to the App::Dochazka::REST server
#     when this is set to '' or undef, App::Dochazka::CLI will prompt for it
#set( 'DOCHAZKA_REST_LOGIN_NICK', undef );

# DOCHAZKA_REST_LOGIN_NICK
#     password to use when we authenticate ourselves to the App::Dochazka::REST server
#     when this is set to '' or undef, App::Dochazka::CLI will prompt for a password
#     WARNING: PUTTING YOUR PASSWORD HERE MAY BE CONVENIENT, BUT IS PROBABLY UNSAFE
#set( 'DOCHAZKA_REST_LOGIN_PASSWORD', undef );

# MREST_CLI_COOKIE_JAR
#     default location of the cookie jar
#set( 'MREST_CLI_COOKIE_JAR', "$ENV{HOME}/.cookies.txt" );

# DOCHAZKA_CLI_LOG_FILE
#     default location of the log file
#set( 'DOCHAZKA_CLI_LOG_FILE', "$ENV{HOME}/.dochazka-cli.log" );

lib/App/Dochazka/CLI/CommandMap.pm  view on Meta::CPAN

use App::Dochazka::CLI::Commands::Employee qw( 
    employee_ldap
    employee_ldap_import
    employee_list
    employee_profile
    employee_team
    set_employee_self_sec_id 
    set_employee_other_sec_id 
    set_employee_self_fullname
    set_employee_other_fullname 
    set_employee_self_password
    set_employee_other_password 
    set_employee_supervisor
);
use App::Dochazka::CLI::Commands::History qw(
    add_priv_history
    add_schedule_history
    dump_priv_history
    dump_schedule_history
    set_history_remark
);
use App::Dochazka::CLI::Commands::Interval qw(

lib/App/Dochazka/CLI/CommandMap.pm  view on Meta::CPAN

    "EMPLOYEE SEC_ID _TERM" => \&set_employee_self_sec_id,
    "EMPLOYEE SET SEC_ID _TERM" => \&set_employee_self_sec_id,
    "EMPLOYEE FULLNAME" => \&set_employee_self_fullname,
    "EMPLOYEE SET FULLNAME" => \&set_employee_self_fullname,
    "EMPLOYEE_SPEC SEC_ID _TERM" => \&set_employee_other_sec_id,
    "EMPLOYEE_SPEC SET SEC_ID _TERM" => \&set_employee_other_sec_id,
    "EMPLOYEE_SPEC FULLNAME" => \&set_employee_other_fullname,
    "EMPLOYEE_SPEC SET FULLNAME" => \&set_employee_other_fullname,
    "EMPLOYEE_SPEC SUPERVISOR _TERM" => \&set_employee_supervisor,
    "EMPLOYEE_SPEC SET SUPERVISOR _TERM" => \&set_employee_supervisor,
    "EMPLOYEE PASSWORD" => \&set_employee_self_password,
    "EMPLOYEE SET PASSWORD" => \&set_employee_self_password,
    "EMPLOYEE_SPEC PASSWORD" => \&set_employee_other_password,
    "EMPLOYEE_SPEC SET PASSWORD" => \&set_employee_other_password,
    "EMPLOYEE TEAM" => \&employee_team,
    "EMPLOYEE_SPEC TEAM" => \&employee_team,

    # History commands
    "PRIV HISTORY" => \&dump_priv_history,
    "EMPLOYEE_SPEC PRIV HISTORY" => \&dump_priv_history,
    "SCHEDULE HISTORY" => \&dump_schedule_history,
    "EMPLOYEE_SPEC SCHEDULE HISTORY" => \&dump_schedule_history,
    "EMPLOYEE_SPEC PRIV_SPEC _DATE" => \&add_priv_history,
    "EMPLOYEE_SPEC PRIV_SPEC EFFECTIVE _DATE" => \&add_priv_history,

lib/App/Dochazka/CLI/Commands/Employee.pm  view on Meta::CPAN

our @EXPORT_OK = qw( 
    employee_ldap
    employee_ldap_import
    employee_list
    employee_profile
    employee_team
    set_employee_self_sec_id 
    set_employee_other_sec_id
    set_employee_self_fullname 
    set_employee_other_fullname
    set_employee_self_password 
    set_employee_other_password
    set_employee_supervisor
);


=head1 FUNCTIONS

=head2 Command handlers

=head3 employee_profile

lib/App/Dochazka/CLI/Commands/Employee.pm  view on Meta::CPAN

    return parse_test( $ts, $th ) if $ts eq 'PARSE_TEST';

    return _set_employee( 
        emp_spec => $th->{'EMPLOYEE_SPEC'},
        prop => 'fullname', 
        val => $th->{'_REST'},
    );
}


=head3 set_employee_self_password

Reset one's own password

    EMPLOYEE PASSWORD
    EMPLOYEE SET PASSWORD

=cut

sub set_employee_self_password {
    my ( $ts, $th ) = @_;

    # parse test
    return parse_test( $ts, $th ) if $ts eq 'PARSE_TEST';

    return _set_password( 
        eid => $current_emp->eid,
        password => $th->{'_REST'},
    ); 
}


=head3 set_employee_other_password

Reset password of an arbitrary employee

    EMPLOYEE_SPEC PASSWORD
    EMPLOYEE_SPEC SET PASSWORD

=cut

sub set_employee_other_password {
    print "Entering " . __PACKAGE__ . "::set_employee_other_password\n" if $debug_mode;
    my ( $ts, $th ) = @_;

    # parse test
    return parse_test( $ts, $th ) if $ts eq 'PARSE_TEST';

    my $status = determine_employee( $th->{EMPLOYEE_SPEC} );
    return $status unless $status->ok;
    my $emp = $status->payload;

    return _set_password( 
        eid => $emp->eid,
        password => $th->{'_REST'},
    ); 
}


=head3 set_employee_supervisor

Set supervisor of an arbitrary employee

    EMPLOYEE_SPEC SUPERVISOR _TERM
    EMPLOYEE_SPEC SET SUPERVISOR _TERM

lib/App/Dochazka/CLI/Commands/Employee.pm  view on Meta::CPAN

EOS
    return rest_error( $status, "Modify employee profile" ) unless $status->ok;

    my $message = "Profile of employee " . $emp_obj->nick . 
        " has been modified ($prop -> $val)\n";

    return $CELL->status_ok( 'DOCHAZKA_CLI_NORMAL_COMPLETION', payload => $message );
}


=head3 _set_password

Takes PARAMHASH with following properties:

     eid => EID of employee
     password => the new password (*optional*)

=cut

sub _set_password {
    my %PH = @_;
    my $eid = $PH{'eid'};
    my $newpass = $PH{'password'};

    print "It is important that the new password really be what you intended.\n";
    print "Therefore, we are going to ask you to enter the desired password\n";
    print "twice, so you have a chance to double-check. ";
    print "\n\n";

    # prompt for new password and ask nicely for confirmation
    if ( ! $newpass ) {
        ReadMode ('noecho');
        print "New password      : ";
        chomp( $newpass = <> );
        ReadMode ('restore');
        print "\n";
    }
    ReadMode ('noecho');
    print "New password again: ";
    chomp( my $confirm = <> );
    ReadMode ('restore');
    print "\n";
    return $CELL->status_err( 'DOCHAZKA_CLI_NO_MATCH' ) unless $newpass eq $confirm;

    # send REST request
    my $status = send_req( 'PUT', "employee/eid/$eid", <<"EOS" );
{ "password" : "$newpass" }
EOS

    return $status unless $status->ok;
    return $CELL->status_ok( 'DOCHAZKA_CLI_NORMAL_COMPLETION', 
        payload => "Password changed" );
}


=head3 _display_employee_ok

lib/App/Dochazka/CLI/Guide.pm  view on Meta::CPAN

automatically the first time they log in. Alternatively, employees can be
created manually:

    PUT employee nick $NICK { "fullname" : "King George III" }

In addition to "fullname", the following properties can be specified inside
the curly brackets:

    sec_id
    email
    password
    remark

For example, to add an employee with nick 'nancy' the command might look like
this:

    PUT employee nick nancy { "fullname" : "Nancy Bright Red", "email" : "nancy@identity.org" }
   
The new employee will have passerby privileges only until a privhistory record
is created (see L<"Add privilege history record">, below).

lib/App/Dochazka/CLI/Guide.pm  view on Meta::CPAN

    EMPLOYEE_SPEC SET FULLNAME $WHATEVER

Examples:

    EMPLOYEE=baird FULLNAME Joseph Baird
    EMPLOYEE=baird FULLNAME JUDr. Joseph Peacock Baird LLM, Esq.

Do not use any quotes in or around the full name.


=head3 Set employee password

To manually change an employee's password, do:

    EMPLOYEE_SPEC PASSWORD
    EMPLOYEE_SPEC SET PASSWORD

You will be asked to type in the new password twice. This updates the password
that Dochazka stores in the 'employees' table. Whether this has any effect on
the user's ability to log in depends on what authentication method is being
used and where the passwords for that method are stored.


=head2 Schedules

=head3 View all schedules (including disabled schedules)

All existing schedules can be dumped to the screen:

    SCHEDULES FETCH ALL

lib/App/Dochazka/CLI/Guide.pm  view on Meta::CPAN

=head3 View own employee profile

Each Dochazka employee has a "profile" with some basic information. One's own
profile can be viewed by doing any of the following:

    EMPLOYEE
    EMPLOYEE PROFILE
    EMPLOYEE SHOW


=head3 Set own password

To manually change your password, do:

    EMPLOYEE PASSWORD
    EMPLOYEE SET PASSWORD

You will be asked to type in the new password twice. If you still can't log in
after doing this, contact your local Dochazka administrator.


=head2 Privilege levels

=head3 View one's own privilege level

Although C<EMPLOYEE PROFILE> shows the employee's privilege level, there
is also a dedicated command:

lib/App/Dochazka/CLI/Testers.pm  view on Meta::CPAN

    sh test-drive.sh

If you have never run Docker containers before, you may be surprised that
the script downloads quite a lot of data from Docker Hub. The script should
complete without any error messages.

=item Web browser test

When the C<test-drive.sh> script completes, you should be able to access
the REST server by pointing your browser at L<http://localhost:5000>. At
the login dialog, enter username "demo" and password "demo".

=back


=head3 The traditional way

Alternatively, if you don't like Docker or can't use it for some reason,
you can install and set up PostgreSQL and the Dochazka REST server
yourself.

lib/App/Dochazka/CLI/Testers.pm  view on Meta::CPAN

    Setting up logging
    Logging to /home/smithfarm/mrest.log
    Calling App::Dochazka::REST::Dispatch::init()
    Starting server
    HTTP::Server::PSGI: Accepting connections at http://0:5000/

=item Web browser test

After completing the above, you should be able to access the REST server by
pointing your browser at L<http://localhost:5000>. At the login dialog,
enter username "demo" and password "demo".

=back


=head2 Prereq 3: Install Dochazka CLI client

Now that the server part is working, install the CLI:

    zypper install perl-App-Dochazka-CLI

You should now be able to start the CLI and login as "demo" with password
"demo":

    $ dochazka-cli -u demo -p demo
    Loading configuration files from
    /usr/lib/perl5/vendor_perl/5.18.2/auto/share/dist/App-Dochazka-CLI
    Cookie jar: /root/.cookies.txt
    URI base http://localhost:5000 set from site configuration
    Authenticating to server at http://localhost:5000 as user root
    Server is alive
    Dochazka(2016-01-12) demo PASSERBY>

lib/App/Dochazka/CLI/Testers.pm  view on Meta::CPAN


Here, the error is the same C<403 Forbidden> but the output is more detailed.
This is because we used a special type of command that is ordinarily only
used to test the REST API.

=head2 Log in as root

For the rest of this session, we will be logged in as the C<root> employee, 
which has a special status in that it is created when the database is
initialized and it is difficult or impossible to delete. In a freshly
initialized database, the C<root> employee's password is "immutable".

The username and password need not be specified on the command line.
Try it this way:

    $ dochazka-cli
    Loading configuration files from /usr/lib/perl5/vendor_perl/5.18.2/auto/share/dist/App-Dochazka-CLI
    Cookie jar: /root/.cookies.txt
    URI base http://localhost:5000 set from site configuration
    Username: root
    Authenticating to server at http://localhost:5000 as user root
    Password: 
    Server is alive

lib/App/Dochazka/CLI/Testers.pm  view on Meta::CPAN

Now, exit the CLI and run it again as C<george>, our new employee.

    $ dochazka-cli 
    ...
    Username: george
    Authenticating to server at http://localhost:5000 as user george
    Password: 
    MREST_CLI_UNAUTHORIZED (ERR) MREST_CLI_UNAUTHORIZED
    Response: '401 Unauthorized'

This happens because there is no password set for C<george>.


=head2 Assign george a password

Fortunately, Dochazka admins can assign any password to any user. This
capability may or may not be useful, depending on whether LDAP
authentication is active at the site. In our current testing scenario, LDAP
authentication is disabled, so the password is taken from the Dochazka
database. So, let's give george a password:

    $ dochazka-cli -u root -p immutable
    ...
    Dochazka(2016-01-27) root ADMIN> emp=george password
    It is important that the new password really be what you intended.
    Therefore, we are going to ask you to enter the desired password
    twice, so you have a chance to double-check. 

    New password      : <type george>
    New password again: <type george again>
    Password changed

Now you can log in with credentials C<george/george>:

    $ dochazka-cli -u george -p george
    ...
    Authenticating to server at http://localhost:5000 as user george
    Server is alive
    Dochazka(2016-01-27) george PASSERBY>

lib/App/Dochazka/CLI/Testers.pm  view on Meta::CPAN

=back


=head2 George the passerby

The current privilege level is determined by consulting the employee's
privilege history, which is a database table containing records for each
change in the employee's status. Employee status changes, for example, when
the employee is hired, leaves the company, goes on parental leave, etc.

Now, our test employee "george" has a password and can log in. However, he
has no privilege history so his priv level defaults to "passerby" - the
lowest possible level.

In this section, we see that passers-by cannot do much at all in Dochazka:

    $ dochazka-cli -u george -p george
    Dochazka(2016-01-29) george PASSERBY> emp=root profile
    *** REST ERROR ***

    Error encountered on attempted operation "Employee lookup"

lib/App/Dochazka/CLI/Testers.pm  view on Meta::CPAN

    Dochazka(2016-01-30) george ACTIVE> scode=VPP-1 fetch
    ...
    Dochazka(2016-01-30) george ACTIVE> sid=1 fetch
    ...


=head2 List schedules as passerby

These commands use C<GET schedule/...> REST operations whose ACL profile is
"inactive". This can be verified by logging in as the employee C<demo>
(password C<demo>):

    $ dochazka-cli -u demo -p demo
    Dochazka(2016-01-30) demo PASSERBY> schedule fetch all
    *** Anomaly detected ***
    Status:      403 Forbidden
    Explanation: ACL check failed for resource schedule/all (ERR)

    Dochazka(2016-01-30) demo PASSERBY> scode=VPP-1 fetch
    *** Anomaly detected ***
    Status:      403 Forbidden

lib/App/Dochazka/CLI/TokenMap.pm  view on Meta::CPAN

    meta => 'META',
    monday => '_DOW',
    new => 'NEW',
    nick => 'NICK',
    'nick=' => 'EMPLOYEE_SPEC',
    noop => 'NOOP',
    november => '_MONTH',
    october => '_MONTH',
    param => 'PARAM',
    passerby => 'PRIV_SPEC',
    password => 'PASSWORD',
    path => 'PATH',
    phid => 'PHID',
    'phid=' => 'PHISTORY_SPEC',
    pod => 'POD',
    post => 'POST',
    priv => 'PRIV',
    profile => 'PROFILE',
    prompt => 'PROMPT',
    put => 'PUT',
    quit => 'QUIT',

lib/App/Dochazka/CLI/Util.pm  view on Meta::CPAN



=head1 FUNCTIONS


=head2 authenticate_to_server

All communication between L<App::Dochazka::CLI> and the L<App::Dochazka::REST>
server goes via the C<send_req> routine in L<Web::MREST::CLI>. This
routine takes its connection parameters (address of REST server, nick and
password) from the following configuration parameters:

    $meta->MREST_CLI_URI_BASE
    $meta->CURRENT_EMPLOYEE_NICK
    $meta->CURRENT_EMPLOYEE_PASSWORD

The first parameter, C<MREST_CLI_URI_BASE>, is assumed to be set before this
routine is called. The second and third are meta parameters and are set by
this routine.

After setting the meta parameters, the routine causes a GET request for the

lib/App/Dochazka/CLI/Util.pm  view on Meta::CPAN

imported from the L<App::Dochazka::CLI> package.

Takes PROPLIST with two properties:

=over

=item C<< user >>

The username to authenticate as (defaults to 'demo')

=item C<< password >>

The password to use (defaults to the value of the C<user> parameter)

=back

Since this routine returns the status object returned by the "GET
employee/self/full" request, it is actually a wrapper around C<send_req>.

=cut

sub authenticate_to_server {
    my %PROPLIST = ( 
        user => 'demo',
        @_,
    );
    $PROPLIST{'password'} = $PROPLIST{'password'} || $PROPLIST{'user'};

    $meta->set( 'CURRENT_EMPLOYEE_NICK', $PROPLIST{'user'} );
    $meta->set( 'CURRENT_EMPLOYEE_PASSWORD', $PROPLIST{'password'} );

    # get info about us
    my $status;
    try {
        $status = send_req( 'GET', '/employee/self/full' );
    } catch {
        $status = $_;
    };
    if ( !ref( $status ) ) {
        die "AGHAUFF! $status\n";

t/001-init.t  view on Meta::CPAN

sub create_employees_carefully {
    my ( $nick, $privlevel, $fullname ) = @_;
    my ( $cmd, $rv, $status );

    $cmd = "EMPLOYEE=$nick PROFILE";
    $rv = process_command( $cmd );
    is( ref( $rv ), 'App::CELL::Status' );
    unless ( $rv->level eq 'OK' and $rv->code eq 'DOCHAZKA_CLI_NORMAL_COMPLETION' ) {

        # create employee $nick and assign privlevel $privlevel
        $cmd = "PUT employee nick $nick { \"fullname\" : \"$fullname\", \"password\" : \"$nick\" }";
        $rv = process_command( $cmd );
        is( ref( $rv ), 'App::CELL::Status' );
        is( $rv->{'http_status'}, '200 OK' );
        is( $rv->code, 'DOCHAZKA_CUD_OK' );

        $cmd = "POST priv history nick $nick { \"priv\" : \"$privlevel\", \"effective\" : \"2000-01-01 00:00\" }";
        $rv = process_command( $cmd );
        is( ref( $rv ), 'App::CELL::Status' );
        is( $rv->{'http_status'}, '201 Created' );
        is( $rv->code, 'DOCHAZKA_CUD_OK' );

t/001-init.t  view on Meta::CPAN

my ( $cmd, $rv, $rv_type, $status );

$rv = init_unit();
$rv_type = ref( $rv );
if ( $rv_type ne 'App::CELL::Status' or $rv->not_ok ) {
    diag "init_unit returned unexpected status:";
    diag( Dumper $rv );
    BAIL_OUT(0);
}

$rv = authenticate_to_server( user => 'root', password => 'immutable', quiet => 1 );
$rv_type = ref( $rv );
if ( $rv_type ne 'App::CELL::Status' or $rv->not_ok ) {
    if ( $rv->{'http_status'} =~ m/500 Can\'t connect/ ) {
        plan skip_all => "Can't connect to server";
    } else {
        diag "authenticate_to_server returned unexpected status:";
        diag( Dumper $rv );
        BAIL_OUT(0);
    }
}

t/cmd_active/activity.t  view on Meta::CPAN

use Test::More;
use Test::Warnings;

my ( $cmd, $rv );

note( 'initialize unit' );
$rv = init_unit();
plan skip_all => "init_unit failed with status " . $rv->text unless $rv->ok;

note( 'authenticate to server' );
$rv = authenticate_to_server( user => 'worker', password => 'worker', quiet => 1 );
if ( $rv->not_ok and $rv->{'http_status'} =~ m/500 Can\'t connect/ ) {
    plan skip_all => "Can't connect to server";
}

isnt( $meta->MREST_CLI_URI_BASE, undef, 'MREST_CLI_URI_BASE is defined after initialization' );

$cmd = "ACTIVITY";
$rv = process_command( $cmd );
is( ref( $rv ), 'App::CELL::Status' );
is( $rv->level, 'OK' );

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

use App::Dochazka::CLI::Util qw( authenticate_to_server );
use Data::Dumper;
use Test::More;
use Test::Warnings;

my ( $cmd, $rv );

$rv = init_unit();
plan skip_all => "init_unit failed with status " . $rv->text unless $rv->ok;

$rv = authenticate_to_server( user => 'worker', password => 'worker', quiet => 1 );
if ( $rv->not_ok and $rv->{'http_status'} =~ m/500 Can\'t connect/ ) {
    plan skip_all => "Can't connect to server";
}

isnt( $meta->MREST_CLI_URI_BASE, undef, 'MREST_CLI_URI_BASE is defined after initialization' );

$cmd = "EMPLOYEE";
$rv = process_command( $cmd );
ok( ref( $rv ) eq 'App::CELL::Status' );
is( $rv->level, 'OK' );

t/cmd_active/interval.t  view on Meta::CPAN

use App::Dochazka::CLI::Util qw( authenticate_to_server );
use Data::Dumper;
use Test::More;
use Test::Warnings;

my ( $cmd, $rv );

$rv = init_unit();
plan skip_all => "init_unit failed with status " . $rv->text unless $rv->ok;

$rv = authenticate_to_server( user => 'worker', password => 'worker', quiet => 1 );
if ( $rv->not_ok and $rv->{'http_status'} =~ m/500 Can\'t connect/ ) {
    plan skip_all => "Can't connect to server";
}

isnt( $meta->MREST_CLI_URI_BASE, undef, 'MREST_CLI_URI_BASE is defined after initialization' );

note( 'INTERVAL displays today\'s intervals (but there are none)' );
$cmd = "INTERVAL";
$rv = process_command( $cmd );
is( ref( $rv ), 'App::CELL::Status' );

t/cmd_admin/activity.t  view on Meta::CPAN

use App::Dochazka::CLI::Util qw( authenticate_to_server );
use Data::Dumper;
use Test::More;
use Test::Warnings;

my ( $cmd, $rv );

$rv = init_unit();
plan skip_all => "init_unit failed with status " . $rv->text unless $rv->ok;

$rv = authenticate_to_server( user => 'root', password => 'immutable', quiet => 1 );
if ( $rv->not_ok and $rv->{'http_status'} =~ m/500 Can\'t connect/ ) {
    plan skip_all => "Can't connect to server";
}

isnt( $meta->MREST_CLI_URI_BASE, undef, 'MREST_CLI_URI_BASE is defined after initialization' );

$cmd = "ACTIVITY ALL";
$rv = process_command( $cmd );
is( ref( $rv ), 'App::CELL::Status' );
is( $rv->level, 'OK' );

t/cmd_admin/employee-utf8.t  view on Meta::CPAN

use Test::More;
use Test::Warnings;

$debug_mode = 1;

my ( $cmd, $rv );

$rv = init_unit();
plan skip_all => "init_unit failed with status " . $rv->text unless $rv->ok;

$rv = authenticate_to_server( user => 'root', password => 'immutable', quiet => 1 );
if ( $rv->not_ok and $rv->{'http_status'} =~ m/500 Can\'t connect/ ) {
    plan skip_all => "Can't connect to server";
}

isnt( $meta->MREST_CLI_URI_BASE, undef, 'MREST_CLI_URI_BASE is defined after initialization' );

note( 'Create testing employee with UTF-8 characters' );
$cmd = "PUT employee nick george { \"fullname\" : \"Karel Omáčka\" }";
$rv = process_command( $cmd );
ok( ref( $rv ) eq 'App::CELL::Status' );

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

use Test::More;
use Test::Warnings;

$debug_mode = 1;

my ( $cmd, $rv );

$rv = init_unit();
plan skip_all => "init_unit failed with status " . $rv->text unless $rv->ok;

$rv = authenticate_to_server( user => 'root', password => 'immutable', quiet => 1 );
if ( $rv->not_ok and $rv->{'http_status'} =~ m/500 Can\'t connect/ ) {
    plan skip_all => "Can't connect to server";
}

isnt( $meta->MREST_CLI_URI_BASE, undef, 'MREST_CLI_URI_BASE is defined after initialization' );

#=====================================
# EMPLOYEE
# EMPLOYEE SHOW
# EMPLOYEE PROFILE

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

use Test::More;
use Test::Warnings;

my ( $cmd, $rv );

note( 'initialize unit' );
$rv = init_unit();
plan skip_all => "init_unit failed with status " . $rv->text unless $rv->ok;

note( 'authenticate to server' );
$rv = authenticate_to_server( user => 'root', password => 'immutable', quiet => 1 );
if ( $rv->not_ok and $rv->{'http_status'} =~ m/500 Can\'t connect/ ) {
    plan skip_all => "Can't connect to server";
}

isnt( $meta->MREST_CLI_URI_BASE, undef, 'MREST_CLI_URI_BASE is defined after initialization' );

note( '****************************************************************************' );
note( 'In t/001-init.t we created an employee "worker" with privlevel "active"' );
note( 'This privlevel was achieved by inserting a record in the privhistory table' );
note( 'Since t/001-init.t always runs first, we can assume that "worker" will have' );

t/cmd_inactive/activity.t  view on Meta::CPAN

use Data::Dumper;
use Test::More;
use Test::Warnings;

my ( $cmd, $rv );

note( 'initialize unit' );
$rv = init_unit();
plan skip_all => "init_unit failed with status " . $rv->text unless $rv->ok;

$rv = authenticate_to_server( user => 'absent', password => 'absent', quiet => 1 );
if ( $rv->not_ok and $rv->{'http_status'} =~ m/500 Can\'t connect/ ) {
    plan skip_all => "Can't connect to server";
}

isnt( $meta->MREST_CLI_URI_BASE, undef, 'MREST_CLI_URI_BASE is defined after initialization' );

$cmd = "ACTIVITY ALL";
$rv = process_command( $cmd );
is( ref( $rv ), 'App::CELL::Status' );
is( $rv->{'http_status'}, '403 Forbidden' );

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

use Data::Dumper;
use Test::More;
use Test::Warnings;

my ( $cmd, $rv );

note( 'initialize unit' );
$rv = init_unit();
plan skip_all => "init_unit failed with status " . $rv->text unless $rv->ok;

$rv = authenticate_to_server( user => 'absent', password => 'absent', quiet => 1 );
if ( $rv->not_ok and $rv->{'http_status'} =~ m/500 Can\'t connect/ ) {
    plan skip_all => "Can't connect to server";
}

isnt( $meta->MREST_CLI_URI_BASE, undef, 'MREST_CLI_URI_BASE is defined after initialization' );

$cmd = "EMPLOYEE";
$rv = process_command( $cmd );
ok( ref( $rv ) eq 'App::CELL::Status' );
is( $rv->level, 'OK' );

t/cmd_passerby/activity.t  view on Meta::CPAN

use Data::Dumper;
use Test::More;
use Test::Warnings;

my ( $cmd, $rv );

note( 'initialize unit' );
$rv = init_unit();
plan skip_all => "init_unit failed with status " . $rv->text unless $rv->ok;

$rv = authenticate_to_server( user => 'demo', password => 'demo', quiet => 1 );
if ( $rv->not_ok and $rv->{'http_status'} =~ m/500 Can\'t connect/ ) {
    plan skip_all => "Can't connect to server";
}

isnt( $meta->MREST_CLI_URI_BASE, undef, 'MREST_CLI_URI_BASE is defined after initialization' );

$cmd = "ACTIVITY ALL";
$rv = process_command( $cmd );
is( ref( $rv ), 'App::CELL::Status' );
is( $rv->{'http_status'}, '403 Forbidden' );

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

use Test::More;
use Test::Warnings;

my ( $cmd, $rv );

note( 'initialize unit' );
$rv = init_unit();
plan skip_all => "init_unit failed with status " . $rv->text unless $rv->ok;

note( 'authenticate to server' );
$rv = authenticate_to_server( user => 'demo', password => 'demo', quiet => 1 );
if ( $rv->not_ok and $rv->{'http_status'} =~ m/500 Can\'t connect/ ) {
    plan skip_all => "Can't connect to server";
}

note( 'sanity check' );
isnt( $meta->MREST_CLI_URI_BASE, undef, 'MREST_CLI_URI_BASE is defined after initialization' );

note( 'EMPLOYEE command' );
$cmd = "EMPLOYEE";
$rv = process_command( $cmd );

t/cmd_passerby/schedule.t  view on Meta::CPAN

use Test::More;
use Test::Warnings;

my ( $cmd, $rv );

note( 'initialize unit' );
$rv = init_unit();
plan skip_all => "init_unit failed with status " . $rv->text unless $rv->ok;

note( 'authenticate to server' );
$rv = authenticate_to_server( user => 'demo', password => 'demo', quiet => 1 );
if ( $rv->not_ok and $rv->{'http_status'} =~ m/500 Can\'t connect/ ) {
    plan skip_all => "Can't connect to server";
}

isnt( $meta->MREST_CLI_URI_BASE, undef, 'MREST_CLI_URI_BASE is defined after initialization' );

$cmd = "SCHEDULE DUMP";
$rv = process_command( $cmd );
is( ref( $rv ), 'App::CELL::Status' );
is( $rv->level, 'OK' );

t/parser/completion.t  view on Meta::CPAN

use App::Dochazka::CLI::Completion qw( dochazka_cli_completion );
use Test::More;
use Test::Warnings;

my @rv;

@rv = dochazka_cli_completion( 'em', 'em', 0 );
cmp_deeply( \@rv, bag( 'employee', 'employee=' ) );

@rv = dochazka_cli_completion( 'p', 'employee p', 9 );
cmp_deeply( \@rv, bag( 'profile', 'password' ) );

@rv = dochazka_cli_completion( 'ni', 'ni ni', 3 );
cmp_deeply( \@rv, bag() );

done_testing;

t/util/auth.t  view on Meta::CPAN

use Test::More;
use Test::Warnings;

my ( $status, $rv, $rv_type );

note( 'init_cli_client' );
$rv = init_unit();
diag( Dumper $rv ) unless $rv->ok;

note( 'authenticate_to_server as root' );
$rv = authenticate_to_server( user => 'root', password => 'immutable', quiet => 1 );
if ( $rv->not_ok and $rv->{'http_status'} =~ m/500 Can\'t connect/ ) {
    plan skip_all => "Can't connect to server";
}

isnt( $meta->MREST_CLI_URI_BASE, undef, 'MREST_CLI_URI_BASE is defined after initialization' );

#note( "authenticate to server with no arguments" );
#is( $current_emp, undef, '$current_emp is undefined before authentication' );
#is( $current_priv, undef, '$current_priv is undefined before authentication' );
#$rv = authenticate_to_server();
#is( $rv->level, 'OK' );
#is( $rv->code, 'DOCHAZKA_CLI_AUTHENTICATION_OK' );
#is( $meta->CURRENT_EMPLOYEE_NICK, 'demo', 'authenticate_to_server nick defaults to demo' );
#is( $meta->CURRENT_EMPLOYEE_PASSWORD, 'demo', 'authenticate_to_server password defaults to demo' );
#is( ref( $current_emp ), 'App::Dochazka::Common::Model::Employee', 'authenticate_to_server created Employee object' );
#is( $current_emp->nick, 'demo', 'the Employee object has the right nick' );
#is( $current_priv, 'passerby', 'authenticate_to_server set $current_priv properly' );

done_testing;

t/util/determine_employee.t  view on Meta::CPAN

use Test::Warnings;
use Test::Fatal;

my ( $status, $rv, $rv_type );

note( 'init_unit' );
$rv = init_unit();
diag( Dumper $rv ) unless $rv->ok;

note( 'authenticate to server' );
$rv = authenticate_to_server( user => 'root', password => 'immutable', quiet => 1 );
$rv_type = ref( $rv );
if ( $rv_type ne 'App::CELL::Status' or $rv->not_ok ) {
    if ( $rv->{'http_status'} =~ m/500 Can\'t connect/ ) {
        plan skip_all => "Can't connect to server";
    } else {
        diag "authenticate_to_server returned unexpected status:";
        diag( Dumper $rv );
        BAIL_OUT(0);
    }
}



( run in 2.708 seconds using v1.01-cache-2.11-cpan-49f99fa48dc )