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