Line 0
Link Here
|
0 |
- |
1 |
#!/usr/bin/env perl |
|
|
2 |
|
3 |
# This file is part of Koha. |
4 |
# |
5 |
# Koha is free software; you can redistribute it and/or modify it |
6 |
# under the terms of the GNU General Public License as published by |
7 |
# the Free Software Foundation; either version 3 of the License, or |
8 |
# (at your option) any later version. |
9 |
# |
10 |
# Koha is distributed in the hope that it will be useful, but |
11 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
# GNU General Public License for more details. |
14 |
# |
15 |
# You should have received a copy of the GNU General Public License |
16 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
17 |
|
18 |
use Modern::Perl; |
19 |
|
20 |
use Test::More tests => 17; |
21 |
use Test::MockModule; |
22 |
use Test::Mojo; |
23 |
use t::lib::Mocks; |
24 |
use t::lib::TestBuilder; |
25 |
|
26 |
use DateTime; |
27 |
|
28 |
use C4::Context; |
29 |
use C4::Circulation qw( AddIssue AddReturn CanBookBeIssued ); |
30 |
|
31 |
use Koha::Database; |
32 |
use Koha::DateUtils qw( dt_from_string output_pref ); |
33 |
use Koha::Token; |
34 |
|
35 |
my $schema = Koha::Database->schema; |
36 |
my $builder = t::lib::TestBuilder->new; |
37 |
|
38 |
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 ); |
39 |
my $t = Test::Mojo->new('Koha::REST::V1'); |
40 |
|
41 |
$schema->storage->txn_begin; |
42 |
|
43 |
my $dbh = C4::Context->dbh; |
44 |
|
45 |
my $branchcode = $builder->build( { source => 'Branch' } )->{branchcode}; |
46 |
my $librarian = $builder->build_object( |
47 |
{ |
48 |
class => 'Koha::Patrons', |
49 |
value => { flags => 2, branchcode => $branchcode } |
50 |
} |
51 |
); |
52 |
my $password = 'thePassword123'; |
53 |
$librarian->set_password( { password => $password, skip_validation => 1 } ); |
54 |
my $userid = $librarian->userid; |
55 |
|
56 |
my $other_branchcode = $builder->build( { source => 'Branch' } )->{branchcode}; |
57 |
my $patron = $builder->build_object( |
58 |
{ |
59 |
class => 'Koha::Patrons', |
60 |
value => { flags => 0, branchcode => $other_branchcode }, |
61 |
} |
62 |
); |
63 |
my $unauth_password = 'thePassword000'; |
64 |
$patron->set_password( |
65 |
{ password => $unauth_password, skip_validattion => 1 } ); |
66 |
my $unauth_userid = $patron->userid; |
67 |
my $patron_id = $patron->borrowernumber; |
68 |
|
69 |
my $item1 = $builder->build_sample_item; |
70 |
my $item1_id = $item1->id; |
71 |
|
72 |
my $not_issued_item = $builder->build_sample_item; |
73 |
my $not_issued_item_id = $not_issued_item->id; |
74 |
my $not_issued_item_barcode = $not_issued_item->barcode; |
75 |
|
76 |
my $barcode = '321321321321'; |
77 |
my $matching_items = Koha::Items->search({ barcode => $barcode }); |
78 |
while (my $item = $matching_items->next) { |
79 |
$item->delete; |
80 |
} |
81 |
|
82 |
my $unknown_item1_id = 578; |
83 |
my $del_item = Koha::Items->find($unknown_item1_id); |
84 |
if ($del_item){ |
85 |
$del_item->delete; |
86 |
} |
87 |
|
88 |
my $checkout = $builder->build_object( |
89 |
{ |
90 |
class => 'Koha::Checkouts', |
91 |
value => { |
92 |
itemnumber => $item1_id, |
93 |
branchcode => $branchcode, |
94 |
borrowernumber => $patron_id, |
95 |
note => undef, |
96 |
notedate => undef, |
97 |
noteseen => undef |
98 |
} |
99 |
} |
100 |
); |
101 |
my $checkout_id = $checkout->id; |
102 |
|
103 |
# empty checkin |
104 |
$t->post_ok( |
105 |
"//$userid:$password@/api/v1/checkin" => json => { } ) |
106 |
->status_is(400); |
107 |
|
108 |
# checkin on unknow item id |
109 |
$t->post_ok( |
110 |
"//$userid:$password@/api/v1/checkin" => json => { item_id => $unknown_item1_id } ) |
111 |
->status_is(409)->json_is( { error => "Item not found." } ); |
112 |
|
113 |
# checkin with unknow barcode |
114 |
$t->post_ok( |
115 |
"//$userid:$password@/api/v1/checkin" => json => { external_id => $barcode } ) |
116 |
->status_is(409)->json_is( '/error' => "Item not found." )->json_is( '/messages' => { BadBarcode => $barcode} ); |
117 |
|
118 |
# not isued |
119 |
$t->post_ok( |
120 |
"//$userid:$password@/api/v1/checkin" => json => { item_id => $not_issued_item_id, library_id => $branchcode } ) |
121 |
->status_is(403)->json_is( '/returned' => 0 )->json_has( '/messages' => { NotIssued => $not_issued_item_barcode } ); |
122 |
|
123 |
# checkin okay |
124 |
$t->post_ok( |
125 |
"//$userid:$password@/api/v1/checkin" => json => { item_id => $item1_id, library_id => $branchcode } ) |
126 |
->status_is(201)->json_is( '/returned' => 1 )->json_has( '/messages' => { TransferTrigger => 'ReturnToHome', WasReturned => '1' } ); |
127 |
|
128 |
|
129 |
|
130 |
$schema->storage->txn_rollback; |