Line 0
Link Here
|
0 |
- |
1 |
#!/usr/bin/env perl |
|
|
2 |
|
3 |
# Copyright Koha-Suomi Oy 2017 |
4 |
# |
5 |
# This file is part of Koha. |
6 |
# |
7 |
# Koha is free software; you can redistribute it and/or modify it under the |
8 |
# terms of the GNU General Public License as published by the Free Software |
9 |
# Foundation; either version 3 of the License, or (at your option) any later |
10 |
# version. |
11 |
# |
12 |
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY |
13 |
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
14 |
# A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
15 |
# |
16 |
# You should have received a copy of the GNU General Public License along |
17 |
# with Koha; if not, write to the Free Software Foundation, Inc., |
18 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
19 |
|
20 |
use Modern::Perl; |
21 |
|
22 |
|
23 |
use Test::More tests => 2; |
24 |
use Test::Mojo; |
25 |
use t::lib::Mocks; |
26 |
use t::lib::TestBuilder; |
27 |
|
28 |
use C4::Auth; |
29 |
use C4::Context; |
30 |
|
31 |
use Koha::Database; |
32 |
use Koha::Notice::Templates; |
33 |
use Koha::Patron::Message::Preferences; |
34 |
|
35 |
my $schema = Koha::Database->new->schema; |
36 |
my $builder = t::lib::TestBuilder->new; |
37 |
|
38 |
# FIXME: sessionStorage defaults to mysql, but it seems to break transaction handling |
39 |
# this affects the other REST api tests |
40 |
t::lib::Mocks::mock_preference( 'SessionStorage', 'tmp' ); |
41 |
|
42 |
my $remote_address = '127.0.0.1'; |
43 |
my $t = Test::Mojo->new('Koha::REST::V1'); |
44 |
|
45 |
subtest 'list() tests' => sub { |
46 |
plan tests => 34; |
47 |
|
48 |
$schema->storage->txn_begin; |
49 |
|
50 |
my $path = '/api/v1/messaging_preferences'; |
51 |
|
52 |
my ($patron, $session) = create_user_and_session(); |
53 |
my $borrowernumber = $patron->borrowernumber; |
54 |
my $categorycode = $patron->categorycode; |
55 |
my ($another_patron, undef) = create_user_and_session(); |
56 |
my ($librarian, $librarian_session) = create_user_and_session({ |
57 |
flags => 16 |
58 |
}); |
59 |
my ($preference, $mtt1, $mtt2) = build_a_test_complete_preference({ |
60 |
patron => $patron |
61 |
}); |
62 |
my ($preference2, $mtt1_2, $mtt2_2) = build_a_test_category_preference({ |
63 |
patron => $patron |
64 |
}); |
65 |
|
66 |
t::lib::Mocks::mock_preference('EnhancedMessagingPreferences', 1); |
67 |
my $tx = $t->ua->build_tx(GET => $path); |
68 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
69 |
$t->request_ok($tx) |
70 |
->status_is(401); |
71 |
|
72 |
$tx = $t->ua->build_tx(GET => "$path?borrowernumber=" |
73 |
.$another_patron->borrowernumber); |
74 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
75 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
76 |
$t->request_ok($tx) |
77 |
->status_is(403); |
78 |
|
79 |
$tx = $t->ua->build_tx(GET => "$path?categorycode=$categorycode"); |
80 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
81 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
82 |
$t->request_ok($tx) |
83 |
->status_is(403); |
84 |
|
85 |
$tx = $t->ua->build_tx(GET => $path); |
86 |
$tx->req->cookies({name => 'CGISESSID', value => $librarian_session->id}); |
87 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
88 |
$t->request_ok($tx) |
89 |
->status_is(400) |
90 |
->json_is('/error', 'Patron or category not found'); |
91 |
|
92 |
t::lib::Mocks::mock_preference('EnhancedMessagingPreferences', 0); |
93 |
$tx = $t->ua->build_tx(GET => "$path?borrowernumber=$borrowernumber"); |
94 |
$tx->req->cookies({name => 'CGISESSID', value => $librarian_session->id}); |
95 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
96 |
$t->request_ok($tx) |
97 |
->status_is(403) |
98 |
->json_is('/error' => 'Enhanced messaging preferences are not enabled'); |
99 |
t::lib::Mocks::mock_preference('EnhancedMessagingPreferences', 1); |
100 |
|
101 |
$tx = $t->ua->build_tx(GET => "$path?borrowernumber=$borrowernumber"); |
102 |
$tx->req->cookies({name => 'CGISESSID', value => $librarian_session->id}); |
103 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
104 |
$t->request_ok($tx) |
105 |
->status_is(200) |
106 |
->json_has('/'.$preference->message_name); |
107 |
|
108 |
$tx = $t->ua->build_tx(GET => "$path?borrowernumber=$borrowernumber"); |
109 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
110 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
111 |
$t->request_ok($tx) |
112 |
->status_is(200) |
113 |
->json_has('/'.$preference->message_name) |
114 |
->json_has('/'.$preference2->message_name) |
115 |
->json_is('/'.$preference->message_name.'/transport_types/' |
116 |
.$mtt1->message_transport_type => Mojo::JSON->true) |
117 |
->json_is('/'.$preference->message_name.'/transport_types/' |
118 |
.$mtt2->message_transport_type => Mojo::JSON->true) |
119 |
->json_is('/'.$preference->message_name.'/days_in_advance/configurable' |
120 |
=> Mojo::JSON->false) |
121 |
->json_is('/'.$preference->message_name.'/days_in_advance/value' |
122 |
=> undef) |
123 |
->json_is('/'.$preference->message_name.'/digest/configurable' |
124 |
=> Mojo::JSON->false) |
125 |
->json_is('/'.$preference->message_name.'/digest/value' |
126 |
=> Mojo::JSON->false); |
127 |
|
128 |
$tx = $t->ua->build_tx(GET => "$path?categorycode=$categorycode"); |
129 |
$tx->req->cookies({name => 'CGISESSID', value => $librarian_session->id}); |
130 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
131 |
$t->request_ok($tx) |
132 |
->status_is(200) |
133 |
->json_has('/'.$preference->message_name) |
134 |
->json_has('/'.$preference2->message_name) |
135 |
->json_is('/'.$preference2->message_name.'/transport_types/' |
136 |
.$mtt1_2->message_transport_type => Mojo::JSON->true) |
137 |
->json_is('/'.$preference2->message_name.'/transport_types/' |
138 |
.$mtt2_2->message_transport_type => Mojo::JSON->true) |
139 |
->json_is('/'.$preference2->message_name.'/days_in_advance/configurable' |
140 |
=> Mojo::JSON->false) |
141 |
->json_is('/'.$preference2->message_name.'/days_in_advance/value' |
142 |
=> undef) |
143 |
->json_is('/'.$preference2->message_name.'/digest/value' |
144 |
=> Mojo::JSON->false); |
145 |
|
146 |
$schema->storage->txn_rollback; |
147 |
}; |
148 |
|
149 |
subtest 'edit() tests' => sub { |
150 |
plan tests => 28; |
151 |
|
152 |
$schema->storage->txn_begin; |
153 |
|
154 |
my $path = '/api/v1/messaging_preferences'; |
155 |
|
156 |
my ($patron, $session) = create_user_and_session(); |
157 |
my ($librarian, $librarian_session) = create_user_and_session({ |
158 |
flags => 16 |
159 |
}); |
160 |
my ($another_patron, undef) = create_user_and_session(); |
161 |
my $borrowernumber = $patron->borrowernumber; |
162 |
my $categorycode = $patron->categorycode; |
163 |
my ($preference, $mtt1, $mtt2) = build_a_test_complete_preference({ |
164 |
patron => $patron |
165 |
}); |
166 |
my ($preference2, $mtt1_2, $mtt2_2) = build_a_test_category_preference({ |
167 |
patron => $patron |
168 |
}); |
169 |
|
170 |
my $edited_preference = { |
171 |
$preference->message_name => { |
172 |
digest => { |
173 |
value => Mojo::JSON->true |
174 |
}, |
175 |
days_in_advance => { |
176 |
value => 15 |
177 |
}, |
178 |
transport_types => { |
179 |
$mtt1->message_transport_type => Mojo::JSON->true, |
180 |
$mtt2->message_transport_type => Mojo::JSON->false |
181 |
} |
182 |
} |
183 |
}; |
184 |
|
185 |
t::lib::Mocks::mock_preference('EnhancedMessagingPreferences', 1); |
186 |
my $tx = $t->ua->build_tx(PUT => $path => json => $edited_preference); |
187 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
188 |
$t->request_ok($tx) |
189 |
->status_is(401); |
190 |
|
191 |
$tx = $t->ua->build_tx(PUT => "$path?borrowernumber=" |
192 |
.$another_patron->borrowernumber |
193 |
=> json => $edited_preference); |
194 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
195 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
196 |
$t->request_ok($tx) |
197 |
->status_is(403); |
198 |
|
199 |
$tx = $t->ua->build_tx(PUT => "$path?categorycode=$categorycode" |
200 |
=> json => $edited_preference); |
201 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
202 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
203 |
$t->request_ok($tx) |
204 |
->status_is(403); |
205 |
|
206 |
$tx = $t->ua->build_tx(PUT => $path => json => $edited_preference); |
207 |
$tx->req->cookies({name => 'CGISESSID', value => $librarian_session->id}); |
208 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
209 |
$t->request_ok($tx) |
210 |
->status_is(400) |
211 |
->json_is('/error', 'Patron or category not found'); |
212 |
|
213 |
t::lib::Mocks::mock_preference('EnhancedMessagingPreferences', 0); |
214 |
$tx = $t->ua->build_tx(PUT => "$path?borrowernumber=$borrowernumber" => |
215 |
json => $edited_preference); |
216 |
$tx->req->cookies({name => 'CGISESSID', value => $librarian_session->id}); |
217 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
218 |
$t->request_ok($tx) |
219 |
->status_is(403) |
220 |
->json_is('/error' => 'Enhanced messaging preferences are not enabled'); |
221 |
t::lib::Mocks::mock_preference('EnhancedMessagingPreferences', 1); |
222 |
|
223 |
$tx = $t->ua->build_tx(PUT => "$path?borrowernumber=$borrowernumber" => |
224 |
json => $edited_preference); |
225 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
226 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
227 |
$t->request_ok($tx) |
228 |
->status_is(400) |
229 |
->json_like('/error' => qr/^days_in_advance cannot/); |
230 |
|
231 |
$edited_preference->{$preference->message_name}-> |
232 |
{days_in_advance}->{value} = undef; |
233 |
$tx = $t->ua->build_tx(PUT => "$path?borrowernumber=$borrowernumber" => |
234 |
json => $edited_preference); |
235 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
236 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
237 |
$t->request_ok($tx) |
238 |
->status_is(400) |
239 |
->json_like('/error' => qr/igest not available/); |
240 |
|
241 |
$edited_preference->{$preference->message_name}->{digest}->{value} |
242 |
= Mojo::JSON->false; |
243 |
$tx = $t->ua->build_tx(PUT => "$path?borrowernumber=$borrowernumber" => |
244 |
json => $edited_preference); |
245 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
246 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
247 |
$t->request_ok($tx) |
248 |
->status_is(200) |
249 |
->json_is('/'.$preference->message_name.'/transport_types/'. |
250 |
$mtt2->message_transport_type => Mojo::JSON->false) |
251 |
->json_is('/'.$preference->message_name.'/transport_types/'. |
252 |
$mtt1->message_transport_type => Mojo::JSON->true); |
253 |
|
254 |
Koha::Patron::Message::Transports->search({ |
255 |
message_attribute_id => $preference->message_attribute_id, |
256 |
})->update({ is_digest => 1 }); |
257 |
$edited_preference->{$preference->message_name}->{digest}->{value} |
258 |
= Mojo::JSON->true; |
259 |
$tx = $t->ua->build_tx(PUT => "$path?borrowernumber=$borrowernumber" => |
260 |
json => $edited_preference); |
261 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
262 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
263 |
$t->request_ok($tx) |
264 |
->status_is(200) |
265 |
->json_is('/'.$preference->message_name.'/digest/value' => Mojo::JSON->true); |
266 |
|
267 |
# Test librarian access |
268 |
$tx = $t->ua->build_tx(PUT => "$path?borrowernumber=$borrowernumber" => |
269 |
json => $edited_preference); |
270 |
$tx->req->cookies({name => 'CGISESSID', value => $librarian_session->id}); |
271 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
272 |
$t->request_ok($tx) |
273 |
->status_is(200) |
274 |
->json_is('/'.$preference->message_name.'/digest/value' => Mojo::JSON->true); |
275 |
|
276 |
$schema->storage->txn_rollback; |
277 |
}; |
278 |
|
279 |
sub create_user_and_session { |
280 |
my ($params) = @_; |
281 |
|
282 |
my $categorycode = $builder->build({ source => 'Category' })->{categorycode}; |
283 |
my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode }; |
284 |
|
285 |
my $borrower = $builder->build({ |
286 |
source => 'Borrower', |
287 |
value => { |
288 |
branchcode => $branchcode, |
289 |
categorycode => $categorycode, |
290 |
flags => $params->{'flags'} || 0, |
291 |
} |
292 |
}); |
293 |
|
294 |
my $session = C4::Auth::get_session(''); |
295 |
$session->param('number', $borrower->{ borrowernumber }); |
296 |
$session->param('id', $borrower->{ userid }); |
297 |
$session->param('ip', '127.0.0.1'); |
298 |
$session->param('lasttime', time()); |
299 |
$session->flush; |
300 |
my $patron = Koha::Patrons->find($borrower->{borrowernumber}); |
301 |
|
302 |
return ($patron, $session); |
303 |
} |
304 |
|
305 |
|
306 |
sub build_a_test_attribute { |
307 |
my ($params) = @_; |
308 |
|
309 |
$params->{takes_days} = $params->{takes_days} && $params->{takes_days} > 0 |
310 |
? 1 : 0; |
311 |
|
312 |
my $attribute = $builder->build({ |
313 |
source => 'MessageAttribute', |
314 |
value => $params, |
315 |
}); |
316 |
|
317 |
return Koha::Patron::Message::Attributes->find( |
318 |
$attribute->{message_attribute_id} |
319 |
); |
320 |
} |
321 |
|
322 |
sub build_a_test_category { |
323 |
my $categorycode = $builder->build({ |
324 |
source => 'Category' })->{categorycode}; |
325 |
|
326 |
return Koha::Patron::Categories->find($categorycode); |
327 |
} |
328 |
|
329 |
sub build_a_test_letter { |
330 |
my ($params) = @_; |
331 |
|
332 |
my $mtt = $params->{mtt} ? $params->{mtt} : 'email'; |
333 |
my $branchcode = $builder->build({ |
334 |
source => 'Branch' })->{branchcode}; |
335 |
my $letter = $builder->build({ |
336 |
source => 'Letter', |
337 |
value => { |
338 |
branchcode => '', |
339 |
is_html => 0, |
340 |
message_transport_type => $mtt |
341 |
} |
342 |
}); |
343 |
|
344 |
return Koha::Notice::Templates->find({ |
345 |
module => $letter->{module}, |
346 |
code => $letter->{code}, |
347 |
branchcode => $letter->{branchcode}, |
348 |
}); |
349 |
} |
350 |
|
351 |
sub build_a_test_transport_type { |
352 |
my $mtt = $builder->build({ |
353 |
source => 'MessageTransportType' }); |
354 |
|
355 |
return Koha::Patron::Message::Transport::Types->find( |
356 |
$mtt->{message_transport_type} |
357 |
); |
358 |
} |
359 |
|
360 |
sub build_a_test_category_preference { |
361 |
my ($params) = @_; |
362 |
|
363 |
my $patron = $params->{patron}; |
364 |
my $attr = $params->{attr} |
365 |
? $params->{attr} |
366 |
: build_a_test_attribute($params->{days_in_advance}); |
367 |
|
368 |
my $letter = $params->{letter} ? $params->{letter} : build_a_test_letter(); |
369 |
my $mtt1 = $params->{mtt1} ? $params->{mtt1} : build_a_test_transport_type(); |
370 |
my $mtt2 = $params->{mtt2} ? $params->{mtt2} : build_a_test_transport_type(); |
371 |
|
372 |
Koha::Patron::Message::Transport->new({ |
373 |
message_attribute_id => $attr->message_attribute_id, |
374 |
message_transport_type => $mtt1->message_transport_type, |
375 |
is_digest => $params->{digest} ? 1 : 0, |
376 |
letter_module => $letter->module, |
377 |
letter_code => $letter->code, |
378 |
})->store; |
379 |
|
380 |
Koha::Patron::Message::Transport->new({ |
381 |
message_attribute_id => $attr->message_attribute_id, |
382 |
message_transport_type => $mtt2->message_transport_type, |
383 |
is_digest => $params->{digest} ? 1 : 0, |
384 |
letter_module => $letter->module, |
385 |
letter_code => $letter->code, |
386 |
})->store; |
387 |
|
388 |
my $default = Koha::Patron::Message::Preference->new({ |
389 |
categorycode => $patron->categorycode, |
390 |
message_attribute_id => $attr->message_attribute_id, |
391 |
wants_digest => $params->{digest} ? 1 : 0, |
392 |
days_in_advance => $params->{days_in_advance} |
393 |
? $params->{days_in_advance} : undef, |
394 |
})->store; |
395 |
|
396 |
Koha::Patron::Message::Transport::Preference->new({ |
397 |
borrower_message_preference_id => $default->borrower_message_preference_id, |
398 |
message_transport_type => $mtt1->message_transport_type, |
399 |
})->store; |
400 |
Koha::Patron::Message::Transport::Preference->new({ |
401 |
borrower_message_preference_id => $default->borrower_message_preference_id, |
402 |
message_transport_type => $mtt2->message_transport_type, |
403 |
})->store; |
404 |
|
405 |
return ($default, $mtt1, $mtt2); |
406 |
} |
407 |
|
408 |
sub build_a_test_complete_preference { |
409 |
my ($params) = @_; |
410 |
|
411 |
my ($default, $mtt1, $mtt2) = build_a_test_category_preference($params); |
412 |
my $patron = $params->{patron}; |
413 |
$patron->set_default_messaging_preferences; |
414 |
return (Koha::Patron::Message::Preferences->search({ |
415 |
borrowernumber => $patron->borrowernumber |
416 |
})->next, $mtt1, $mtt2); |
417 |
} |