<<

NAME

Koha::SIP2::MessageTester - Generate a SIP2 response for a saved account without a socket

SYNOPSIS

    my $tester = Koha::SIP2::MessageTester->new( { account => $sip_account } );
    my $result = $tester->run( { message => 'patron_information', patron_id => '23529000000001' } );

DESCRIPTION

Builds the same server context the SIP server builds at login time ( minus the authentication ) from a saved Koha::SIP2::Account, hands a constructed request to the real message handlers, and returns the generated response. This lets a librarian check the value of the custom template fields configured on an account without knowing the SIP account password.

The handlers run inside a transaction that is always rolled back, so database changes made by even checkout and checkin messages are undone. That is the only thing that is undone: plugin hooks ( for instance after_circ_action ) still run, and anything a plugin sends elsewhere is not recalled. Publishing of background job messages is suppressed while the handler runs and the "last seen" activity is not recorded. Every test is logged ( who, which account, which message ) so it can be audited.

A patron password is never checked. A request that carries one says what the outcome should be instead: GOOD makes the password count as the patron's, BAD makes it count as wrong. A real password is refused. If it were not, the tester would let anyone who can use it try passwords for the patrons they can see, without the lockout of the real SIP server, as the failed attempts are rolled back with everything else.

The handlers run with the session of the SIP account's own user and institution, as they do after a real SIP login, not the session of the staff member running the test.

API

Class methods

new

    my $tester = Koha::SIP2::MessageTester->new( { account => $sip_account } );

run

    my $result = $tester->run(
        {
            message         => 'patron_information',
            patron_id       => $patron_barcode,
            item_id         => $item_barcode,
            patron_password => 'GOOD',
            summary         => 'fine_items',
        }
    );

    # or run a raw request string built elsewhere ( the staff interface )
    my $result = $tester->run( { request => '6300120240101    100000          AOMPL|AA23529000000001|' } );

Generates the response for the requested message and returns a hashref:

    {
        request  => $raw_request_string,
        response => $raw_response_string,
        fixed    => $fixed_length_portion,
        fields   => [ { field => 'AO', value => 'MPL' }, ... ],
    }

A raw 'request' string is used as-is if given, otherwise the request is built from 'message' and the patron/item parameters. Valid messages are 'patron_status', 'patron_information', 'item_information', 'checkout' and 'checkin'.

A patron information request can ask for one list of items in the response, with the 'summary' parameter: 'hold_items', 'overdue_items', 'charged_items', 'fine_items', 'recall_items' or 'unavailable_holds'. Without it the lists are left out, and so are the templates that format them ( the fines, for one ).

The logged in user's restrictions apply: a patron the user cannot see throws Koha::Exceptions::Patron::NotFound, and when IndependentBranches is enabled an item from another library throws Koha::Exceptions::NoPermission.

Internal methods

_sip_userenv

Returns the arguments for C4::Context->set_userenv that describe the session a real SIP login would have had: the patron the account logs in as, at the institution's library.

_restore_userenv

Puts back the session that was in place before the test.

_log_test

Logs who ran which message against which account. This is done outside the transaction that is rolled back, so the record of a test always survives. The patron and item identifiers are left out on purpose.

The prefix that Koha::Logger adds under Plack is turned off: the default configuration only logs plack-api from WARN, so an INFO line would be dropped for every request the web server handles.

_build_server

Builds the server context ( config, account, institution, policy and ils ) for the saved account, the same way C4::SIP::Sip::MsgType::login_core does but without authenticating.

_build_request

Builds the raw SIP request string for the given message type.

_check_request

Refuses a request the logged in user should not be able to run: a message the tester does not support, a patron the user cannot see, or when IndependentBranches is enabled, an item from another library.

_parse_response

Splits a raw response into its fixed length portion and the list of variable length fields, so the variable fields ( where the custom template values live ) can be displayed one per row.

_summary

    my $summary = _summary('fine_items');

Builds the ten character summary field of a patron information request: spaces, or a Y at the position of the detail that is asked for. Without a detail, the server leaves out the lists of items.

_field

Builds a single variable length field. An optional field with no value is omitted entirely. A value containing the field delimiter is refused, it would add fields to the request that the form didn't ask for.

<<