diff options
author | Mark Wells <mark@freeside.biz> | 2016-04-26 12:10:43 -0700 |
---|---|---|
committer | Mark Wells <mark@freeside.biz> | 2016-04-26 12:10:51 -0700 |
commit | cb6214a6c0b0ecc8c3a5c264ee98ff3f301c33a6 (patch) | |
tree | 49a74db191bfd375117e1bfd34423b35c8f161a6 /FS/t | |
parent | bc34518143fe485eec412e709271e681a1e41aac (diff) |
more robust testing platform, #37340
Diffstat (limited to 'FS/t')
-rwxr-xr-x | FS/t/suite/00-new_customer.t | 67 | ||||
-rwxr-xr-x | FS/t/suite/01-order_pkg.t | 49 | ||||
-rwxr-xr-x | FS/t/suite/02-bill_customer.t | 36 | ||||
-rwxr-xr-x | FS/t/suite/03-realtime_pay.t | 40 | ||||
-rw-r--r-- | FS/t/suite/WRITING | 93 |
5 files changed, 285 insertions, 0 deletions
diff --git a/FS/t/suite/00-new_customer.t b/FS/t/suite/00-new_customer.t new file mode 100755 index 0000000..8e86459 --- /dev/null +++ b/FS/t/suite/00-new_customer.t @@ -0,0 +1,67 @@ +#!/usr/bin/perl + +use FS::Test; +use Test::More tests => 4; + +my $FS = FS::Test->new; +# get the form +$FS->post('/edit/cust_main.cgi'); +my $form = $FS->form('CustomerForm'); + +my %params = ( + residential_commercial => 'Residential', + agentnum => 1, + refnum => 1, + last => 'Customer', + first => 'New', + invoice_email => 'newcustomer@fake.freeside.biz', + bill_address1 => '123 Example Street', + bill_address2 => 'Apt. Z', + bill_city => 'Sacramento', + bill_state => 'CA', + bill_zip => '94901', + bill_country => 'US', + bill_coord_auto => 'Y', + daytime => '916-555-0100', + night => '916-555-0200', + ship_address1 => '125 Example Street', + ship_address2 => '3rd Floor', + ship_city => 'Sacramento', + ship_state => 'CA', + ship_zip => '94901', + ship_country => 'US', + ship_coord_auto => 'Y', + invoice_ship_address => 'Y', + postal_invoice => 'Y', + billday => '1', + no_credit_limit => 1, + # payment method + custpaybynum0_payby => 'CARD', + custpaybynum0_payinfo => '4012888888881881', + custpaybynum0_paydate_month => '12', + custpaybynum0_paydate_year => '2020', + custpaybynum0_paycvv => '123', + custpaybynum0_payname => '', + custpaybynum0_weight => 1, +); +foreach (keys %params) { + $form->value($_, $params{$_}); +} +$FS->post($form); +ok( $FS->error eq '' , 'form posted' ); +if ( + ok($FS->redirect =~ m[^/view/cust_main.cgi\?(\d+)], 'new customer accepted') +) { + my $custnum = $1; + my $cust = $FS->qsearchs('cust_main', { custnum => $1 }); + isa_ok ( $cust, 'FS::cust_main' ); + $FS->post($FS->redirect); + ok ( $FS->error eq '' , 'can view customer' ); +} else { + # try to display the error message, or if not, show everything + $FS->post($FS->redirect); + diag ($FS->error); + done_testing(2); +} + +1; diff --git a/FS/t/suite/01-order_pkg.t b/FS/t/suite/01-order_pkg.t new file mode 100755 index 0000000..1511350 --- /dev/null +++ b/FS/t/suite/01-order_pkg.t @@ -0,0 +1,49 @@ +#!/usr/bin/perl + +use Test::More tests => 4; +use FS::Test; +use Date::Parse 'str2time'; +my $FS = FS::Test->new; + +# get the form +$FS->post('/misc/order_pkg.html', custnum => 2); +my $form = $FS->form('OrderPkgForm'); + +# Customer #2 has three packages: +# a $30 monthly prorate, a $90 monthly prorate, and a $25 annual prorate. +# Next bill date on the monthly prorates is 2016-04-01. +# Add a new package that will start billing on 2016-03-20 (to make prorate +# behavior visible). + +my %params = ( + pkgpart => 5, + quantity => 1, + start => 'on_date', + start_date => '03/20/2016', + package_comment0 => $0, # record the test we're executing +); + +$form->find_input('start')->disabled(0); # JS +foreach (keys %params) { + $form->value($_, $params{$_}); +} +$FS->post($form); +ok( $FS->error eq '' , 'form posted' ); +if ( + ok( $FS->page =~ m[location = '.*/view/cust_main.cgi.*\#cust_pkg(\d+)'], + 'new package accepted' ) +) { + # on success, sends us back to cust_main view with #cust_pkg$pkgnum + # but with an in-page javascript redirect + my $pkg = $FS->qsearchs('cust_pkg', { pkgnum => $1 }); + isa_ok( $pkg, 'FS::cust_pkg' ); + ok($pkg->start_date == str2time('2016-03-20'), 'start date set'); +} else { + # try to display the error message, or if not, show everything + $FS->post($FS->redirect); + diag ($FS->error); + done_testing(2); +} + +1; + diff --git a/FS/t/suite/02-bill_customer.t b/FS/t/suite/02-bill_customer.t new file mode 100755 index 0000000..0afffaa --- /dev/null +++ b/FS/t/suite/02-bill_customer.t @@ -0,0 +1,36 @@ +#!/usr/bin/perl + +use FS::Test; +use Test::More tests => 6; +use Test::MockTime 'set_fixed_time'; +use Date::Parse 'str2time'; +use FS::cust_main; + +my $FS = FS::Test->new; + +# After test 01: cust#2 has a package set to bill on 2016-03-20. +# Set local time. +my $date = '2016-03-20'; +set_fixed_time(str2time($date)); +my $cust_main = FS::cust_main->by_key(2); +my @return; + +# Bill the customer. +my $error = $cust_main->bill( return_bill => \@return ); +ok($error eq '', "billed on $date") or diag($error); + +# should be an invoice now +my $cust_bill = $return[0]; +isa_ok($cust_bill, 'FS::cust_bill'); + +# $60/month * (30 days - 19 days)/30 days = $42 +ok( $cust_bill->charged == 42.00, 'prorated first month correctly' ); + +# the package bill date should now be 2016-04-01 +my @lineitems = $cust_bill->cust_bill_pkg; +ok( scalar(@lineitems) == 1, 'one package was billed' ); +my $pkg = $lineitems[0]->cust_pkg; +ok( $pkg->status eq 'active', 'package is now active' ); +ok( $pkg->bill == str2time('2016-04-01'), 'package bill date set correctly' ); + +1; diff --git a/FS/t/suite/03-realtime_pay.t b/FS/t/suite/03-realtime_pay.t new file mode 100755 index 0000000..17456bb --- /dev/null +++ b/FS/t/suite/03-realtime_pay.t @@ -0,0 +1,40 @@ +#!/usr/bin/perl + +use FS::Test; +use Test::More tests => 2; +use FS::cust_main; + +my $FS = FS::Test->new; + +# In the stock database, cust#5 has open invoices +my $cust_main = FS::cust_main->by_key(5); +my $balance = $cust_main->balance; +ok( $balance > 10.00, 'customer has an outstanding balance of more than $10.00' ); + +# Get the payment form +$FS->post('/misc/payment.cgi?payby=CARD;custnum=5'); +my $form = $FS->form('OneTrueForm'); +$form->value('amount' => '10.00'); +$form->value('custpaybynum' => ''); +$form->value('payinfo' => '4012888888881881'); +$form->value('month' => '01'); +$form->value('year' => '2020'); +# payname and location fields should already be set +$form->value('save' => 1); +$form->value('auto' => 1); +$FS->post($form); + +# on success, gives a redirect to the payment receipt +my $paynum; +if ($FS->redirect =~ m[^/view/cust_pay.html\?(\d+)]) { + pass('payment processed'); + $paynum = $1; +} elsif ( $FS->error ) { + fail('payment rejected'); + diag ( $FS->error ); +} else { + fail('unknown result'); + diag ( $FS->page ); +} + +1; diff --git a/FS/t/suite/WRITING b/FS/t/suite/WRITING new file mode 100644 index 0000000..d9421cc --- /dev/null +++ b/FS/t/suite/WRITING @@ -0,0 +1,93 @@ +WRITING TESTS + +Load the test database (kept in FS-Test/share/test.sql for now). This has +a large set of customers in a known initial state. You can login through +the web interface as "admin"/"admin" to examine the state of things and plan +your test. + +The test scripts now have access to BOTH sides of the web interface, so you +can create an object through the UI and then examine its internal +properties, etc. + + use Test::More tests => 1; + use FS::Test; + my $FS = FS::Test->new; + +$FS has qsearch and qsearchs methods for finding objects directly. You can +do anything with those objects that Freeside backend code could normally do. +For example, this will bill a customer: + + my $cust = $FS->qsearchs('cust_main', { custnum => 52 }); + my $error = $cust->bill; + +TESTING UI INTERACTION + +To fetch a page from the UI, use the post() method: + + $FS->post('/view/cust_main.cgi?52'); + ok( $FS->error eq '', 'fetched customer view' ) or diag($FS->error); + ok( $FS->page =~ /Customer, New/, 'customer is named "Customer, New"' ); + +To simulate a user filling in and submitting a form, first fetch the form, +and select it by name: + + $FS->post('/edit/svc_acct.cgi?98'); + my $form = $FS->form('OneTrueForm'); + +then fill it in and submit it: + + $form->value('clear_password', '1234abcd'); + $FS->post($form); + +and examine the result: + + my $svc_acct = $FS->qsearch('svc_acct', { svcnum => 98 }); + ok( $svc_acct->_password eq '1234abcd', 'password was changed' ); + +TESTING UI FLOW (EDIT/PROCESS/VIEW SEQUENCE) + +Forms for editing records will post to a processing page. $FS->post($form) +handles this. The processing page will usually redirect back to the view +page on success, and back to the edit form with an error on failure. +Determine which kind of redirect it is. If it's a redirect to the edit form, +you need to follow it to report the error. + + if ( $FS->redirect =~ m[^/view/svc_acct.cgi] ) { + + pass('redirected to view page'); + + } elsif ( $FS->redirect =~ m[^/edit/svc_acct.cgi] ) { + + fail('redirected back to edit form'); + $FS->post($FS->redirect); + diag($FS->error); + + } else { + + fail('unsure what happened'); + diag($FS->page); + + } + +RUNNING TESTS AT A SPECIFIC DATE + +Important for testing package billing. Test::MockTime provides the +set_fixed_time() function, which will freeze the time returned by the time() +function at a specific value. I recommend giving it a unix timestamp rather +than a date string to avoid any confusion about time zones. + +Note that FS::Cron::bill and some other parts of the system look at the $^T +variable (the time that the current program started running). You can +override that by just assigning to the variable. + +Customers in the test database are billed up through Mar 1 2016. This will +bill a customer for the next month after that: + + use Test::MockTime qw(set_fixed_time); + use Date::Parse qw(str2time); + + my $cust = $FS->qsearchs('cust_main', { custnum => 52 }); + set_fixed_time( str2time('2016-04-01') ); + $cust->bill; + + |