Line 0
Link Here
|
|
|
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 under the |
6 |
# terms of the GNU General Public License as published by the Free Software |
7 |
# Foundation; either version 3 of the License, or (at your option) any later |
8 |
# version. |
9 |
# |
10 |
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY |
11 |
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
12 |
# A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
13 |
# |
14 |
# You should have received a copy of the GNU General Public License |
15 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
16 |
|
17 |
use Modern::Perl; |
18 |
|
19 |
use Test::More tests => 5; |
20 |
use Test::Mojo; |
21 |
use Test::Warn; |
22 |
|
23 |
use t::lib::TestBuilder; |
24 |
use t::lib::Mocks; |
25 |
|
26 |
use Koha::SearchFilters; |
27 |
use Koha::Database; |
28 |
|
29 |
my $schema = Koha::Database->new->schema; |
30 |
my $builder = t::lib::TestBuilder->new; |
31 |
|
32 |
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 ); |
33 |
|
34 |
my $t = Test::Mojo->new('Koha::REST::V1'); |
35 |
|
36 |
$schema->storage->txn_begin; |
37 |
|
38 |
subtest 'list() tests' => sub { |
39 |
plan tests => 10; |
40 |
|
41 |
Koha::SearchFilters->search()->delete(); |
42 |
|
43 |
my $patron_1 = $builder->build_object({ |
44 |
class => 'Koha::Patrons', |
45 |
value => { flags => 3 } |
46 |
}); |
47 |
my $password = 'thePassword123'; |
48 |
$patron_1->set_password({ password => $password, skip_validation => 1 }); |
49 |
my $userid = $patron_1->userid; |
50 |
|
51 |
# Create test context |
52 |
my $search_filter_1 = $builder->build_object({ class => 'Koha::SearchFilters', value => |
53 |
{ |
54 |
name => 'Test1', |
55 |
query => 'kw:this', |
56 |
limits => 'mc-itype,phr:BK', |
57 |
opac => 1, |
58 |
staff_client => 1 |
59 |
} |
60 |
}); |
61 |
my $search_filter_2 = $builder->build_object({ class => 'Koha::SearchFilters', value => |
62 |
{ |
63 |
name => 'Test2', |
64 |
query => 'kw:that', |
65 |
limits => 'mc-itype,phr:BK', |
66 |
opac => 0, |
67 |
staff_client => 1 |
68 |
} |
69 |
}); |
70 |
my $search_filter_3 = $builder->build_object({ class => 'Koha::SearchFilters', value => |
71 |
{ |
72 |
name => 'Test3', |
73 |
query => 'kw:any', |
74 |
limits => 'mc-itype,phr:CD', |
75 |
opac => 0, |
76 |
staff_client => 0 |
77 |
} |
78 |
}); |
79 |
my $search_filter_4 = $builder->build_object({ class => 'Koha::SearchFilters', value => |
80 |
{ |
81 |
name => 'Test4', |
82 |
query => 'kw:some', |
83 |
limits => 'mc-itype,phr:CD', |
84 |
opac => 1, |
85 |
staff_client => 0 |
86 |
} |
87 |
}); |
88 |
|
89 |
# Make sure we are returned with the correct amount of macros |
90 |
$t->get_ok( "//$userid:$password@/api/v1/search_filters" ) |
91 |
->status_is( 200, 'SWAGGER3.2.2' ) |
92 |
->json_has('/0/search_filter_id') |
93 |
->json_has('/1/search_filter_id') |
94 |
->json_has('/2/search_filter_id') |
95 |
->json_has('/3/search_filter_id'); |
96 |
|
97 |
subtest 'query parameters' => sub { |
98 |
|
99 |
plan tests => 12; |
100 |
$t->get_ok("//$userid:$password@/api/v1/search_filters?name=" . $search_filter_2->name) |
101 |
->status_is(200) |
102 |
->json_is( [ $search_filter_2->to_api ] ); |
103 |
$t->get_ok("//$userid:$password@/api/v1/search_filters?name=NotAName") |
104 |
->status_is(200) |
105 |
->json_is( [ ] ); |
106 |
$t->get_ok("//$userid:$password@/api/v1/search_filters?filter_query=kw:any") |
107 |
->status_is(200) |
108 |
->json_is( [ $search_filter_3->to_api ] ); |
109 |
$t->get_ok("//$userid:$password@/api/v1/search_filters?filter_limits=mc-itype,phr:BK") |
110 |
->status_is(200) |
111 |
->json_is( [ $search_filter_1->to_api, $search_filter_2->to_api ] ); |
112 |
}; |
113 |
|
114 |
# Warn on unsupported query parameter |
115 |
$t->get_ok( "//$userid:$password@/api/v1/search_filters?filter_blah=blah" ) |
116 |
->status_is(400) |
117 |
->json_is( [{ path => '/query/filter_blah', message => 'Malformed query string'}] ); |
118 |
|
119 |
}; |
120 |
|
121 |
subtest 'get() tests' => sub { |
122 |
|
123 |
plan tests => 9; |
124 |
|
125 |
my $patron = $builder->build_object({ |
126 |
class => 'Koha::Patrons', |
127 |
value => { flags => 3 } |
128 |
}); |
129 |
my $password = 'thePassword123'; |
130 |
$patron->set_password({ password => $password, skip_validation => 1 }); |
131 |
my $userid = $patron->userid; |
132 |
|
133 |
my $search_filter_1 = $builder->build_object( { class => 'Koha::SearchFilters' } ); |
134 |
my $search_filter_2 = $builder->build_object( { class => 'Koha::SearchFilters' } ); |
135 |
my $search_filter_3 = $builder->build_object( { class => 'Koha::SearchFilters' } ); |
136 |
|
137 |
$t->get_ok( "//$userid:$password@/api/v1/search_filters/" . $search_filter_1->id ) |
138 |
->status_is( 200, 'Filter retrieved correctly' ) |
139 |
->json_is( $search_filter_1->to_api ); |
140 |
|
141 |
my $non_existent_code = $search_filter_1->id; |
142 |
$search_filter_1->delete; |
143 |
|
144 |
$t->get_ok( "//$userid:$password@/api/v1/search_filters/" . $non_existent_code ) |
145 |
->status_is(404) |
146 |
->json_is( '/error' => 'Search filter not found' ); |
147 |
|
148 |
$patron->flags(4)->store; |
149 |
$t->get_ok( "//$userid:$password/api/v1/search_filters/" . $search_filter_2->id ) |
150 |
->status_is( 401, 'Cannot search filters without permission' ) |
151 |
->json_is( '/error' => 'Authentication failure.' ); |
152 |
|
153 |
}; |
154 |
|
155 |
subtest 'add() tests' => sub { |
156 |
|
157 |
plan tests => 17; |
158 |
|
159 |
my $authorized_patron = $builder->build_object({ |
160 |
class => 'Koha::Patrons', |
161 |
value => { flags => 0 } |
162 |
}); |
163 |
$builder->build({ |
164 |
source => 'UserPermission', |
165 |
value => { |
166 |
borrowernumber => $authorized_patron->borrowernumber, |
167 |
module_bit => 3, |
168 |
code => 'manage_search_filters', |
169 |
}, |
170 |
}); |
171 |
|
172 |
my $password = 'thePassword123'; |
173 |
$authorized_patron->set_password({ password => $password, skip_validation => 1 }); |
174 |
my $auth_userid = $authorized_patron->userid; |
175 |
|
176 |
my $unauthorized_patron = $builder->build_object({ |
177 |
class => 'Koha::Patrons', |
178 |
value => { flags => 0 } |
179 |
}); |
180 |
$unauthorized_patron->set_password({ password => $password, skip_validation => 1 }); |
181 |
my $unauth_userid = $unauthorized_patron->userid; |
182 |
|
183 |
my $search_filter = $builder->build_object({ class => 'Koha::SearchFilters' }); |
184 |
my $search_filter_values = $search_filter->to_api; |
185 |
delete $search_filter_values->{search_filter_id}; |
186 |
$search_filter->delete; |
187 |
|
188 |
# Unauthorized attempt to write |
189 |
$t->post_ok( "//$unauth_userid:$password@/api/v1/search_filters" => json => $search_filter_values ) |
190 |
->status_is(403); |
191 |
|
192 |
# Authorized attempt to write invalid data |
193 |
my $search_filter_with_invalid_field = { %$search_filter_values }; |
194 |
$search_filter_with_invalid_field->{'coffee_filter'} = 'Chemex'; |
195 |
|
196 |
$t->post_ok( "//$auth_userid:$password@/api/v1/search_filters" => json => $search_filter_with_invalid_field ) |
197 |
->status_is(400) |
198 |
->json_is( |
199 |
"/errors" => [ |
200 |
{ |
201 |
message => "Properties not allowed: coffee_filter.", |
202 |
path => "/body" |
203 |
} |
204 |
] |
205 |
); |
206 |
|
207 |
# Authorized attempt to write |
208 |
$t->post_ok( "//$auth_userid:$password@/api/v1/search_filters" => json => $search_filter_values ) |
209 |
->status_is( 201, 'SWAGGER3.2.1' ) |
210 |
->json_has( '/search_filter_id', 'We generated a new id' ) |
211 |
->json_is( '/name' => $search_filter_values->{name}, 'The name matches what we supplied' ) |
212 |
->json_is( '/query' => $search_filter_values->{query}, 'The query matches what we supplied' ) |
213 |
->json_is( '/limits' => $search_filter_values->{limits}, 'The limits match what we supplied' ) |
214 |
->json_is( '/opac' => $search_filter_values->{opac}, 'The limits match what we supplied' ) |
215 |
->json_is( '/staff_client' => $search_filter_values->{staff_client}, 'The limits match what we supplied' ) |
216 |
->header_like( Location => qr|^\/api\/v1\/search_filters\/d*|, 'Correct location' ); |
217 |
|
218 |
# save the library_id |
219 |
my $search_filter_id = 999; |
220 |
|
221 |
# Authorized attempt to create with existing id |
222 |
$search_filter_values->{search_filter_id} = $search_filter_id; |
223 |
|
224 |
$t->post_ok( "//$auth_userid:$password@/api/v1/search_filters" => json => $search_filter_values ) |
225 |
->status_is(400) |
226 |
->json_is( '/errors' => [ |
227 |
{ |
228 |
message => "Read-only.", |
229 |
path => "/body/search_filter_id" |
230 |
} |
231 |
] |
232 |
); |
233 |
|
234 |
}; |
235 |
|
236 |
subtest 'update() tests' => sub { |
237 |
plan tests => 15; |
238 |
|
239 |
my $authorized_patron = $builder->build_object({ |
240 |
class => 'Koha::Patrons', |
241 |
value => { flags => 0 } |
242 |
}); |
243 |
$builder->build({ |
244 |
source => 'UserPermission', |
245 |
value => { |
246 |
borrowernumber => $authorized_patron->borrowernumber, |
247 |
module_bit => 3, |
248 |
code => 'manage_search_filters', |
249 |
}, |
250 |
}); |
251 |
|
252 |
my $password = 'thePassword123'; |
253 |
$authorized_patron->set_password({ password => $password, skip_validation => 1 }); |
254 |
my $auth_userid = $authorized_patron->userid; |
255 |
|
256 |
my $unauthorized_patron = $builder->build_object({ |
257 |
class => 'Koha::Patrons', |
258 |
value => { flags => 0 } |
259 |
}); |
260 |
$unauthorized_patron->set_password({ password => $password, skip_validation => 1 }); |
261 |
my $unauth_userid = $unauthorized_patron->userid; |
262 |
|
263 |
my $search_filter = $builder->build_object({ class => 'Koha::SearchFilters' }); |
264 |
my $search_filter_id = $search_filter->id; |
265 |
my $search_filter_values = $search_filter->to_api; |
266 |
delete $search_filter_values->{search_filter_id}; |
267 |
|
268 |
# Unauthorized attempt to update |
269 |
$t->put_ok( "//$unauth_userid:$password@/api/v1/search_filters/$search_filter_id" |
270 |
=> json => { name => 'New unauthorized name change' } ) |
271 |
->status_is(403); |
272 |
|
273 |
my $search_filter_update = { |
274 |
name => "Filter update", |
275 |
filter_query => "ti:The hobbit", |
276 |
filter_limits => "mc-ccode:fantasy", |
277 |
}; |
278 |
|
279 |
my $test = $t->put_ok( "//$auth_userid:$password@/api/v1/search_filters/$search_filter_id" => json => $search_filter_update ) |
280 |
->status_is(200, 'Authorized user can update a macro') |
281 |
->json_is( '/search_filter_id' => $search_filter_id, 'We get back the id' ) |
282 |
->json_is( '/name' => $search_filter_update->{name}, 'We get back the name' ) |
283 |
->json_is( '/filter_query' => $search_filter_update->{filter_query}, 'We get back our query' ) |
284 |
->json_is( '/filter_limits' => $search_filter_update->{filter_limits}, 'We get back our limits' ) |
285 |
->json_is( '/opac' => 1, 'We get back our opac visibility unchanged' ) |
286 |
->json_is( '/staff_client' => 1, 'We get back our staff client visibility unchanged' ); |
287 |
|
288 |
# Authorized attempt to write invalid data |
289 |
my $search_filter_with_invalid_field = { %$search_filter_update }; |
290 |
$search_filter_with_invalid_field->{'coffee_filter'} = 'Chemex'; |
291 |
|
292 |
$t->put_ok( "//$auth_userid:$password@/api/v1/search_filters/$search_filter_id" => json => $search_filter_with_invalid_field ) |
293 |
->status_is(400) |
294 |
->json_is( |
295 |
"/errors" => [ |
296 |
{ |
297 |
message => "Properties not allowed: coffee_filter.", |
298 |
path => "/body" |
299 |
} |
300 |
] |
301 |
); |
302 |
|
303 |
my $non_existent_macro = $builder->build_object({class => 'Koha::SearchFilters'}); |
304 |
my $non_existent_code = $non_existent_macro->id; |
305 |
$non_existent_macro->delete; |
306 |
|
307 |
$t->put_ok("//$auth_userid:$password@/api/v1/search_filters/$non_existent_code" => json => $search_filter_update) |
308 |
->status_is(404); |
309 |
|
310 |
}; |
311 |
|
312 |
subtest 'delete() tests' => sub { |
313 |
plan tests => 4; |
314 |
|
315 |
my $authorized_patron = $builder->build_object({ |
316 |
class => 'Koha::Patrons', |
317 |
value => { flags => 0 } |
318 |
}); |
319 |
$builder->build({ |
320 |
source => 'UserPermission', |
321 |
value => { |
322 |
borrowernumber => $authorized_patron->borrowernumber, |
323 |
module_bit => 3, |
324 |
code => 'manage_search_filters', |
325 |
}, |
326 |
}); |
327 |
|
328 |
my $password = 'thePassword123'; |
329 |
$authorized_patron->set_password({ password => $password, skip_validation => 1 }); |
330 |
my $auth_userid = $authorized_patron->userid; |
331 |
|
332 |
my $unauthorized_patron = $builder->build_object({ |
333 |
class => 'Koha::Patrons', |
334 |
value => { flags => 0 } |
335 |
}); |
336 |
$unauthorized_patron->set_password({ password => $password, skip_validation => 1 }); |
337 |
my $unauth_userid = $unauthorized_patron->userid; |
338 |
|
339 |
my $search_filter = $builder->build_object({ class => 'Koha::SearchFilters' }); |
340 |
my $search_filter_2 = $builder->build_object({ class => 'Koha::SearchFilters' }); |
341 |
my $search_filter_id = $search_filter->id; |
342 |
my $search_filter_2_id = $search_filter_2->id; |
343 |
|
344 |
# Unauthorized attempt to delete |
345 |
$t->delete_ok( "//$unauth_userid:$password@/api/v1/search_filters/$search_filter_2_id") |
346 |
->status_is(403, "Cannot delete search filter without permission"); |
347 |
|
348 |
$t->delete_ok( "//$auth_userid:$password@/api/v1/search_filters/$search_filter_id") |
349 |
->status_is( 204, 'Can delete search filter with permission'); |
350 |
|
351 |
}; |
352 |
|
353 |
$schema->storage->txn_rollback; |