|
Lines 36-47
my $schema = Koha::Database->new->schema;
Link Here
|
| 36 |
my $builder = t::lib::TestBuilder->new; |
36 |
my $builder = t::lib::TestBuilder->new; |
| 37 |
my $dbh = C4::Context->dbh; |
37 |
my $dbh = C4::Context->dbh; |
| 38 |
|
38 |
|
| 39 |
# FIXME: sessionStorage defaults to mysql, but it seems to break transaction handling |
39 |
my $t = Test::Mojo->new('Koha::REST::V1'); |
| 40 |
# this affects the other REST api tests |
40 |
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 ); |
| 41 |
t::lib::Mocks::mock_preference( 'SessionStorage', 'tmp' ); |
|
|
| 42 |
|
| 43 |
my $remote_address = '127.0.0.1'; |
| 44 |
my $t = Test::Mojo->new('Koha::REST::V1'); |
| 45 |
|
41 |
|
| 46 |
subtest 'add() tests' => sub { |
42 |
subtest 'add() tests' => sub { |
| 47 |
plan tests => 2; |
43 |
plan tests => 2; |
|
Lines 50-59
subtest 'add() tests' => sub {
Link Here
|
| 50 |
|
46 |
|
| 51 |
my ($club_with_enrollments, $club_without_enrollments, $item, @enrollments) = create_test_data(); |
47 |
my ($club_with_enrollments, $club_without_enrollments, $item, @enrollments) = create_test_data(); |
| 52 |
|
48 |
|
| 53 |
unauthorized_access_tests('POST', "/api/v1/clubs/".$club_with_enrollments->id."/holds", undef, { |
49 |
unauthorized_access_tests( |
| 54 |
biblio_id => $item->biblionumber, |
50 |
'POST', |
| 55 |
pickup_library_id => $item->home_branch->branchcode |
51 |
"/api/v1/clubs/" . $club_with_enrollments->id . "/holds", |
| 56 |
}); |
52 |
undef, |
|
|
53 |
{ |
| 54 |
biblio_id => $item->biblionumber, |
| 55 |
pickup_library_id => $item->home_branch->branchcode |
| 56 |
} |
| 57 |
); |
| 57 |
|
58 |
|
| 58 |
$schema->storage->txn_rollback; |
59 |
$schema->storage->txn_rollback; |
| 59 |
|
60 |
|
|
Lines 65-91
subtest 'add() tests' => sub {
Link Here
|
| 65 |
my ($club_with_enrollments, $club_without_enrollments, $item, @enrollments) = create_test_data(); |
66 |
my ($club_with_enrollments, $club_without_enrollments, $item, @enrollments) = create_test_data(); |
| 66 |
my $club_with_enrollments_id = $club_with_enrollments->id; |
67 |
my $club_with_enrollments_id = $club_with_enrollments->id; |
| 67 |
|
68 |
|
| 68 |
my ( undef, $session_id ) = create_user_and_session({ authorized => 1 }); |
69 |
my $librarian = $builder->build_object( |
|
|
70 |
{ |
| 71 |
class => 'Koha::Patrons', |
| 72 |
value => { flags => 2**6 } # reserveforothers flag = 6 |
| 73 |
} |
| 74 |
); |
| 75 |
my $password = 'thePassword123'; |
| 76 |
$librarian->set_password( { password => $password, skip_validation => 1 } ); |
| 77 |
my $userid = $librarian->userid; |
| 78 |
|
| 69 |
my $data = { |
79 |
my $data = { |
| 70 |
biblio_id => $item->biblionumber, |
80 |
biblio_id => $item->biblionumber, |
| 71 |
pickup_library_id => $item->home_branch->branchcode |
81 |
pickup_library_id => $item->home_branch->branchcode |
| 72 |
}; |
82 |
}; |
| 73 |
my $tx = $t->ua->build_tx(POST => "/api/v1/clubs/".$club_without_enrollments->id."/holds" => json => $data); |
83 |
|
| 74 |
$tx->req->cookies({ name => 'CGISESSID', value => $session_id }); |
84 |
$t->post_ok( "//$userid:$password@/api/v1/clubs/" |
| 75 |
$t->request_ok($tx) |
85 |
. $club_without_enrollments->id |
| 76 |
->status_is(500) |
86 |
. "/holds" => json => $data ) |
| 77 |
->json_is('/error' => "Cannot place a hold on a club without patrons."); |
87 |
->status_is(500) |
| 78 |
|
88 |
->json_is( '/error' => "Cannot place a hold on a club without patrons." ); |
| 79 |
$tx = $t->ua->build_tx(POST => "/api/v1/clubs/".$club_with_enrollments->id."/holds" => json => $data); |
89 |
|
| 80 |
$tx->req->cookies({ name => 'CGISESSID', value => $session_id }); |
90 |
$t->post_ok( "//$userid:$password@/api/v1/clubs/" |
| 81 |
$t->request_ok($tx) |
91 |
. $club_with_enrollments->id |
| 82 |
->status_is(201, 'Created Hold') |
92 |
. "/holds" => json => $data ) |
| 83 |
->json_has('/club_hold_id', 'got a club hold id') |
93 |
->status_is( 201, 'Created Hold' ) |
| 84 |
->json_is( '/club_id' => $club_with_enrollments->id) |
94 |
->json_has( '/club_hold_id', 'got a club hold id' ) |
| 85 |
->json_is( '/biblio_id' => $item->biblionumber) |
95 |
->json_is( '/club_id' => $club_with_enrollments->id ) |
| 86 |
->header_like( |
96 |
->json_is( '/biblio_id' => $item->biblionumber )->header_like( |
| 87 |
Location => qr|^\/api\/v1\/clubs/$club_with_enrollments_id/holds|, |
97 |
Location => qr|^\/api\/v1\/clubs/$club_with_enrollments_id/holds|, |
| 88 |
'SWAGGER3.4.1' |
98 |
'SWAGGER3.4.1' |
| 89 |
); |
99 |
); |
| 90 |
|
100 |
|
| 91 |
$schema->storage->txn_rollback; |
101 |
$schema->storage->txn_rollback; |
|
Lines 100-119
subtest "default patron home" => sub {
Link Here
|
| 100 |
my ($club_with_enrollments, $club_without_enrollments, $item, @enrollments) = create_test_data(); |
110 |
my ($club_with_enrollments, $club_without_enrollments, $item, @enrollments) = create_test_data(); |
| 101 |
my $club_with_enrollments_id = $club_with_enrollments->id; |
111 |
my $club_with_enrollments_id = $club_with_enrollments->id; |
| 102 |
|
112 |
|
| 103 |
my ( undef, $session_id ) = create_user_and_session({ authorized => 1 }); |
113 |
my $librarian = $builder->build_object( |
|
|
114 |
{ |
| 115 |
class => 'Koha::Patrons', |
| 116 |
value => { flags => 2**6 } # reserveforothers flag = 6 |
| 117 |
} |
| 118 |
); |
| 119 |
my $password = 'thePassword123'; |
| 120 |
$librarian->set_password( { password => $password, skip_validation => 1 } ); |
| 121 |
my $userid = $librarian->userid; |
| 122 |
|
| 104 |
my $data = { |
123 |
my $data = { |
| 105 |
biblio_id => $item->biblionumber, |
124 |
biblio_id => $item->biblionumber, |
| 106 |
pickup_library_id => $item->home_branch->branchcode, |
125 |
pickup_library_id => $item->home_branch->branchcode, |
| 107 |
default_patron_home => 1 |
126 |
default_patron_home => 1 |
| 108 |
}; |
127 |
}; |
| 109 |
|
128 |
|
| 110 |
my $tx = $t->ua->build_tx(POST => "/api/v1/clubs/".$club_with_enrollments->id."/holds" => json => $data); |
129 |
$t->post_ok( "//$userid:$password@/api/v1/clubs/" |
| 111 |
$tx->req->cookies({ name => 'CGISESSID', value => $session_id }); |
130 |
. $club_with_enrollments->id |
| 112 |
$t->request_ok($tx) |
131 |
. "/holds" => json => $data ) |
| 113 |
->status_is(201, 'Created Hold') |
132 |
->status_is( 201, 'Created Hold' ) |
| 114 |
->header_like( |
133 |
->header_like( |
| 115 |
Location => qr|^\/api\/v1\/clubs/$club_with_enrollments_id/holds|, |
134 |
Location => qr|^\/api\/v1\/clubs/$club_with_enrollments_id/holds|, |
| 116 |
'SWAGGER3.4.1' |
135 |
'SWAGGER3.4.1' |
| 117 |
); |
136 |
); |
| 118 |
|
137 |
|
| 119 |
my $json_response = decode_json $t->tx->res->content->get_body_chunk; |
138 |
my $json_response = decode_json $t->tx->res->content->get_body_chunk; |
|
Lines 138-188
sub unauthorized_access_tests {
Link Here
|
| 138 |
subtest 'unauthorized access tests' => sub { |
157 |
subtest 'unauthorized access tests' => sub { |
| 139 |
plan tests => 5; |
158 |
plan tests => 5; |
| 140 |
|
159 |
|
| 141 |
my $tx = $t->ua->build_tx($verb => $endpoint => json => $json); |
160 |
my $verb_ok = lc($verb) . '_ok'; |
| 142 |
$t->request_ok($tx) |
161 |
|
|
|
162 |
$t->$verb_ok($endpoint => json => $json) |
| 143 |
->status_is(401); |
163 |
->status_is(401); |
| 144 |
|
164 |
|
| 145 |
my ($borrowernumber, $session_id) = create_user_and_session({ |
165 |
my $unauthorized_patron = $builder->build_object( |
| 146 |
authorized => 0 }); |
166 |
{ |
|
|
167 |
class => 'Koha::Patrons', |
| 168 |
value => { flags => 0 } |
| 169 |
} |
| 170 |
); |
| 171 |
my $password = "thePassword123!"; |
| 172 |
$unauthorized_patron->set_password( |
| 173 |
{ password => $password, skip_validation => 1 } ); |
| 174 |
my $unauth_userid = $unauthorized_patron->userid; |
| 147 |
|
175 |
|
| 148 |
$tx = $t->ua->build_tx($verb => $endpoint => json => $json); |
176 |
$t->$verb_ok( "//$unauth_userid:$password\@$endpoint" => json => $json ) |
| 149 |
$tx->req->cookies({name => 'CGISESSID', value => $session_id}); |
|
|
| 150 |
$t->request_ok($tx) |
| 151 |
->status_is(403) |
177 |
->status_is(403) |
| 152 |
->json_has('/required_permissions'); |
178 |
->json_has('/required_permissions'); |
| 153 |
}; |
179 |
}; |
| 154 |
} |
180 |
} |
| 155 |
|
181 |
|
| 156 |
sub create_user_and_session { |
|
|
| 157 |
|
| 158 |
my $args = shift; |
| 159 |
my $flags = ( $args->{authorized} ) ? 64 : 0; |
| 160 |
|
| 161 |
my $user = $builder->build( |
| 162 |
{ |
| 163 |
source => 'Borrower', |
| 164 |
value => { |
| 165 |
flags => $flags, |
| 166 |
gonenoaddress => 0, |
| 167 |
lost => 0, |
| 168 |
email => 'nobody@example.com', |
| 169 |
emailpro => 'nobody@example.com', |
| 170 |
B_email => 'nobody@example.com' |
| 171 |
} |
| 172 |
} |
| 173 |
); |
| 174 |
|
| 175 |
# Create a session for the authorized user |
| 176 |
my $session = C4::Auth::get_session(''); |
| 177 |
$session->param( 'number', $user->{borrowernumber} ); |
| 178 |
$session->param( 'id', $user->{userid} ); |
| 179 |
$session->param( 'ip', '127.0.0.1' ); |
| 180 |
$session->param( 'lasttime', time() ); |
| 181 |
$session->flush; |
| 182 |
|
| 183 |
return ( $user->{borrowernumber}, $session->id ); |
| 184 |
} |
| 185 |
|
| 186 |
sub create_test_data { |
182 |
sub create_test_data { |
| 187 |
my $club_with_enrollments = $builder->build_object( { class => 'Koha::Clubs' } ); |
183 |
my $club_with_enrollments = $builder->build_object( { class => 'Koha::Clubs' } ); |
| 188 |
my $club_without_enrollments = $builder->build_object( { class => 'Koha::Clubs' } ); |
184 |
my $club_without_enrollments = $builder->build_object( { class => 'Koha::Clubs' } ); |
| 189 |
- |
|
|