Add a TestUtils library for live tests
authorJason Etheridge <jason@esilibrary.com>
Thu, 22 Aug 2013 16:48:53 +0000 (12:48 -0400)
committerBill Erickson <berick@esilibrary.com>
Thu, 10 Oct 2013 18:49:58 +0000 (14:49 -0400)
Package commonly used live test calls in a new Perl module to they can
be shared by different test scripts.

Based it off of Cronscript.  Added a cache method to Cronscript for getting at
memcached.  TestUtils currently has methods for checkin, checkout, and
workstation registration.

Ported existing live tests to use TestUtils.

Signed-off-by: Jason Etheridge <jason@esilibrary.com>
Signed-off-by: Bill Erickson <berick@esilibrary.com>
Open-ILS/src/perlmods/lib/OpenILS/Utils/Cronscript.pm.in
Open-ILS/src/perlmods/lib/OpenILS/Utils/TestUtils.pm [new file with mode: 0644]
Open-ILS/src/perlmods/live_t/00-simple.t
Open-ILS/src/perlmods/live_t/01-auth.t
Open-ILS/src/perlmods/live_t/02-simple_circ.t
Open-ILS/src/perlmods/live_t/03-overdue_circ.t
Open-ILS/src/perlmods/live_t/04-overdue_with_closed_dates.t
Open-ILS/src/perlmods/live_t/05-pay_bills.t

index e0f7ba4..32ac711 100644 (file)
@@ -393,6 +393,14 @@ sub authtime {
     return $self->{authtime};
 }
 
+sub cache {
+    my $self = shift;
+    my $cache = "OpenSRF::Utils::Cache";
+    $cache->use;
+    $self->{memcache} = $cache->new('global') unless $self->{memcache};
+    return $self->{memcache};
+}
+
 1;
 __END__
 
diff --git a/Open-ILS/src/perlmods/lib/OpenILS/Utils/TestUtils.pm b/Open-ILS/src/perlmods/lib/OpenILS/Utils/TestUtils.pm
new file mode 100644 (file)
index 0000000..c98f39d
--- /dev/null
@@ -0,0 +1,36 @@
+package OpenILS::Utils::TestUtils;
+use base "OpenILS::Utils::Cronscript";
+
+# The purpose of this module is to consolidate common routines that may
+# be used by the integration tests in src/perlmods/live_t/
+
+use strict; use warnings;
+
+my $apputils = 'OpenILS::Application::AppUtils';
+
+sub register_workstation {
+    my ($self,$name,$lib) = (shift,shift,shift);
+    my $resp = $apputils->simplereq(
+        'open-ils.actor',
+        'open-ils.actor.workstation.register',
+        $self->authtoken, $name, $lib);
+    return $resp;
+}
+
+sub do_checkout {
+    my ($self,$args) = (shift,shift);
+    my $resp = $apputils->simplereq(
+        'open-ils.circ',
+        'open-ils.circ.checkout.full', $self->authtoken, $args);
+    return $resp;
+}
+
+sub do_checkin {
+    my ($self,$args) = (shift,shift);
+    my $resp = $apputils->simplereq(
+        'open-ils.circ',
+        'open-ils.circ.checkin', $self->authtoken, $args );
+    return $resp;
+}
+
+1;
index f6f0c02..764a57d 100644 (file)
@@ -5,20 +5,11 @@ use Test::More tests => 2;
 diag("Simple tests against the open-ils.storage service and the stock test data.");
 
 use strict; use warnings;
-use OpenSRF::System;
-use OpenSRF::AppSession;
-use OpenILS::Utils::Fieldmapper;
-use OpenSRF::Utils::SettingsClient;
 
-my $config = `osrf_config --sysconfdir`;
-chomp $config;
-$config .= '/opensrf_core.xml';
-
-OpenSRF::System->bootstrap_client(config_file => $config);
-Fieldmapper->import(IDL =>
-    OpenSRF::Utils::SettingsClient->new->config_value("IDL"));
+use OpenILS::Utils::TestUtils;
+my $script = OpenILS::Utils::TestUtils->new();
  
