Bugzilla – Attachment 76410 Details for
Bug 20942
Add route to get patron's account balance
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 20942: Unit tests for /patrons/{patron_id}/account
Bug-20942-Unit-tests-for-patronspatronidaccount.patch (text/plain), 5.65 KB, created by
Josef Moravec
on 2018-06-25 19:18:44 UTC
(
hide
)
Description:
Bug 20942: Unit tests for /patrons/{patron_id}/account
Filename:
MIME Type:
Creator:
Josef Moravec
Created:
2018-06-25 19:18:44 UTC
Size:
5.65 KB
patch
obsolete
>From c3d9b50d83cd6800cbbd135e6ca3550c90c46cb1 Mon Sep 17 00:00:00 2001 >From: Tomas Cohen Arazi <tomascohen@theke.io> >Date: Wed, 13 Jun 2018 14:02:30 -0300 >Subject: [PATCH] Bug 20942: Unit tests for /patrons/{patron_id}/account > >This patch adds tests for the /patrons/{patron_id}/account endpoint. >To test: > >- Run: > $ kshell > k$ prove t/db_dependent/api/v1/patrons_accounts.t >=> FAIL: Tests should fail because the endpoint is not implemented. > >Signed-off-by: Josef Moravec <josef.moravec@gmail.com> >--- > t/db_dependent/api/v1/patrons_accounts.t | 143 +++++++++++++++++++++++++++++++ > 1 file changed, 143 insertions(+) > create mode 100644 t/db_dependent/api/v1/patrons_accounts.t > >diff --git a/t/db_dependent/api/v1/patrons_accounts.t b/t/db_dependent/api/v1/patrons_accounts.t >new file mode 100644 >index 0000000..dc770f4 >--- /dev/null >+++ b/t/db_dependent/api/v1/patrons_accounts.t >@@ -0,0 +1,143 @@ >+#!/usr/bin/perl >+ >+# This file is part of Koha. >+# >+# Koha is free software; you can redistribute it and/or modify it under the >+# terms of the GNU General Public License as published by the Free Software >+# Foundation; either version 3 of the License, or (at your option) any later >+# version. >+# >+# Koha is distributed in the hope that it will be useful, but WITHOUT ANY >+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR >+# A PARTICULAR PURPOSE. See the GNU General Public License for more details. >+# >+# You should have received a copy of the GNU General Public License along >+# with Koha; if not, write to the Free Software Foundation, Inc., >+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. >+ >+use Modern::Perl; >+ >+use Test::More tests => 1; >+ >+use Test::Mojo; >+use Test::Warn; >+ >+use t::lib::TestBuilder; >+use t::lib::Mocks; >+ >+use C4::Accounts qw(manualinvoice); >+use C4::Auth; >+use Koha::Account::Line; >+ >+my $schema = Koha::Database->new->schema; >+my $builder = t::lib::TestBuilder->new; >+ >+# FIXME: sessionStorage defaults to mysql, but it seems to break transaction handling >+# this affects the other REST api tests >+t::lib::Mocks::mock_preference( 'SessionStorage', 'tmp' ); >+ >+my $remote_address = '127.0.0.1'; >+my $t = Test::Mojo->new('Koha::REST::V1'); >+ >+subtest 'get_balance() tests' => sub { >+ >+ plan tests => 9; >+ >+ $schema->storage->txn_begin; >+ >+ my ( $patron_id, $session_id ) = create_user_and_session({ authorized => 0 }); >+ my $patron = Koha::Patrons->find($patron_id); >+ >+ my $tx = $t->ua->build_tx(GET => "/api/v1/patrons/$patron_id/account"); >+ $tx->req->cookies({ name => 'CGISESSID', value => $session_id }); >+ $tx->req->env({ REMOTE_ADDR => '127.0.0.1' }); >+ $t->request_ok($tx) >+ ->status_is(200) >+ ->json_is( { balance => 0.00 } ); >+ >+ my $account_line_1 = Koha::Account::Line->new( >+ { >+ borrowernumber => $patron->borrowernumber, >+ date => \'NOW()', >+ amount => 50, >+ description => "A description", >+ accounttype => "N", # New card >+ amountoutstanding => 50, >+ manager_id => $patron->borrowernumber, >+ } >+ )->store(); >+ $account_line_1->discard_changes; >+ >+ my $account_line_2 = Koha::Account::Line->new( >+ { >+ borrowernumber => $patron->borrowernumber, >+ date => \'NOW()', >+ amount => 50.01, >+ description => "A description", >+ accounttype => "N", # New card >+ amountoutstanding => 50.01, >+ manager_id => $patron->borrowernumber, >+ } >+ )->store(); >+ $account_line_2->discard_changes; >+ >+ $tx = $t->ua->build_tx( GET => "/api/v1/patrons/$patron_id/account" ); >+ $tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); >+ $tx->req->env( { REMOTE_ADDR => '127.0.0.1' } ); >+ $t->request_ok($tx)->status_is(200)->json_is( >+ { balance => 100.01, >+ outstanding_lines => [ >+ Koha::REST::V1::Patrons::Account::_to_api( $account_line_1->TO_JSON ), >+ Koha::REST::V1::Patrons::Account::_to_api( $account_line_2->TO_JSON ) >+ >+ ] >+ } >+ ); >+ >+ Koha::Account->new({ patron_id => $patron_id })->pay( >+ { amount => 100.01, >+ note => 'He paid!', >+ description => 'Finally!', >+ library_id => $patron->branchcode, >+ account_type => 'Pay', >+ offset_type => 'Payment' >+ } >+ ); >+ >+ $tx = $t->ua->build_tx( GET => "/api/v1/patrons/$patron_id/account" ); >+ $tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); >+ $tx->req->env( { REMOTE_ADDR => '127.0.0.1' } ); >+ $t->request_ok($tx)->status_is(200)->json_is( { balance => 0 } ); >+ >+ $schema->storage->txn_rollback; >+}; >+ >+sub create_user_and_session { >+ >+ my $args = shift; >+ my $flags = ( $args->{authorized} ) ? 16 : 0; >+ >+ my $user = $builder->build( >+ { >+ source => 'Borrower', >+ value => { >+ flags => $flags, >+ gonenoaddress => 0, >+ lost => 0, >+ email => 'nobody@example.com', >+ emailpro => 'nobody@example.com', >+ B_email => 'nobody@example.com' >+ } >+ } >+ ); >+ >+ # Create a session for the authorized user >+ my $session = C4::Auth::get_session(''); >+ $session->param( 'number', $user->{borrowernumber} ); >+ $session->param( 'id', $user->{userid} ); >+ $session->param( 'ip', '127.0.0.1' ); >+ $session->param( 'lasttime', time() ); >+ $session->flush; >+ >+ return ( $user->{borrowernumber}, $session->id ); >+} >-- >2.1.4
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 20942
:
76053
|
76054
|
76055
|
76393
|
76409
|
76410
|
76411
|
76412
|
76641
|
76808
|
76809
|
76810
|
76811
|
76812