|
Lines 18-23
Link Here
|
| 18 |
use Modern::Perl; |
18 |
use Modern::Perl; |
| 19 |
|
19 |
|
| 20 |
use Test::More tests => 1; |
20 |
use Test::More tests => 1; |
|
|
21 |
use Test::MockModule; |
| 21 |
use Test::Mojo; |
22 |
use Test::Mojo; |
| 22 |
use Test::Warn; |
23 |
use Test::Warn; |
| 23 |
|
24 |
|
|
Lines 30-37
use Koha::Illrequests;
Link Here
|
| 30 |
my $schema = Koha::Database->new->schema; |
31 |
my $schema = Koha::Database->new->schema; |
| 31 |
my $builder = t::lib::TestBuilder->new; |
32 |
my $builder = t::lib::TestBuilder->new; |
| 32 |
|
33 |
|
| 33 |
# FIXME: sessionStorage defaults to mysql, but it seems to break transaction handling |
|
|
| 34 |
# this affects the other REST api tests |
| 35 |
t::lib::Mocks::mock_preference( 'SessionStorage', 'tmp' ); |
34 |
t::lib::Mocks::mock_preference( 'SessionStorage', 'tmp' ); |
| 36 |
|
35 |
|
| 37 |
my $remote_address = '127.0.0.1'; |
36 |
my $remote_address = '127.0.0.1'; |
|
Lines 39-51
my $t = Test::Mojo->new('Koha::REST::V1');
Link Here
|
| 39 |
|
38 |
|
| 40 |
subtest 'list() tests' => sub { |
39 |
subtest 'list() tests' => sub { |
| 41 |
|
40 |
|
| 42 |
plan tests => 6; |
41 |
plan tests => 15; |
|
|
42 |
|
| 43 |
my $illreqmodule = Test::MockModule->new('Koha::Illrequest'); |
| 44 |
# Mock ->capabilities |
| 45 |
$illreqmodule->mock( 'capabilities', sub { return 'capable'; } ); |
| 46 |
# Mock ->metadata |
| 47 |
$illreqmodule->mock( 'metadata', sub { return 'metawhat?'; } ); |
| 43 |
|
48 |
|
| 44 |
$schema->storage->txn_begin; |
49 |
$schema->storage->txn_begin; |
| 45 |
|
50 |
|
| 46 |
Koha::Illrequests->search->delete; |
51 |
Koha::Illrequests->search->delete; |
| 47 |
my ( $borrowernumber, $session_id ) = |
52 |
# ill => 22 (userflags.sql) |
| 48 |
create_user_and_session( { authorized => 1 } ); |
53 |
my ( $borrowernumber, $session_id ) = create_user_and_session({ authorized => 22 }); |
| 49 |
|
54 |
|
| 50 |
## Authorized user tests |
55 |
## Authorized user tests |
| 51 |
# No requests, so empty array should be returned |
56 |
# No requests, so empty array should be returned |
|
Lines 54-93
subtest 'list() tests' => sub {
Link Here
|
| 54 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
59 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
| 55 |
$t->request_ok($tx)->status_is(200)->json_is( [] ); |
60 |
$t->request_ok($tx)->status_is(200)->json_is( [] ); |
| 56 |
|
61 |
|
| 57 |
# my $city_country = 'France'; |
62 |
my $library = $builder->build_object( { class => 'Koha::Libraries' } ); |
| 58 |
# my $city = $builder->build( |
63 |
my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); |
| 59 |
# { source => 'City', value => { city_country => $city_country } } ); |
64 |
|
| 60 |
# |
65 |
# Create an ILL request |
| 61 |
# # One city created, should get returned |
66 |
my $illrequest = $builder->build_object( |
| 62 |
# $tx = $t->ua->build_tx( GET => '/api/v1/cities' ); |
67 |
{ |
| 63 |
# $tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); |
68 |
class => 'Koha::Illrequests', |
| 64 |
# $tx->req->env( { REMOTE_ADDR => $remote_address } ); |
69 |
value => { |
| 65 |
# $t->request_ok($tx)->status_is(200)->json_is( [$city] ); |
70 |
branchcode => $library->branchcode, |
| 66 |
# |
71 |
borrowernumber => $patron->borrowernumber |
| 67 |
# my $another_city = $builder->build( |
72 |
} |
| 68 |
# { source => 'City', value => { city_country => $city_country } } ); |
73 |
} |
| 69 |
# my $city_with_another_country = $builder->build( { source => 'City' } ); |
74 |
); |
| 70 |
# |
75 |
|
| 71 |
# # Two cities created, they should both be returned |
76 |
# One illrequest created, should get returned |
| 72 |
# $tx = $t->ua->build_tx( GET => '/api/v1/cities' ); |
77 |
$tx = $t->ua->build_tx( GET => '/api/v1/illrequests' ); |
| 73 |
# $tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); |
78 |
$tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); |
| 74 |
# $tx->req->env( { REMOTE_ADDR => $remote_address } ); |
79 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
| 75 |
# $t->request_ok($tx)->status_is(200) |
80 |
$t->request_ok($tx)->status_is(200)->json_is( [ $illrequest->TO_JSON ] ); |
| 76 |
# ->json_is( [ $city, $another_city, $city_with_another_country ] ); |
81 |
|
| 77 |
# |
82 |
# One illrequest created, returned with augmented data |
| 78 |
# # Filtering works, two cities sharing city_country |
83 |
$tx = $t->ua->build_tx( GET => |
| 79 |
# $tx = |
84 |
'/api/v1/illrequests?embed=patron,branch,capabilities,metadata' ); |
| 80 |
# $t->ua->build_tx( GET => "/api/v1/cities?city_country=" . $city_country ); |
85 |
$tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); |
| 81 |
# $tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); |
86 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
| 82 |
# $tx->req->env( { REMOTE_ADDR => $remote_address } ); |
87 |
$t->request_ok($tx)->status_is(200)->json_is( |
| 83 |
# my $result = |
88 |
[ |
| 84 |
# $t->request_ok($tx)->status_is(200)->json_is( [ $city, $another_city ] ); |
89 |
$illrequest->TO_JSON( |
| 85 |
# |
90 |
{ patron => 1, branch => 1, capabilities => 1, metadata => 1 } |
| 86 |
# $tx = $t->ua->build_tx( |
91 |
) |
| 87 |
# GET => "/api/v1/cities?city_name=" . $city->{city_name} ); |
92 |
] |
| 88 |
# $tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); |
93 |
); |
| 89 |
# $tx->req->env( { REMOTE_ADDR => $remote_address } ); |
94 |
|
| 90 |
# $t->request_ok($tx)->status_is(200)->json_is( [$city] ); |
95 |
# Create another ILL request |
|
|
96 |
my $illrequest2 = $builder->build_object( |
| 97 |
{ |
| 98 |
class => 'Koha::Illrequests', |
| 99 |
value => { |
| 100 |
branchcode => $library->branchcode, |
| 101 |
borrowernumber => $patron->borrowernumber |
| 102 |
} |
| 103 |
} |
| 104 |
); |
| 105 |
|
| 106 |
# Two illrequest created, should get returned |
| 107 |
$tx = $t->ua->build_tx( GET => '/api/v1/illrequests' ); |
| 108 |
$tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); |
| 109 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
| 110 |
$t->request_ok($tx)->status_is(200) |
| 111 |
->json_is( [ $illrequest->TO_JSON, $illrequest2->TO_JSON ] ); |
| 91 |
|
112 |
|
| 92 |
# Warn on unsupported query parameter |
113 |
# Warn on unsupported query parameter |
| 93 |
$tx = $t->ua->build_tx( GET => '/api/v1/illrequests?request_blah=blah' ); |
114 |
$tx = $t->ua->build_tx( GET => '/api/v1/illrequests?request_blah=blah' ); |
|
Lines 102-110
subtest 'list() tests' => sub {
Link Here
|
| 102 |
|
123 |
|
| 103 |
sub create_user_and_session { |
124 |
sub create_user_and_session { |
| 104 |
|
125 |
|
| 105 |
my $args = shift; |
126 |
my $args = shift; |
| 106 |
my $flags = ( $args->{authorized} ) ? $args->{authorized} : 0; |
127 |
my $dbh = C4::Context->dbh; |
| 107 |
my $dbh = C4::Context->dbh; |
128 |
|
|
|
129 |
my $flags = ( $args->{authorized} ) ? 2**$args->{authorized} : 0; |
| 108 |
|
130 |
|
| 109 |
my $user = $builder->build( |
131 |
my $user = $builder->build( |
| 110 |
{ |
132 |
{ |
|
Lines 123-135
sub create_user_and_session {
Link Here
|
| 123 |
$session->param( 'lasttime', time() ); |
145 |
$session->param( 'lasttime', time() ); |
| 124 |
$session->flush; |
146 |
$session->flush; |
| 125 |
|
147 |
|
| 126 |
if ( $args->{authorized} ) { |
|
|
| 127 |
$dbh->do( " |
| 128 |
INSERT INTO user_permissions (borrowernumber,module_bit,code) |
| 129 |
VALUES (?,3,'parameters_remaining_permissions')", undef, |
| 130 |
$user->{borrowernumber} ); |
| 131 |
} |
| 132 |
|
| 133 |
return ( $user->{borrowernumber}, $session->id ); |
148 |
return ( $user->{borrowernumber}, $session->id ); |
| 134 |
} |
149 |
} |
| 135 |
|
150 |
|
| 136 |
- |
|
|