Lines 36-110
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 { |
|
|
43 |
|
47 |
plan tests => 2; |
44 |
plan tests => 2; |
48 |
|
45 |
|
49 |
$schema->storage->txn_begin; |
46 |
$schema->storage->txn_begin; |
50 |
|
47 |
|
51 |
my ($club_with_enrollments, $club_without_enrollments, $item, @enrollments) = create_test_data(); |
48 |
my ($club_with_enrollments, $club_without_enrollments, $item, @enrollments) = create_test_data(); |
52 |
|
49 |
|
53 |
unauthorized_access_tests('POST', "/api/v1/clubs/".$club_with_enrollments->id."/holds", undef, { |
50 |
unauthorized_access_tests( |
54 |
biblio_id => $item->biblionumber, |
51 |
'POST', |
55 |
pickup_library_id => $item->home_branch->branchcode |
52 |
"/api/v1/clubs/" . $club_with_enrollments->id . "/holds", |
56 |
}); |
53 |
undef, |
|
|
54 |
{ |
55 |
biblio_id => $item->biblionumber, |
56 |
pickup_library_id => $item->home_branch->branchcode |
57 |
} |
58 |
); |
57 |
|
59 |
|
58 |
$schema->storage->txn_rollback; |
60 |
$schema->storage->txn_rollback; |
59 |
|
61 |
|
60 |
subtest 'librarian access tests' => sub { |
62 |
subtest 'librarian access tests' => sub { |
|
|
63 |
|
61 |
plan tests => 8; |
64 |
plan tests => 8; |
62 |
|
65 |
|
63 |
$schema->storage->txn_begin; |
66 |
$schema->storage->txn_begin; |
64 |
|
67 |
|
65 |
my ($club_with_enrollments, $club_without_enrollments, $item, @enrollments) = create_test_data(); |
68 |
my ($club_with_enrollments, $club_without_enrollments, $item, @enrollments) = create_test_data(); |
|
|
69 |
my $club_with_enrollments_id = $club_with_enrollments->id; |
70 |
|
71 |
my $librarian = $builder->build_object( |
72 |
{ |
73 |
class => 'Koha::Patrons', |
74 |
value => { flags => 2**6 } # reserveforothers flag = 6 |
75 |
} |
76 |
); |
77 |
my $password = 'thePassword123'; |
78 |
$librarian->set_password( { password => $password, skip_validation => 1 } ); |
79 |
my $userid = $librarian->userid; |
66 |
|
80 |
|
67 |
my ( undef, $session_id ) = create_user_and_session({ authorized => 1 }); |
|
|
68 |
my $data = { |
81 |
my $data = { |
69 |
biblio_id => $item->biblionumber, |
82 |
biblio_id => $item->biblionumber, |
70 |
pickup_library_id => $item->home_branch->branchcode |
83 |
pickup_library_id => $item->home_branch->branchcode |
71 |
}; |
84 |
}; |
72 |
my $tx = $t->ua->build_tx(POST => "/api/v1/clubs/".$club_without_enrollments->id."/holds" => json => $data); |
85 |
|
73 |
$tx->req->cookies({ name => 'CGISESSID', value => $session_id }); |
86 |
$t->post_ok( "//$userid:$password@/api/v1/clubs/" |
74 |
$t->request_ok($tx) |
87 |
. $club_without_enrollments->id |
75 |
->status_is(500) |
88 |
. "/holds" => json => $data ) |
76 |
->json_is('/error' => "Cannot place a hold on a club without patrons."); |
89 |
->status_is(500) |
77 |
|
90 |
->json_is( '/error' => "Cannot place a hold on a club without patrons." ); |
78 |
$tx = $t->ua->build_tx(POST => "/api/v1/clubs/".$club_with_enrollments->id."/holds" => json => $data); |
91 |
|
79 |
$tx->req->cookies({ name => 'CGISESSID', value => $session_id }); |
92 |
$t->post_ok( "//$userid:$password@/api/v1/clubs/" |
80 |
$t->request_ok($tx) |
93 |
. $club_with_enrollments->id |
81 |
->status_is(201, 'Created Hold') |
94 |
. "/holds" => json => $data ) |
82 |
->json_has('/club_hold_id', 'got a club hold id') |
95 |
->status_is( 201, 'Created Hold' ) |
83 |
->json_is( '/club_id' => $club_with_enrollments->id) |
96 |
->json_has( '/club_hold_id', 'got a club hold id' ) |
84 |
->json_is( '/biblio_id' => $item->biblionumber); |
97 |
->json_is( '/club_id' => $club_with_enrollments->id ) |
|
|
98 |
->json_is( '/biblio_id' => $item->biblionumber ); |
85 |
|
99 |
|
86 |
$schema->storage->txn_rollback; |
100 |
$schema->storage->txn_rollback; |
87 |
}; |
101 |
}; |
88 |
}; |
102 |
}; |
89 |
|
103 |
|
90 |
subtest "default patron home" => sub { |
104 |
subtest "default patron home" => sub { |
|
|
105 |
|
91 |
plan tests => 8; |
106 |
plan tests => 8; |
92 |
|
107 |
|
93 |
$schema->storage->txn_begin; |
108 |
$schema->storage->txn_begin; |
94 |
|
109 |
|
95 |
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(); |
|
|
111 |
my $club_with_enrollments_id = $club_with_enrollments->id; |
112 |
|
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; |
96 |
|
122 |
|
97 |
my ( undef, $session_id ) = create_user_and_session({ authorized => 1 }); |
|
|
98 |
my $data = { |
123 |
my $data = { |
99 |
biblio_id => $item->biblionumber, |
124 |
biblio_id => $item->biblionumber, |
100 |
pickup_library_id => $item->home_branch->branchcode, |
125 |
pickup_library_id => $item->home_branch->branchcode, |
101 |
default_patron_home => 1 |
126 |
default_patron_home => 1 |
102 |
}; |
127 |
}; |
103 |
|
128 |
|
104 |
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/" |
105 |
$tx->req->cookies({ name => 'CGISESSID', value => $session_id }); |
130 |
. $club_with_enrollments->id |
106 |
$t->request_ok($tx) |
131 |
. "/holds" => json => $data ) |
107 |
->status_is(201, 'Created Hold'); |
132 |
->status_is( 201, 'Created Hold' ); |
108 |
|
133 |
|
109 |
my $json_response = decode_json $t->tx->res->content->get_body_chunk; |
134 |
my $json_response = decode_json $t->tx->res->content->get_body_chunk; |
110 |
|
135 |
|
Lines 128-178
sub unauthorized_access_tests {
Link Here
|
128 |
subtest 'unauthorized access tests' => sub { |
153 |
subtest 'unauthorized access tests' => sub { |
129 |
plan tests => 5; |
154 |
plan tests => 5; |
130 |
|
155 |
|
131 |
my $tx = $t->ua->build_tx($verb => $endpoint => json => $json); |
156 |
my $verb_ok = lc($verb) . '_ok'; |
132 |
$t->request_ok($tx) |
157 |
|
|
|
158 |
$t->$verb_ok($endpoint => json => $json) |
133 |
->status_is(401); |
159 |
->status_is(401); |
134 |
|
160 |
|
135 |
my ($borrowernumber, $session_id) = create_user_and_session({ |
161 |
my $unauthorized_patron = $builder->build_object( |
136 |
authorized => 0 }); |
162 |
{ |
|
|
163 |
class => 'Koha::Patrons', |
164 |
value => { flags => 0 } |
165 |
} |
166 |
); |
167 |
my $password = "thePassword123!"; |
168 |
$unauthorized_patron->set_password( |
169 |
{ password => $password, skip_validation => 1 } ); |
170 |
my $unauth_userid = $unauthorized_patron->userid; |
137 |
|
171 |
|
138 |
$tx = $t->ua->build_tx($verb => $endpoint => json => $json); |
172 |
$t->$verb_ok( "//$unauth_userid:$password\@$endpoint" => json => $json ) |
139 |
$tx->req->cookies({name => 'CGISESSID', value => $session_id}); |
|
|
140 |
$t->request_ok($tx) |
141 |
->status_is(403) |
173 |
->status_is(403) |
142 |
->json_has('/required_permissions'); |
174 |
->json_has('/required_permissions'); |
143 |
}; |
175 |
}; |
144 |
} |
176 |
} |
145 |
|
177 |
|
146 |
sub create_user_and_session { |
|
|
147 |
|
148 |
my $args = shift; |
149 |
my $flags = ( $args->{authorized} ) ? 64 : 0; |
150 |
|
151 |
my $user = $builder->build( |
152 |
{ |
153 |
source => 'Borrower', |
154 |
value => { |
155 |
flags => $flags, |
156 |
gonenoaddress => 0, |
157 |
lost => 0, |
158 |
email => 'nobody@example.com', |
159 |
emailpro => 'nobody@example.com', |
160 |
B_email => 'nobody@example.com' |
161 |
} |
162 |
} |
163 |
); |
164 |
|
165 |
# Create a session for the authorized user |
166 |
my $session = C4::Auth::get_session(''); |
167 |
$session->param( 'number', $user->{borrowernumber} ); |
168 |
$session->param( 'id', $user->{userid} ); |
169 |
$session->param( 'ip', '127.0.0.1' ); |
170 |
$session->param( 'lasttime', time() ); |
171 |
$session->flush; |
172 |
|
173 |
return ( $user->{borrowernumber}, $session->id ); |
174 |
} |
175 |
|
176 |
sub create_test_data { |
178 |
sub create_test_data { |
177 |
my $club_with_enrollments = $builder->build_object( { class => 'Koha::Clubs' } ); |
179 |
my $club_with_enrollments = $builder->build_object( { class => 'Koha::Clubs' } ); |
178 |
my $club_without_enrollments = $builder->build_object( { class => 'Koha::Clubs' } ); |
180 |
my $club_without_enrollments = $builder->build_object( { class => 'Koha::Clubs' } ); |
Lines 197-200
sub create_test_data {
Link Here
|
197 |
$lib = $builder->build_object({ class => 'Koha::Libraries', value => {pickup_location => 1}}); |
199 |
$lib = $builder->build_object({ class => 'Koha::Libraries', value => {pickup_location => 1}}); |
198 |
my $item = $builder->build_sample_item({homebranch => $lib->branchcode}); |
200 |
my $item = $builder->build_sample_item({homebranch => $lib->branchcode}); |
199 |
return ( $club_with_enrollments, $club_without_enrollments, $item, [ $enrollment1, $enrollment2, $enrollment3, $enrollment4, $enrollment5, $enrollment6 ] ); |
201 |
return ( $club_with_enrollments, $club_without_enrollments, $item, [ $enrollment1, $enrollment2, $enrollment3, $enrollment4, $enrollment5, $enrollment6 ] ); |
200 |
} |
202 |
} |
201 |
- |
|
|