Line 0
Link Here
|
0 |
- |
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 along |
15 |
# with Koha; if not, write to the Free Software Foundation, Inc., |
16 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
17 |
|
18 |
use Modern::Perl; |
19 |
|
20 |
use Test::More tests => 1; |
21 |
use Test::Mojo; |
22 |
use Test::Warn; |
23 |
|
24 |
use t::lib::Mocks; |
25 |
use t::lib::TestBuilder; |
26 |
|
27 |
use C4::Auth; |
28 |
use Koha::Biblios; |
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 'delete() tests' => sub { |
42 |
|
43 |
plan tests => 9; |
44 |
|
45 |
$schema->storage->txn_begin; |
46 |
|
47 |
my ( $unauthorized_borrowernumber, $unauthorized_session_id ) = |
48 |
create_user_and_session( { authorized => 0 } ); |
49 |
my ( $authorized_borrowernumber, $authorized_session_id ) = |
50 |
create_user_and_session( { authorized => 1 } ); |
51 |
|
52 |
my $biblio_id = $builder->build_object({ class => 'Koha::Biblios' })->id; |
53 |
my $item = $builder->build_object( { class => 'Koha::Items', value => { biblionumber => $biblio_id } } ); |
54 |
|
55 |
# Unauthorized attempt to delete |
56 |
my $tx = $t->ua->build_tx( DELETE => "/api/v1/biblios/$biblio_id" ); |
57 |
$tx->req->cookies({ name => 'CGISESSID', value => $unauthorized_session_id }); |
58 |
$tx->req->env({ REMOTE_ADDR => $remote_address }); |
59 |
$t->request_ok($tx) |
60 |
->status_is(403); |
61 |
|
62 |
$tx = $t->ua->build_tx( DELETE => "/api/v1/biblios/$biblio_id" ); |
63 |
$tx->req->cookies({ name => 'CGISESSID', value => $authorized_session_id }); |
64 |
$tx->req->env({ REMOTE_ADDR => $remote_address }); |
65 |
$t->request_ok($tx) |
66 |
->status_is(409); |
67 |
|
68 |
$item->delete(); |
69 |
|
70 |
$tx = $t->ua->build_tx( DELETE => "/api/v1/biblios/$biblio_id" ); |
71 |
$tx->req->cookies({ name => 'CGISESSID', value => $authorized_session_id }); |
72 |
$tx->req->env({ REMOTE_ADDR => $remote_address }); |
73 |
$t->request_ok($tx) |
74 |
->status_is(200) |
75 |
->content_is('""'); |
76 |
|
77 |
$tx = $t->ua->build_tx( DELETE => "/api/v1/biblios/$biblio_id" ); |
78 |
$tx->req->cookies({ name => 'CGISESSID', value => $authorized_session_id }); |
79 |
$tx->req->env({ REMOTE_ADDR => $remote_address }); |
80 |
$t->request_ok($tx) |
81 |
->status_is(404); |
82 |
|
83 |
$schema->storage->txn_rollback; |
84 |
}; |
85 |
|
86 |
sub create_user_and_session { |
87 |
|
88 |
my $args = shift; |
89 |
my $flags = ( $args->{authorized} ) ? $args->{authorized} : 0; |
90 |
my $dbh = C4::Context->dbh; |
91 |
|
92 |
my $user = $builder->build( |
93 |
{ |
94 |
source => 'Borrower', |
95 |
value => { |
96 |
flags => $flags |
97 |
} |
98 |
} |
99 |
); |
100 |
|
101 |
# Create a session for the authorized user |
102 |
my $session = C4::Auth::get_session(''); |
103 |
$session->param( 'number', $user->{borrowernumber} ); |
104 |
$session->param( 'id', $user->{userid} ); |
105 |
$session->param( 'ip', '127.0.0.1' ); |
106 |
$session->param( 'lasttime', time() ); |
107 |
$session->flush; |
108 |
|
109 |
if ( $args->{authorized} ) { |
110 |
$dbh->do( " |
111 |
INSERT INTO user_permissions (borrowernumber,module_bit,code) |
112 |
VALUES (?,3,'parameters_remaining_permissions')", undef, |
113 |
$user->{borrowernumber} ); |
114 |
} |
115 |
|
116 |
return ( $user->{borrowernumber}, $session->id ); |
117 |
} |
118 |
|