Line 0
Link Here
|
0 |
- |
1 |
|
|
|
2 |
#!/usr/bin/env perl |
3 |
|
4 |
# This file is part of Koha. |
5 |
# |
6 |
# Koha is free software; you can redistribute it and/or modify it under the |
7 |
# terms of the GNU General Public License as published by the Free Software |
8 |
# Foundation; either version 3 of the License, or (at your option) any later |
9 |
# version. |
10 |
# |
11 |
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY |
12 |
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
13 |
# A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
14 |
# |
15 |
# You should have received a copy of the GNU General Public License along |
16 |
# with Koha; if not, write to the Free Software Foundation, Inc., |
17 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
18 |
|
19 |
use Modern::Perl; |
20 |
|
21 |
use Test::More tests => 1; |
22 |
use Test::Mojo; |
23 |
use Test::Warn; |
24 |
|
25 |
use t::lib::TestBuilder; |
26 |
use t::lib::Mocks; |
27 |
|
28 |
use C4::Auth; |
29 |
use Koha::Database; |
30 |
|
31 |
my $schema = Koha::Database->new->schema; |
32 |
my $builder = t::lib::TestBuilder->new; |
33 |
|
34 |
# FIXME: sessionStorage defaults to mysql, but it seems to break transaction handling |
35 |
# this affects the other REST api tests |
36 |
t::lib::Mocks::mock_preference( 'SessionStorage', 'tmp' ); |
37 |
|
38 |
my $remote_address = '127.0.0.1'; |
39 |
my $t = Test::Mojo->new('Koha::REST::V1'); |
40 |
|
41 |
subtest 'add() tests' => sub { |
42 |
plan tests => 2; |
43 |
|
44 |
$schema->storage->txn_begin; |
45 |
|
46 |
my ($club_with_enrollments, $club_without_enrollments, $item, @enrollments) = create_test_data(); |
47 |
|
48 |
unauthorized_access_tests('POST', "/api/v1/clubs/".$club_with_enrollments->id."/holds", undef, { |
49 |
biblio_id => $item->biblionumber, |
50 |
pickup_library_id => $item->home_branch->branchcode |
51 |
}); |
52 |
|
53 |
$schema->storage->txn_rollback; |
54 |
|
55 |
subtest 'librarian access tests' => sub { |
56 |
plan tests => 8; |
57 |
|
58 |
$schema->storage->txn_begin; |
59 |
|
60 |
my ($club_with_enrollments, $club_without_enrollments, $item, @enrollments) = create_test_data(); |
61 |
|
62 |
my ( undef, $session_id ) = create_user_and_session({ authorized => 1 }); |
63 |
my $data = { |
64 |
biblio_id => $item->biblionumber, |
65 |
pickup_library_id => $item->home_branch->branchcode |
66 |
}; |
67 |
my $tx = $t->ua->build_tx(POST => "/api/v1/clubs/".$club_without_enrollments->id."/holds" => json => $data); |
68 |
$tx->req->cookies({ name => 'CGISESSID', value => $session_id }); |
69 |
$t->request_ok($tx) |
70 |
->status_is(500) |
71 |
->json_is('/error' => "Cannot place a hold on a club without patrons."); |
72 |
|
73 |
$tx = $t->ua->build_tx(POST => "/api/v1/clubs/".$club_with_enrollments->id."/holds" => json => $data); |
74 |
$tx->req->cookies({ name => 'CGISESSID', value => $session_id }); |
75 |
$t->request_ok($tx) |
76 |
->status_is(201, 'Created Hold') |
77 |
->json_has('/club_hold_id', 'got a club hold id') |
78 |
->json_is( '/club_id' => $club_with_enrollments->id) |
79 |
->json_is( '/biblio_id' => $item->biblionumber); |
80 |
|
81 |
$schema->storage->txn_rollback; |
82 |
}; |
83 |
}; |
84 |
|
85 |
sub unauthorized_access_tests { |
86 |
my ($verb, $endpoint, $club_hold_id, $json) = @_; |
87 |
|
88 |
$endpoint .= ($club_hold_id) ? "/$club_hold_id" : ''; |
89 |
|
90 |
subtest 'unauthorized access tests' => sub { |
91 |
plan tests => 5; |
92 |
|
93 |
my $tx = $t->ua->build_tx($verb => $endpoint => json => $json); |
94 |
$t->request_ok($tx) |
95 |
->status_is(401); |
96 |
|
97 |
my ($borrowernumber, $session_id) = create_user_and_session({ |
98 |
authorized => 0 }); |
99 |
|
100 |
$tx = $t->ua->build_tx($verb => $endpoint => json => $json); |
101 |
$tx->req->cookies({name => 'CGISESSID', value => $session_id}); |
102 |
$t->request_ok($tx) |
103 |
->status_is(403) |
104 |
->json_has('/required_permissions'); |
105 |
}; |
106 |
} |
107 |
|
108 |
sub create_user_and_session { |
109 |
|
110 |
my $args = shift; |
111 |
my $flags = ( $args->{authorized} ) ? 64 : 0; |
112 |
|
113 |
my $user = $builder->build( |
114 |
{ |
115 |
source => 'Borrower', |
116 |
value => { |
117 |
flags => $flags, |
118 |
gonenoaddress => 0, |
119 |
lost => 0, |
120 |
email => 'nobody@example.com', |
121 |
emailpro => 'nobody@example.com', |
122 |
B_email => 'nobody@example.com' |
123 |
} |
124 |
} |
125 |
); |
126 |
|
127 |
# Create a session for the authorized user |
128 |
my $session = C4::Auth::get_session(''); |
129 |
$session->param( 'number', $user->{borrowernumber} ); |
130 |
$session->param( 'id', $user->{userid} ); |
131 |
$session->param( 'ip', '127.0.0.1' ); |
132 |
$session->param( 'lasttime', time() ); |
133 |
$session->flush; |
134 |
|
135 |
return ( $user->{borrowernumber}, $session->id ); |
136 |
} |
137 |
|
138 |
sub create_test_data { |
139 |
my $club_with_enrollments = $builder->build_object( { class => 'Koha::Clubs' } ); |
140 |
my $club_without_enrollments = $builder->build_object( { class => 'Koha::Clubs' } ); |
141 |
my $enrollment1 = $builder->build_object( { class => 'Koha::Club::Enrollments', value => { club_id => $club_with_enrollments->id, date_canceled => undef } } ); |
142 |
my $enrollment2 = $builder->build_object( { class => 'Koha::Club::Enrollments', value => { club_id => $club_with_enrollments->id, date_canceled => undef } } ); |
143 |
my $enrollment3 = $builder->build_object( { class => 'Koha::Club::Enrollments', value => { club_id => $club_with_enrollments->id, date_canceled => undef } } ); |
144 |
my $enrollment4 = $builder->build_object( { class => 'Koha::Club::Enrollments', value => { club_id => $club_with_enrollments->id, date_canceled => undef } } ); |
145 |
my $enrollment5 = $builder->build_object( { class => 'Koha::Club::Enrollments', value => { club_id => $club_with_enrollments->id, date_canceled => undef } } ); |
146 |
my $enrollment6 = $builder->build_object( { class => 'Koha::Club::Enrollments', value => { club_id => $club_with_enrollments->id, date_canceled => undef } } ); |
147 |
my $item = $builder->build_sample_item(); |
148 |
return ( $club_with_enrollments, $club_without_enrollments, $item, [ $enrollment1, $enrollment2, $enrollment3, $enrollment4, $enrollment5, $enrollment6 ] ); |
149 |
} |