-my $ses = OpenSRF::AppSession->create('open-ils.storage');
+my $ses = $script->session('open-ils.storage');
 my $req = $ses->request('open-ils.storage.direct.actor.user.retrieve', 1);
 if (my $resp = $req->recv) {
     if (my $user = $resp->content) {
index a522040..75da00b 100644 (file)
@@ -4,162 +4,43 @@ use Test::More tests => 4;
 
 diag("Simple tests against the open-ils.auth service, memcached, and the stock test data.");
 
-use strict;
-use warnings;
-use Data::Dumper;
-use OpenSRF::System;
-use OpenSRF::AppSession;
-use Digest::MD5 qw(md5_hex);
-use OpenILS::Application::AppUtils;
-use OpenSRF::Utils::SettingsClient;
+use strict; use warnings;
 
-# Some useful objects
-our $cache      = "OpenSRF::Utils::Cache";
-our $apputils   = "OpenILS::Application::AppUtils";
-our $memcache;
-our $authtoken;
-our $authtime;
-
-#----------------------------------------------------------------
-# Exit a script
-#----------------------------------------------------------------
-sub err {
-    my ($pkg, $file, $line, $sub)  = _caller();
-    no warnings;
-    die "Script halted with error ".
-        "($pkg : $file : $line : $sub):\n" . shift() . "\n";
-}
-
-#----------------------------------------------------------------
-# This is not the function you're looking for
-#----------------------------------------------------------------
-sub _caller {
-    my ($pkg, $file, $line, $sub)  = caller(2);
-    if(!$line) {
-        ($pkg, $file, $line)  = caller(1);
-        $sub = "";
-    }
-    return ($pkg, $file, $line, $sub);
-}
-
-#----------------------------------------------------------------
-# Connect to the servers
-#----------------------------------------------------------------
-sub osrf_connect {
-    my $config = `osrf_config --sysconfdir`;
-    chomp $config;
-    $config .= '/opensrf_core.xml';
-    err("Bootstrap config required") unless $config;
-    OpenSRF::System->bootstrap_client( config_file => $config );
-}
-
-#----------------------------------------------------------------
-# Get a handle for the memcache object
-#----------------------------------------------------------------
-sub osrf_cache {
-    $cache->use;
-    $memcache = $cache->new('global') unless $memcache;
-    return $memcache;
-}
-
-#----------------------------------------------------------------
-# Is the given object an OILS event?
-#----------------------------------------------------------------
-sub oils_is_event {
-    my $e = shift;
-    if( $e and ref($e) eq 'HASH' ) {
-        return 1 if defined($e->{ilsevent});
-    }
-    return 0;
-}
-
-#----------------------------------------------------------------
-# If the given object is an event, this prints the event info 
-# and exits the script
-#----------------------------------------------------------------
-sub oils_event_die {
-    my $evt = shift;
-    my ($pkg, $file, $line, $sub)  = _caller();
-    if(oils_is_event($evt)) {
-        if($evt->{ilsevent}) {
-            diag("\nReceived Event($pkg : $file : $line : $sub): \n" . Dumper($evt));
-            exit 1;
-        }
-    }
-}
-
-#----------------------------------------------------------------
-# Login to the auth server and set the global $authtoken var
-#----------------------------------------------------------------
-sub oils_login {
-    my( $username, $password, $type ) = @_;
-
-    $type |= "staff";
-
-    my $seed = $apputils->simplereq( 'open-ils.auth',
-        'open-ils.auth.authenticate.init', $username );
-    err("No auth seed") unless $seed;
-
-    my $response = $apputils->simplereq( 'open-ils.auth',
-        'open-ils.auth.authenticate.complete',
-        {   username => $username,
-            password => md5_hex($seed . md5_hex($password)),
-            type => $type });
-
-    err("No auth response returned on login") unless $response;
-
-    oils_event_die($response);
-
-    $authtime  = $response->{payload}->{authtime};
-    $authtoken = $response->{payload}->{authtoken};
-    diag("authtime is $authtime, authtoken is $authtoken");
-    return $authtoken;
-}
-
-#----------------------------------------------------------------
-# Destroys the login session on the server
-#----------------------------------------------------------------
-sub oils_logout {
-    $apputils->simplereq(
-        'open-ils.auth',
-        'open-ils.auth.session.delete', (@_ ? shift : $authtoken) );
-}
-
-#----------------------------------------------------------------
-# var $response = simplereq( $service, $method, @params );
-#----------------------------------------------------------------
-sub simplereq    { return $apputils->simplereq(@_); }
-sub osrf_request { return $apputils->simplereq(@_); }
+use OpenILS::Utils::TestUtils;
+my $script = OpenILS::Utils::TestUtils->new();
 
 #----------------------------------------------------------------
 # The tests...  assumes stock sample data, full-auto install by
 # eg_wheezy_installer.sh, etc.
 #----------------------------------------------------------------
 
-osrf_connect();
-oils_login('admin','demo123','staff');
+$script->authenticate({
+    username => 'admin',
+    password => 'demo123',
+    type => 'staff'});
+
+my $authtoken = $script->authtoken;
 
 ok(
     $authtoken,
     'Have an authtoken'
 );
 is(
-    $authtime,
+    $script->authtime,
     7200,
     'Default authtime for staff login is 7200 seconds'
 );
 
-osrf_cache();
-my $cached_obj = $memcache->get_cache("oils_auth_$authtoken");
+my $cached_obj = $script->cache()->get_cache("oils_auth_$authtoken");
 
 ok(
     ref $cached_obj,
     'Can retrieve authtoken from memcached'
 );
 
-oils_logout();
+$script->logout();
 
-$cached_obj = $memcache->get_cache("oils_auth_$authtoken");
+$cached_obj = $script->cache()->get_cache("oils_auth_$authtoken");
 ok(
     ! $cached_obj,
     'Authtoken is removed from memcached after logout'
index 5d366e2..820b6f0 100644 (file)
@@ -9,162 +9,17 @@ use constant WORKSTATION_LIB => 7;
 use constant ITEM_BARCODE => 'CONC70000345';
 use constant ITEM_ID => 310;
 
-use strict;
-use warnings;
-use Data::Dumper;
-use OpenSRF::System;
-use OpenSRF::AppSession;
-use Digest::MD5 qw(md5_hex);
-use OpenILS::Utils::Fieldmapper;
-use OpenILS::Application::AppUtils;
-use OpenSRF::Utils::SettingsClient;
+use strict; use warnings;
 
-# Some useful objects
-our $cache      = "OpenSRF::Utils::Cache";
-our $apputils   = "OpenILS::Application::AppUtils";
-our $memcache;
-our $authtoken;
-our $authtime;
-
-#----------------------------------------------------------------
-# Exit a script
-#----------------------------------------------------------------
-sub err {
-    my ($pkg, $file, $line, $sub)  = _caller();
-    no warnings;
-    die "Script halted with error ".
-        "($pkg : $file : $line : $sub):\n" . shift() . "\n";
-}
-
-#----------------------------------------------------------------
-# This is not the function you're looking for
-#----------------------------------------------------------------
-sub _caller {
-    my ($pkg, $file, $line, $sub)  = caller(2);
-    if(!$line) {
-        ($pkg, $file, $line)  = caller(1);
-        $sub = "";
-    }
-    return ($pkg, $file, $line, $sub);
-}
-
-#----------------------------------------------------------------
-# Connect to the servers
-#----------------------------------------------------------------
-sub osrf_connect {
-    my $config = `osrf_config --sysconfdir`;
-    chomp $config;
-    $config .= '/opensrf_core.xml';
-    err("Bootstrap config required") unless $config;
-    OpenSRF::System->bootstrap_client( config_file => $config );
-    Fieldmapper->import(IDL =>
-        OpenSRF::Utils::SettingsClient->new->config_value("IDL"));
-}
-
-#----------------------------------------------------------------
-# Is the given object an OILS event?
-#----------------------------------------------------------------
-sub oils_is_event {
-    my $e = shift;
-    if( $e and ref($e) eq 'HASH' ) {
-        return 1 if defined($e->{ilsevent});
-    }
-    return 0;
-}
-
-#----------------------------------------------------------------
-# If the given object is an event, this prints the event info 
-# and exits the script
-#----------------------------------------------------------------
-sub oils_event_die {
-    my $evt = shift;
-    my ($pkg, $file, $line, $sub)  = _caller();
-    if(oils_is_event($evt)) {
-        if($evt->{ilsevent}) {
-            diag("\nReceived Event($pkg : $file : $line : $sub): \n" . Dumper($evt));
-            exit 1;
-        }
-    }
-}
-
-#----------------------------------------------------------------
-# Login to the auth server and set the global $authtoken var
-#----------------------------------------------------------------
-sub oils_login {
-    my( $username, $password, $type, $ws ) = @_;
-
-    $type |= "staff";
-
-    my $seed = $apputils->simplereq( 'open-ils.auth',
-        'open-ils.auth.authenticate.init', $username );
-    err("No auth seed") unless $seed;
-
-    my $response = $apputils->simplereq( 'open-ils.auth',
-        'open-ils.auth.authenticate.complete',
-        {   username => $username,
-            password => md5_hex($seed . md5_hex($password)),
-            type => $type, workstation => $ws });
-
-    err("No auth response returned on login") unless $response;
-
-    oils_event_die($response);
-
-    $authtime  = $response->{payload}->{authtime};
-    $authtoken = $response->{payload}->{authtoken};
-    diag("authtime is $authtime, authtoken is $authtoken");
-    return $authtoken;
-}
-
-#----------------------------------------------------------------
-# Destroys the login session on the server
-#----------------------------------------------------------------
-sub oils_logout {
-    $apputils->simplereq(
-        'open-ils.auth',
-        'open-ils.auth.session.delete', (@_ ? shift : $authtoken) );
-}
-
-#----------------------------------------------------------------
-# var $response = simplereq( $service, $method, @params );
-#----------------------------------------------------------------
-sub simplereq    { return $apputils->simplereq(@_); }
-sub osrf_request { return $apputils->simplereq(@_); }
-
-#----------------------------------------------------------------
-
-sub register_workstation {
-    my $resp = osrf_request(
-        'open-ils.actor',
-        'open-ils.actor.workstation.register',
-        $authtoken, WORKSTATION_NAME, WORKSTATION_LIB);
-    return $resp;
-}
-
-sub do_checkout {
-    my( $patronid, $barcode ) = @_;
-    my $args = { patron => $patronid, barcode => $barcode };
-    my $resp = osrf_request(
-        'open-ils.circ',
-        'open-ils.circ.checkout.full', $authtoken, $args );
-    return $resp;
-}
-
-sub do_checkin {
-    my $barcode  = shift;
-    my $args = { barcode => $barcode };
-    my $resp = osrf_request(
-        'open-ils.circ',
-        'open-ils.circ.checkin', $authtoken, $args );
-    return $resp;
-}
+use OpenILS::Utils::TestUtils;
+my $script = OpenILS::Utils::TestUtils->new();
 
 #----------------------------------------------------------------
 # The tests...  assumes stock sample data, full-auto install by
 # eg_wheezy_installer.sh, etc.
 #----------------------------------------------------------------
 
-osrf_connect();
-my $storage_ses = OpenSRF::AppSession->create('open-ils.storage');
+my $storage_ses = $script->session('open-ils.storage');
 
 my $user_req = $storage_ses->request('open-ils.storage.direct.actor.user.retrieve', 1);
 if (my $user_resp = $user_req->recv) {
@@ -202,25 +57,34 @@ if (my $item_resp = $item_req->recv) {
     }
 }
 
-oils_login('admin','demo123','staff');
+$script->authenticate({
+    username => 'admin',
+    password => 'demo123',
+    type => 'staff'});
 ok(
-    $authtoken,
+    $script->authtoken,
     'Have an authtoken'
 );
-my $ws = register_workstation();
+my $ws = $script->register_workstation(WORKSTATION_NAME,WORKSTATION_LIB);
 ok(
     ! ref $ws,
     'Registered a new workstation'
 );
 
-oils_logout();
-oils_login('admin','demo123','staff',WORKSTATION_NAME);
+$script->logout();
+$script->authenticate({
+    username => 'admin',
+    password => 'demo123',
+    type => 'staff',
+    workstation => WORKSTATION_NAME});
 ok(
-    $authtoken,
+    $script->authtoken,
     'Have an authtoken associated with the workstation'
 );
 
-my $checkout_resp = do_checkout(1, ITEM_BARCODE);
+my $checkout_resp = $script->do_checkout({
+    patron => 1,
+    barcode => ITEM_BARCODE});
 is(
     ref $checkout_resp,
     'HASH',
@@ -243,7 +107,8 @@ if (my $item_resp = $item_req->recv) {
     }
 }
 
-my $checkin_resp = do_checkin(ITEM_BARCODE);
+my $checkin_resp = $script->do_checkin({
+    barcode => ITEM_BARCODE});
 is(
     ref $checkin_resp,
     'HASH',
@@ -265,6 +130,5 @@ if (my $item_resp = $item_req->recv) {
     }
 }
 
-oils_logout();
-
+$script->logout();
 
index f073e3e..7981d32 100644 (file)
@@ -9,167 +9,25 @@ use constant WORKSTATION_LIB => 7;
 use constant ITEM_BARCODE => 'CONC71000345';
 use constant ITEM_ID => 810;
 
-use strict;
-use warnings;
-use Data::Dumper;
-use OpenSRF::System;
-use OpenSRF::AppSession;
-use Digest::MD5 qw(md5_hex);
-use OpenILS::Utils::Fieldmapper;
-use OpenILS::Application::AppUtils;
+use strict; use warnings;
+
+use OpenILS::Utils::TestUtils;
+my $script = OpenILS::Utils::TestUtils->new();
+
 use DateTime;
 use DateTime::Format::ISO8601;
 use OpenSRF::Utils qw/cleanse_ISO8601/;
-use OpenSRF::Utils::SettingsClient;
-
-# Some useful objects
-our $cache      = "OpenSRF::Utils::Cache";
-our $apputils   = "OpenILS::Application::AppUtils";
-our $memcache;
-our $authtoken;
-our $authtime;
-
-#----------------------------------------------------------------
-# Exit a script
-#----------------------------------------------------------------
-sub err {
-    my ($pkg, $file, $line, $sub)  = _caller();
-    no warnings;
-    die "Script halted with error ".
-        "($pkg : $file : $line : $sub):\n" . shift() . "\n";
-}
-
-#----------------------------------------------------------------
-# This is not the function you're looking for
-#----------------------------------------------------------------
-sub _caller {
-    my ($pkg, $file, $line, $sub)  = caller(2);
-    if(!$line) {
-        ($pkg, $file, $line)  = caller(1);
-        $sub = "";
-    }
-    return ($pkg, $file, $line, $sub);
-}
-
-#----------------------------------------------------------------
-# Connect to the servers
-#----------------------------------------------------------------
-sub osrf_connect {
-    my $config = `osrf_config --sysconfdir`;
-    chomp $config;
-    $config .= '/opensrf_core.xml';
-    err("Bootstrap config required") unless $config;
-    OpenSRF::System->bootstrap_client( config_file => $config );
-    Fieldmapper->import(IDL =>
-        OpenSRF::Utils::SettingsClient->new->config_value("IDL"));
-}
-
-#----------------------------------------------------------------
-# Is the given object an OILS event?
-#----------------------------------------------------------------
-sub oils_is_event {
-    my $e = shift;
-    if( $e and ref($e) eq 'HASH' ) {
-        return 1 if defined($e->{ilsevent});
-    }
-    return 0;
-}
-
-#----------------------------------------------------------------
-# If the given object is an event, this prints the event info 
-# and exits the script
-#----------------------------------------------------------------
-sub oils_event_die {
-    my $evt = shift;
-    my ($pkg, $file, $line, $sub)  = _caller();
-    if(oils_is_event($evt)) {
-        if($evt->{ilsevent}) {
-            diag("\nReceived Event($pkg : $file : $line : $sub): \n" . Dumper($evt));
-            exit 1;
-        }
-    }
-}
-
-#----------------------------------------------------------------
-# Login to the auth server and set the global $authtoken var
-#----------------------------------------------------------------
-sub oils_login {
-    my( $username, $password, $type, $ws ) = @_;
-
-    $type |= "staff";
-
-    my $seed = $apputils->simplereq( 'open-ils.auth',
-        'open-ils.auth.authenticate.init', $username );
-    err("No auth seed") unless $seed;
-
-    my $response = $apputils->simplereq( 'open-ils.auth',
-        'open-ils.auth.authenticate.complete',
-        {   username => $username,
-            password => md5_hex($seed . md5_hex($password)),
-            type => $type, workstation => $ws });
 
-    err("No auth response returned on login") unless $response;
-
-    oils_event_die($response);
-
-    $authtime  = $response->{payload}->{authtime};
-    $authtoken = $response->{payload}->{authtoken};
-    diag("authtime is $authtime, authtoken is $authtoken");
-    return $authtoken;
-}
-
-#----------------------------------------------------------------
-# Destroys the login session on the server
-#----------------------------------------------------------------
-sub oils_logout {
-    $apputils->simplereq(
-        'open-ils.auth',
-        'open-ils.auth.session.delete', (@_ ? shift : $authtoken) );
-}
-
-#----------------------------------------------------------------
-# var $response = simplereq( $service, $method, @params );
-#----------------------------------------------------------------
-sub simplereq    { return $apputils->simplereq(@_); }
-sub osrf_request { return $apputils->simplereq(@_); }
-
-#----------------------------------------------------------------
-
-sub register_workstation {
-    my $resp = osrf_request(
-        'open-ils.actor',
-        'open-ils.actor.workstation.register',
-        $authtoken, WORKSTATION_NAME, WORKSTATION_LIB);
-    return $resp;
-}
-
-sub do_checkout {
-    my( $patronid, $barcode ) = @_;
-    my $args = { patron => $patronid, barcode => $barcode };
-    my $resp = osrf_request(
-        'open-ils.circ',
-        'open-ils.circ.checkout.full', $authtoken, $args );
-    return $resp;
-}
-
-sub do_checkin {
-    my $barcode  = shift;
-    my $args = { barcode => $barcode };
-    my $resp = osrf_request(
-        'open-ils.circ',
-        'open-ils.circ.checkin', $authtoken, $args );
-    return $resp;
-}
+our $apputils = 'OpenILS::Application::AppUtils';
 
 #----------------------------------------------------------------
 # The tests...  assumes stock sample data, full-auto install by
 # eg_wheezy_installer.sh, etc.
 #----------------------------------------------------------------
 
-osrf_connect();
-my $storage_ses = OpenSRF::AppSession->create('open-ils.storage');
-my $circ_ses = OpenSRF::AppSession->create('open-ils.circ');
-my $cstore_ses = OpenSRF::AppSession->connect('open-ils.cstore');
+my $storage_ses = $script->session('open-ils.storage');
+my $circ_ses = $script->session('open-ils.circ');
+my $cstore_ses = $script->session('open-ils.cstore');
 
 my $user_req = $storage_ses->request('open-ils.storage.direct.actor.user.retrieve', 1);
 if (my $user_resp = $user_req->recv) {
@@ -207,25 +65,34 @@ if (my $item_resp = $item_req->recv) {
     }
 }
 
-oils_login('admin','demo123','staff');
+$script->authenticate({
+    username => 'admin',
+    password => 'demo123',
+    type => 'staff'});
 ok(
-    $authtoken,
+    $script->authtoken,
     'Have an authtoken'
 );
-my $ws = register_workstation();
+my $ws = $script->register_workstation(WORKSTATION_NAME,WORKSTATION_LIB);
 ok(
     ! ref $ws,
     'Registered a new workstation'
 );
 
-oils_logout();
-oils_login('admin','demo123','staff',WORKSTATION_NAME);
+$script->logout();
+$script->authenticate({
+    username => 'admin',
+    password => 'demo123',
+    type => 'staff',
+    workstation => WORKSTATION_NAME});
 ok(
-    $authtoken,
+    $script->authtoken,
     'Have an authtoken associated with the workstation'
 );
 
-my $checkout_resp = do_checkout(1, ITEM_BARCODE);
+my $checkout_resp = $script->do_checkout({
+    patron => 1,
+    barcode => ITEM_BARCODE});
 is(
     ref $checkout_resp,
     'HASH',
@@ -265,7 +132,7 @@ if (my $item_resp = $item_req->recv) {
 
 my $bill_req = $circ_ses->request(
     'open-ils.circ.money.billing.retrieve.all',
-    $authtoken,
+    $script->authtoken,
     $circ->id
 );
 if (my $bill_resp = $bill_req->recv) {
@@ -286,6 +153,7 @@ my $twenty_days = OpenSRF::Utils->interval_to_seconds('480 h 0 m 0 s');
 $circ->xact_start( $apputils->epoch2ISO8601($xact_start - $twenty_days) );
 $circ->due_date( $apputils->epoch2ISO8601($due_date - $twenty_days) );
 
+$cstore_ses->connect; # need stateful connection
 my $xact = $cstore_ses->request('open-ils.cstore.transaction.begin')->gather(1);
 my $update_req = $cstore_ses->request(
     'open-ils.cstore.direct.action.circulation.update',
@@ -304,7 +172,8 @@ $cstore_ses->request('open-ils.cstore.transaction.commit')->gather(1);
 
 ########
 
-my $checkin_resp = do_checkin(ITEM_BARCODE);
+my $checkin_resp = $script->do_checkin({
+    barcode => ITEM_BARCODE});
 is(
     ref $checkin_resp,
     'HASH',
@@ -328,7 +197,7 @@ if (my $item_resp = $item_req->recv) {
 
 $bill_req = $circ_ses->request(
     'open-ils.circ.money.billing.retrieve.all',
-    $authtoken,
+    $script->authtoken,
     $circ->id
 );
 if (my $bill_resp = $bill_req->recv) {
@@ -342,6 +211,6 @@ if (my $bill_resp = $bill_req->recv) {
 }
 
 
-oils_logout();
+$script->logout();
 
 
index efcc693..3f9f3f5 100644 (file)
@@ -9,157 +9,16 @@ use constant WORKSTATION_LIB => 7;
 use constant ITEM_BARCODE => 'CONC72000345';
 use constant ITEM_ID => 1310;
 
-use strict;
-use warnings;
-use Data::Dumper;
-use OpenSRF::System;
-use OpenSRF::AppSession;
-use Digest::MD5 qw(md5_hex);
-use OpenILS::Utils::Fieldmapper;
-use OpenILS::Application::AppUtils;
+use strict; use warnings;
+
+use OpenILS::Utils::TestUtils;
+my $script = OpenILS::Utils::TestUtils->new();
+
 use DateTime;
 use DateTime::Format::ISO8601;
 use OpenSRF::Utils qw/cleanse_ISO8601/;
-use OpenSRF::Utils::SettingsClient;
 
-# Some useful objects
-our $cache      = "OpenSRF::Utils::Cache";
 our $apputils   = "OpenILS::Application::AppUtils";
-our $memcache;
-our $authtoken;
-our $authtime;
-
-#----------------------------------------------------------------
-# Exit a script
-#----------------------------------------------------------------
-sub err {
-    my ($pkg, $file, $line, $sub)  = _caller();
-    no warnings;
-    die "Script halted with error ".
-        "($pkg : $file : $line : $sub):\n" . shift() . "\n";
-}
-
-#----------------------------------------------------------------
-# This is not the function you're looking for
-#----------------------------------------------------------------
-sub _caller {
-    my ($pkg, $file, $line, $sub)  = caller(2);
-    if(!$line) {
-        ($pkg, $file, $line)  = caller(1);
-        $sub = "";
-    }
-    return ($pkg, $file, $line, $sub);
-}
-
-#----------------------------------------------------------------
-# Connect to the servers
-#----------------------------------------------------------------
-sub osrf_connect {
-    my $config = `osrf_config --sysconfdir`;
-    chomp $config;
-    $config .= '/opensrf_core.xml';
-    err("Bootstrap config required") unless $config;
-    OpenSRF::System->bootstrap_client( config_file => $config );
-    Fieldmapper->import(IDL =>
-        OpenSRF::Utils::SettingsClient->new->config_value("IDL"));
-}
-
-#----------------------------------------------------------------
-# Is the given object an OILS event?
-#----------------------------------------------------------------
-sub oils_is_event {
-    my $e = shift;
-    if( $e and ref($e) eq 'HASH' ) {
-        return 1 if defined($e->{ilsevent});
-    }
-    return 0;
-}
-
-#----------------------------------------------------------------
-# If the given object is an event, this prints the event info 
-# and exits the script
-#----------------------------------------------------------------
-sub oils_event_die {
-    my $evt = shift;
-    my ($pkg, $file, $line, $sub)  = _caller();
-    if(oils_is_event($evt)) {
-        if($evt->{ilsevent}) {
-            diag("\nReceived Event($pkg : $file : $line : $sub): \n" . Dumper($evt));
-            exit 1;
-        }
-    }
-}
-
-#----------------------------------------------------------------
-# Login to the auth server and set the global $authtoken var
-#----------------------------------------------------------------
-sub oils_login {
-    my( $username, $password, $type, $ws ) = @_;
-
-    $type |= "staff";
-
-    my $seed = $apputils->simplereq( 'open-ils.auth',
-        'open-ils.auth.authenticate.init', $username );
-    err("No auth seed") unless $seed;
-
-    my $response = $apputils->simplereq( 'open-ils.auth',
-        'open-ils.auth.authenticate.complete',
-        {   username => $username,
-            password => md5_hex($seed . md5_hex($password)),
-            type => $type, workstation => $ws });
-
-    err("No auth response returned on login") unless $response;
-
-    oils_event_die($response);
-
-    $authtime  = $response->{payload}->{authtime};
-    $authtoken = $response->{payload}->{authtoken};
-    diag("authtime is $authtime, authtoken is $authtoken");
-    return $authtoken;
-}
-
-#----------------------------------------------------------------
-# Destroys the login session on the server
-#----------------------------------------------------------------
-sub oils_logout {
-    $apputils->simplereq(
-        'open-ils.auth',
-        'open-ils.auth.session.delete', (@_ ? shift : $authtoken) );
-}
-
-#----------------------------------------------------------------
-# var $response = simplereq( $service, $method, @params );
-#----------------------------------------------------------------
-sub simplereq    { return $apputils->simplereq(@_); }
-sub osrf_request { return $apputils->simplereq(@_); }
-
-#----------------------------------------------------------------
-
-sub register_workstation {
-    my $resp = osrf_request(
-        'open-ils.actor',
-        'open-ils.actor.workstation.register',
-        $authtoken, WORKSTATION_NAME, WORKSTATION_LIB);
-    return $resp;
-}
-
-sub do_checkout {
-    my( $patronid, $barcode ) = @_;
-    my $args = { patron => $patronid, barcode => $barcode };
-    my $resp = osrf_request(
-        'open-ils.circ',
-        'open-ils.circ.checkout.full', $authtoken, $args );
-    return $resp;
-}
-
-sub do_checkin {
-    my $barcode  = shift;
-    my $args = { barcode => $barcode };
-    my $resp = osrf_request(
-        'open-ils.circ',
-        'open-ils.circ.checkin', $authtoken, $args );
-    return $resp;
-}
 
 sub create_closed_date {
     my $ten_days = OpenSRF::Utils->interval_to_seconds('240 h 0 m 0 s');
@@ -179,19 +38,19 @@ sub create_closed_date {
             DateTime->today()->epoch() - $ten_days + $almost_twenty_four_hours
         )
     );
-    my $resp = osrf_request(
+    my $resp = $apputils->simplereq(
         'open-ils.actor',
         'open-ils.actor.org_unit.closed.create',
-        $authtoken, $aoucd);
+        $script->authtoken, $aoucd);
     return $resp;
 }
 
 sub delete_closed_date {
     my $aoucd = shift;
-    my $resp = osrf_request(
+    my $resp = $apputils->simplereq(
         'open-ils.actor',
         'open-ils.actor.org_unit.closed.delete',
-        $authtoken, ref $aoucd ? $aoucd->id : $aoucd );
+        $script->authtoken, ref $aoucd ? $aoucd->id : $aoucd );
     return $resp;
 }
 
@@ -200,10 +59,9 @@ sub delete_closed_date {
 # eg_wheezy_installer.sh, etc.
 #----------------------------------------------------------------
 
-osrf_connect();
-my $storage_ses = OpenSRF::AppSession->create('open-ils.storage');
-my $circ_ses = OpenSRF::AppSession->create('open-ils.circ');
-my $cstore_ses = OpenSRF::AppSession->connect('open-ils.cstore');
+my $storage_ses = $script->session('open-ils.storage');
+my $circ_ses = $script->session('open-ils.circ');
+my $cstore_ses = $script->session('open-ils.cstore');
 
 my $user_req = $storage_ses->request('open-ils.storage.direct.actor.user.retrieve', 1);
 if (my $user_resp = $user_req->recv) {
@@ -241,21 +99,28 @@ if (my $item_resp = $item_req->recv) {
     }
 }
 
-oils_login('admin','demo123','staff');
+$script->authenticate({
+    username => 'admin',
+    password => 'demo123',
+    type => 'staff'});
 ok(
-    $authtoken,
+    $script->authtoken,
     'Have an authtoken'
 );
-my $ws = register_workstation();
+my $ws = $script->register_workstation(WORKSTATION_NAME,WORKSTATION_LIB);
 ok(
     ! ref $ws,
     'Registered a new workstation'
 );
 
-oils_logout();
-oils_login('admin','demo123','staff',WORKSTATION_NAME);
+$script->logout();
+$script->authenticate({
+    username => 'admin',
+    password => 'demo123',
+    type => 'staff',
+    workstation => WORKSTATION_NAME});
 ok(
-    $authtoken,
+    $script->authtoken,
     'Have an authtoken associated with the workstation'
 );
 
@@ -266,7 +131,9 @@ is(
     'Created a closed date for 10 days ago'
 );
 
-my $checkout_resp = do_checkout(1, ITEM_BARCODE);
+my $checkout_resp = $script->do_checkout({
+    patron => 1,
+    barcode => ITEM_BARCODE});
 is(
     ref $checkout_resp,
     'HASH',
@@ -306,7 +173,7 @@ if (my $item_resp = $item_req->recv) {
 
 my $bill_req = $circ_ses->request(
     'open-ils.circ.money.billing.retrieve.all',
-    $authtoken,
+    $script->authtoken,
     $circ->id
 );
 if (my $bill_resp = $bill_req->recv) {
@@ -327,6 +194,7 @@ my $twenty_days = OpenSRF::Utils->interval_to_seconds('480 h 0 m 0 s');
 $circ->xact_start( $apputils->epoch2ISO8601($xact_start - $twenty_days) );
 $circ->due_date( $apputils->epoch2ISO8601($due_date - $twenty_days) );
 
+$cstore_ses->connect; # need stateful connection
 my $xact = $cstore_ses->request('open-ils.cstore.transaction.begin')->gather(1);
 my $update_req = $cstore_ses->request(
     'open-ils.cstore.direct.action.circulation.update',
@@ -345,7 +213,8 @@ $cstore_ses->request('open-ils.cstore.transaction.commit')->gather(1);
 
 ########
 
-my $checkin_resp = do_checkin(ITEM_BARCODE);
+my $checkin_resp = $script->do_checkin({
+    barcode => ITEM_BARCODE});
 is(
     ref $checkin_resp,
     'HASH',
@@ -369,7 +238,7 @@ if (my $item_resp = $item_req->recv) {
 
 $bill_req = $circ_ses->request(
     'open-ils.circ.money.billing.retrieve.all',
-    $authtoken,
+    $script->authtoken,
     $circ->id
 );
 if (my $bill_resp = $bill_req->recv) {
@@ -385,6 +254,6 @@ if (my $bill_resp = $bill_req->recv) {
 my $tmp = delete_closed_date($closed_date_obj);
 is($tmp,    1,  'Removed closed date');
 
-oils_logout();
+$script->logout();
 
 
index ed82861..94d380d 100644 (file)
@@ -9,145 +9,22 @@ use constant WORKSTATION_LIB => 7;
 use constant USER_ID => 1;
 use constant USER_USRNAME => 'admin';
 
-use strict;
-use warnings;
-use Data::Dumper;
-use OpenSRF::System;
-use OpenSRF::AppSession;
-use Digest::MD5 qw(md5_hex);
-use OpenILS::Utils::Fieldmapper;
-use OpenILS::Application::AppUtils;
+use strict; use warnings;
+
+use OpenILS::Utils::TestUtils;
+my $script = OpenILS::Utils::TestUtils->new();
+
 use DateTime;
 use DateTime::Format::ISO8601;
 use OpenSRF::Utils qw/cleanse_ISO8601/;
-use OpenSRF::Utils::SettingsClient;
 
-# Some useful objects
-our $cache      = "OpenSRF::Utils::Cache";
 our $apputils   = "OpenILS::Application::AppUtils";
-our $memcache;
-our $authtoken;
-our $authtime;
-
-#----------------------------------------------------------------
-# Exit a script
-#----------------------------------------------------------------
-sub err {
-    my ($pkg, $file, $line, $sub)  = _caller();
-    no warnings;
-    die "Script halted with error ".
-        "($pkg : $file : $line : $sub):\n" . shift() . "\n";
-}
-
-#----------------------------------------------------------------
-# This is not the function you're looking for
-#----------------------------------------------------------------
-sub _caller {
-    my ($pkg, $file, $line, $sub)  = caller(2);
-    if(!$line) {
-        ($pkg, $file, $line)  = caller(1);
-        $sub = "";
-    }
-    return ($pkg, $file, $line, $sub);
-}
-
-#----------------------------------------------------------------
-# Connect to the servers
-#----------------------------------------------------------------
-sub osrf_connect {
-    my $config = `osrf_config --sysconfdir`;
-    chomp $config;
-    $config .= '/opensrf_core.xml';
-    err("Bootstrap config required") unless $config;
-    OpenSRF::System->bootstrap_client( config_file => $config );
-    Fieldmapper->import(IDL =>
-        OpenSRF::Utils::SettingsClient->new->config_value("IDL"));
-}
-
-#----------------------------------------------------------------
-# Is the given object an OILS event?
-#----------------------------------------------------------------
-sub oils_is_event {
-    my $e = shift;
-    if( $e and ref($e) eq 'HASH' ) {
-        return 1 if defined($e->{ilsevent});
-    }
-    return 0;
-}
-
-#----------------------------------------------------------------
-# If the given object is an event, this prints the event info 
-# and exits the script
-#----------------------------------------------------------------
-sub oils_event_die {
-    my $evt = shift;
-    my ($pkg, $file, $line, $sub)  = _caller();
-    if(oils_is_event($evt)) {
-        if($evt->{ilsevent}) {
-            diag("\nReceived Event($pkg : $file : $line : $sub): \n" . Dumper($evt));
-            exit 1;
-        }
-    }
-}
-
-#----------------------------------------------------------------
-# Login to the auth server and set the global $authtoken var
-#----------------------------------------------------------------
-sub oils_login {
-    my( $username, $password, $type, $ws ) = @_;
-
-    $type |= "staff";
-
-    my $seed = $apputils->simplereq( 'open-ils.auth',
-        'open-ils.auth.authenticate.init', $username );
-    err("No auth seed") unless $seed;
-
-    my $response = $apputils->simplereq( 'open-ils.auth',
-        'open-ils.auth.authenticate.complete',
-        {   username => $username,
-            password => md5_hex($seed . md5_hex($password)),
-            type => $type, workstation => $ws });
-
-    err("No auth response returned on login") unless $response;
-
-    oils_event_die($response);
-
-    $authtime  = $response->{payload}->{authtime};
-    $authtoken = $response->{payload}->{authtoken};
-    diag("authtime is $authtime, authtoken is $authtoken");
-    return $authtoken;
-}
-
-#----------------------------------------------------------------
-# Destroys the login session on the server
-#----------------------------------------------------------------
-sub oils_logout {
-    $apputils->simplereq(
-        'open-ils.auth',
-        'open-ils.auth.session.delete', (@_ ? shift : $authtoken) );
-}
-
-#----------------------------------------------------------------
-# var $response = simplereq( $service, $method, @params );
-#----------------------------------------------------------------
-sub simplereq    { return $apputils->simplereq(@_); }
-sub osrf_request { return $apputils->simplereq(@_); }
-
-#----------------------------------------------------------------
-
-sub register_workstation {
-    my $resp = osrf_request(
-        'open-ils.actor',
-        'open-ils.actor.workstation.register',
-        $authtoken, WORKSTATION_NAME, WORKSTATION_LIB);
-    return $resp;
-}
 
 sub fetch_billing_summaries {
-    my $resp = osrf_request(
+    my $resp = $apputils->simplereq(
         'open-ils.actor',
         'open-ils.actor.user.transactions.history.have_balance.authoritative',
-        $authtoken,
+        $script->authtoken,
         USER_ID
     );
     return $resp;
@@ -155,10 +32,10 @@ sub fetch_billing_summaries {
 
 sub pay_bills {
     my ($user_obj, $payment_blob) = (shift, shift);
-    my $resp = osrf_request(
+    my $resp = $apputils->simplereq(
         'open-ils.circ',
         'open-ils.circ.money.payment',
-        $authtoken,
+        $script->authtoken,
         $payment_blob,
         $user_obj->last_xact_id
     );
@@ -170,8 +47,7 @@ sub pay_bills {
 # eg_wheezy_installer.sh, etc.
 #----------------------------------------------------------------
 
-osrf_connect();
-my $storage_ses = OpenSRF::AppSession->create('open-ils.storage');
+my $storage_ses = $script->session('open-ils.storage');
 
 my $user_obj;
 my $user_req = $storage_ses->request('open-ils.storage.direct.actor.user.retrieve', USER_ID);
@@ -190,21 +66,28 @@ if (my $user_resp = $user_req->recv) {
     }
 }
 
-oils_login('admin','demo123','staff');
+$script->authenticate({
+    username => 'admin',
+    password => 'demo123',
+    type => 'staff'});
 ok(
-    $authtoken,
+    $script->authtoken,
     'Have an authtoken'
 );
-my $ws = register_workstation();
+my $ws = $script->register_workstation(WORKSTATION_NAME,WORKSTATION_LIB);
 ok(
     ! ref $ws,
     'Registered a new workstation'
 );
 
-oils_logout();
-oils_login('admin','demo123','staff',WORKSTATION_NAME);
+$script->logout();
+$script->authenticate({
+    username => 'admin',
+    password => 'demo123',
+    type => 'staff',
+    workstation => WORKSTATION_NAME});
 ok(
-    $authtoken,
+    $script->authtoken,
     'Have an authtoken associated with the workstation'
 );
 
@@ -251,6 +134,6 @@ is(
     'Zero billable xacts for ' . USER_USRNAME . ' user after payment'
 );
 
-oils_logout();
+$script->logout();