Line 0
Link Here
|
0 |
- |
1 |
#!/usr/bin/env perl |
|
|
2 |
|
3 |
# Copyright 2023 Theke Solutions |
4 |
# |
5 |
# This file is part of Koha. |
6 |
# |
7 |
# Koha is free software; you can redistribute it and/or modify it |
8 |
# under the terms of the GNU General Public License as published by |
9 |
# the Free Software Foundation; either version 3 of the License, or |
10 |
# (at your option) any later version. |
11 |
# |
12 |
# Koha is distributed in the hope that it will be useful, but |
13 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
# GNU General Public License for more details. |
16 |
# |
17 |
# You should have received a copy of the GNU General Public License |
18 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
19 |
|
20 |
use Modern::Perl; |
21 |
|
22 |
use Test::More tests => 5; |
23 |
use Test::Mojo; |
24 |
|
25 |
use t::lib::TestBuilder; |
26 |
use t::lib::Mocks; |
27 |
|
28 |
use Koha::RecordSources; |
29 |
use Koha::Database; |
30 |
|
31 |
my $schema = Koha::Database->new->schema; |
32 |
my $builder = t::lib::TestBuilder->new; |
33 |
|
34 |
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 ); |
35 |
|
36 |
my $t = Test::Mojo->new('Koha::REST::V1'); |
37 |
|
38 |
subtest 'list() tests' => sub { |
39 |
|
40 |
plan tests => 12; |
41 |
|
42 |
$schema->storage->txn_begin; |
43 |
|
44 |
my $source = $builder->build_object( { class => 'Koha::RecordSources' } ); |
45 |
my $patron = $builder->build_object( |
46 |
{ |
47 |
class => 'Koha::Patrons', |
48 |
value => { flags => 3 } |
49 |
} |
50 |
); |
51 |
|
52 |
for ( 1 .. 10 ) { |
53 |
$builder->build_object( { class => 'Koha::RecordSources' } ); |
54 |
} |
55 |
|
56 |
my $nonprivilegedpatron = $builder->build_object( |
57 |
{ |
58 |
class => 'Koha::Patrons', |
59 |
value => { flags => 0 } |
60 |
} |
61 |
); |
62 |
|
63 |
my $password = 'thePassword123'; |
64 |
|
65 |
$nonprivilegedpatron->set_password( { password => $password, skip_validation => 1 } ); |
66 |
my $userid = $nonprivilegedpatron->userid; |
67 |
|
68 |
$t->get_ok("//$userid:$password@/api/v1/record_sources")->status_is(403) |
69 |
->json_is( '/error' => 'Authorization failure. Missing required permission(s).' ); |
70 |
|
71 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
72 |
$userid = $patron->userid; |
73 |
|
74 |
$t->get_ok("//$userid:$password@/api/v1/record_sources?_per_page=10")->status_is( 200, 'SWAGGER3.2.2' ); |
75 |
|
76 |
my $response_count = scalar @{ $t->tx->res->json }; |
77 |
|
78 |
is( $response_count, 10, 'The API returns 10 sources' ); |
79 |
|
80 |
my $id = $source->record_source_id; |
81 |
$t->get_ok("//$userid:$password@/api/v1/record_sources?q={\"record_source_id\": $id}")->status_is(200) |
82 |
->json_is( '' => [ $source->to_api ], 'SWAGGER3.3.2' ); |
83 |
|
84 |
$source->delete; |
85 |
|
86 |
$t->get_ok("//$userid:$password@/api/v1/record_sources?q={\"record_source_id\": $id}")->status_is(200) |
87 |
->json_is( '' => [] ); |
88 |
|
89 |
$schema->storage->txn_rollback; |
90 |
}; |
91 |
|
92 |
subtest 'get() tests' => sub { |
93 |
|
94 |
plan tests => 9; |
95 |
|
96 |
$schema->storage->txn_begin; |
97 |
|
98 |
my $source = $builder->build_object( { class => 'Koha::RecordSources' } ); |
99 |
my $patron = $builder->build_object( |
100 |
{ |
101 |
class => 'Koha::Patrons', |
102 |
value => { flags => 3 } |
103 |
} |
104 |
); |
105 |
|
106 |
my $nonprivilegedpatron = $builder->build_object( |
107 |
{ |
108 |
class => 'Koha::Patrons', |
109 |
value => { flags => 0 } |
110 |
} |
111 |
); |
112 |
|
113 |
my $password = 'thePassword123'; |
114 |
|
115 |
$nonprivilegedpatron->set_password( { password => $password, skip_validation => 1 } ); |
116 |
my $userid = $nonprivilegedpatron->userid; |
117 |
|
118 |
my $id = $source->record_source_id; |
119 |
|
120 |
$t->get_ok("//$userid:$password@/api/v1/record_sources/$id")->status_is(403) |
121 |
->json_is( '/error' => 'Authorization failure. Missing required permission(s).' ); |
122 |
|
123 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
124 |
$userid = $patron->userid; |
125 |
|
126 |
$t->get_ok("//$userid:$password@/api/v1/record_sources/$id")->status_is( 200, 'SWAGGER3.2.2' ) |
127 |
->json_is( '' => $source->to_api, 'SWAGGER3.3.2' ); |
128 |
|
129 |
$source->delete; |
130 |
|
131 |
$t->get_ok("//$userid:$password@/api/v1/record_sources/$id")->status_is(404) |
132 |
->json_is( '/error' => 'Object not found' ); |
133 |
|
134 |
$schema->storage->txn_rollback; |
135 |
}; |
136 |
|
137 |
subtest 'delete() tests' => sub { |
138 |
|
139 |
plan tests => 6; |
140 |
|
141 |
$schema->storage->txn_begin; |
142 |
|
143 |
my $source = $builder->build_object( { class => 'Koha::RecordSources' } ); |
144 |
my $patron = $builder->build_object( |
145 |
{ |
146 |
class => 'Koha::Patrons', |
147 |
value => { flags => 3 } |
148 |
} |
149 |
); |
150 |
|
151 |
my $nonprivilegedpatron = $builder->build_object( |
152 |
{ |
153 |
class => 'Koha::Patrons', |
154 |
value => { flags => 0 } |
155 |
} |
156 |
); |
157 |
|
158 |
my $password = 'thePassword123'; |
159 |
|
160 |
$nonprivilegedpatron->set_password( { password => $password, skip_validation => 1 } ); |
161 |
my $userid = $nonprivilegedpatron->userid; |
162 |
|
163 |
my $id = $source->record_source_id; |
164 |
|
165 |
$t->delete_ok("//$userid:$password@/api/v1/record_sources/$id")->status_is(403) |
166 |
->json_is( '/error' => 'Authorization failure. Missing required permission(s).' ); |
167 |
|
168 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
169 |
$userid = $patron->userid; |
170 |
|
171 |
$t->delete_ok("//$userid:$password@/api/v1/record_sources/$id")->status_is( 204, 'SWAGGER3.2.2' ); |
172 |
|
173 |
my $deleted_source = Koha::RecordSources->search( { record_source_id => $id } ); |
174 |
|
175 |
is( $deleted_source->count, 0, 'No record source found' ); |
176 |
|
177 |
$schema->storage->txn_rollback; |
178 |
}; |
179 |
|
180 |
subtest 'add() tests' => sub { |
181 |
|
182 |
plan tests => 8; |
183 |
|
184 |
$schema->storage->txn_begin; |
185 |
|
186 |
my $patron = $builder->build_object( |
187 |
{ |
188 |
class => 'Koha::Patrons', |
189 |
value => { flags => 3 } |
190 |
} |
191 |
); |
192 |
|
193 |
my $nonprivilegedpatron = $builder->build_object( |
194 |
{ |
195 |
class => 'Koha::Patrons', |
196 |
value => { flags => 0 } |
197 |
} |
198 |
); |
199 |
|
200 |
my $password = 'thePassword123'; |
201 |
|
202 |
$nonprivilegedpatron->set_password( { password => $password, skip_validation => 1 } ); |
203 |
my $userid = $nonprivilegedpatron->userid; |
204 |
my $patron_id = $nonprivilegedpatron->borrowernumber; |
205 |
|
206 |
$t->post_ok( "//$userid:$password@/api/v1/record_sources" => json => { name => 'test1' } )->status_is(403) |
207 |
->json_is( '/error' => 'Authorization failure. Missing required permission(s).' ); |
208 |
|
209 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
210 |
$userid = $patron->userid; |
211 |
|
212 |
my $source_id = |
213 |
$t->post_ok( "//$userid:$password@/api/v1/record_sources" => json => { name => 'test1' } ) |
214 |
->status_is( 201, 'SWAGGER3.2.2' )->json_is( '/name', 'test1' )->json_is( '/can_be_edited', 0 ) |
215 |
->tx->res->json->{record_source_id}; |
216 |
|
217 |
my $created_source = Koha::RecordSources->find($source_id); |
218 |
|
219 |
is( $created_source->name, 'test1', 'Record source found' ); |
220 |
|
221 |
$schema->storage->txn_rollback; |
222 |
}; |
223 |
|
224 |
subtest 'update() tests' => sub { |
225 |
|
226 |
plan tests => 15; |
227 |
|
228 |
$schema->storage->txn_begin; |
229 |
|
230 |
my $librarian = $builder->build_object( |
231 |
{ |
232 |
class => 'Koha::Patrons', |
233 |
value => { flags => 2**3 } # parameters flag = 2 |
234 |
} |
235 |
); |
236 |
my $password = 'thePassword123'; |
237 |
$librarian->set_password( { password => $password, skip_validation => 1 } ); |
238 |
my $userid = $librarian->userid; |
239 |
|
240 |
my $patron = $builder->build_object( |
241 |
{ |
242 |
class => 'Koha::Patrons', |
243 |
value => { flags => 0 } |
244 |
} |
245 |
); |
246 |
|
247 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
248 |
my $unauth_userid = $patron->userid; |
249 |
|
250 |
my $source = Koha::RecordSource->new( { name => 'old_name' } )->store; |
251 |
my $source_id = $source->id; |
252 |
|
253 |
# Unauthorized attempt to update |
254 |
$t->put_ok( "//$unauth_userid:$password@/api/v1/record_sources/$source_id" => json => |
255 |
{ name => 'New unauthorized name change' } )->status_is(403); |
256 |
|
257 |
# Attempt partial update on a PUT |
258 |
my $source_with_missing_field = {}; |
259 |
|
260 |
$t->put_ok( "//$userid:$password@/api/v1/record_sources/$source_id" => json => $source_with_missing_field ) |
261 |
->status_is(400)->json_is( "/errors" => [ { message => "Missing property.", path => "/body/name" } ] ); |
262 |
|
263 |
# Full object update on PUT |
264 |
my $source_with_updated_field = { |
265 |
name => "new_name", |
266 |
}; |
267 |
|
268 |
$t->put_ok( "//$userid:$password@/api/v1/record_sources/$source_id" => json => $source_with_updated_field ) |
269 |
->status_is(200)->json_is( '/name' => $source_with_updated_field->{name} ); |
270 |
|
271 |
# Authorized attempt to write invalid data |
272 |
my $source_with_invalid_field = { |
273 |
name => "blah", |
274 |
potato => "yeah", |
275 |
}; |
276 |
|
277 |
$t->put_ok( "//$userid:$password@/api/v1/record_sources/$source_id" => json => $source_with_invalid_field ) |
278 |
->status_is(400)->json_is( |
279 |
"/errors" => [ |
280 |
{ |
281 |
message => "Properties not allowed: potato.", |
282 |
path => "/body" |
283 |
} |
284 |
] |
285 |
); |
286 |
|
287 |
my $source_to_delete = $builder->build_object( { class => 'Koha::RecordSources' } ); |
288 |
my $non_existent_id = $source_to_delete->id; |
289 |
$source_to_delete->delete; |
290 |
|
291 |
$t->put_ok( "//$userid:$password@/api/v1/record_sources/$non_existent_id" => json => $source_with_updated_field ) |
292 |
->status_is(404); |
293 |
|
294 |
# Wrong method (POST) |
295 |
$source_with_updated_field->{record_source_id} = 2; |
296 |
|
297 |
$t->post_ok( "//$userid:$password@/api/v1/record_sources/$source_id" => json => $source_with_updated_field ) |
298 |
->status_is(404); |
299 |
|
300 |
$schema->storage->txn_rollback; |
301 |
}; |