Lines 19-41
use Modern::Perl;
Link Here
|
19 |
|
19 |
|
20 |
use Test::More tests => 1; |
20 |
use Test::More tests => 1; |
21 |
use Test::Mojo; |
21 |
use Test::Mojo; |
22 |
use Test::Warn; |
|
|
23 |
|
22 |
|
24 |
use t::lib::TestBuilder; |
23 |
use t::lib::TestBuilder; |
25 |
use t::lib::Mocks; |
24 |
use t::lib::Mocks; |
26 |
|
25 |
|
27 |
use C4::Auth; |
|
|
28 |
use Koha::StockRotationStages; |
26 |
use Koha::StockRotationStages; |
29 |
|
27 |
|
30 |
my $schema = Koha::Database->new->schema; |
28 |
my $schema = Koha::Database->new->schema; |
31 |
my $builder = t::lib::TestBuilder->new; |
29 |
my $builder = t::lib::TestBuilder->new; |
32 |
|
30 |
|
33 |
# FIXME: sessionStorage defaults to mysql, but it seems to break transaction handling |
31 |
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 ); |
34 |
# this affects the other REST api tests |
|
|
35 |
t::lib::Mocks::mock_preference( 'SessionStorage', 'tmp' ); |
36 |
|
32 |
|
37 |
my $remote_address = '127.0.0.1'; |
33 |
my $t = Test::Mojo->new('Koha::REST::V1'); |
38 |
my $t = Test::Mojo->new('Koha::REST::V1'); |
|
|
39 |
|
34 |
|
40 |
subtest 'move() tests' => sub { |
35 |
subtest 'move() tests' => sub { |
41 |
|
36 |
|
Lines 43-52
subtest 'move() tests' => sub {
Link Here
|
43 |
|
38 |
|
44 |
$schema->storage->txn_begin; |
39 |
$schema->storage->txn_begin; |
45 |
|
40 |
|
46 |
my ( $unauthorized_borrowernumber, $unauthorized_session_id ) = |
41 |
my $authorized_patron = $builder->build_object({ |
47 |
create_user_and_session( { authorized => 0 } ); |
42 |
class => 'Koha::Patrons', |
48 |
my ( $authorized_borrowernumber, $authorized_session_id ) = |
43 |
value => { flags => 2 ** 24 } # stockrotation => 24 |
49 |
create_user_and_session( { authorized => 1 } ); |
44 |
}); |
|
|
45 |
my $password = 'thePassword123'; |
46 |
$authorized_patron->set_password({ password => $password, skip_validation => 1 }); |
47 |
my $auth_userid = $authorized_patron->userid; |
48 |
|
49 |
my $unauthorized_patron = $builder->build_object({ |
50 |
class => 'Koha::Patrons', |
51 |
value => { flags => 0 } |
52 |
}); |
53 |
$unauthorized_patron->set_password({ password => $password, skip_validation => 1 }); |
54 |
my $unauth_userid = $unauthorized_patron->userid; |
50 |
|
55 |
|
51 |
my $library1 = $builder->build({ source => 'Branch' }); |
56 |
my $library1 = $builder->build({ source => 'Branch' }); |
52 |
my $library2 = $builder->build({ source => 'Branch' }); |
57 |
my $library2 = $builder->build({ source => 'Branch' }); |
Lines 69-172
subtest 'move() tests' => sub {
Link Here
|
69 |
my $stage1_id = $stage1->{stage_id}; |
74 |
my $stage1_id = $stage1->{stage_id}; |
70 |
|
75 |
|
71 |
# Unauthorized attempt to update |
76 |
# Unauthorized attempt to update |
72 |
my $tx = $t->ua->build_tx( |
77 |
$t->put_ok( "//$unauth_userid:$password@/api/v1/rotas/$rota_id/stages/$stage1_id/position" |
73 |
PUT => "/api/v1/rotas/$rota_id/stages/$stage1_id/position" => |
78 |
=> json => 2 ) |
74 |
json => 2 |
79 |
->status_is(403); |
75 |
); |
|
|
76 |
$tx->req->cookies( |
77 |
{ name => 'CGISESSID', value => $unauthorized_session_id } ); |
78 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
79 |
$t->request_ok($tx)->status_is(403); |
80 |
|
80 |
|
81 |
# Invalid attempt to move a stage on a non-existant rota |
81 |
# Invalid attempt to move a stage on a non-existant rota |
82 |
$tx = $t->ua->build_tx( |
82 |
$t->put_ok( "//$auth_userid:$password@/api/v1/rotas/99999999/stages/$stage1_id/position" |
83 |
PUT => "/api/v1/rotas/99999999/stages/$stage1_id/position" => |
83 |
=> json => 2 ) |
84 |
json => 2 |
84 |
->status_is(404) |
85 |
); |
|
|
86 |
$tx->req->cookies( |
87 |
{ name => 'CGISESSID', value => $authorized_session_id } ); |
88 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
89 |
$t->request_ok($tx)->status_is(404) |
90 |
->json_is( '/error' => "Not found - Invalid rota or stage ID" ); |
85 |
->json_is( '/error' => "Not found - Invalid rota or stage ID" ); |
91 |
|
86 |
|
92 |
# Invalid attempt to move an non-existant stage |
87 |
# Invalid attempt to move an non-existant stage |
93 |
$tx = $t->ua->build_tx( |
88 |
$t->put_ok( "//$auth_userid:$password@/api/v1/rotas/$rota_id/stages/999999999/position" |
94 |
PUT => "/api/v1/rotas/$rota_id/stages/999999999/position" => |
89 |
=> json => 2 ) |
95 |
json => 2 |
90 |
->status_is(404) |
96 |
); |
|
|
97 |
$tx->req->cookies( |
98 |
{ name => 'CGISESSID', value => $authorized_session_id } ); |
99 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
100 |
$t->request_ok($tx)->status_is(404) |
101 |
->json_is( '/error' => "Not found - Invalid rota or stage ID" ); |
91 |
->json_is( '/error' => "Not found - Invalid rota or stage ID" ); |
102 |
|
92 |
|
103 |
# Invalid attempt to move stage to current position |
93 |
# Invalid attempt to move stage to current position |
104 |
my $curr_position = $stage1->{position}; |
94 |
my $curr_position = $stage1->{position}; |
105 |
$tx = $t->ua->build_tx( |
95 |
$t->put_ok( "//$auth_userid:$password@/api/v1/rotas/$rota_id/stages/$stage1_id/position" |
106 |
PUT => "/api/v1/rotas/$rota_id/stages/$stage1_id/position" => |
96 |
=> json => $curr_position ) |
107 |
json => $curr_position |
97 |
->status_is(400) |
108 |
); |
|
|
109 |
$tx->req->cookies( |
110 |
{ name => 'CGISESSID', value => $authorized_session_id } ); |
111 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
112 |
$t->request_ok($tx)->status_is(400) |
113 |
->json_is( '/error' => "Bad request - new position invalid" ); |
98 |
->json_is( '/error' => "Bad request - new position invalid" ); |
114 |
|
99 |
|
115 |
# Invalid attempt to move stage to invalid position |
100 |
# Invalid attempt to move stage to invalid position |
116 |
$tx = $t->ua->build_tx( |
101 |
$t->put_ok( "//$auth_userid:$password@/api/v1/rotas/$rota_id/stages/$stage1_id/position" |
117 |
PUT => "/api/v1/rotas/$rota_id/stages/$stage1_id/position" => |
102 |
=> json => 99999999 ) |
118 |
json => 99999999 |
103 |
->status_is(400) |
119 |
); |
|
|
120 |
$tx->req->cookies( |
121 |
{ name => 'CGISESSID', value => $authorized_session_id } ); |
122 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
123 |
$t->request_ok($tx)->status_is(400) |
124 |
->json_is( '/error' => "Bad request - new position invalid" ); |
104 |
->json_is( '/error' => "Bad request - new position invalid" ); |
125 |
|
105 |
|
126 |
# Valid, authorised move |
106 |
# Valid, authorised move |
127 |
$tx = $t->ua->build_tx( |
107 |
$t->put_ok( "//$auth_userid:$password@/api/v1/rotas/$rota_id/stages/$stage1_id/position" |
128 |
PUT => "/api/v1/rotas/$rota_id/stages/$stage1_id/position" => |
108 |
=> json => 2 ) |
129 |
json => 2 |
109 |
->status_is(200); |
130 |
); |
|
|
131 |
$tx->req->cookies( |
132 |
{ name => 'CGISESSID', value => $authorized_session_id } ); |
133 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
134 |
$t->request_ok($tx)->status_is(200); |
135 |
|
110 |
|
136 |
$schema->storage->txn_rollback; |
111 |
$schema->storage->txn_rollback; |
137 |
}; |
112 |
}; |
138 |
|
|
|
139 |
sub create_user_and_session { |
140 |
|
141 |
my $args = shift; |
142 |
my $flags = ( $args->{authorized} ) ? 2 ** 24 : 0; # stockrotation == 24 |
143 |
my $dbh = C4::Context->dbh; |
144 |
|
145 |
my $user = $builder->build( |
146 |
{ |
147 |
source => 'Borrower', |
148 |
value => { |
149 |
flags => $flags |
150 |
} |
151 |
} |
152 |
); |
153 |
|
154 |
# Create a session for the authorized user |
155 |
my $session = C4::Auth::get_session(''); |
156 |
$session->param( 'number', $user->{borrowernumber} ); |
157 |
$session->param( 'id', $user->{userid} ); |
158 |
$session->param( 'ip', '127.0.0.1' ); |
159 |
$session->param( 'lasttime', time() ); |
160 |
$session->flush; |
161 |
|
162 |
if ( $args->{authorized} ) { |
163 |
$dbh->do( " |
164 |
INSERT INTO user_permissions (borrowernumber,module_bit,code) |
165 |
VALUES (?,3,'parameters_remaining_permissions')", undef, |
166 |
$user->{borrowernumber} ); |
167 |
} |
168 |
|
169 |
return ( $user->{borrowernumber}, $session->id ); |
170 |
} |
171 |
|
172 |
1; |
173 |
- |