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::NoWarnings; |
21 |
use Test::More tests => 3; |
22 |
use Test::MockModule; |
23 |
use Test::Mojo; |
24 |
|
25 |
use t::lib::TestBuilder; |
26 |
use t::lib::Mocks; |
27 |
|
28 |
use Koha::Database; |
29 |
|
30 |
my $schema = Koha::Database->new->schema; |
31 |
my $builder = t::lib::TestBuilder->new(); |
32 |
|
33 |
my $t = Test::Mojo->new('Koha::REST::V1'); |
34 |
|
35 |
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 ); |
36 |
|
37 |
subtest 'list() tests' => sub { |
38 |
|
39 |
plan tests => 6; |
40 |
|
41 |
$schema->storage->txn_begin; |
42 |
|
43 |
my $patron = $builder->build_object( |
44 |
{ |
45 |
class => 'Koha::Patrons', |
46 |
value => { flags => 2**4 } # 'borrowers' flag == 4 |
47 |
} |
48 |
); |
49 |
my $password = 'thePassword123'; |
50 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
51 |
my $userid = $patron->userid; |
52 |
|
53 |
$t->get_ok( "//$userid:$password@/api/v1/patrons/" . $patron->id . '/hold_groups' )->status_is( 200, 'REST3.2.2' ) |
54 |
->json_is( [] ); |
55 |
|
56 |
my $hold_group_1 = $builder->build_object( |
57 |
{ |
58 |
class => 'Koha::HoldGroups', |
59 |
value => { borrowernumber => $patron->id } |
60 |
} |
61 |
); |
62 |
my $hold_1 = $builder->build_object( |
63 |
{ |
64 |
class => 'Koha::Holds', |
65 |
value => { borrowernumber => $patron->id, hold_group_id => $hold_group_1->hold_group_id } |
66 |
} |
67 |
); |
68 |
my $hold_2 = $builder->build_object( |
69 |
{ |
70 |
class => 'Koha::Holds', |
71 |
value => { borrowernumber => $patron->id, hold_group_id => $hold_group_1->hold_group_id } |
72 |
} |
73 |
); |
74 |
my $hold_3 = $builder->build_object( |
75 |
{ |
76 |
class => 'Koha::Holds', |
77 |
value => { borrowernumber => $patron->id, hold_group_id => $hold_group_1->hold_group_id } |
78 |
} |
79 |
); |
80 |
|
81 |
$t->get_ok( "//$userid:$password@/api/v1/patrons/" . $patron->id . '/hold_groups' => { 'x-koha-embed' => 'holds' } ) |
82 |
->status_is( 200, 'REST3.2.2' )->json_has( "/0/holds", "holds object correctly embedded" ); |
83 |
|
84 |
$schema->storage->txn_rollback; |
85 |
}; |
86 |
|
87 |
subtest 'add() tests' => sub { |
88 |
|
89 |
plan tests => 15; |
90 |
|
91 |
$schema->storage->txn_begin; |
92 |
|
93 |
my $patron = $builder->build_object( |
94 |
{ |
95 |
class => 'Koha::Patrons', |
96 |
value => { flags => 2**6 } # reserveforothers flag = 6 |
97 |
} |
98 |
); |
99 |
my $other_patron = $builder->build_object( |
100 |
{ |
101 |
class => 'Koha::Patrons', |
102 |
value => { flags => 2**6 } # reserveforothers flag = 6 |
103 |
} |
104 |
); |
105 |
my $password = 'thePassword123'; |
106 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
107 |
my $userid = $patron->userid; |
108 |
|
109 |
my $hold_1 = $builder->build_object( |
110 |
{ |
111 |
class => 'Koha::Holds', |
112 |
value => { borrowernumber => $patron->id, hold_group_id => undef, found => undef } |
113 |
} |
114 |
); |
115 |
my $hold_2 = $builder->build_object( |
116 |
{ |
117 |
class => 'Koha::Holds', |
118 |
value => { borrowernumber => $patron->id, hold_group_id => undef, found => undef } |
119 |
} |
120 |
); |
121 |
my $hold_3 = $builder->build_object( |
122 |
{ |
123 |
class => 'Koha::Holds', |
124 |
value => { borrowernumber => $other_patron->id, hold_group_id => undef, found => 'W' } |
125 |
} |
126 |
); |
127 |
|
128 |
$t->post_ok( |
129 |
"//$userid:$password@/api/v1/patrons/" . $patron->id . "/hold_groups" => json => { hold_ids => [333] } ) |
130 |
->status_is( 400, 'REST3.2.1' ) |
131 |
->json_is( |
132 |
{ error => 'One or more holds do not exist: 333', error_code => "HoldDoesNotExist", hold_ids => [333] } ); |
133 |
|
134 |
$t->post_ok( "//$userid:$password@/api/v1/patrons/" |
135 |
. $other_patron->id |
136 |
. "/hold_groups" => json => { hold_ids => [ $hold_3->reserve_id ] } )->status_is( 400, 'REST3.2.1' ) |
137 |
->json_is( |
138 |
{ |
139 |
error => 'One or more holds have already been found: ' . $hold_3->item->barcode, |
140 |
error_code => "HoldHasAlreadyBeenFound", barcodes => [ $hold_3->item->barcode ] |
141 |
} |
142 |
); |
143 |
|
144 |
$t->post_ok( "//$userid:$password@/api/v1/patrons/" |
145 |
. $patron->id |
146 |
. "/hold_groups" => json => { hold_ids => [ $hold_3->reserve_id ] } )->status_is( 400, 'REST3.2.1' ) |
147 |
->json_is( |
148 |
{ |
149 |
error => 'One or more holds do not belong to patron: ' . $hold_3->reserve_id, |
150 |
error_code => "HoldDoesNotBelongToPatron", hold_ids => [ $hold_3->reserve_id ] |
151 |
} |
152 |
); |
153 |
|
154 |
$t->post_ok( "//$userid:$password@/api/v1/patrons/" |
155 |
. $patron->id |
156 |
. "/hold_groups" => json => { hold_ids => [ $hold_1->reserve_id, $hold_2->reserve_id ] } ) |
157 |
->status_is( 201, 'REST3.2.1' )->json_has( "/holds", "holds object correctly embedded" ); |
158 |
|
159 |
$t->post_ok( "//$userid:$password@/api/v1/patrons/" |
160 |
. $patron->id |
161 |
. "/hold_groups" => json => { hold_ids => [ $hold_1->reserve_id, $hold_2->reserve_id ] } ) |
162 |
->status_is( 400, 'REST3.2.1' )->json_is( |
163 |
{ |
164 |
error => "One or more holds already belong to a hold group: " |
165 |
. $hold_1->reserve_id . ", " |
166 |
. $hold_2->reserve_id, |
167 |
error_code => "HoldAlreadyBelongsToHoldGroup", |
168 |
hold_ids => [ $hold_1->reserve_id, $hold_2->reserve_id ] |
169 |
} |
170 |
); |
171 |
|
172 |
$schema->storage->txn_rollback; |
173 |
}; |